|
| 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 | +} |
0 commit comments