-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathClientGeneratorService.cs
More file actions
141 lines (122 loc) · 4.09 KB
/
Copy pathClientGeneratorService.cs
File metadata and controls
141 lines (122 loc) · 4.09 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
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using GeneralUpdate.Tools.Models;
namespace GeneralUpdate.Tools.Services;
/// <summary>
/// Generates single-file client.csx and upgrade.csx for simulation,
/// using dotnet script with #r directives (exact NuGet version).
/// </summary>
public class ClientGeneratorService
{
private const string ClientTemplate = """
#r "nuget:GeneralUpdate.ClientCore,10.4.6"
using GeneralUpdate.ClientCore;
using GeneralUpdate.Common.Shared.Object;
using GeneralUpdate.Common.Internal.Event;
var log = (string msg) => Console.WriteLine($"[{{DateTime.Now:HH:mm:ss}}] {{msg}}");
try
{{
log("Client started");
log("Install path: {0}");
var config = new Configinfo
{{
ReportUrl = "{1}/Upgrade/Report",
UpdateUrl = "{1}/Upgrade/Verification",
AppName = "{2}",
MainAppName = "{3}",
InstallPath = @"{0}",
ClientVersion = "{4}",
UpgradeClientVersion = "{5}",
ProductId = "{6}",
AppSecretKey = "{7}",
}};
await new GeneralClientBootstrap()
.SetConfig(config)
.AddListenerMultiDownloadStatistics((_, e) =>
{{
var v = e.Version as VersionInfo;
log($"Download: {{v?.Version}} {{e.ProgressPercentage}}% {{e.Speed}}/s");
}})
.AddListenerMultiAllDownloadCompleted((_, e) =>
{{
log(e.IsAllDownloadCompleted ? "All downloads completed" : $"Download failed: {{e.FailedVersions.Count}}");
}})
.AddListenerException((_, e) =>
{{
log($"ERROR: {{e.Exception}}");
}})
.AddListenerUpdateInfo((_, e) =>
{{
log($"Update info: Code={{e.Info.Code}}, Versions={{e.Info.Body?.Count ?? 0}}");
}})
.LaunchAsync();
log("Update process completed");
}}
catch (Exception ex)
{{
log($"FATAL: {{ex.Message}}");
Console.Error.WriteLine(ex);
Environment.Exit(1);
}}
""";
private const string UpgradeTemplate = """
#r "nuget:GeneralUpdate.Core,10.4.6"
using GeneralUpdate.Core;
using GeneralUpdate.Common.Shared;
using GeneralUpdate.Common.Internal.Event;
var log = (string msg) => Console.WriteLine($"[{{DateTime.Now:HH:mm:ss}}] {{msg}}");
try
{{
log("Upgrade process started");
log("Working directory: " + Environment.CurrentDirectory);
await new GeneralUpdateBootstrap()
.AddListenerMultiDownloadStatistics((_, e) =>
{{
var v = e.Version as GeneralUpdate.Common.Shared.Object.VersionInfo;
log($"Download: {{v?.Version}} {{e.ProgressPercentage}}%");
}})
.AddListenerMultiAllDownloadCompleted((_, e) =>
{{
log(e.IsAllDownloadCompleted ? "Downloads done" : "Download failed");
}})
.AddListenerException((_, e) =>
{{
log($"ERROR: {{e.Exception}}");
}})
.LaunchAsync();
log("Upgrade process finished successfully");
}}
catch (Exception ex)
{{
log($"FATAL: {{ex.Message}}");
Console.Error.WriteLine(ex);
Environment.Exit(1);
}}
""";
public async Task GenerateAsync(SimulateConfigModel config, string outputDir)
{
var serverUrl = $"http://127.0.0.1:{config.ServerPort}";
await File.WriteAllTextAsync(Path.Combine(outputDir, "client.csx"),
string.Format(ClientTemplate,
EscapeForCSharp(config.AppDirectory),
serverUrl,
"upgrade.bat",
"client.csx",
config.CurrentVersion,
"1.0.0.0",
config.ProductId,
config.AppSecretKey),
Encoding.UTF8);
await File.WriteAllTextAsync(Path.Combine(outputDir, "upgrade.csx"),
string.Format(UpgradeTemplate,
EscapeForCSharp(config.AppDirectory)),
Encoding.UTF8);
await File.WriteAllTextAsync(Path.Combine(outputDir, "upgrade.bat"),
"@echo off\r\ndotnet script upgrade.csx\r\n",
Encoding.ASCII);
}
private static string EscapeForCSharp(string s) =>
s.Replace(@"\", @"\\").Replace("\"", "\\\"");
}