forked from mattpannella/pupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
198 lines (173 loc) · 7.7 KB
/
Program.cs
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using pannella.analoguepocket;
using System.Runtime.InteropServices;
using CommandLine;
internal class Program
{
private static string version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(3);
private const string USER = "mattpannella";
private const string REPOSITORY = "pocket_core_autoupdate_net";
private const string RELEASE_URL = "https://github.com/mattpannella/pocket_core_autoupdate_net/releases/download/{0}/pocket_updater_{1}.zip";
private static async Task Main(string[] args)
{
string location = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string? path = Path.GetDirectoryName(location);
bool extractAll = false;
bool coreSelector = false;
bool preservePlatformsFolder = false;
Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(o =>
{
if(o.InstallPath != null && o.InstallPath != "") {
Console.WriteLine("path: " + o.InstallPath);
path = o.InstallPath;
}
if(o.ExtractAll) {
extractAll = true;
}
if(o.CoreSelector) {
coreSelector = true;
}
if(o.PreservePlatformsFolder) {
preservePlatformsFolder = true;
}
}
).WithNotParsed<Options>(o =>
{
if(o.IsHelp()) {
Environment.Exit(1);
}
if(o.IsVersion()) {
Environment.Exit(1);
}
}
);
//path = "/Users/mattpannella/pocket-test";
ConsoleKey response;
Console.WriteLine("Analogue Pocket Core Updater v" + version);
Console.WriteLine("Checking for updates...");
if(await CheckVersion(path)) {
Console.WriteLine("Would you like to continue anyway? [Y/n]:");
response = Console.ReadKey(false).Key;
if (response == ConsoleKey.N) {
Console.WriteLine("Come again soon");
Console.ReadLine(); //wait for input so the console doesn't auto close in windows
Environment.Exit(1);
}
}
PocketCoreUpdater updater = new PocketCoreUpdater(path);
SettingsManager settings = new SettingsManager(path);
if(coreSelector || settings.GetConfig().core_selector) {
List<Core> cores = await CoresService.GetCores();
RunCoreSelector(settings, cores);
}
if(preservePlatformsFolder || settings.GetConfig().preserve_platforms_folder) {
updater.PreservePlatformsFolder(true);
}
updater.ExtractAll(extractAll);
updater.SetGithubApiKey(settings.GetConfig().github_token);
updater.DownloadFirmware(settings.GetConfig().download_firmware);
updater.StatusUpdated += updater_StatusUpdated;
updater.UpdateProcessComplete += updater_UpdateProcessComplete;
updater.DownloadAssets(settings.GetConfig().download_assets);
await updater.Initialize();
Console.WriteLine("Starting update process...");
await updater.RunUpdates();
Console.ReadLine(); //wait for input so the console doesn't auto close in windows
}
static void RunCoreSelector(SettingsManager settings, List<Core> cores)
{
ConsoleKey response;
Console.WriteLine("Select your cores! The available cores will be listed 1 at a time. For each one, hit 'n' if you don't want it installed, or just hit enter if you want it. Ok you've got this. Here we go...");
foreach(Core core in cores) {
Console.Write(core.identifier + "?[Y/n] ");
response = Console.ReadKey(false).Key;
if (response == ConsoleKey.N) {
settings.DisableCore(core.identifier);
} else {
settings.EnableCore(core.identifier);
}
Console.WriteLine("");
}
settings.GetConfig().core_selector = false;
settings.SaveSettings();
}
static void updater_StatusUpdated(object sender, StatusUpdatedEventArgs e)
{
Console.WriteLine(e.Message);
}
static void updater_UpdateProcessComplete(object sender, UpdateProcessCompleteEventArgs e)
{
Console.WriteLine("-------------");
Console.WriteLine(e.Message);
if(e.InstalledCores.Count > 0) {
Console.WriteLine("Cores Updated:");
foreach(Dictionary<string, string> core in e.InstalledCores) {
Console.WriteLine(core["core"] + " " + core["version"]);
}
Console.WriteLine("");
}
if(e.InstalledAssets.Count > 0) {
Console.WriteLine("Assets Installed:");
foreach(string asset in e.InstalledAssets) {
Console.WriteLine(asset);
}
Console.WriteLine("");
}
if(e.FirmwareUpdated != "") {
Console.WriteLine("New Firmware was downloaded. Restart your Pocket to install");
Console.WriteLine(e.FirmwareUpdated);
Console.WriteLine("");
}
Console.WriteLine("we did it, come again soon");
}
//return true if newer version is available
async static Task<bool> CheckVersion(string path)
{
try {
List<Github.Release> releases = await GithubApi.GetReleases(USER, REPOSITORY);
string tag_name = releases[0].tag_name;
string? v = SemverUtil.FindSemver(tag_name);
if(v != null) {
bool check = SemverUtil.SemverCompare(v, version);
if(check) {
Console.WriteLine("A new version is available. Downloading now...");
string platform = GetPlatform();
string url = String.Format(RELEASE_URL, tag_name, platform);
string saveLocation = Path.Combine(path, "pocket_updater.zip");
await HttpHelper.DownloadFileAsync(url, saveLocation);
Console.WriteLine("Download complete.");
Console.WriteLine(saveLocation);
Console.WriteLine("Go to " + releases[0].html_url + " for a change log");
}
return check;
}
return false;
} catch (HttpRequestException e) {
return false;
}
}
private static string GetPlatform()
{
if(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
return "win";
}
if(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) {
return "mac";
}
if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
return "linux";
}
return "";
}
}
public class Options
{
[Option('p', "path", HelpText = "Absolute path to install location", Required = false)]
public string? InstallPath { get; set; }
[Option ('a', "all", Required = false, HelpText = "Extract all release assets, instead of just ones containing openFPGA cores.")]
public bool ExtractAll { get; set; }
[Option ('c', "coreselector", Required = false, HelpText = "Run the core selector.")]
public bool CoreSelector { get; set; }
[Option ('f', "platformsfolder", Required = false, HelpText = "Preserve the Platforms folder, so customizations aren't overwritten by updates.")]
public bool PreservePlatformsFolder { get; set; }
}