-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
124 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters