|
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> |
13 | 9 | public class ClientGeneratorService |
14 | 10 | { |
15 | | - private const string ClientTemplate = """ |
| 11 | + private const string ClientScript = """ |
16 | 12 | #r "nuget:GeneralUpdate.ClientCore,10.4.6" |
17 | 13 |
|
18 | 14 | using GeneralUpdate.ClientCore; |
@@ -70,7 +66,7 @@ public class ClientGeneratorService |
70 | 66 | }} |
71 | 67 | """; |
72 | 68 |
|
73 | | - private const string UpgradeTemplate = """ |
| 69 | + private const string UpgradeScript = """ |
74 | 70 | #r "nuget:GeneralUpdate.Core,10.4.6" |
75 | 71 |
|
76 | 72 | using GeneralUpdate.Core; |
@@ -108,28 +104,110 @@ public class ClientGeneratorService |
108 | 104 | Console.Error.WriteLine(ex); |
109 | 105 | Environment.Exit(1); |
110 | 106 | }} |
| 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 | +} |
111 | 159 | """; |
112 | 160 |
|
113 | 161 | public async Task GenerateAsync(SimulateConfigModel config, string outputDir) |
114 | 162 | { |
115 | 163 | var serverUrl = $"http://127.0.0.1:{config.ServerPort}"; |
| 164 | + var appName = config.CompileUpgrade ? "upgrade.exe" : "upgrade.csx"; |
116 | 165 |
|
117 | 166 | await File.WriteAllTextAsync(Path.Combine(outputDir, "client.csx"), |
118 | | - string.Format(ClientTemplate, |
| 167 | + string.Format(ClientScript, |
119 | 168 | EscapeForCSharp(config.AppDirectory), |
120 | 169 | serverUrl, |
121 | | - "upgrade.csx", |
| 170 | + appName, |
122 | 171 | "client.csx", |
123 | 172 | config.CurrentVersion, |
124 | 173 | "1.0.0.0", |
125 | 174 | config.ProductId, |
126 | 175 | config.AppSecretKey), |
127 | 176 | Encoding.UTF8); |
128 | 177 |
|
129 | | - await File.WriteAllTextAsync(Path.Combine(outputDir, "upgrade.csx"), |
130 | | - string.Format(UpgradeTemplate, |
131 | | - EscapeForCSharp(config.AppDirectory)), |
132 | | - Encoding.UTF8); |
| 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); |
133 | 211 | } |
134 | 212 |
|
135 | 213 | private static string EscapeForCSharp(string s) => |
|
0 commit comments