import all shipped nuget packages as built-in
All checks were successful
Build / Compute Version (push) Successful in 6s
Build / Build Nuget package (SharedCringe) (push) Successful in 53s
Build / Build Nuget package (CringeBootstrap.Abstractions) (push) Successful in 1m0s
Build / Build Nuget package (NuGet) (push) Successful in 58s
Build / Build Nuget package (CringePlugins) (push) Successful in 1m13s
Build / Build Launcher (push) Successful in 1m42s

also would now throw if version gets changed
This commit is contained in:
zznty
2025-05-13 00:19:22 +07:00
parent 4ac3989115
commit a441498c09
7 changed files with 144 additions and 35 deletions

View File

@@ -10,7 +10,7 @@ public class FrameworkJsonConverter(FrameworkNameFormat format) : JsonConverter<
{
if (reader.TokenType != JsonTokenType.String)
throw new JsonException("Invalid framework string");
var s = reader.GetString()!;
return format switch
{

View File

@@ -8,7 +8,7 @@ public class ManifestPackageKeyJsonConverter : JsonConverter<ManifestPackageKey>
{
public override ManifestPackageKey Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
if (reader.TokenType is not (JsonTokenType.String or JsonTokenType.PropertyName))
throw new JsonException("Invalid package key string");
return ManifestPackageKey.Parse(reader.GetString()!);

View File

@@ -0,0 +1,29 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using NuGet.Models;
namespace NuGet.Converters;
public class RuntimeFrameworkJsonConverter : JsonConverter<NuGetRuntimeFramework>
{
public override NuGetRuntimeFramework Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType is not (JsonTokenType.String or JsonTokenType.PropertyName))
throw new JsonException("Invalid runtime framework string");
return NuGetRuntimeFramework.Parse(reader.GetString()!);
}
public override void Write(Utf8JsonWriter writer, NuGetRuntimeFramework value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString());
}
public override NuGetRuntimeFramework ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options) => Read(ref reader, typeToConvert, options);
public override void WriteAsPropertyName(Utf8JsonWriter writer, NuGetRuntimeFramework value, JsonSerializerOptions options)
{
writer.WritePropertyName(value.ToString());
}
}

View File

