Skip to content

Commit

Permalink
chore: format output
Browse files Browse the repository at this point in the history
  • Loading branch information
guitarrapc committed Dec 11, 2024
1 parent 28b131b commit 1ad979b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/Actions/Commands/CreateDummyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ public void CreateDummyFiles(string basePath)

foreach (var (file, contents) in new[] { upm, godot, directoryBuildProps })
{
var path = Path.Combine(basePath, file);
if (!Directory.Exists(basePath))
Directory.CreateDirectory(basePath);

var path = Path.Combine(basePath, file);
Console.WriteLine($"- {path} ...");
File.WriteAllText(path, contents);
}
}
Expand Down
38 changes: 18 additions & 20 deletions src/Actions/Commands/UpdateVersionCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace Actions.Commands
{
public record struct UpdateVersionCommandResult(string Before, string After);
public class UpdateVersionCommand(string version, string path)
{
public (string before, string after) UpdateVersion(bool dryRun)
public UpdateVersionCommandResult UpdateVersion(bool dryRun)
{
var writeBack = !dryRun;
if (!File.Exists(path)) throw new FileNotFoundException(path);

var writeBack = !dryRun;
var fileName = Path.GetFileName(path);
return fileName switch
{
Expand All @@ -24,35 +26,33 @@ public class UpdateVersionCommand(string version, string path)
};
}

private (string before, string after) HandleUpm(bool writeBack)
private UpdateVersionCommandResult HandleUpm(bool writeBack)
{
// replace
var result = Sed.Replace(path, @"""version"":\s*""(.*?)""", $@"""version"": ""{version}""", writeBack);
var (before, after) = Sed.Replace(path, @"""version"":\s*""(.*?)""", $@"""version"": ""{version}""", writeBack);

// validate
Validate(result.after, version);
Validate(after, version);

return result;
return new UpdateVersionCommandResult(before, after);

static void Validate(string contents, string version)
{
var packageJson = JsonSerializer.Deserialize<UpmPackageJson>(contents);
if (packageJson is null)
throw new ActionCommandException($"UPM package.json updated, but failed to load as valid JSON. contents: {contents}");
var packageJson = JsonSerializer.Deserialize<UpmPackageJson>(contents) ?? throw new ActionCommandException($"UPM package.json updated, but failed to load as valid JSON. contents: {contents}");
if (packageJson.Version != version)
throw new ActionCommandException($"UPM package.json updated, but version miss-match. actual {packageJson?.Version}, expected {version}");
}
}

private (string before, string after) HandleGodot(bool writeBack)
private UpdateVersionCommandResult HandleGodot(bool writeBack)
{
// replace
var result = Sed.Replace(path, @"(version=)""(.*?)""", $@"$1""{version}""", writeBack);
var (before, after) = Sed.Replace(path, @"(version=)""(.*?)""", $@"$1""{version}""", writeBack);

// validate
Validate(result.after, version);
Validate(after, version);

return result;
return new UpdateVersionCommandResult(before, after);

static void Validate(string contents, string version)
{
Expand Down Expand Up @@ -81,23 +81,21 @@ static void Validate(string contents, string version)
}
}

private (string before, string after) HandleDirectoryBuildProps(bool writeBack)
private UpdateVersionCommandResult HandleDirectoryBuildProps(bool writeBack)
{
// replace
var result = Sed.Replace(path, @"<VersionPrefix>.*</VersionPrefix>", $@"<VersionPrefix>{version}</VersionPrefix>", writeBack);
var (before, after) = Sed.Replace(path, @"<VersionPrefix>.*</VersionPrefix>", $@"<VersionPrefix>{version}</VersionPrefix>", writeBack);

// validate
Validate(result.after, version);
Validate(after, version);

return result;
return new UpdateVersionCommandResult(before, after);

static void Validate(string contents, string version)
{
var xmlDoc = new System.Xml.XmlDocument();
xmlDoc.LoadXml(contents);
var versionPrefixNode = xmlDoc.SelectSingleNode("//VersionPrefix");
if (versionPrefixNode == null)
throw new ActionCommandException($"Directory.Build.props updated, but VersionPrefix key not found.");
var versionPrefixNode = xmlDoc.SelectSingleNode("//VersionPrefix") ?? throw new ActionCommandException($"Directory.Build.props updated, but VersionPrefix key not found.");
if (versionPrefixNode.InnerText != version)
throw new ActionCommandException($"Directory.Build.props updated, but version miss-match. actual {versionPrefixNode.InnerText}, expected {version}");

Expand Down
27 changes: 20 additions & 7 deletions src/Actions/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Actions;
using Actions.Commands;
using Actions.Utils;
using ConsoleAppFramework;

namespace Actions;
Expand Down Expand Up @@ -29,7 +30,8 @@ public void Versioning(string tag, string prefix = "", VersionIncrement versionI
var command = new VersioningCommand(tag, prefix, versionIncrement, isPrelease, prerelease);
var versioning = command.Versioning(withoutPrefix);

Console.WriteLine(OutputFormat("version", versioning, outputFormat));
var output = OutputFormat("version", versioning, outputFormat);
Console.WriteLine(output);
}

/// <summary>
Expand All @@ -41,16 +43,23 @@ public void Versioning(string tag, string prefix = "", VersionIncrement versionI
[Command("update-version")]
public void UpdateVersion(string version, string path, bool dryRun)
{
Console.WriteLine($"Update begin, {path} ...");
if (string.IsNullOrWhiteSpace(path))
Console.WriteLine("Input is empty path, skip execution.");
{
Console.WriteLine("Empty path detected, skip execution.");
return;
}

using (var githubGroup = new GitHubActionsGroupLogger("Before"))
Console.WriteLine(File.ReadAllText(path));

var command = new UpdateVersionCommand(version, path);
var (_, after) = command.UpdateVersion(dryRun);
var result = command.UpdateVersion(dryRun);

if (dryRun)
{
Console.WriteLine(after);
}
using (var githubGroup = new GitHubActionsGroupLogger("After"))
Console.WriteLine(result.After);

Console.WriteLine($"Completed ...");
}

/// <summary>
Expand All @@ -60,8 +69,12 @@ public void UpdateVersion(string version, string path, bool dryRun)
[Command("create-dummy")]
public void CreateDummy(string basePath)
{
Console.WriteLine($"Creating dummy files, under {basePath} ...");

var command = new CreateDummyCommand();
command.CreateDummyFiles(basePath);

Console.WriteLine($"Completed ...");
}

private static string OutputFormat(string key, string value, OutputFormatType format) => format switch
Expand Down
12 changes: 12 additions & 0 deletions src/Actions/Utils/GitHubActionsGroupLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Actions.Utils
{
public class GitHubActionsGroupLogger : IDisposable
{
public GitHubActionsGroupLogger(string title)
{
Console.WriteLine($"::group::{title}");
}

public void Dispose() => Console.WriteLine("::endgroup::");
}
}

0 comments on commit 1ad979b

Please sign in to comment.