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 },