-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathGeneralClientOSS.cs
More file actions
102 lines (93 loc) · 4.26 KB
/
Copy pathGeneralClientOSS.cs
File metadata and controls
102 lines (93 loc) · 4.26 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using GeneralUpdate.Core.FileSystem;
using GeneralUpdate.Core;
using GeneralUpdate.Core.Configuration;
using GeneralUpdate.Core.JsonContext;
using GeneralUpdate.Core.Configuration;
namespace GeneralUpdate.Core;
[Obsolete("Use GeneralUpdateBootstrap with AppType=AppType.OSS instead.")] public sealed class GeneralClientOSS
{
private GeneralClientOSS() { }
/// <summary>
/// Starting an OSS update for windows platform.
/// </summary>
public static async Task Start(GlobalConfigInfoOSS configGlobalConfigInfo, string upgradeAppName = "GeneralUpdate.Upgrade.exe")
{
await Task.Run(() =>
{
try
{
GeneralTracer.Debug("1.Starting OSS upgrade mode.");
var basePath = Thread.GetDomain().BaseDirectory;
//Download the version information file from OSS to be updated.(JSON)
GeneralTracer.Debug("2.Download the version information file from OSS to be updated.(JSON)");
var versionsFilePath = Path.Combine(basePath, configGlobalConfigInfo.VersionFileName);
DownloadFile(configGlobalConfigInfo.Url, versionsFilePath);
if (!File.Exists(versionsFilePath)) return;
var versions = StorageManager.GetJson<List<VersionOSS>>(versionsFilePath,
VersionOSSJsonContext.Default.ListVersionOSS);
if (versions == null || versions.Count == 0) return;
versions = versions.OrderByDescending(x => x.PubTime).ToList();
var newVersion = versions.First();
//Determine whether the current client version needs to be upgraded.
GeneralTracer.Debug("3.Determine whether the current client version needs to be upgraded.");
if (!IsUpgrade(configGlobalConfigInfo.CurrentVersion, newVersion.Version))
return;
//If you confirm that an update is required, start the upgrade application.
var appPath = Path.Combine(basePath, $"{upgradeAppName}");
if (!File.Exists(appPath))
throw new Exception($"The application does not exist {upgradeAppName} !");
GeneralTracer.Debug("4.Start upgrade app.");
var json = JsonSerializer.Serialize(configGlobalConfigInfo,
GlobalConfigInfoOSSJsonContext.Default.GlobalConfigInfoOSS);
Environments.SetEnvironmentVariable("GlobalConfigInfoOSS", json);
Process.Start(appPath);
Process.GetCurrentProcess().Kill();
}
catch (Exception exception)
{
GeneralTracer.Error("The BaseStart method in the GeneralClientOSS class throws an exception.",
exception);
throw exception;
}
finally
{
GeneralTracer.Dispose();
}
});
}
/// <summary>
/// Determine whether the current client version needs to be upgraded.
/// </summary>
/// <param name="clientVersion"></param>
/// <param name="serverVersion"></param>
/// <returns>true: Upgrade required , false: No upgrade is required</returns>
private static bool IsUpgrade(string clientVersion, string serverVersion)
{
if (string.IsNullOrWhiteSpace(clientVersion) || string.IsNullOrWhiteSpace(serverVersion))
return false;
var isParseClientVersion = Version.TryParse(clientVersion, out var currentClientVersion);
var isParseServerVersion = Version.TryParse(serverVersion, out var currentServerVersion);
if (!isParseClientVersion || !isParseServerVersion) return false;
if (currentClientVersion < currentServerVersion) return true;
return false;
}
private static void DownloadFile(string url, string path)
{
if (File.Exists(path))
{
File.SetAttributes(path, FileAttributes.Normal);
File.Delete(path);
}
using var webClient = new WebClient();
webClient.DownloadFile(new Uri(url), path);
}
}