Skip to content
Open
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
1 change: 1 addition & 0 deletions Golang.Org.X.Mod/Golang.Org.X.Mod.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions Lip.Context/Lip.Context.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Lip.Core/Lip.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAotCompatible>true</IsAotCompatible>
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>

<ItemGroup>
Expand Down
21 changes: 17 additions & 4 deletions Lip.Core/PackageLock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public async Task ToStream(Stream stream)
}

[ExcludeFromCodeCoverage]
file record RawPackageLock
internal record RawPackageLock
{
public record Package
{
Expand Down Expand Up @@ -166,14 +166,27 @@ public record Package

public static async Task<RawPackageLock> FromStream(Stream stream)
{
return await JsonSerializer.DeserializeAsync<RawPackageLock>(
return await JsonSerializer.DeserializeAsync(
stream,
_jsonSerializerOptions)
RawPackageLockJsonContext.Default.RawPackageLock)
?? throw new SchemaViolationException("", "JSON bytes deserialized to null.");
}

public async Task ToStream(Stream stream)
{
await JsonSerializer.SerializeAsync(stream, this, _jsonSerializerOptions);
await JsonSerializer.SerializeAsync(stream, this, RawPackageLockJsonContext.Default.RawPackageLock);
}
}

[JsonSourceGenerationOptions(
AllowTrailingCommas = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
IndentSize = 4,
ReadCommentHandling = JsonCommentHandling.Skip,
WriteIndented = true,
GenerationMode = JsonSourceGenerationMode.Metadata
)]
[JsonSerializable(typeof(RawPackageLock))]
internal partial class RawPackageLockJsonContext : JsonSerializerContext
{
}
35 changes: 25 additions & 10 deletions Lip.Core/PackageManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ public static PackageManifest FromJsonElement(JsonElement jsonElement)
kvp => (kvp.Value.ValueKind == JsonValueKind.Array
&& kvp.Value.EnumerateArray()
.All(elem => elem.ValueKind == JsonValueKind.String))
? kvp.Value.Deserialize<List<string>>()!
? kvp.Value.Deserialize(RawPackageManifestJsonContext.Default.ListString)!
: throw new SchemaViolationException(
$"variants[].assets[].scripts.'{kvp.Key}'",
$"Invalid script list"
Expand Down Expand Up @@ -418,7 +418,7 @@ public JsonElement ToJsonElement()
PostUninstall = variant.Scripts.PostUninstall,
AdditionalProperties = variant.Scripts.AdditionalScripts.ToDictionary(
kvp => kvp.Key,
kvp => JsonSerializer.SerializeToElement(kvp.Value, _jsonSerializerOptions)
kvp => JsonSerializer.SerializeToElement(kvp.Value, RawPackageManifestJsonContext.Default.ListString)
)
}
})
Expand All @@ -437,11 +437,11 @@ public async Task ToStream(Stream stream)
}

[ExcludeFromCodeCoverage]
file record RawPackageManifest
internal record RawPackageManifest
{
public record Asset
{
[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonConverter(typeof(JsonStringEnumConverter<TypeEnum>))]
public enum TypeEnum
{
[JsonStringEnumMemberName("self")]
Expand Down Expand Up @@ -483,7 +483,7 @@ public record InfoType

public record Placement
{
[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonConverter(typeof(JsonStringEnumConverter<TypeEnum>))]
public enum TypeEnum
{
[JsonStringEnumMemberName("file")]
Expand Down Expand Up @@ -529,7 +529,7 @@ public record ScriptsType
public List<string>? PostUninstall { get; init; }

[JsonExtensionData]
public Dictionary<string, JsonElement>? AdditionalProperties { get; init; }
public Dictionary<string, JsonElement>? AdditionalProperties { get; set; }
}

public record Variant
Expand Down Expand Up @@ -585,20 +585,20 @@ public record Variant

public static RawPackageManifest FromJsonElement(JsonElement jsonElement)
{
return JsonSerializer.Deserialize<RawPackageManifest>(
return JsonSerializer.Deserialize(
jsonElement,
_jsonSerializerOptions)
RawPackageManifestJsonContext.Default.RawPackageManifest)
?? throw new SchemaViolationException("", "JSON bytes deserialized to null.");
}

public JsonElement ToJsonElement()
{
return JsonSerializer.SerializeToElement(this, _jsonSerializerOptions);
return JsonSerializer.SerializeToElement(this, RawPackageManifestJsonContext.Default.RawPackageManifest);
}

public RawPackageManifest WithTemplateRendered()
{
string jsonText = JsonSerializer.Serialize(this);
string jsonText = JsonSerializer.Serialize(this, RawPackageManifestJsonContext.Default.RawPackageManifest);

Template template = Template.Parse(jsonText);

Expand All @@ -611,3 +611,18 @@ public RawPackageManifest WithTemplateRendered()
return FromJsonElement(jsonElementRendered);
}
}

[JsonSourceGenerationOptions(
AllowTrailingCommas = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
IndentSize = 4,
ReadCommentHandling = JsonCommentHandling.Skip,
WriteIndented = true,
GenerationMode = JsonSourceGenerationMode.Metadata
)]
[JsonSerializable(typeof(RawPackageManifest))]
[JsonSerializable(typeof(RawPackageManifest.Asset.TypeEnum), TypeInfoPropertyName = "RawPackageManifestAssetTypeEnumTypeInfo")]
[JsonSerializable(typeof(RawPackageManifest.Placement.TypeEnum), TypeInfoPropertyName = "RawPackageManifestPlacementTypeEnumTypeInfo")]
internal partial class RawPackageManifestJsonContext : JsonSerializerContext
{
}
51 changes: 48 additions & 3 deletions Lip.Core/RuntimeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,17 @@ public static RuntimeConfig FromJsonBytes(byte[] bytes)
{
try
{
return JsonSerializer.Deserialize<RuntimeConfig>(
var raw = JsonSerializer.Deserialize(
bytes,
s_jsonSerializerOptions
RawRuntimeConfigJsonContext.Default.RawRuntimeConfig
) ?? throw new JsonException("JSON bytes deserialized to null.");
return new RuntimeConfig
{
Cache = raw.Cache ?? Path.Join(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "lip", "cache"),
GitHubProxiesText = raw.GitHubProxies ?? "",
GoModuleProxiesText = raw.GoModuleProxies ?? "https://goproxy.io"
};
}
catch (Exception ex) when (ex is JsonException)
{
Expand All @@ -58,6 +65,44 @@ public static RuntimeConfig FromJsonBytes(byte[] bytes)

public byte[] ToJsonBytes()
{
return JsonSerializer.SerializeToUtf8Bytes(this, s_jsonSerializerOptions);
return JsonSerializer.SerializeToUtf8Bytes(this, RuntimeConfigJsonContext.Default.RuntimeConfig);
}
}

internal record RawRuntimeConfig
{
[JsonPropertyName("cache")]
public string? Cache { get; init; }

[JsonPropertyName("github_proxies")]
public string? GitHubProxies { get; init; }

[JsonPropertyName("go_module_proxies")]
public string? GoModuleProxies { get; init; }
}

[JsonSourceGenerationOptions(
AllowTrailingCommas = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
IndentSize = 4,
ReadCommentHandling = JsonCommentHandling.Skip,
WriteIndented = true,
GenerationMode = JsonSourceGenerationMode.Metadata
)]
[JsonSerializable(typeof(RawRuntimeConfig))]
internal partial class RawRuntimeConfigJsonContext : JsonSerializerContext
{
}

[JsonSourceGenerationOptions(
AllowTrailingCommas = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
IndentSize = 4,
ReadCommentHandling = JsonCommentHandling.Skip,
WriteIndented = true,
GenerationMode = JsonSourceGenerationMode.Metadata
)]
[JsonSerializable(typeof(RuntimeConfig))]
internal partial class RuntimeConfigJsonContext : JsonSerializerContext
{
}
2 changes: 2 additions & 0 deletions Lip.Migration/Lip.Migration.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAotCompatible>true</IsAotCompatible>
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 9 additions & 2 deletions Lip.Migration/Manifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public record Asset
[JsonPropertyName("placements")]
public List<Placement>? Placements { get; set; }

[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonConverter(typeof(JsonStringEnumConverter<TypeEnum>))]
public enum TypeEnum
{
[JsonStringEnumMemberName("self")]
Expand Down Expand Up @@ -78,7 +78,7 @@ public record Placement
[JsonPropertyName("dest")]
public required string Dest { get; set; }

[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonConverter(typeof(JsonStringEnumConverter<TypeEnum>))]
public enum TypeEnum
{
[JsonStringEnumMemberName("file")]
Expand Down Expand Up @@ -142,3 +142,10 @@ public record Variant
public ScriptsType? Scripts { get; set; }
}
}

[JsonSerializable(typeof(Manifest))]
[JsonSerializable(typeof(Manifest.Asset.TypeEnum), TypeInfoPropertyName = "AssetTypeEnumTypeInfo")]
[JsonSerializable(typeof(Manifest.Placement.TypeEnum), TypeInfoPropertyName = "PlacementTypeEnumTypeInfo")]
public partial class ManifestJsonContext : JsonSerializerContext
{
}
5 changes: 5 additions & 0 deletions Lip.Migration/ManifestV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,8 @@ public record Command
public string? GOARCH { get; set; }
}
}

[JsonSerializable(typeof(ManifestV1))]
public partial class ManifestV1JsonContext : JsonSerializerContext
{
}
4 changes: 2 additions & 2 deletions Lip.Migration/MigratorFromV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static bool IsMigratable(JsonElement json)

public static JsonElement Migrate(JsonElement json)
{
var manifestV1 = json.Deserialize<ManifestV1>()
var manifestV1 = JsonSerializer.Deserialize(json, ManifestV1JsonContext.Default.ManifestV1)
?? throw new JsonException("Failed to deserialize the obsolete manifest.");

var info = new ManifestV2.InfoType
Expand Down Expand Up @@ -81,6 +81,6 @@ public static JsonElement Migrate(JsonElement json)
Platforms = null // No conversion for platforms from ManifestV1
};

return MigratorFromV2.Migrate(JsonSerializer.SerializeToElement(manifestV2));
return MigratorFromV2.Migrate(JsonSerializer.SerializeToElement(manifestV2, ManifestV2JsonContext.Default.ManifestV2));
}
}
9 changes: 7 additions & 2 deletions Lip.Migration/MigratorFromV2.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Runtime.InteropServices;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;

namespace Lip.Migration;
Expand All @@ -24,7 +25,7 @@ public static JsonElement Migrate(JsonElement json)
Regex regex = new(@"\$\(([^)]+?)\)");
string jsonString = regex.Replace(json.GetRawText(), "{{$1}}");

ManifestV2 manifestV2 = JsonSerializer.Deserialize<ManifestV2>(jsonString)!;
ManifestV2 manifestV2 = JsonSerializer.Deserialize(jsonString, ManifestV2JsonContext.Default.ManifestV2)!;

Manifest manifest = new()
{
Expand Down Expand Up @@ -155,7 +156,7 @@ .. manifestV2.Platforms?
manifest.Variants.Insert(0, new Manifest.Variant { Platform = RuntimeInformation.RuntimeIdentifier });
}

return JsonSerializer.SerializeToElement(manifest);
return JsonSerializer.SerializeToElement(manifest, ManifestJsonContext.Default.Manifest);
}

private static Manifest.ScriptsType ConvertCommandsToScripts(ManifestV2.CommandsType commands)
Expand Down Expand Up @@ -208,3 +209,7 @@ private static Manifest.Placement ConvertPlaceToPlacement(ManifestV2.PlaceType p
};
}
}
[JsonSerializable(typeof(ManifestV2))]
public partial class ManifestV2JsonContext : JsonSerializerContext
{
}