Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
if: matrix.arch == 'x64'
run: dotnet test tests\BrowserWrangler.Core.Tests -c Release

- name: Publish (self-contained, ReadyToRun)
- name: Publish (self-contained, trimmed, ReadyToRun)
run: >
dotnet publish src\BrowserWrangler -c Release
-r win-${{ matrix.arch }}
Expand Down
12 changes: 12 additions & 0 deletions src/BrowserWrangler.Core/Configuration/AppConfigJsonContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace BrowserWrangler.Core.Configuration;

/// <summary>Source-generated JSON contract for <see cref="AppConfig"/> so config
/// (de)serialization keeps working under assembly trimming.</summary>
[JsonSourceGenerationOptions(
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
UseStringEnumConverter = true)]
[JsonSerializable(typeof(AppConfig))]
internal sealed partial class AppConfigJsonContext : JsonSerializerContext;
11 changes: 2 additions & 9 deletions src/BrowserWrangler.Core/Configuration/ConfigStore.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using BrowserWrangler.Core.Models;

namespace BrowserWrangler.Core.Configuration;
Expand All @@ -10,12 +9,6 @@ namespace BrowserWrangler.Core.Configuration;
/// </summary>
public sealed class ConfigStore
{
private static readonly JsonSerializerOptions Options = new()
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
Converters = { new JsonStringEnumConverter() },
};
private static readonly string DefaultManagedUpdatesDirectoryPath = Path.GetFullPath(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"BrowserWrangler",
Expand Down Expand Up @@ -50,7 +43,7 @@ public AppConfig Load(Version? runningVersion = null)
try
{
using FileStream stream = File.OpenRead(ConfigFilePath);
config = JsonSerializer.Deserialize<AppConfig>(stream, Options) ?? new AppConfig();
config = JsonSerializer.Deserialize(stream, AppConfigJsonContext.Default.AppConfig) ?? new AppConfig();
}
catch (JsonException)
{
Expand All @@ -75,7 +68,7 @@ public void Save(AppConfig config)
string tmp = ConfigFilePath + ".tmp";
using (FileStream stream = File.Create(tmp))
{
JsonSerializer.Serialize(stream, config, Options);
JsonSerializer.Serialize(stream, config, AppConfigJsonContext.Default.AppConfig);
}

File.Move(tmp, ConfigFilePath, overwrite: true);
Expand Down
8 changes: 8 additions & 0 deletions src/BrowserWrangler.Core/Logging/RuleHitLogJsonContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Text.Json.Serialization;

namespace BrowserWrangler.Core.Logging;

/// <summary>Source-generated JSON contract for <see cref="RuleHitLogEntry"/> so the
/// rule-hit log keeps working under assembly trimming.</summary>
[JsonSerializable(typeof(RuleHitLogEntry))]
internal sealed partial class RuleHitLogJsonContext : JsonSerializerContext;
9 changes: 2 additions & 7 deletions src/BrowserWrangler.Core/Logging/RuleHitLogStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ public sealed class RuleHitLogStore
{
public const int MaxRetainedEntries = 500;

private static readonly JsonSerializerOptions Options = new()
{
WriteIndented = false,
};

public RuleHitLogStore()
: this(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
Expand All @@ -35,7 +30,7 @@ public void Append(RuleHitLogEntry entry)
Directory.CreateDirectory(dir);
}

string line = JsonSerializer.Serialize(entry, Options);
string line = JsonSerializer.Serialize(entry, RuleHitLogJsonContext.Default.RuleHitLogEntry);
byte[] payload = Encoding.UTF8.GetBytes(line + Environment.NewLine);
using (var stream = new FileStream(LogFilePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
{
Expand Down Expand Up @@ -64,7 +59,7 @@ public IReadOnlyList<RuleHitLogEntry> ReadLatest(int maxEntries = 200)

try
{
RuleHitLogEntry? entry = JsonSerializer.Deserialize<RuleHitLogEntry>(line, Options);
RuleHitLogEntry? entry = JsonSerializer.Deserialize(line, RuleHitLogJsonContext.Default.RuleHitLogEntry);
if (entry is not null)
{
latestWindow.Enqueue(entry);
Expand Down
6 changes: 6 additions & 0 deletions src/BrowserWrangler/BrowserWrangler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
<EnableMsixTooling>true</EnableMsixTooling>
<WindowsPackageType>None</WindowsPackageType>
<WindowsAppSDKSelfContained>true</WindowsAppSDKSelfContained>
<!-- Trim unused framework/SDK code on publish; JSON uses source-generated
contexts and the UI avoids reflection-based XAML bindings by populating views in code-behind.
Partial mode processes only assemblies that explicitly opt in to trimming;
dependencies that are not trim-compatible are copied intact. -->
<PublishTrimmed>true</PublishTrimmed>
Comment thread
damianh marked this conversation as resolved.
<TrimMode>partial</TrimMode>
<DefineConstants>$(DefineConstants);DISABLE_XAML_GENERATED_MAIN</DefineConstants>
<ApplicationIcon>Assets\app.ico</ApplicationIcon>
<Nullable>enable</Nullable>
Expand Down