From 8ec8f81337b3b39f27acb64b7e051b002f2a2ce5 Mon Sep 17 00:00:00 2001 From: Kouji Matsui Date: Tue, 15 Oct 2024 14:10:27 +0900 Subject: [PATCH 1/7] Updated package. --- RelaxVersioner.Core.Tests/RelaxVersioner.Core.Tests.csproj | 1 + RelaxVersioner.Core/RelaxVersioner.Core.csproj | 2 +- RelaxVersioner.Tasks/RelaxVersioner.Tasks.csproj | 2 +- RelaxVersioner/RelaxVersioner.csproj | 2 +- rv-cli/rv-cli.csproj | 4 ++-- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/RelaxVersioner.Core.Tests/RelaxVersioner.Core.Tests.csproj b/RelaxVersioner.Core.Tests/RelaxVersioner.Core.Tests.csproj index 8921292..41393f2 100644 --- a/RelaxVersioner.Core.Tests/RelaxVersioner.Core.Tests.csproj +++ b/RelaxVersioner.Core.Tests/RelaxVersioner.Core.Tests.csproj @@ -11,6 +11,7 @@ + diff --git a/RelaxVersioner.Core/RelaxVersioner.Core.csproj b/RelaxVersioner.Core/RelaxVersioner.Core.csproj index f6b3494..6b064d6 100644 --- a/RelaxVersioner.Core/RelaxVersioner.Core.csproj +++ b/RelaxVersioner.Core/RelaxVersioner.Core.csproj @@ -16,7 +16,7 @@ + Include="RelaxVersioner" Version="3.7.0" PrivateAssets="all" /> diff --git a/RelaxVersioner.Tasks/RelaxVersioner.Tasks.csproj b/RelaxVersioner.Tasks/RelaxVersioner.Tasks.csproj index b1a5e97..9ee4507 100644 --- a/RelaxVersioner.Tasks/RelaxVersioner.Tasks.csproj +++ b/RelaxVersioner.Tasks/RelaxVersioner.Tasks.csproj @@ -29,7 +29,7 @@ + Include="RelaxVersioner" Version="3.7.0" PrivateAssets="all" /> diff --git a/RelaxVersioner/RelaxVersioner.csproj b/RelaxVersioner/RelaxVersioner.csproj index fe6b6e2..82214b0 100644 --- a/RelaxVersioner/RelaxVersioner.csproj +++ b/RelaxVersioner/RelaxVersioner.csproj @@ -37,7 +37,7 @@ + Include="RelaxVersioner" Version="3.7.0" PrivateAssets="all" /> diff --git a/rv-cli/rv-cli.csproj b/rv-cli/rv-cli.csproj index 06d725a..3a8b60a 100644 --- a/rv-cli/rv-cli.csproj +++ b/rv-cli/rv-cli.csproj @@ -27,7 +27,7 @@ + Include="RelaxVersioner" Version="3.7.0" PrivateAssets="all" /> @@ -35,7 +35,7 @@ - + From 1254b55600c831ff2444ddbdda68c036d6731421 Mon Sep 17 00:00:00 2001 From: Kouji Matsui Date: Tue, 15 Oct 2024 15:32:22 +0900 Subject: [PATCH 2/7] Added NPM versioning feature. --- RelaxVersioner.Core/Processor.cs | 1 + .../RelaxVersioner.Core.csproj | 1 + .../Writers/NpmReplaceProvider.cs | 103 ++++++++++++++++++ RelaxVersioner/Program.cs | 25 ++++- 4 files changed, 124 insertions(+), 6 deletions(-) create mode 100644 RelaxVersioner.Core/Writers/NpmReplaceProvider.cs diff --git a/RelaxVersioner.Core/Processor.cs b/RelaxVersioner.Core/Processor.cs index 6bb3238..c35bb9a 100644 --- a/RelaxVersioner.Core/Processor.cs +++ b/RelaxVersioner.Core/Processor.cs @@ -41,6 +41,7 @@ public sealed class ProcessorContext public string BracketStart; public string BracketEnd; public bool IsDryRun; + public string[] NpmPrefixes; } public sealed class Processor diff --git a/RelaxVersioner.Core/RelaxVersioner.Core.csproj b/RelaxVersioner.Core/RelaxVersioner.Core.csproj index 6b064d6..38ca466 100644 --- a/RelaxVersioner.Core/RelaxVersioner.Core.csproj +++ b/RelaxVersioner.Core/RelaxVersioner.Core.csproj @@ -14,6 +14,7 @@ + diff --git a/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs b/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs new file mode 100644 index 0000000..c11e6d8 --- /dev/null +++ b/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs @@ -0,0 +1,103 @@ +//////////////////////////////////////////////////////////////////////////////////////// +// +// RelaxVersioner - Git tag/branch based, full-automatic version generator. +// Copyright (c) Kouji Matsui (@kozy_kekyo, @kekyo@mastodon.cloud) +// +// Licensed under Apache-v2: https://opensource.org/licenses/Apache-2.0 +// +//////////////////////////////////////////////////////////////////////////////////////// + +#nullable enable + +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using NamingFormatter; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace RelaxVersioner.Writers; + +internal sealed class NpmReplaceProvider : WriteProviderBase +{ + public override string Language => "NPM"; + + public override void Write( + ProcessorContext context, + Dictionary keyValues, + DateTimeOffset generated) + { + void Replace(TextReader tr, TextWriter tw) + { + var jr = new JsonTextReader(tr); + var jt = JToken.ReadFrom(jr); + + var formattedVersion = Named.Format( + CultureInfo.InvariantCulture, + context.TextFormat, + keyValues, + key => string.Empty, + new(context.BracketStart, context.BracketEnd)); + + jt["version"] = formattedVersion; + + if (context.NpmPrefixes.Length >= 1) + { + void ReplaceSubKey(string key) + { + if (jt[key] is JObject jo) + { + foreach (var jp in jo.Properties()) + { + if (context.NpmPrefixes.Any(jp.Name.StartsWith)) + { + jp.Value = JValue.CreateString(formattedVersion); + } + } + } + } + + ReplaceSubKey("dependencies"); + ReplaceSubKey("peerDependencies"); + ReplaceSubKey("devDependencies"); + } + + var jw = new JsonTextWriter(tw); + jt.WriteTo(jw); + + jw.Flush(); + tw.Flush(); + } + + if (!string.IsNullOrWhiteSpace(context.OutputPath)) + { + if (context.IsDryRun) + { + return; + } + + Processor.WriteSafeTransacted( + context.OutputPath, + stream => + { + using var tr = context.ReplaceInputPath is { } rip ? + new StreamReader(rip, Encoding.UTF8, true) : + Console.In; + var tw = new StreamWriter(stream, Encoding.UTF8); + + Replace(tr, tw); + }); + } + else + { + using var tr = context.ReplaceInputPath is { } rip ? + new StreamReader(rip, Encoding.UTF8, true) : + Console.In; + Replace(tr, Console.Out); + } + } +} diff --git a/RelaxVersioner/Program.cs b/RelaxVersioner/Program.cs index b0e0f7a..df94c50 100644 --- a/RelaxVersioner/Program.cs +++ b/RelaxVersioner/Program.cs @@ -33,6 +33,7 @@ public static async Task Main(string[] args) Language = "Text", GenerateStatic = true, TextFormat = "{versionLabel}", + NpmPrefixes = Array.Empty(), }; string? resultPath = null; @@ -52,12 +53,7 @@ public static async Task Main(string[] args) { "propertiesPath=", $"properties file", v => context.PropertiesPath = v }, { "o|outputPath=", $"output source file", v => context.OutputPath = v }, { "resultPath=", $"output result via xml file", v => resultPath = v }, - { "f|format=", $"set text format", v => - { - context.TextFormat = v; - context.Language = "Text"; - } - }, + { "f|format=", $"set text format", v => context.TextFormat = v }, { "r|replace", "replace standard input", _ => { context.ReplaceInputPath = null; @@ -79,6 +75,23 @@ public static async Task Main(string[] args) } } }, + { "n|npm", "replace NPM package.json", v => + { + context.Language = "NPM"; + context.ReplaceInputPath = "package.json"; + context.OutputPath = "package.json"; + context.TextFormat = "^{versionLabel}"; + } + }, + { "npmpn=", "NPM dependency prefix namespaces", v => + { + context.NpmPrefixes = v.Split(',').Select(n => n.Trim()).ToArray(); + context.Language = "NPM"; + context.ReplaceInputPath = "package.json"; + context.OutputPath = "package.json"; + context.TextFormat = "^{versionLabel}"; + } + }, { "dryrun", "dryrun mode", _ => context.IsDryRun = true }, { "launchDebugger", "Launch debugger", _ => launchDebugger = true }, { "help", "help", v => help = v != null }, From 754d4f904ff5acd89c9d0bf5b7b88e71e61df21d Mon Sep 17 00:00:00 2001 From: Kouji Matsui Date: Tue, 15 Oct 2024 16:04:21 +0900 Subject: [PATCH 3/7] Fixed invalid printing versions when built on packaging. --- RelaxVersioner.Core/Processor.cs | 26 +++++++++++--------------- RelaxVersioner/Program.cs | 1 + 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/RelaxVersioner.Core/Processor.cs b/RelaxVersioner.Core/Processor.cs index c35bb9a..0123a35 100644 --- a/RelaxVersioner.Core/Processor.cs +++ b/RelaxVersioner.Core/Processor.cs @@ -155,7 +155,10 @@ static string FormatSignature(Signature? sig) => sig is { } s ? keyValues[entry.key] = entry.value; } - writeProvider.Write(context, keyValues, generated); + if (!string.IsNullOrWhiteSpace(context.OutputPath)) + { + writeProvider.Write(context, keyValues, generated); + } return new Result( versionLabel, @@ -182,20 +185,13 @@ public async Task RunAsync( using var repository = await Utilities.OpenRepositoryAsync( logger, context.ProjectDirectory); - try - { - return await WriteVersionSourceFileAsync( - logger, - writeProvider, - context, - repository?.Head, - DateTimeOffset.Now, - ct); - } - finally - { - repository?.Dispose(); - } + return await WriteVersionSourceFileAsync( + logger, + writeProvider, + context, + repository?.Head, + DateTimeOffset.Now, + ct); } public static void WriteSafeTransacted( diff --git a/RelaxVersioner/Program.cs b/RelaxVersioner/Program.cs index df94c50..dac3ec9 100644 --- a/RelaxVersioner/Program.cs +++ b/RelaxVersioner/Program.cs @@ -128,6 +128,7 @@ public static async Task Main(string[] args) { "Text" => context.IsDryRun, "Replace" => context.IsDryRun, + "NPM" => context.IsDryRun, _ => true, }) { From 39990b4fa9121c3cd52f8bcc5291358a6b49c84c Mon Sep 17 00:00:00 2001 From: Kouji Matsui Date: Tue, 15 Oct 2024 16:30:37 +0900 Subject: [PATCH 4/7] Fixed invalid version string when package itself. --- RelaxVersioner.Core/Writers/NpmReplaceProvider.cs | 14 +++++++++++--- RelaxVersioner/Program.cs | 4 +--- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs b/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs index c11e6d8..76054f6 100644 --- a/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs +++ b/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs @@ -34,7 +34,11 @@ public override void Write( void Replace(TextReader tr, TextWriter tw) { var jr = new JsonTextReader(tr); - var jt = JToken.ReadFrom(jr); + var jt = JToken.ReadFrom(jr, new JsonLoadSettings + { + CommentHandling = CommentHandling.Load, + LineInfoHandling = LineInfoHandling.Load, + }); var formattedVersion = Named.Format( CultureInfo.InvariantCulture, @@ -55,7 +59,7 @@ void ReplaceSubKey(string key) { if (context.NpmPrefixes.Any(jp.Name.StartsWith)) { - jp.Value = JValue.CreateString(formattedVersion); + jp.Value = JValue.CreateString($"^{formattedVersion}"); } } } @@ -67,7 +71,11 @@ void ReplaceSubKey(string key) } var jw = new JsonTextWriter(tw); - jt.WriteTo(jw); + var s = new JsonSerializer() + { + Formatting = Formatting.Indented, + }; + s.Serialize(jw, jt); jw.Flush(); tw.Flush(); diff --git a/RelaxVersioner/Program.cs b/RelaxVersioner/Program.cs index dac3ec9..a611192 100644 --- a/RelaxVersioner/Program.cs +++ b/RelaxVersioner/Program.cs @@ -80,16 +80,14 @@ public static async Task Main(string[] args) context.Language = "NPM"; context.ReplaceInputPath = "package.json"; context.OutputPath = "package.json"; - context.TextFormat = "^{versionLabel}"; } }, - { "npmpn=", "NPM dependency prefix namespaces", v => + { "npmns=", "NPM dependency prefix namespaces", v => { context.NpmPrefixes = v.Split(',').Select(n => n.Trim()).ToArray(); context.Language = "NPM"; context.ReplaceInputPath = "package.json"; context.OutputPath = "package.json"; - context.TextFormat = "^{versionLabel}"; } }, { "dryrun", "dryrun mode", _ => context.IsDryRun = true }, From a97277f8636818a2b012d37a0e62006cd9aea77f Mon Sep 17 00:00:00 2001 From: Kouji Matsui Date: Tue, 15 Oct 2024 17:57:41 +0900 Subject: [PATCH 5/7] Fixed unprinted version when default usage of cli. --- RelaxVersioner.Core/Logger.cs | 15 ++++-- RelaxVersioner.Core/Processor.cs | 21 +------- .../Writers/NpmReplaceProvider.cs | 5 ++ .../Writers/SourceCodeWriteProviderBase.cs | 8 +-- .../Writers/TextReplaceProvider.cs | 5 ++ .../Writers/TextWriteProvider.cs | 5 ++ RelaxVersioner/Program.cs | 51 ++++++++++--------- RelaxVersioner/build/RelaxVersioner.targets | 4 +- 8 files changed, 61 insertions(+), 53 deletions(-) diff --git a/RelaxVersioner.Core/Logger.cs b/RelaxVersioner.Core/Logger.cs index e068701..74792e2 100644 --- a/RelaxVersioner.Core/Logger.cs +++ b/RelaxVersioner.Core/Logger.cs @@ -10,6 +10,7 @@ #nullable enable using System; +using System.ComponentModel; using System.IO; namespace RelaxVersioner; @@ -18,7 +19,9 @@ public enum LogImportance { Low = 1, Normal = 2, - High =3 + High = 3, + [EditorBrowsable(EditorBrowsableState.Never)] + Ignore = 100, } public abstract class Logger @@ -28,6 +31,8 @@ public abstract class Logger protected Logger(string header) => this.Header = header; + public abstract void SetImportance(LogImportance lowerImportance); + public abstract void Message(LogImportance importance, string message); public virtual void Message(LogImportance importance, string format, params object?[] args) => @@ -55,11 +60,12 @@ public static Logger Create(string header, LogImportance lowerImportance, TextWr internal sealed class TextWriterLogger : Logger { - private readonly LogImportance lowerImportance; private readonly TextWriter @out; private readonly TextWriter warning; private readonly TextWriter error; + private LogImportance lowerImportance; + public TextWriterLogger(string header, LogImportance lowerImportance, TextWriter @out, TextWriter warning, TextWriter error) : base(header) { @@ -69,9 +75,12 @@ public TextWriterLogger(string header, LogImportance lowerImportance, TextWriter this.error = error; } + public override void SetImportance(LogImportance lowerImportance) => + this.lowerImportance = lowerImportance; + public override void Message(LogImportance importance, string message) { - if (importance >= lowerImportance) + if (importance >= this.lowerImportance) { @out.WriteLine(message); } diff --git a/RelaxVersioner.Core/Processor.cs b/RelaxVersioner.Core/Processor.cs index 0123a35..066fb53 100644 --- a/RelaxVersioner.Core/Processor.cs +++ b/RelaxVersioner.Core/Processor.cs @@ -41,6 +41,7 @@ public sealed class ProcessorContext public string BracketStart; public string BracketEnd; public bool IsDryRun; + public bool IsQuietOnStandardOutput; public string[] NpmPrefixes; } @@ -60,21 +61,6 @@ public Processor(Logger logger) public string[] Languages { get; } - private readonly struct TargetCommit - { - public readonly int StartDepth; - public readonly Commit Commit; - - public TargetCommit(int startDepth, Commit commit) - { - this.StartDepth = startDepth; - this.Commit = commit; - } - - public override string ToString() => - $"StartDepth={this.StartDepth}, {this.Commit}"; - } - private static async Task WriteVersionSourceFileAsync( Logger logger, WriteProviderBase writeProvider, @@ -155,10 +141,7 @@ static string FormatSignature(Signature? sig) => sig is { } s ? keyValues[entry.key] = entry.value; } - if (!string.IsNullOrWhiteSpace(context.OutputPath)) - { - writeProvider.Write(context, keyValues, generated); - } + writeProvider.Write(context, keyValues, generated); return new Result( versionLabel, diff --git a/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs b/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs index 76054f6..b98fad2 100644 --- a/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs +++ b/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs @@ -102,6 +102,11 @@ void ReplaceSubKey(string key) } else { + if (context.IsQuietOnStandardOutput) + { + return; + } + using var tr = context.ReplaceInputPath is { } rip ? new StreamReader(rip, Encoding.UTF8, true) : Console.In; diff --git a/RelaxVersioner.Core/Writers/SourceCodeWriteProviderBase.cs b/RelaxVersioner.Core/Writers/SourceCodeWriteProviderBase.cs index 767142b..ca98b11 100644 --- a/RelaxVersioner.Core/Writers/SourceCodeWriteProviderBase.cs +++ b/RelaxVersioner.Core/Writers/SourceCodeWriteProviderBase.cs @@ -129,16 +129,12 @@ public override sealed void Write( var importSet = Utilities.AggregateImports(elementSet); var ruleSet = Utilities.AggregateRules(elementSet); - if (context.IsDryRun) + if (context.IsDryRun || + string.IsNullOrWhiteSpace(context.OutputPath)) { return; } - if (string.IsNullOrWhiteSpace(context.OutputPath)) - { - throw new ArgumentException("Output path required."); - } - this.Write(context, keyValues, generated, ruleSet, importSet); } diff --git a/RelaxVersioner.Core/Writers/TextReplaceProvider.cs b/RelaxVersioner.Core/Writers/TextReplaceProvider.cs index 890c294..d7b80b1 100644 --- a/RelaxVersioner.Core/Writers/TextReplaceProvider.cs +++ b/RelaxVersioner.Core/Writers/TextReplaceProvider.cs @@ -72,6 +72,11 @@ void Replace(TextReader tr, TextWriter tw) } else { + if (context.IsQuietOnStandardOutput) + { + return; + } + using var tr = context.ReplaceInputPath is { } rip ? new StreamReader(rip, Encoding.UTF8, true) : Console.In; diff --git a/RelaxVersioner.Core/Writers/TextWriteProvider.cs b/RelaxVersioner.Core/Writers/TextWriteProvider.cs index 6574bc1..f4d4af4 100644 --- a/RelaxVersioner.Core/Writers/TextWriteProvider.cs +++ b/RelaxVersioner.Core/Writers/TextWriteProvider.cs @@ -64,6 +64,11 @@ void Write(TextWriter tw, bool emitEol) } else { + if (context.IsQuietOnStandardOutput) + { + return; + } + var tw = Console.Out; Write(tw, true); } diff --git a/RelaxVersioner/Program.cs b/RelaxVersioner/Program.cs index a611192..bc6b64a 100644 --- a/RelaxVersioner/Program.cs +++ b/RelaxVersioner/Program.cs @@ -27,9 +27,11 @@ public static async Task Main(string[] args) { var processor = new Processor(logger); var languages = string.Join("|", processor.Languages); + var verbose = false; var context = new ProcessorContext { + IsDryRun = false, Language = "Text", GenerateStatic = true, TextFormat = "{versionLabel}", @@ -42,7 +44,12 @@ public static async Task Main(string[] args) var options = new OptionSet { - { "language=", $"target language [{languages}]", v => context.Language = v }, + { "l|language=", $"target language [{languages}]", v => + { + context.Language = v; + verbose = true; + } + }, { "namespace=", "applying namespace", v => context.Namespace = v }, { "tfm=", "target framework moniker definition (TargetFramework)", v => context.TargetFramework = v }, { "tfid=", "target framework identity definition (TargetFrameworkIdentifier)", v => context.TargetFrameworkIdentity = v }, @@ -90,7 +97,9 @@ public static async Task Main(string[] args) context.OutputPath = "package.json"; } }, + { "quiet", "quiet on stdout", _ => context.IsQuietOnStandardOutput = true }, { "dryrun", "dryrun mode", _ => context.IsDryRun = true }, + { "verbose", "verbose mode", _ => verbose = true }, { "launchDebugger", "Launch debugger", _ => launchDebugger = true }, { "help", "help", v => help = v != null }, }; @@ -112,6 +121,11 @@ public static async Task Main(string[] args) logger.Error(""); return 1; } + + if (!verbose) + { + logger.SetImportance(LogImportance.Ignore); + } context.ProjectDirectory = trails[0]; @@ -122,29 +136,20 @@ public static async Task Main(string[] args) ResultWriter.Write(resultPath!, result); } - if (context.Language switch - { - "Text" => context.IsDryRun, - "Replace" => context.IsDryRun, - "NPM" => context.IsDryRun, - _ => true, - }) - { - var dryrunDisplay = context.IsDryRun ? - " (dryrun)" : string.Empty; - var languageDisplay = context.IsDryRun ? - string.Empty : $"Language={context.Language}, "; - var tfmDisplay = context.IsDryRun ? - string.Empty : $"TFM={context.TargetFramework}, "; + var dryrunDisplay = context.IsDryRun ? + " (dryrun)" : string.Empty; + var languageDisplay = context.IsDryRun ? + string.Empty : $"Language={context.Language}, "; + var tfmDisplay = (context.IsDryRun || string.IsNullOrWhiteSpace(context.TargetFramework)) ? + string.Empty : $"TFM={context.TargetFramework}, "; - logger.Message( - LogImportance.High, - "Generated versions code{0}: {1}{2}Version={3}", - dryrunDisplay, - languageDisplay, - tfmDisplay, - result.Version); - } + logger.Message( + LogImportance.High, + "Generated versions code{0}: {1}{2}Version={3}", + dryrunDisplay, + languageDisplay, + tfmDisplay, + result.Version); } catch (Exception ex) { diff --git a/RelaxVersioner/build/RelaxVersioner.targets b/RelaxVersioner/build/RelaxVersioner.targets index d8e3920..2342ab2 100644 --- a/RelaxVersioner/build/RelaxVersioner.targets +++ b/RelaxVersioner/build/RelaxVersioner.targets @@ -156,7 +156,7 @@ + Command="$(RelaxVersionerToolingRuntimeName)"$(RelaxVersionerToolingPath)" $(_RVB_LaunchDebuggerOption) --verbose --language="$(RelaxVersionerLanguage)" --namespace="$(RelaxVersionerNamespace)" --tfm="$(_RVB_TargetFramework)" --tfid="$(_RVB_TargetFrameworkIdentifier)" --tfv="$(_RVB_TargetFrameworkVersion)" --tfp="$(_RVB_TargetFrameworkProfile)" --genStatic="$(RelaxVersionerGenerateStatic)" --buildIdentifier="$(RelaxVersionerBuildIdentifier)" --propertiesPath="$(RelaxVersionerPropertiesPath)" --outputPath=@(_RVB_OutputPath->'"%(FullPath)"',' ') --resultPath="$(RelaxVersionerResultPath)" "$(MSBuildProjectFullPath)"" /> @@ -328,7 +328,7 @@ + Command="$(RelaxVersionerToolingRuntimeName)"$(RelaxVersionerToolingPath)" $(_RVB_LaunchDebuggerOption) --quiet --buildIdentifier="$(RelaxVersionerBuildIdentifier)" --namespace="$(RelaxVersionerNamespace)" --tfm="$(_RVB_TargetFramework)" --tfid="$(_RVB_TargetFrameworkIdentifier)" --tfv="$(_RVB_TargetFrameworkVersion)" --tfp="$(_RVB_TargetFrameworkProfile)" --genStatic="$(RelaxVersionerGenerateStatic)" --propertiesPath="$(RelaxVersionerPropertiesPath)" --resultPath="$(RelaxVersionerResultPath)" "$(MSBuildProjectFullPath)"" /> From 8bc6ff187638cfde1eee7cebfb706f80397c2ce0 Mon Sep 17 00:00:00 2001 From: Kouji Matsui Date: Tue, 15 Oct 2024 18:32:28 +0900 Subject: [PATCH 6/7] Fixed nothing output on standard output when using npm mode. --- RelaxVersioner/Program.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/RelaxVersioner/Program.cs b/RelaxVersioner/Program.cs index bc6b64a..23c5582 100644 --- a/RelaxVersioner/Program.cs +++ b/RelaxVersioner/Program.cs @@ -87,6 +87,7 @@ public static async Task Main(string[] args) context.Language = "NPM"; context.ReplaceInputPath = "package.json"; context.OutputPath = "package.json"; + verbose = true; } }, { "npmns=", "NPM dependency prefix namespaces", v => @@ -95,6 +96,7 @@ public static async Task Main(string[] args) context.Language = "NPM"; context.ReplaceInputPath = "package.json"; context.OutputPath = "package.json"; + verbose = true; } }, { "quiet", "quiet on stdout", _ => context.IsQuietOnStandardOutput = true }, From c47bebe680fd938a81d4ec216b4618e0f78dc067 Mon Sep 17 00:00:00 2001 From: Kouji Matsui Date: Tue, 15 Oct 2024 19:20:14 +0900 Subject: [PATCH 7/7] Updated readme. --- README.ja.md | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++- README.md | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 137 insertions(+), 2 deletions(-) diff --git a/README.ja.md b/README.ja.md index a780582..f3d1c54 100644 --- a/README.ja.md +++ b/README.ja.md @@ -46,7 +46,8 @@ ILSpyで見れば、すべての情報も確認できるでしょう: * また、埋め込む対象の情報をカスタマイズする事もできます。例えば、MSBuildで使われる変数の値を埋め込むことで、ビルド時の詳細な情報も埋め込むことが出来ます。 * バージョンの埋め込みは、完全にMSBuildに統合されます。つまり、NuGetパッケージをインストールするだけで、Visual Studio、Rider、Visual Studio CodeといったIDEや 、CIのビルドプロセスでそのままバージョン埋め込みを実現できます。 * RelaxVersionerには、環境に依存するコードは含まれていないので、ほぼすべての.NET環境で動作します。 -* CLIインターフェイスも存在します。テキストドキュメントにバージョン番号を埋め込んだり、.NETとは異なるプロジェクトシステム(例えばNPMの`project.json`、`Makefile`など)に対しても、同じバージョン表記を適用できます。 +* CLIインターフェイスも存在します。テキストドキュメントにバージョン番号を埋め込んだり、.NETとは異なるプロジェクトシステム(例えば`Makefile`など)に対しても、同じバージョン表記を適用できます。 + * NPM (Node.js package manager) には専用のモードがあります。CLIを使用することで、簡単にNPMプロジェクトとバージョン操作の一元化を実現できます。 ### 出力されるコードの例 @@ -220,6 +221,70 @@ ABC0123456789abc ... XYZ 例えば、GitHub ActionsのようなCI/CD環境で、NPMパッケージ生成にバージョンを適用したり、テキストのドキュメントにバージョンを埋め込んだりすることが出来ます。 +---- + +## Node.jsプロジェクトで使う + +CLIインターフェイスには、NPM (Node.js package manager) 向けの特別なオプションがあります。 +`package.json` ファイルが存在するディレクトリに移動して、以下のようにCLIを実行します: + +```bash +$ rv --npm . +RelaxVersioner [3.8.0]: Generated versions code: Language=NPM, Version=12.34.56 +``` + +すると、 `package.json` 内の `version` キーの値が、正しいバージョン値に置き換わります。 + +この機能は、NPM標準の `npm version` コマンドと似ています。 +バージョンの検索アルゴリズムはRelaxVersionerに準じるため、.NETプロジェクトと共通化を図りたい場合に便利です。 + +更に、 `--npmns` オプションを使用すると、パッケージが依存するパッケージバージョンも同時に変更することが出来ます。 +例えば、複数のパッケージ群を管理していて、かつパッケージ同士に依存関係がある場合: + +```json +{ + "name": "webapi-interaction", + "version": "0.0.1", // <-- このパッケージのバージョン(更新前) + "dependencies": { + "@foobar/helpers": "^1.4.0", // <-- 依存関係にある管理パッケージのバージョン + "@foobar/common": "^1.7.2", // <-- 依存関係にある管理パッケージのバージョン + "dayjs": "^1.2.3" + } +} +``` + +依存関係にあるパッケージ名の名前空間 (`@foobar`) を指定してCLIを実行します: + +```bash +$ rv --npmns @foobar . +RelaxVersioner [3.8.0]: Generated versions code: Language=NPM, Version=12.34.56 +``` + +これにより、全てのバージョン番号が統一されます: + +```json +{ + "name": "webapi-interaction", + "version": "13.24.5", // <-- 自動的に更新される + "dependencies": { + "@foobar/helpers": "^13.24.5", // <-- 自動的に更新される(依存関係) + "@foobar/common": "^13.24.5", // <-- 自動的に更新される(依存関係) + "dayjs": "^1.2.3" // <-- 更新されない + } +} +``` + +* 依存関係のチェックは、 `dependencies`, `peerDependencies`, `devDependencies` キーに対して行います。 +* 依存関係にあるパッケージを識別するため、パッケージの名前空間を統一してください。 + 実際には単純な前方一致で確認するため、識別可能な名前であればどのような名前でも問題ありません。 +* 依存関係にあるパッケージのバージョンには、自動的に `^` が付きます。 + +この機能は、 [NPM workspaces](https://docs.npmjs.com/cli/v7/using-npm/workspaces) と一緒にCIプロセスで扱う事を想定しています。 +その場合、参照するパッケージのバージョン番号は `*` とするなど、NPM workspacesの取扱いに準じて下さい。 +各パッケージのバージョンを更新したら、ワークスペースルートディレクトリで `npm install` を行うことで、 +`package-lock.json` に正しいバージョン番号が反映されます。 + + ---- ## ヒントや参考情報 @@ -480,6 +545,9 @@ nuspecファイルを使ってパッケージを生成する場合は、デフ ## 履歴 +* 3.8.0: + * NPM `package.json` のバージョンを自動挿入する、NPMモードを追加。 + * NuGetパッケージをビルドする時に、ビルドログにバージョン番号のゴミが出力されていたのを修正。 * 3.7.0: * CLIでカスタムブラケット指定に対応しました。 * 3.6.0: diff --git a/README.md b/README.md index dc7542b..5b657a4 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,8 @@ If you look in ILSpy, you will also see all the information: * You can also customize the information to be embedded. For example, by embedding the value of variables used in MSBuild, you can also embed detailed information at build time. * Version embedding is fully integrated into MSBuild. This means that you can simply install the NuGet package and embed versions directly in the build process of IDEs such as Visual Studio, Rider, Visual Studio Code or in the build process of CI. * RelaxVersioner does not contain any environment-dependent code, so it works in almost any .NET environment. -* A CLI interface also exists. You can embed version numbers in text documents and apply the same version notation to different project systems than .NET (e.g. NPM's `project.json`, `Makefile` and etc.). +* A CLI interface also exists. You can embed version numbers in text documents and apply the same version notation to different project systems than .NET (e.g. `Makefile` and etc.). + * CLI has a dedicated NPM (Node.js package manager) mode, which allows you to easily centralize NPM project and version operations. ## Sample output code @@ -218,6 +219,69 @@ With this CLI, you can use a combination of RelaxVersioner for different targets For example, in a CI/CD environment like GitHub Actions, you can apply versions to NPM package generation or embed versions in your text documentation. +---- + +## Using with Node.js projects + +The CLI interface has special options for NPM (Node.js package manager). +Change to the directory where the `package.json` file is located and run the CLI as follows: + +```bash +$ rv --npm . +RelaxVersioner [3.8.0]: Generated versions code: Language=NPM, Version=12.34.56 +``` + +The value of the `version` key in `package.json` will then be replaced with the correct version value. + +* This function is similar to the standard NPM `npm version` command. +* The version search algorithm is based on RelaxVersioner, so it is useful if you want to share the same version with .NET projects. + +Furthermore, if you use the `--npmns` option, you can also change the package versions that the package depends on at the same time. +For example, if you are managing multiple packages and there are dependencies between the packages: + +```json +{ + "name": "webapi-interaction", + "version": "0.0.1", // <-- The version of this package (before updating) + "dependencies": { + "@foobar/helpers": "^1.4.0", // <-- Version of the managed package that is a dependency + "@foobar/common": "^1.7.2", // <-- Version of the managed package that is a dependency + "dayjs": "^1.2.3" + } +} +``` + +Run the CLI specifying the namespace of the package name (`@foobar`) that is a dependency: + +```bash +$ rv --npmns @foobar . +``` + +This will unify all the version numbers: + +```json +{ + "name": "webapi-interaction", + "version": "13.24.5", // <-- Automatically updated + "dependencies": { + "@foobar/helpers": "^13.24.5", // <-- automatically updated (dependency) + "@foobar/common": "^13.24.5", // <-- automatically updated (dependency) + "dayjs": "^1.2.3" // <-- not updated + } +} +``` + +* Dependency checks are performed on the `dependencies`, `peerDependencies` and `devDependencies` keys. +* Please use a consistent package namespace to identify dependent packages. + * In practice, this is a simple prefix match, so any name that can be identified is fine. + * If you are targeting multiple namespaces, specify them separated by commas. +* The version of a dependent package is automatically prefixed with `^`. + +This function is intended to be used with [NPM workspaces](https://docs.npmjs.com/cli/v7/using-npm/workspaces) and inside the CI process. +In that case, please follow the NPM workspaces handling, such as using `*` as the version number of the referenced package. +After updating the version of each package, run `npm install` in the workspace root directory to update the correct version numbers in `package-lock.json`. + + ---- ## Hints and Tips @@ -462,6 +526,9 @@ When you are using a nuspec file to generate a NuGet package, there are addition ## History +* 3.8.0: + * Added NPM mode, which automatically inserts the version of NPM `package.json`. + * Fixed version number print garbage in the build log when building NuGet packages. * 3.7.0: * CLI now supports custom bracket specification. * 3.6.0: