-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathBootstrapper.cs
More file actions
130 lines (112 loc) · 4.14 KB
/
Bootstrapper.cs
File metadata and controls
130 lines (112 loc) · 4.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Licensed to Spectre Systems AB under one or more agreements.
// Spectre Systems AB licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Windows;
using Autofac;
using Caliburn.Micro;
using Jarvis.Addin.Files;
using Jarvis.Addin.Google;
using Jarvis.Addin.Processes;
using Jarvis.Addin.Wikipedia;
using Jarvis.Bootstrapping;
using Jarvis.Core;
using Jarvis.Infrastructure.Utilities;
using Jarvis.Services;
using Jarvis.ViewModels;
using IContainer = Autofac.IContainer;
namespace Jarvis
{
public class Bootstrapper : BootstrapperBase
{
private IContainer _container;
private IDisposable _hotKey;
private JarvisTaskbarIcon _taskbarIcon;
public Bootstrapper()
{
Initialize();
}
protected override void Configure()
{
// Initialize paths.
PathUtility.Initialize();
// Configure container.
var builder = new ContainerBuilder();
builder.RegisterInstance(Application).As<JarvisApplication>();
builder.RegisterModule(new JarvisModule());
builder.RegisterModule<UpdaterModule>();
builder.RegisterModule<LoggingModule>();
// Configure addins.
builder.RegisterModule(new AddinModule(
typeof(FileAddin).Assembly,
typeof(GoogleAddin).Assembly,
typeof(WikipediaAddin).Assembly,
typeof(ProcessAddin).Assembly));
// Build the container.
_container = builder.Build();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
// Set custom namespace mappings.
ViewLocator.AddNamespaceMapping("Jarvis.ViewModels.Settings", "Jarvis.Views.Settings");
// Initialize everything that needs to.
var initializables = IoC.GetAll<IInitializable>();
foreach (var initializable in initializables)
{
initializable.Initialize();
}
// Show the root view.
var windowSettings = new Dictionary<string, object> { { "Visibility", Visibility.Hidden } };
DisplayRootViewFor<ShellViewModel>(windowSettings);
Application?.MainWindow?.Hide();
// Create the taskbar icon.
_taskbarIcon = IoC.Get<JarvisTaskbarIcon>();
// Start all background services.
IoC.Get<ServiceOrchestrator>().Start();
// Register the hotkey.
var service = IoC.Get<ApplicationService>();
_hotKey = new KeyboardHook(() => service.Toggle());
}
protected override void OnExit(object sender, EventArgs e)
{
// Unregister the hot key
_hotKey?.Dispose();
_taskbarIcon?.Dispose();
// Stop the service orchestrator.
var services = IoC.Get<ServiceOrchestrator>();
services.Stop();
services.Join();
}
protected override object GetInstance(Type service, string key)
{
if (service == null)
{
throw new ArgumentNullException(nameof(service));
}
if (string.IsNullOrWhiteSpace(key))
{
if (_container.IsRegistered(service))
{
return _container.Resolve(service);
}
}
else
{
if (_container.IsRegisteredWithName(key, service))
{
return _container.ResolveNamed(key, service);
}
}
throw new Exception($"Could not locate an instances of '{key ?? service.Name}'.");
}
protected override IEnumerable<object> GetAllInstances(Type service)
{
return _container.Resolve(typeof(IEnumerable<>).MakeGenericType(service)) as IEnumerable<object>;
}
protected override void BuildUp(object instance)
{
_container.InjectProperties(instance);
}
}
}