77namespace GeneralUpdate . Tools . Services ;
88
99/// <summary>
10- /// Generates client and upgrade console projects for simulation,
11- /// each with a minimal .csproj + Program.cs using dotnet run --project .
10+ /// Generates single-file client.cs and upgrade.cs for simulation,
11+ /// using dotnet run with #r directives (exact NuGet version) .
1212/// </summary>
1313public class ClientGeneratorService
1414{
15- private const string ClientCsproj = """
16- <Project Sdk="Microsoft.NET.Sdk">
17- <PropertyGroup>
18- <OutputType>Exe</OutputType>
19- <TargetFramework>net10.0</TargetFramework>
20- <Nullable>enable</Nullable>
21- <ImplicitUsings>enable</ImplicitUsings>
22- </PropertyGroup>
23- <ItemGroup>
24- <PackageReference Include="GeneralUpdate.ClientCore" Version="10.*" />
25- <PackageReference Include="GeneralUpdate.Core" Version="10.*" />
26- </ItemGroup>
27- </Project>
28- """ ;
15+ private const string ClientTemplate = """
16+ #r "nuget:GeneralUpdate.ClientCore,10.4.6"
17+ #r "nuget:GeneralUpdate.Core,10.4.6"
18+
19+ using GeneralUpdate.ClientCore;
20+ using GeneralUpdate.Common.Shared.Object;
21+ using GeneralUpdate.Common.Internal.Event;
2922
30- private const string ClientProgram = """
31- var log = (string msg) => Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] {msg}");
23+ var log = (string msg) => Console.WriteLine($"[{{DateTime.Now:HH:mm:ss}}] {{msg}}");
3224
3325try
34- {
26+ {{
3527 log("Client started");
3628 log("Install path: {0}");
3729
38- var config = new GeneralUpdate.Common.Shared.Object. Configinfo
39- {
30+ var config = new Configinfo
31+ {{
4032 ReportUrl = "{1}/Upgrade/Report",
4133 UpdateUrl = "{1}/Upgrade/Verification",
4234 AppName = "{2}",
@@ -46,114 +38,98 @@ public class ClientGeneratorService
4638 UpgradeClientVersion = "{5}",
4739 ProductId = "{6}",
4840 AppSecretKey = "{7}",
49- };
41+ }} ;
5042
51- await new GeneralUpdate.ClientCore. GeneralClientBootstrap()
43+ await new GeneralClientBootstrap()
5244 .SetConfig(config)
5345 .AddListenerMultiDownloadStatistics((_, e) =>
54- {
55- var v = e.Version as GeneralUpdate.Common.Shared.Object. VersionInfo;
56- log($"Download: {v?.Version} { e.ProgressPercentage}% {e.Speed}/s");
57- })
46+ {{
47+ var v = e.Version as VersionInfo;
48+ log($"Download: {{ v?.Version}} {{ e.ProgressPercentage}} % {{ e.Speed} }/s");
49+ }} )
5850 .AddListenerMultiAllDownloadCompleted((_, e) =>
59- {
60- log(e.IsAllDownloadCompleted ? "All downloads completed" : $"Download failed: {e.FailedVersions.Count}");
61- })
51+ {{
52+ log(e.IsAllDownloadCompleted ? "All downloads completed" : $"Download failed: {{ e.FailedVersions.Count} }");
53+ }} )
6254 .AddListenerException((_, e) =>
63- {
64- log($"ERROR: {e.Exception}");
65- })
55+ {{
56+ log($"ERROR: {{ e.Exception} }");
57+ }} )
6658 .AddListenerUpdateInfo((_, e) =>
67- {
68- log($"Update info: Code={e.Info.Code}, Versions={e.Info.Body?.Count ?? 0}");
69- })
59+ {{
60+ log($"Update info: Code={{ e.Info.Code}} , Versions={{ e.Info.Body?.Count ?? 0} }");
61+ }} )
7062 .LaunchAsync();
7163
7264 log("Update process completed");
73- }
65+ }}
7466catch (Exception ex)
75- {
76- log($"FATAL: {ex.Message}");
67+ {{
68+ log($"FATAL: {{ ex.Message} }");
7769 Console.Error.WriteLine(ex);
7870 Environment.Exit(1);
79- }
71+ }}
8072""" ;
8173
82- private const string UpgradeCsproj = """
83- <Project Sdk="Microsoft.NET.Sdk">
84- <PropertyGroup>
85- <OutputType>Exe</OutputType>
86- <TargetFramework>net10.0</TargetFramework>
87- <Nullable>enable</Nullable>
88- <ImplicitUsings>enable</ImplicitUsings>
89- </PropertyGroup>
90- <ItemGroup>
91- <PackageReference Include="GeneralUpdate.Core" Version="10.*" />
92- <PackageReference Include="GeneralUpdate.ClientCore" Version="10.*" />
93- </ItemGroup>
94- </Project>
95- """ ;
74+ private const string UpgradeTemplate = """
75+ #r "nuget:GeneralUpdate.Core,10.4.6"
76+ #r "nuget:GeneralUpdate.ClientCore,10.4.6"
9677
97- private const string UpgradeProgram = """
98- var log = (string msg) => Console.WriteLine($"[{DateTime.Now:HH:mm:ss}] {msg}");
78+ using GeneralUpdate.Core;
79+ using GeneralUpdate.Common.Shared;
80+ using GeneralUpdate.Common.Internal.Event;
81+
82+ var log = (string msg) => Console.WriteLine($"[{{DateTime.Now:HH:mm:ss}}] {{msg}}");
9983
10084try
101- {
85+ {{
10286 log("Upgrade process started");
10387 log("Working directory: " + Environment.CurrentDirectory);
10488
105- await new GeneralUpdate.Core. GeneralUpdateBootstrap()
89+ await new GeneralUpdateBootstrap()
10690 .AddListenerMultiDownloadStatistics((_, e) =>
107- {
91+ {{
10892 var v = e.Version as GeneralUpdate.Common.Shared.Object.VersionInfo;
109- log($"Download: {v?.Version} { e.ProgressPercentage}%");
110- })
93+ log($"Download: {{ v?.Version}} {{ e.ProgressPercentage} }%");
94+ }} )
11195 .AddListenerMultiAllDownloadCompleted((_, e) =>
112- {
96+ {{
11397 log(e.IsAllDownloadCompleted ? "Downloads done" : "Download failed");
114- })
98+ }} )
11599 .AddListenerException((_, e) =>
116- {
117- log($"ERROR: {e.Exception}");
118- })
100+ {{
101+ log($"ERROR: {{ e.Exception} }");
102+ }} )
119103 .LaunchAsync();
120104
121105 log("Upgrade process finished successfully");
122- }
106+ }}
123107catch (Exception ex)
124- {
125- log($"FATAL: {ex.Message}");
108+ {{
109+ log($"FATAL: {{ ex.Message} }");
126110 Console.Error.WriteLine(ex);
127111 Environment.Exit(1);
128- }
112+ }}
129113""" ;
130114
131115 public async Task GenerateAsync ( SimulateConfigModel config , string outputDir )
132116 {
133117 var serverUrl = $ "http://127.0.0.1:{ config . ServerPort } ";
134118
135- // client/
136- var clientDir = Path . Combine ( outputDir , "client" ) ;
137- Directory . CreateDirectory ( clientDir ) ;
138- await File . WriteAllTextAsync ( Path . Combine ( clientDir , "client.csproj" ) , ClientCsproj , Encoding . UTF8 ) ;
139- await File . WriteAllTextAsync ( Path . Combine ( clientDir , "Program.cs" ) ,
140- string . Format ( ClientProgram ,
119+ await File . WriteAllTextAsync ( Path . Combine ( outputDir , "client.cs" ) ,
120+ string . Format ( ClientTemplate ,
141121 EscapeForCSharp ( config . AppDirectory ) ,
142122 serverUrl ,
143- "upgrade.exe" , // AppName
144- "client.exe" , // MainAppName
123+ "upgrade.cs" ,
124+ "client.cs" ,
145125 config . CurrentVersion ,
146- "1.0.0.0" , // upgrade client version
126+ "1.0.0.0" ,
147127 config . ProductId ,
148128 config . AppSecretKey ) ,
149129 Encoding . UTF8 ) ;
150130
151- // upgrade/
152- var upgradeDir = Path . Combine ( outputDir , "upgrade" ) ;
153- Directory . CreateDirectory ( upgradeDir ) ;
154- await File . WriteAllTextAsync ( Path . Combine ( upgradeDir , "upgrade.csproj" ) , UpgradeCsproj , Encoding . UTF8 ) ;
155- await File . WriteAllTextAsync ( Path . Combine ( upgradeDir , "Program.cs" ) ,
156- string . Format ( UpgradeProgram ,
131+ await File . WriteAllTextAsync ( Path . Combine ( outputDir , "upgrade.cs" ) ,
132+ string . Format ( UpgradeTemplate ,
157133 EscapeForCSharp ( config . AppDirectory ) ) ,
158134 Encoding . UTF8 ) ;
159135 }
0 commit comments