Skip to content

Commit

Permalink
generate release json for updatePackages
Browse files Browse the repository at this point in the history
  • Loading branch information
natsurainko committed Dec 26, 2024
1 parent d716cf9 commit 4204328
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 25 deletions.
25 changes: 24 additions & 1 deletion FluentLauncher.PreviewChannel.PackageInstaller/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FluentLauncher.PreviewChannel.PackageInstaller.Scripts;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO.Compression;
using System.Runtime.InteropServices;

Expand All @@ -13,6 +14,13 @@
queryCommand.AddOption(GetBuildCountOfVersion);
queryCommand.SetHandler(async (versionToGetBuildCount) => await QueryScripts.QueryAsync(versionToGetBuildCount), GetBuildCountOfVersion);

var generateReleaseJsonCommand = new Command("generateReleaseJson");
generateReleaseJsonCommand.AddOption(UpdatePackageFiles);
generateReleaseJsonCommand.AddOption(StableVersion);
generateReleaseJsonCommand.AddOption(Commit);
generateReleaseJsonCommand.SetHandler(async (updatePackageFiles, stableVersion, commit)
=> await ReleaseScripts.GenerateReleaseJson(commit, stableVersion, updatePackageFiles), UpdatePackageFiles, StableVersion, Commit);

manualTargetPackageCommand.SetHandler(async (certificationPath, packagePath, dependencyPackagesPath, launchAfterInstalled)
=> await InstallScripts.InstallPackage(packagePath, dependencyPackagesPath, certificationPath, launchAfterInstalled),
CertificationPath, PackagePath, DependencyPackagesPath, LaunchAfterInstalled);
Expand Down Expand Up @@ -46,6 +54,7 @@ await InstallScripts.InstallPackage(

rootCommand.AddCommand(manualTargetPackageCommand);
rootCommand.AddCommand(queryCommand);
rootCommand.AddCommand(generateReleaseJsonCommand);

return await rootCommand.InvokeAsync(args);

Expand All @@ -55,9 +64,23 @@ public partial class Program

static Option<string> PackagePath { get; } = new(name: "--packagePath") { IsRequired = true };

static Option<string[]> DependencyPackagesPath { get; } = new(name: "--dependencyPackagesPath") { IsRequired = true };
static Option<string[]> DependencyPackagesPath { get; } = new(name: "--dependencyPackagesPath")
{
IsRequired = true,
AllowMultipleArgumentsPerToken = true
};

static Option<bool> LaunchAfterInstalled { get; } = new(name: "--launchAfterInstalled", getDefaultValue: () => true) { IsRequired = false, };

static Option<string> GetBuildCountOfVersion { get; } = new(name: "--getBuildCountOfVersion") { IsRequired = false };

static Option<string> StableVersion { get; } = new(name: "--stableVersion") { IsRequired = true, };

static Option<string> Commit { get; } = new(name: "--commit") { IsRequired = true, };

static Option<string[]> UpdatePackageFiles { get; } = new(name: "--updatePackageFiles")
{
IsRequired = true,
AllowMultipleArgumentsPerToken = true
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"FluentLauncher.PreviewChannel.PackageInstaller": {
"commandName": "Project",
"commandLineArgs": "query --getBuildCountOfVersion 2.3.2.0"
"commandLineArgs": "generateReleaseJson --commit \"7c9061e\" --stableVersion \"2.3.2.0\" --updatePackageFiles \"C:\\Users\\Natsuraniko\\Downloads\\updatePackage-x64.zip\" \"C:\\Users\\Natsuraniko\\Downloads\\updatePackage-arm64.zip\""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,35 @@ static QueryScripts()
}

public static async Task QueryAsync(string? versionToGetBuildCount)
{
if (!string.IsNullOrEmpty(versionToGetBuildCount))
Console.WriteLine($"BuildCount: {await GetBuildCountOfVersionAsync(versionToGetBuildCount)}");
}

public static async Task<int> GetBuildCountOfVersionAsync(string version)
{
string releasesContent = await _httpClient.GetStringAsync(GithubReleasesApi);
string pattern = @"(?<=``` json)([\s\S]+?)(?=```)";

if (!string.IsNullOrEmpty(versionToGetBuildCount))
foreach (var node in JsonArray.Parse(releasesContent)!.AsArray())
{
int build = 0;

foreach (var node in JsonArray.Parse(releasesContent)!.AsArray())
if (node!.AsObject().ContainsKey("prerelease") && node["prerelease"]!.GetValue<bool>())
{
if (node!.AsObject().ContainsKey("prerelease") && node["prerelease"]!.GetValue<bool>())
{
string body = node["body"]!.GetValue<string>();
Match match = Regex.Match(body, pattern);
string body = node["body"]!.GetValue<string>();
Match match = Regex.Match(body, pattern);

if (!match.Success)
continue;
if (!match.Success)
continue;

JsonNode jsonBody = JsonNode.Parse(match.Groups[1].Value)!;
JsonNode jsonBody = JsonNode.Parse(match.Groups[1].Value)!;

if (jsonBody["previousStableVersion"]!.GetValue<string>() != versionToGetBuildCount)
continue;
if (jsonBody["previousStableVersion"]!.GetValue<string>() != version)
continue;

build = jsonBody["build"]!.GetValue<int>();
}
return jsonBody["build"]!.GetValue<int>();
}

Console.WriteLine($"BuildCount: {build}");
}

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Text.Json.Nodes;

namespace FluentLauncher.PreviewChannel.PackageInstaller.Scripts;

Expand All @@ -19,8 +16,30 @@ namespace FluentLauncher.PreviewChannel.PackageInstaller.Scripts;

public static class ReleaseScripts
{
public static async Task GenerateReleaseJson(string commit, string[] packageFiles)
public static async Task GenerateReleaseJson(string commit, string stableVersion, string[] packageFiles)
{
int build = await QueryScripts.GetBuildCountOfVersionAsync(stableVersion);
JsonObject json = new()
{
{ "commit", commit },
{ "build", build + 1 },
{ "releaseTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") },
{ "previousStableVersion", stableVersion }
};

JsonObject hashes = [];

foreach (var packageFile in packageFiles)
{
using var md5 = MD5.Create();
using var stream = File.OpenRead(packageFile);
byte[] hashBytes = md5.ComputeHash(stream);

string hash = Convert.ToHexStringLower(hashBytes);
hashes.Add(Path.GetFileName(packageFile), hash);
}

json.Add("hashes", hashes);
Console.WriteLine(json.ToString());
}
}

0 comments on commit 4204328

Please sign in to comment.