build: Enable AOT

Add the JSON code generation needed to make my code fully trimmable, and
therefore able to be published with full native AOT compilation.

NOTE: This may not guarantee stable builds as the logging library
serilog is still not fully trimmable, I have made changes to my logging
statements to avoid destructuring, but there is a chance that a
published build could randomly crash.
The only solution is to either wait for serilog to become fully
trimmable, or to remove the logging from AOT builds
This commit is contained in:
Robert Morrison 2024-06-03 04:04:16 +01:00
parent af1e340816
commit 53344de6ea
Signed by: robert
GPG Key ID: 73E012EB3F4EC696
4 changed files with 33 additions and 20 deletions

View File

@ -26,6 +26,7 @@ using System.Diagnostics;
using System.Reflection;
using Spectre.Console;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace csSiteGen;
@ -153,14 +154,16 @@ class Program
List<SiteFile> siteFiles = new();
Utils.GetFiles(settings.InputDirectory).ForEach(x => siteFiles.Add(new SiteFile(x)));
Log.Information("SiteFiles: {@sf} {count}", siteFiles, siteFiles.Count);
Utils.GetFiles(settings.InputDirectory).ForEach(x => {
siteFiles.Add(new SiteFile(x));
Log.Debug("Found file {file}",x.FullName);
});
Log.Information("SiteFiles Found {count}", siteFiles, siteFiles.Count);
Console.WriteLine($"Converting {siteFiles.Count} files from {settings.InputDirectory.FullName} to {settings.OutputDirectory.FullName}");
Dictionary<string,bool> fileStatus = new();
Log.Debug("fileStatus {@fileStatus}",fileStatus);
AnsiConsole.Progress()
.AutoRefresh(true)
@ -190,15 +193,12 @@ class Program
Log.Warning("{name} Failed...",siteFiles[i].FullName);
tasks[i].Description += " [red]FAILED[/]";
}
Log.Information("adding {@siteFile} conversion status to fileStatus", siteFiles[i]);
Log.Debug("FileStatus {@fileStatus}",fileStatus);
Log.Information("adding {siteFile} conversion status to fileStatus", siteFiles[i].FullName);
fileStatus.Add(siteFiles[i].FullName,res);
overallTask.Increment(1);
}
});
Log.Information("Conversion Status {@status}", fileStatus);
var Failed = fileStatus.Where(x => x.Value == false);
if ( Failed.Count() > 0)
{
@ -289,7 +289,8 @@ class Program
.Deserialize<ProjectSettings>(
projectFile
.OpenText()
.ReadToEnd()
.ReadToEnd(),
SourceGenerationContext.Default.ProjectSettings
);
if (projectSettings is null)
@ -299,8 +300,13 @@ class Program
}
projectSettings.setProjectRoot(ProjectDirectory);
Log.Information("{@ps}",projectSettings);
return projectSettings;
}
}
[JsonSourceGenerationOptions(WriteIndented = true)]
[JsonSerializable(typeof(ProjectSettings))]
internal partial class SourceGenerationContext : JsonSerializerContext
{
}

View File

@ -1,4 +1,5 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Serilog;
namespace csSiteGen;
@ -58,7 +59,7 @@ public partial class SiteFile
{
Metadata.Add(info.FullName,info.LastWriteTimeUtc);
}
Log.Debug("Metadata Dictionary now {@Metadata}", Metadata);
Log.Debug("Metadata Dictionary now has {Metadata} items", Metadata.Count);
SaveMetadata(settings);
}
@ -95,8 +96,8 @@ public partial class SiteFile
{
string metaJson = File.ReadAllText(metaFile);
Log.Debug("Read Json {metaJson}", metaJson);
Metadata = JsonSerializer.Deserialize<Dictionary<string, DateTime>>(metaJson);
Log.Debug("Deserialized to {@Metadata}",Metadata);
Metadata = JsonSerializer.Deserialize<Dictionary<string, DateTime>>(metaJson,SerializeMetadataContext.Default.DictionaryStringDateTime);
Log.Debug("Deserialized Metadata with {c} items",Metadata!.Count);
}
catch (IOException e)
{
@ -124,7 +125,7 @@ public partial class SiteFile
string metaJson;
try
{
metaJson = JsonSerializer.Serialize<Dictionary<string, DateTime>>(Metadata);
metaJson = JsonSerializer.Serialize<Dictionary<string, DateTime>>(Metadata,SerializeMetadataContext.Default.DictionaryStringDateTime);
Log.Debug("metaJson: {metaJson}",metaJson);
}
catch (JsonException e)
@ -136,3 +137,9 @@ public partial class SiteFile
File.WriteAllText(metaFile, metaJson);
}
}
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Serialization)]
[JsonSerializable(typeof(Dictionary<string,DateTime>))]
internal partial class SerializeMetadataContext : JsonSerializerContext
{
}

View File

@ -48,7 +48,7 @@ public static partial class Utils {
result = candidateExecutables.First();
PathSearchMemo.Add(Program,result);
Log.Information("Found {program} at {path}",Program, result);
Log.Debug("Adding {@entry} to PathSearchMemo", PathSearchMemo.Last());
Log.Debug("Adding to PathSearchMemo", PathSearchMemo.Last().Value);
return result;
}
}

View File

@ -16,10 +16,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog" Version="4.0.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
<PackageReference Include="spectre.console" Version="0.48.0" />
<PackageReference Include="spectre.console" Version="0.49.1" />
<PackageReference Include="system.commandline" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
@ -37,8 +37,8 @@
NOTE: This build requires specifying a platform
-->
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<PublishReadyToRun>true</PublishReadyToRun>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<PublishAOT>true</PublishAOT>
<SuppressTrimAnalysisWarnings>false</SuppressTrimAnalysisWarnings>
<TrimmerSingleWarn>false</TrimmerSingleWarn>
</PropertyGroup>
</Project>