@@ -10,9 +10,10 @@ using NuGet.Versioning;
namespace NuGet.Deps;
public record DependenciesManifest(RuntimeTarget RuntimeTarget,
ImmutableDictionary<NuGetFramework, string> CompilationOptions,
ImmutableDictionary<NuGetFramework, ImmutableDictionary<ManifestPackageKey, DependencyTarget>> Targets,
public record DependenciesManifest(
RuntimeTarget RuntimeTarget,
ImmutableDictionary<NuGetRuntimeFramework, string> CompilationOptions,
ImmutableDictionary<NuGetRuntimeFramework, ImmutableDictionary<ManifestPackageKey, DependencyTarget>> Targets,
ImmutableDictionary<ManifestPackageKey, DependencyLibrary> Libraries);
public record DependencyLibrary(
@@ -26,7 +27,9 @@ public record DependencyLibrary(
public enum LibraryType
{
Project,
Package
Package,
Reference,
Runtimepack
}
public record DependencyTarget(ImmutableDictionary<string, NuGetVersion>? Dependencies,
@@ -39,7 +42,7 @@ public record Dependency(Version? FileVersion = null);
public record RuntimeDependency(Version? AssemblyVersion = null, Version? FileVersion = null) : Dependency(FileVersion);
public record RuntimeTarget([property: JsonPropertyName("name")] NuGetFramework Framework, string Signature = "");
public record RuntimeTarget([property: JsonPropertyName("name")] NuGetRuntimeFramework RuntimeFramework, string Signature = "");
[JsonConverter(typeof(ManifestPackageKeyJsonConverter))]
public record ManifestPackageKey(string Id, NuGetVersion Version)
@@ -56,7 +59,7 @@ public record ManifestPackageKey(string Id, NuGetVersion Version)
public override string ToString() => $"{Id}/{Version}";
}
public class DependencyManifestBuilder(DirectoryInfo cacheDirectory, PackageSourceMapping packageSources, Func<Models.Dependency, CatalogEntry?> catalogEntryResolver)
public static class DependencyManifestSerializer
{
private static readonly JsonSerializerOptions SerializerOptions = new(JsonSerializerDefaults.Web)
{
@@ -68,8 +71,15 @@ public class DependencyManifestBuilder(DirectoryInfo cacheDirectory, PackageSour
new VersionJsonConverter()
}
};
public static Task SerializeAsync(Stream stream, DependenciesManifest manifest) => JsonSerializer.SerializeAsync(stream, manifest, SerializerOptions);
public static ValueTask<DependenciesManifest> DeserializeAsync(Stream stream) => JsonSerializer.DeserializeAsync<DependenciesManifest>(stream, SerializerOptions)!;
}
public async ValueTask WriteDependencyManifestAsync(Stream stream, CatalogEntry catalogEntry, NuGetFramework targetFramework)
public class DependencyManifestBuilder(DirectoryInfo cacheDirectory, PackageSourceMapping packageSources, Func<Models.Dependency, CatalogEntry?> catalogEntryResolver)
{
public async ValueTask WriteDependencyManifestAsync(Stream stream, CatalogEntry catalogEntry, NuGetRuntimeFramework targetFramework)
{
var runtimeTarget = new RuntimeTarget(targetFramework);
@@ -77,21 +87,22 @@ public class DependencyManifestBuilder(DirectoryInfo cacheDirectory, PackageSour
await MapCatalogEntryAsync(catalogEntry, targetFramework, targets);
var manifest = new DependenciesManifest(runtimeTarget, ImmutableDictionary<NuGetFramework, string>.Empty,
ImmutableDictionary<NuGetFramework, ImmutableDictionary<ManifestPackageKey, DependencyTarget>>.Empty
var manifest = new DependenciesManifest(runtimeTarget, ImmutableDictionary<NuGetRuntimeFramework, string>.Empty,
ImmutableDictionary<NuGetRuntimeFramework, ImmutableDictionary<ManifestPackageKey, DependencyTarget>>.Empty
.Add(targetFramework, targets.ToImmutable()),
ImmutableDictionary<ManifestPackageKey, DependencyLibrary>.Empty);
await JsonSerializer.SerializeAsync(stream, manifest, SerializerOptions);
await DependencyManifestSerializer.SerializeAsync(stream, manifest);
}
private async Task MapCatalogEntryAsync(CatalogEntry catalogEntry, NuGetFramework targetFramework,
private async Task MapCatalogEntryAsync(CatalogEntry catalogEntry, NuGetRuntimeFramework targetFramework,
ImmutableDictionary<ManifestPackageKey, DependencyTarget>.Builder targets)
{
if (targets.ContainsKey(new(catalogEntry.Id, catalogEntry.Version)) || !catalogEntry.DependencyGroups.HasValue)
return;
var nearest = NuGetFrameworkUtility.GetNearest(catalogEntry.DependencyGroups.Value, targetFramework,
// TODO take into account the target framework runtime identifier
var nearest = NuGetFrameworkUtility.GetNearest(catalogEntry.DependencyGroups.Value, targetFramework.Framework,
group => group.TargetFramework);
if (nearest is null)

View File

@@ -0,0 +1,27 @@
using System.Text.Json.Serialization;
using NuGet.Converters;
using NuGet.Frameworks;
namespace NuGet.Models;
/// <summary>
/// Represents a NuGetFramework with a runtime identifier
/// </summary>
[JsonConverter(typeof(RuntimeFrameworkJsonConverter))]
public record NuGetRuntimeFramework(NuGetFramework Framework, string? RuntimeIdentifier)
{
public static NuGetRuntimeFramework Parse(string str)
{
var index = str.IndexOf('/');
if (index < 0)
return new NuGetRuntimeFramework(NuGetFramework.Parse(str), null);
return new NuGetRuntimeFramework(NuGetFramework.Parse(str[..index]), str[(index + 1)..]);
}
public override string ToString()
{
return string.IsNullOrEmpty(RuntimeIdentifier) ? Framework.ToString() : $"{Framework}/{RuntimeIdentifier}";
}
}