Skip to content

Commit df6e2b9

Browse files
committed
更新OSS功能
1 parent 7ef01df commit df6e2b9

File tree

7 files changed

+122
-98
lines changed

7 files changed

+122
-98
lines changed

src/c#/GeneralUpdate.Client/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ private static void Main(string[] args)
2222
await DifferentialCore.Instance?.Dirty(source, patch);
2323
});*/
2424

25-
Task.Run(() =>
25+
/*Task.Run(() =>
2626
{
2727
//Console.WriteLine("主程序启动辣!!!!");
2828
//await Task.Delay(3000);
@@ -66,7 +66,10 @@ private static void Main(string[] args)
6666
.Option(UpdateOption.Encoding, Encoding.UTF8)
6767
.Option(UpdateOption.Format, Format.ZIP)
6868
.LaunchAsync();
69-
});
69+
});*/
70+
71+
GeneralClientOSS.Start(new ParamsOSS("http://192.168.50.203/versions.json", "GeneralUpdate.Client.exe","1.0.0.0", "versions.json"));
72+
7073
Console.Read();
7174
}
7275

src/c#/GeneralUpdate.ClientCore/GeneralClientOSS.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Linq;
77
using System.Net;
8+
using System.Runtime.CompilerServices;
89
using System.Text.Json;
910
using System.Threading;
1011
using System.Threading.Tasks;
@@ -17,15 +18,12 @@ namespace GeneralUpdate.ClientCore;
1718

