From 1ad979bf2c9ded2dde2a53326a16e12f171e7a23 Mon Sep 17 00:00:00 2001 From: Ikiru Yoshizaki <3856350+guitarrapc@users.noreply.github.com> Date: Wed, 11 Dec 2024 19:50:06 +0900 Subject: [PATCH] chore: format output --- src/Actions/Commands/CreateDummyCommand.cs | 3 +- src/Actions/Commands/UpdateVersionCommand.cs | 38 +++++++++---------- src/Actions/Program.cs | 27 +++++++++---- src/Actions/Utils/GitHubActionsGroupLogger.cs | 12 ++++++ 4 files changed, 52 insertions(+), 28 deletions(-) create mode 100644 src/Actions/Utils/GitHubActionsGroupLogger.cs diff --git a/src/Actions/Commands/CreateDummyCommand.cs b/src/Actions/Commands/CreateDummyCommand.cs index dcdc08a..68b5dc2 100644 --- a/src/Actions/Commands/CreateDummyCommand.cs +++ b/src/Actions/Commands/CreateDummyCommand.cs @@ -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); } } diff --git a/src/Actions/Commands/UpdateVersionCommand.cs b/src/Actions/Commands/UpdateVersionCommand.cs index 4d75ec3..1b937c8 100644 --- a/src/Actions/Commands/UpdateVersionCommand.cs +++ b/src/Actions/Commands/UpdateVersionCommand.cs @@ -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 { @@ -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(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(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) { @@ -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, @".*", $@"{version}", writeBack); + var (before, after) = Sed.Replace(path, @".*", $@"{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 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}"); diff --git a/src/Actions/Program.cs b/src/Actions/Program.cs index c1834be..11114bc 100644 --- a/src/Actions/Program.cs +++ b/src/Actions/Program.cs @@ -1,5 +1,6 @@ using Actions; using Actions.Commands; +using Actions.Utils; using ConsoleAppFramework; namespace Actions; @@ -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); } /// @@ -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 ..."); } /// @@ -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 diff --git a/src/Actions/Utils/GitHubActionsGroupLogger.cs b/src/Actions/Utils/GitHubActionsGroupLogger.cs new file mode 100644 index 0000000..fd9636b --- /dev/null +++ b/src/Actions/Utils/GitHubActionsGroupLogger.cs @@ -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::"); + } +}