Use shared location for Json options

This commit is contained in:
Caelan Sayler
2024-01-15 21:23:40 +00:00
parent 5c4159ad1d
commit 329742ad1e
4 changed files with 18 additions and 20 deletions

View File

@@ -4,6 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using Velopack.Json;
using Velopack.Packaging.Exceptions; using Velopack.Packaging.Exceptions;
namespace Velopack.Packaging namespace Velopack.Packaging
@@ -19,19 +20,13 @@ namespace Velopack.Packaging
.ToList(); .ToList();
} }
private static readonly JsonSerializerOptions _options = new JsonSerializerOptions {
AllowTrailingCommas = true,
WriteIndented = true,
ReadCommentHandling = JsonCommentHandling.Skip,
};
public static void Write(string outputDir, string channel, IEnumerable<string> files) public static void Write(string outputDir, string channel, IEnumerable<string> files)
{ {
var assets = new BuildAssets { var assets = new BuildAssets {
Files = files.OrderBy(f => f).ToList(), Files = files.OrderBy(f => f).ToList(),
}; };
var path = Path.Combine(outputDir, $"assets.{channel}.json"); var path = Path.Combine(outputDir, $"assets.{channel}.json");
var json = JsonSerializer.Serialize(assets, _options); var json = JsonSerializer.Serialize(assets, SimpleJson.Options);
File.WriteAllText(path, json); File.WriteAllText(path, json);
} }
@@ -42,7 +37,7 @@ namespace Velopack.Packaging
throw new UserInfoException($"Could not find assets file for channel '{channel}' (looking for '{Path.GetFileName(path)}' in directory '{outputDir}'). " + throw new UserInfoException($"Could not find assets file for channel '{channel}' (looking for '{Path.GetFileName(path)}' in directory '{outputDir}'). " +
$"If you've just created a Velopack release, verify you're calling this command with the same '--channel' as you did with 'pack'."); $"If you've just created a Velopack release, verify you're calling this command with the same '--channel' as you did with 'pack'.");
} }
return JsonSerializer.Deserialize<BuildAssets>(File.ReadAllText(path), _options); return JsonSerializer.Deserialize<BuildAssets>(File.ReadAllText(path), SimpleJson.Options);
} }
} }
} }

View File

@@ -174,7 +174,7 @@ namespace Velopack.Packaging
File.Move(f.from, f.to, true); File.Move(f.from, f.to, true);
} }
ReleaseEntryHelper.UpdateReleaseFiles(releaseDir.FullName); ReleaseEntryHelper.UpdateReleaseFiles(releaseDir.FullName, Log);
BuildAssets.Write(releaseDir.FullName, channel, filesToCopy.Select(x => x.to)); BuildAssets.Write(releaseDir.FullName, channel, filesToCopy.Select(x => x.to));
progress(100); progress(100);
return Task.CompletedTask; return Task.CompletedTask;

View File

@@ -3,6 +3,7 @@ using System.Text.Json.Serialization;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NuGet.Versioning; using NuGet.Versioning;
using Velopack.Json;
using Velopack.NuGet; using Velopack.NuGet;
using Velopack.Packaging.Exceptions; using Velopack.Packaging.Exceptions;
using Velopack.Sources; using Velopack.Sources;
@@ -90,7 +91,7 @@ namespace Velopack.Packaging
return assets; return assets;
} }
public static void UpdateReleaseFiles(string outputDir) public static void UpdateReleaseFiles(string outputDir, ILogger log)
{ {
var releases = GetReleasesFromDir(outputDir); var releases = GetReleasesFromDir(outputDir);
foreach (var releaseFile in Directory.EnumerateFiles(outputDir, "RELEASES*")) { foreach (var releaseFile in Directory.EnumerateFiles(outputDir, "RELEASES*")) {
@@ -98,10 +99,16 @@ namespace Velopack.Packaging
} }
foreach (var kvp in releases) { foreach (var kvp in releases) {
if (VelopackRuntimeInfo.IsWindows && kvp.Key == GetDefaultChannel(RuntimeOs.Windows)) { if (VelopackRuntimeInfo.IsWindows && kvp.Key == GetDefaultChannel(RuntimeOs.Windows)) {
var exclude = kvp.Value.Where(x => x.Version.ReleaseLabels.Any(r => r.Contains('.')) || x.Version.HasMetadata).ToArray();
if (exclude.Any()) {
log.Warn($"Excluding {exclude.Length} assets from legacy RELEASES file, because they " +
$"contain an invalid character in the version: {string.Join(", ", exclude.Select(x => x.FileName))}");
}
// We write a legacy RELEASES file to allow older applications to update to velopack // We write a legacy RELEASES file to allow older applications to update to velopack
#pragma warning disable CS0618 // Type or member is obsolete #pragma warning disable CS0618 // Type or member is obsolete
var path = Path.Combine(outputDir, "RELEASES"); var path = Path.Combine(outputDir, "RELEASES");
ReleaseEntry.WriteReleaseFile(kvp.Value.Select(ReleaseEntry.FromVelopackAsset), path); ReleaseEntry.WriteReleaseFile(kvp.Value.Except(exclude).Select(ReleaseEntry.FromVelopackAsset), path);
#pragma warning restore CS0618 // Type or member is obsolete #pragma warning restore CS0618 // Type or member is obsolete
} }
var indexPath = Path.Combine(outputDir, Utility.GetVeloReleaseIndexName(kvp.Key)); var indexPath = Path.Combine(outputDir, Utility.GetVeloReleaseIndexName(kvp.Key));
@@ -119,18 +126,13 @@ namespace Velopack.Packaging
public static string GetAssetFeedJson(VelopackAssetFeed feed) public static string GetAssetFeedJson(VelopackAssetFeed feed)
{ {
return JsonSerializer.Serialize(feed, new JsonSerializerOptions() { return JsonSerializer.Serialize(feed, SimpleJson.Options);
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = {
new JsonStringEnumConverter(),
},
});
} }
public static string GetSuggestedReleaseName(string id, string version, string channel, bool delta) public static string GetSuggestedReleaseName(string id, string version, string channel, bool delta)
{ {
var suffix = GetUniqueAssetSuffix(channel); var suffix = GetUniqueAssetSuffix(channel);
version = SemanticVersion.Parse(version).ToNormalizedString();
if (VelopackRuntimeInfo.IsWindows && channel == GetDefaultChannel(RuntimeOs.Windows)) { if (VelopackRuntimeInfo.IsWindows && channel == GetDefaultChannel(RuntimeOs.Windows)) {
return $"{id}-{version}{(delta ? "-delta" : "-full")}.nupkg"; return $"{id}-{version}{(delta ? "-delta" : "-full")}.nupkg";
} }

View File

@@ -1,14 +1,13 @@
using System; using System;
using NuGet.Versioning; using NuGet.Versioning;
using System.Text.Json.Serialization;
#if NET5_0_OR_GREATER #if NET5_0_OR_GREATER
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization;
#else #else
using System.Reflection; using System.Reflection;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Converters; using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Serialization;
#endif #endif
@@ -30,6 +29,8 @@ namespace Velopack.Json
AllowTrailingCommas = true, AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip, ReadCommentHandling = JsonCommentHandling.Skip,
PropertyNameCaseInsensitive = true, PropertyNameCaseInsensitive = true,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Converters = { new JsonStringEnumConverter(), new SemanticVersionConverter() }, Converters = { new JsonStringEnumConverter(), new SemanticVersionConverter() },
}; };