Skip to content

Commit

Permalink
Added NPM versioning feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed Oct 15, 2024
1 parent 8ec8f81 commit 1254b55
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 6 deletions.
1 change: 1 addition & 0 deletions RelaxVersioner.Core/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public sealed class ProcessorContext
public string BracketStart;
public string BracketEnd;
public bool IsDryRun;
public string[] NpmPrefixes;
}

public sealed class Processor
Expand Down
1 change: 1 addition & 0 deletions RelaxVersioner.Core/RelaxVersioner.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="GitReader" Version="1.8.0" />
<PackageReference Include="NamingFormatter" Version="2.4.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" />
<PackageReference Condition="'$(RV_BOOTSTRAP)' != 'True'"
Include="RelaxVersioner" Version="3.7.0" PrivateAssets="all" />
Expand Down
103 changes: 103 additions & 0 deletions RelaxVersioner.Core/Writers/NpmReplaceProvider.cs
Original file line number Diff line number Diff line change
@@ -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<string, object?> 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);
}
}
}
25 changes: 19 additions & 6 deletions RelaxVersioner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static async Task<int> Main(string[] args)
Language = "Text",
GenerateStatic = true,
TextFormat = "{versionLabel}",
NpmPrefixes = Array.Empty<string>(),
};

string? resultPath = null;
Expand All @@ -52,12 +53,7 @@ public static async Task<int> 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;
Expand All @@ -79,6 +75,23 @@ public static async Task<int> 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 },
Expand Down

0 comments on commit 1254b55

Please sign in to comment.