Repository navigation
Expand file tree
/
Copy pathSquirrelAwareApp.cs
More file actions
78 lines (72 loc) · 3.4 KB
/
Copy pathSquirrelAwareApp.cs
File metadata and controls
78 lines (72 loc) · 3.4 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Splat;
namespace Squirrel
{
/// <summary>
/// SquirrelAwareApp helps you to handle Squirrel app activation events
/// correctly.
/// </summary>
public static class SquirrelAwareApp
{
/// <summary>
/// Call this method as early as possible in app startup. This method
/// will dispatch to your methods to set up your app. Depending on the
/// parameter, your app will exit after this method is called, which
/// is required by Squirrel. UpdateManager has methods to help you to
/// do this, such as CreateShortcutForThisExe.
/// </summary>
/// <param name="onInitialInstall">Called when your app is initially
/// installed. Set up app shortcuts here as well as file associations.
/// </param>
/// <param name="onAppUpdate">Called when your app is updated to a new
/// version.</param>
/// <param name="onAppObsoleted">Called when your app is no longer the
/// latest version (i.e. they have installed a new version and your app
/// is now the old version)</param>
/// <param name="onAppUninstall">Called when your app is uninstalled
/// via Programs and Features. Remove all of the things that you created
/// in onInitialInstall.</param>
/// <param name="onFirstRun">Called the first time an app is run after
/// being installed. Your application will **not** exit after this is
/// dispatched, you should use this as a hint (i.e. show a 'Welcome'
/// screen, etc etc.</param>
/// <param name="arguments">Use in a unit-test runner to mock the
/// arguments. In your app, leave this as null.</param>
public static void HandleEvents(
Action<Version> onInitialInstall = null,
Action<Version> onAppUpdate = null,
Action<Version> onAppObsoleted = null,
Action<Version> onAppUninstall = null,
Action onFirstRun = null,
string[] arguments = null)
{
Action<Version> defaultBlock = (v => { });
var args = arguments ?? Environment.GetCommandLineArgs().Skip(1).ToArray();
if (args.Length == 0) return;
var lookup = new[] {
new { Key = "--squirrel-install", Value = onInitialInstall ?? defaultBlock },
new { Key = "--squirrel-updated", Value = onAppUpdate ?? defaultBlock },
new { Key = "--squirrel-obsolete", Value = onAppObsoleted ?? defaultBlock },
new { Key = "--squirrel-uninstall", Value = onAppUninstall ?? defaultBlock },
}.ToDictionary(k => k.Key, v => v.Value);
if (args[0] == "--squirrel-firstrun") {
(onFirstRun ?? (() => {}))();
return;
}
if (args.Length != 2) return;
if (!lookup.ContainsKey(args[0])) return;
var version = args[1].ToSemanticVersion().Version;
try {
lookup[args[0]](version);
if (!ModeDetector.InUnitTestRunner()) Environment.Exit(0);
} catch (Exception ex) {
LogHost.Default.ErrorException("Failed to handle Squirrel events", ex);
if (!ModeDetector.InUnitTestRunner()) Environment.Exit(-1);
}
}
}
}