Skip to content

Commit 1254b55

Browse files
committed
Added NPM versioning feature.
1 parent 8ec8f81 commit 1254b55

File tree

4 files changed

+124
-6
lines changed

4 files changed

+124
-6
lines changed

RelaxVersioner.Core/Processor.cs

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public sealed class ProcessorContext
4141
public string BracketStart;
4242
public string BracketEnd;
4343
public bool IsDryRun;
44+
public string[] NpmPrefixes;
4445
}
4546

4647
public sealed class Processor

RelaxVersioner.Core/RelaxVersioner.Core.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<ItemGroup>
1515
<PackageReference Include="GitReader" Version="1.8.0" />
1616
<PackageReference Include="NamingFormatter" Version="2.4.0" />
17+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
1718
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" />
1819
<PackageReference Condition="'$(RV_BOOTSTRAP)' != 'True'"
1920
Include="RelaxVersioner" Version="3.7.0" PrivateAssets="all" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
////////////////////////////////////////////////////////////////////////////////////////
2+
//
3+
// RelaxVersioner - Git tag/branch based, full-automatic version generator.
4+
// Copyright (c) Kouji Matsui (@kozy_kekyo, @[email protected])
5+
//
6+
// Licensed under Apache-v2: https://opensource.org/licenses/Apache-2.0
7+
//
8+
////////////////////////////////////////////////////////////////////////////////////////
9+
10+
#nullable enable
11+
12+
using System;
13+
using System.Collections.Generic;
14+
using System.Diagnostics;
15+
using System.Globalization;
16+
using System.IO;
17+
using System.Linq;
18+
using System.Text;
19+
using NamingFormatter;
20+
using Newtonsoft.Json;
21+
using Newtonsoft.Json.Linq;
22+
23+
namespace RelaxVersioner.Writers;
24+
25+
internal sealed class NpmReplaceProvider : WriteProviderBase
26+
{
27+
public override string Language => "NPM";
28+
29+
public override void Write(
30+
ProcessorContext context,
31+
Dictionary<string, object?> keyValues,
32+
DateTimeOffset generated)
33+
{
34+
void Replace(TextReader tr, TextWriter tw)
35+
{
36+
var jr = new JsonTextReader(tr);
37+
var jt = JToken.ReadFrom(jr);
38+
39+
var formattedVersion = Named.Format(
40+
CultureInfo.InvariantCulture,
41+
context.TextFormat,
42+
keyValues,
43+
key => string.Empty,
44+
new(context.BracketStart, context.BracketEnd));
45+
46+
jt["version"] = formattedVersion;
47+
48+
if (context.NpmPrefixes.Length >= 1)
49+
{
50+
void ReplaceSubKey(string key)
51+
{
52+
if (jt[key] is JObject jo)
53+
{
54+
foreach (var jp in jo.Properties())
55+
{
56+
if (context.NpmPrefixes.Any(jp.Name.StartsWith))
57+
{
58+
jp.Value = JValue.CreateString(formattedVersion);
59+
}
60+
}
61+
}
62+
}
63+
64+
ReplaceSubKey("dependencies");
65+
ReplaceSubKey("peerDependencies");
66+
ReplaceSubKey("devDependencies");
67+
}
68+
69+
var jw = new JsonTextWriter(tw);
70+
jt.WriteTo(jw);
71+
72+
jw.Flush();
73+
tw.Flush();
74+
}
75+
76+
if (!string.IsNullOrWhiteSpace(context.OutputPath))
77+
{
78+
if (context.IsDryRun)
79+
{
80+
return;
81+
}
82+
83+
Processor.WriteSafeTransacted(
84+
context.OutputPath,
85+
stream =>
86+
{
87+
using var tr = context.ReplaceInputPath is { } rip ?
88+
new StreamReader(rip, Encoding.UTF8, true) :
89+
Console.In;
90+
var tw = new StreamWriter(stream, Encoding.UTF8);
91+
92+
Replace(tr, tw);
93+
});
94+
}
95+
else
96+
{
97+
using var tr = context.ReplaceInputPath is { } rip ?
98+
new StreamReader(rip, Encoding.UTF8, true) :
99+
Console.In;
100+
Replace(tr, Console.Out);
101+
}
102+
}
103+
}

RelaxVersioner/Program.cs

+19-6
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public static async Task<int> Main(string[] args)
3333
Language = "Text",
3434
GenerateStatic = true,
3535
TextFormat = "{versionLabel}",
36+
NpmPrefixes = Array.Empty<string>(),
3637
};
3738

3839
string? resultPath = null;
@@ -52,12 +53,7 @@ public static async Task<int> Main(string[] args)
5253
{ "propertiesPath=", $"properties file", v => context.PropertiesPath = v },
5354
{ "o|outputPath=", $"output source file", v => context.OutputPath = v },
5455
{ "resultPath=", $"output result via xml file", v => resultPath = v },
55-
{ "f|format=", $"set text format", v =>
56-
{
57-
context.TextFormat = v;
58-
context.Language = "Text";
59-
}
60-
},
56+
{ "f|format=", $"set text format", v => context.TextFormat = v },
6157
{ "r|replace", "replace standard input", _ =>
6258
{
6359
context.ReplaceInputPath = null;
@@ -79,6 +75,23 @@ public static async Task<int> Main(string[] args)
7975
}
8076
}
8177
},
78+
{ "n|npm", "replace NPM package.json", v =>
79+
{
80+
context.Language = "NPM";
81+
context.ReplaceInputPath = "package.json";
82+
context.OutputPath = "package.json";
83+
context.TextFormat = "^{versionLabel}";
84+
}
85+
},
86+
{ "npmpn=", "NPM dependency prefix namespaces", v =>
87+
{
88+
context.NpmPrefixes = v.Split(',').Select(n => n.Trim()).ToArray();
89+
context.Language = "NPM";
90+
context.ReplaceInputPath = "package.json";
91+
context.OutputPath = "package.json";
92+
context.TextFormat = "^{versionLabel}";
93+
}
94+
},
8295
{ "dryrun", "dryrun mode", _ => context.IsDryRun = true },
8396
{ "launchDebugger", "Launch debugger", _ => launchDebugger = true },
8497
{ "help", "help", v => help = v != null },

0 commit comments

Comments
 (0)