|
6 | 6 |
|
7 | 7 | namespace GeneralUpdate.Tools.Services; |
8 | 8 |
|
| 9 | +/// <summary> |
| 10 | +/// Generates single-file client.csx and upgrade.csx for simulation, |
| 11 | +/// using dotnet script with #r directives (exact NuGet version). |
| 12 | +/// </summary> |
9 | 13 | public class ClientGeneratorService |
10 | 14 | { |
11 | | - private const string ClientScript = """ |
| 15 | + private const string ClientTemplate = """ |
12 | 16 | #r "nuget:GeneralUpdate.ClientCore,10.4.6" |
13 | 17 |
|
14 | 18 | using GeneralUpdate.ClientCore; |
@@ -66,7 +70,7 @@ public class ClientGeneratorService |
66 | 70 | }} |
67 | 71 | """; |
68 | 72 |
|
69 | | - private const string UpgradeScript = """ |
| 73 | + private const string UpgradeTemplate = """ |
70 | 74 | #r "nuget:GeneralUpdate.Core,10.4.6" |
71 | 75 |
|
72 | 76 | using GeneralUpdate.Core; |
@@ -104,110 +108,28 @@ public class ClientGeneratorService |
104 | 108 | Console.Error.WriteLine(ex); |
105 | 109 | Environment.Exit(1); |
106 | 110 | }} |
107 | | -"""; |
108 | | - |
109 | | - private const string UpgradeCsproj = """ |
110 | | -<Project Sdk="Microsoft.NET.Sdk"> |
111 | | - <PropertyGroup> |
112 | | - <OutputType>Exe</OutputType> |
113 | | - <TargetFramework>net10.0</TargetFramework> |
114 | | - <Nullable>enable</Nullable> |
115 | | - <ImplicitUsings>enable</ImplicitUsings> |
116 | | - </PropertyGroup> |
117 | | - <ItemGroup> |
118 | | - <PackageReference Include="GeneralUpdate.Core" Version="10.4.6" /> |
119 | | - </ItemGroup> |
120 | | -</Project> |
121 | | -"""; |
122 | | - |
123 | | - private const string UpgradeProgram = """ |
124 | | -using GeneralUpdate.Core; |
125 | | -using GeneralUpdate.Common.Shared; |
126 | | -using GeneralUpdate.Common.Internal.Event; |
127 | | -
|
128 | | -var log = (string msg) => Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] {{msg}}"); |
129 | | -
|
130 | | -try |
131 | | -{ |
132 | | - log("Upgrade process started"); |
133 | | - log("Working directory: " + Environment.CurrentDirectory); |
134 | | -
|
135 | | - await new GeneralUpdateBootstrap() |
136 | | - .AddListenerMultiDownloadStatistics((_, e) => |
137 | | - { |
138 | | - var v = e.Version as GeneralUpdate.Common.Shared.Object.VersionInfo; |
139 | | - log($"Download: {{v?.Version}} {{e.ProgressPercentage}}%"); |
140 | | - }) |
141 | | - .AddListenerMultiAllDownloadCompleted((_, e) => |
142 | | - { |
143 | | - log(e.IsAllDownloadCompleted ? "Downloads done" : "Download failed"); |
144 | | - }) |
145 | | - .AddListenerException((_, e) => |
146 | | - { |
147 | | - log($"ERROR: {{e.Exception}}"); |
148 | | - }) |
149 | | - .LaunchAsync(); |
150 | | -
|
151 | | - log("Upgrade process finished successfully"); |
152 | | -} |
153 | | -catch (Exception ex) |
154 | | -{ |
155 | | - log($"FATAL: {{ex.Message}}"); |
156 | | - Console.Error.WriteLine(ex); |
157 | | - Environment.Exit(1); |
158 | | -} |
159 | 111 | """; |
160 | 112 |
|
161 | 113 | public async Task GenerateAsync(SimulateConfigModel config, string outputDir) |
162 | 114 | { |
163 | 115 | var serverUrl = $"http://127.0.0.1:{config.ServerPort}"; |
164 | | - var appName = config.CompileUpgrade ? "upgrade.exe" : "upgrade.csx"; |
165 | 116 |
|
166 | 117 | await File.WriteAllTextAsync(Path.Combine(outputDir, "client.csx"), |
167 | | - string.Format(ClientScript, |
| 118 | + string.Format(ClientTemplate, |
168 | 119 | EscapeForCSharp(config.AppDirectory), |
169 | 120 | serverUrl, |
170 | | - appName, |
| 121 | + "upgrade.csx", |
171 | 122 | "client.csx", |
172 | 123 | config.CurrentVersion, |
173 | 124 | "1.0.0.0", |
174 | 125 | config.ProductId, |
175 | 126 | config.AppSecretKey), |
176 | 127 | Encoding.UTF8); |
177 | 128 |
|
178 | | - if (config.CompileUpgrade) |
179 | | - { |
180 | | - var upgradeDir = Path.Combine(outputDir, "upgrade"); |
181 | | - Directory.CreateDirectory(upgradeDir); |
182 | | - await File.WriteAllTextAsync(Path.Combine(upgradeDir, "upgrade.csproj"), UpgradeCsproj, Encoding.UTF8); |
183 | | - await File.WriteAllTextAsync(Path.Combine(upgradeDir, "Program.cs"), |
184 | | - UpgradeProgram, Encoding.UTF8); |
185 | | - } |
186 | | - else |
187 | | - { |
188 | | - await File.WriteAllTextAsync(Path.Combine(outputDir, "upgrade.csx"), |
189 | | - string.Format(UpgradeScript, EscapeForCSharp(config.AppDirectory)), Encoding.UTF8); |
190 | | - } |
191 | | - } |
192 | | - |
193 | | - public async Task PublishUpgradeAsync(string outputDir) |
194 | | - { |
195 | | - var upgradeDir = Path.Combine(outputDir, "upgrade"); |
196 | | - var psi = new System.Diagnostics.ProcessStartInfo("dotnet", "publish -c Release -r win-x64 --self-contained false -o .") |
197 | | - { |
198 | | - WorkingDirectory = upgradeDir, |
199 | | - RedirectStandardOutput = true, |
200 | | - RedirectStandardError = true, |
201 | | - UseShellExecute = false, |
202 | | - CreateNoWindow = true |
203 | | - }; |
204 | | - var p = System.Diagnostics.Process.Start(psi)!; |
205 | | - await p.WaitForExitAsync(); |
206 | | - // Copy the published exe to the output root |
207 | | - var src = Path.Combine(upgradeDir, "upgrade.exe"); |
208 | | - var dst = Path.Combine(outputDir, "upgrade.exe"); |
209 | | - if (File.Exists(src)) |
210 | | - File.Copy(src, dst, true); |
| 129 | + await File.WriteAllTextAsync(Path.Combine(outputDir, "upgrade.csx"), |
| 130 | + string.Format(UpgradeTemplate, |
| 131 | + EscapeForCSharp(config.AppDirectory)), |
| 132 | + Encoding.UTF8); |
211 | 133 | } |
212 | 134 |
|
213 | 135 | private static string EscapeForCSharp(string s) => |
|
0 commit comments