1819
public sealed class GeneralClientOSS
1920
{
20-
private GeneralClientOSS()
21-
{
22-
}
21+
private GeneralClientOSS() { }
2322

2423
/// <summary>
25-
/// Starting an OSS update for windows,Linux,mac platform.
24+
/// Starting an OSS update for windows platform.
2625
/// </summary>
27-
/// <param name="configInfo"></param>
28-
public static async Task Start(ParamsOSS configParams, string upgradeAppName = "GeneralUpdate.Upgrade")
26+
public static async Task Start(ParamsOSS configParams, string upgradeAppName = "GeneralUpdate.Upgrade.exe")
2927
{
3028
await Task.Run(() =>
3129
{
@@ -34,24 +32,29 @@ await Task.Run(() =>
3432
var basePath = Thread.GetDomain().BaseDirectory;
3533
//Download the version information file from OSS to be updated.(JSON)
3634
var versionsFilePath = Path.Combine(basePath, configParams.VersionFileName);
37-
DownloadFile(configParams.Url + "/" + configParams.VersionFileName, versionsFilePath);
35+
DownloadFile(configParams.Url, versionsFilePath);
3836
if (!File.Exists(versionsFilePath)) return;
3937
var versions = GeneralFileManager.GetJson<List<VersionPO>>(versionsFilePath);
4038
if (versions == null || versions.Count == 0) return;
4139
versions = versions.OrderByDescending(x => x.PubTime).ToList();
4240
var newVersion = versions.First();
4341
//Determine whether the current client version needs to be upgraded.
44-
if (!IsUpgrade(configParams.CurrentVersion, newVersion.Version)) return;
45-
var appPath = Path.Combine(basePath, $"{upgradeAppName}.exe");
46-
if (!File.Exists(appPath)) throw new Exception($"The application does not exist {upgradeAppName} !");
47-
//If you confirm that an update is required, start the upgrade application.
42+
if (!IsUpgrade(configParams.CurrentVersion, newVersion.Version))
43+
return;
44+
4845
var json = JsonSerializer.Serialize(configParams);
49-
//TODO: set environment variable
50-
Process.Start(appPath, json);
46+
Environment.SetEnvironmentVariable("ParamsOSS", json, EnvironmentVariableTarget.User);
47+
48+
//If you confirm that an update is required, start the upgrade application.
49+
var appPath = Path.Combine(basePath, $"{upgradeAppName}");
50+
if (!File.Exists(appPath))
51+
throw new Exception($"The application does not exist {upgradeAppName} !");
52+
53+
Process.Start(appPath);
5154
}
5255
catch (Exception ex)
5356
{
54-
throw new Exception($"GeneralClientOSS update exception ! {ex.Message}", ex.InnerException);
57+
EventManager.Instance.Dispatch(new GeneralClientOSS(), new ExceptionEventArgs(ex));
5558
}
5659
finally
5760
{
@@ -61,18 +64,18 @@ await Task.Run(() =>
6164
}
6265

6366
/// <summary>
64-
/// Determine whether the current client version needs to be upgraded.
67+
/// Determine whether the current client version needs to be upgraded.
6568
/// </summary>
6669
/// <param name="clientVersion"></param>
6770
/// <param name="serverVersion"></param>
6871
/// <returns>true: Upgrade required , false: No upgrade is required</returns>
6972
private static bool IsUpgrade(string clientVersion, string serverVersion)
7073
{
71-
if (string.IsNullOrWhiteSpace(clientVersion) || string.IsNullOrWhiteSpace(serverVersion)) return false;
72-
Version currentClientVersion = null;
73-
Version currentServerVersion = null;
74-
var isParseClientVersion = Version.TryParse(clientVersion, out currentClientVersion);
75-
var isParseServerVersion = Version.TryParse(serverVersion, out currentServerVersion);
74+
if (string.IsNullOrWhiteSpace(clientVersion) || string.IsNullOrWhiteSpace(serverVersion))
75+
return false;
76+
77+
var isParseClientVersion = Version.TryParse(clientVersion, out var currentClientVersion);
78+
var isParseServerVersion = Version.TryParse(serverVersion, out var currentServerVersion);
7679
if (!isParseClientVersion || !isParseServerVersion) return false;
7780
if (currentClientVersion < currentServerVersion) return true;
7881
return false;
@@ -89,7 +92,7 @@ public static void AddListenerException(Action<object, ExceptionEventArgs> callb
8992

9093
private static void AddListener<TArgs>(Action<object, TArgs> callbackAction) where TArgs : EventArgs
9194
{
92-
Contract.Requires(callbackAction != null);
95+
Debug.Assert(callbackAction != null);
9396
EventManager.Instance.AddListener(callbackAction);
9497
}
9598
}

src/c#/GeneralUpdate.Common/Shared/Object/ParamsOSS.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
using System;
2+
using System.Text.Json.Serialization;
23

34
namespace GeneralUpdate.Common.Shared.Object
45
{
56
public class ParamsOSS
67
{
8+
[JsonPropertyName("Url")]
79
public string Url { get; set; }
810

11+
[JsonPropertyName("AppName")]
912
public string AppName { get; set; }
1013

14+
[JsonPropertyName("CurrentVersion")]
1115
public string CurrentVersion { get; set; }
1216

17+
[JsonPropertyName("VersionFileName")]
1318
public string VersionFileName { get; set; }
1419

20+
public ParamsOSS()
21+
{
22+
}
23+
1524
public ParamsOSS(string url, string appName, string currentVersion, string versionFileName)
1625
{
1726
Url = url ?? throw new ArgumentNullException(nameof(url));
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace GeneralUpdate.Common.Shared.Object
1+
using System;
2+
using System.Text.Json.Serialization;
3+
4+
namespace GeneralUpdate.Common.Shared.Object
25
{
36
/// <summary>
47
/// Version data persistence.
@@ -8,26 +11,32 @@ public class VersionPO
811
/// <summary>
912
/// Update package release time.
1013
/// </summary>
11-
public long PubTime { get; set; }
14+
15+
[JsonPropertyName("PubTime")]
16+
public DateTime PubTime { get; set; }
1217

1318
/// <summary>
1419
/// Update package name.
1520
/// </summary>
16-
public string Name { get; set; }
21+
[JsonPropertyName("PacketName")]
22+
public string PacketName { get; set; }
1723

1824
/// <summary>
1925
/// Compare and verify with the downloaded update package.
2026
/// </summary>
27+
[JsonPropertyName("Hash")]
2128
public string Hash { get; set; }
2229

2330
/// <summary>
2431
/// The version number.
2532
/// </summary>
33+
[JsonPropertyName("Version")]
2634
public string Version { get; set; }
2735

2836
/// <summary>
2937
/// Remote service url address.
3038
/// </summary>
39+
[JsonPropertyName("Url")]
3140
public string Url { get; set; }
3241
}
3342
}
Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
24
using System.Diagnostics.Contracts;
5+
using System.IO;
36
using System.Runtime.InteropServices;
7+
using System.Text.Json;
48
using System.Threading.Tasks;
59
using GeneralUpdate.Common.Download;
10+
using GeneralUpdate.Common.FileBasic;
611
using GeneralUpdate.Common.Internal;
712
using GeneralUpdate.Common.Internal.Event;
813
using GeneralUpdate.Common.Internal.Strategy;
@@ -14,25 +19,15 @@ namespace GeneralUpdate.Core
1419
{
1520
public sealed class GeneralUpdateOSS
1621
{
17-
#region Constructors
18-
19-
private GeneralUpdateOSS()
20-
{ }
21-
22-
#endregion Constructors
22+
private GeneralUpdateOSS() { }
2323

2424
#region Public Methods
2525

2626
/// <summary>
2727
/// Starting an OSS update for windows,Linux,mac platform.
2828
/// </summary>
29-
/// <typeparam name="T"></typeparam>
30-
/// <param name="parameter"></param>
3129
/// <returns></returns>
32-
public static async Task Start(ParamsOSS parameter)
33-
{
34-
await BaseStart(parameter);
35-
}
30+
public static async Task Start()=> await BaseStart();
3631

3732
public static void AddListenerMultiAllDownloadCompleted(Action<object, MultiAllDownloadCompletedEventArgs> callbackAction)
3833
=> AddListener(callbackAction);
@@ -61,7 +56,7 @@ public static void AddListenerDownloadConfigProcess(Action<object, OSSDownloadAr
6156

6257
private static void AddListener<TArgs>(Action<object, TArgs> callbackAction) where TArgs : EventArgs
6358
{
64-
Contract.Requires(callbackAction != null);
59+
Debug.Assert(callbackAction != null);
6560
EventManager.Instance.AddListener(callbackAction);
6661
}
6762

@@ -71,27 +66,18 @@ private static void AddListener<TArgs>(Action<object, TArgs> callbackAction) whe
7166
/// <typeparam name="T">The class that needs to be injected with the corresponding platform update policy or inherits the abstract update policy.</typeparam>
7267
/// <param name="args">List of parameter.</param>
7368
/// <returns></returns>
74-
private static async Task BaseStart(ParamsOSS parameter)
69+
private static async Task BaseStart()
7570
{
76-
var strategy = StrategyFactory();
77-
//strategy.Create(parameter);
78-
//Implement different update strategies depending on the platform.
71+
var json = Environment.GetEnvironmentVariable("ParamsOSS", EnvironmentVariableTarget.User);
72+
if (string.IsNullOrWhiteSpace(json))
73+
return;
74+
75+
var parameter = JsonSerializer.Deserialize<ParamsOSS>(json);
76+
var strategy = new OSSStrategy();
77+
strategy.Create(parameter);
7978
await strategy.ExecuteAsync();
8079
}
8180

82-
private static IStrategy StrategyFactory()
83-
{
84-
IStrategy strategy = null;
85-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
86-
strategy = new WindowsStrategy();
87-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
88-
strategy = new LinuxStrategy();
89-
else
90-
throw new PlatformNotSupportedException("The current operating system is not supported!");
91-
92-
return strategy;
93-
}
94-
9581
#endregion Private Methods
9682
}
9783
}

0 commit comments

Comments
 (0)