|
| 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.Text; |
| 18 | +using NamingFormatter; |
| 19 | +using Newtonsoft.Json; |
| 20 | + |
| 21 | +namespace RelaxVersioner.Writers; |
| 22 | + |
| 23 | +internal sealed class NpmReplaceProvider : WriteProviderBase |
| 24 | +{ |
| 25 | + public override string Language => "NPM"; |
| 26 | + |
| 27 | + public override void Write( |
| 28 | + ProcessorContext context, |
| 29 | + Dictionary<string, object?> keyValues, |
| 30 | + DateTimeOffset generated) |
| 31 | + { |
| 32 | + void Replace(TextReader tr, TextWriter tw) |
| 33 | + { |
| 34 | + var jr = new JsonTextReader(tr); |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | + while (true) |
| 39 | + { |
| 40 | + var line = tr.ReadLine(); |
| 41 | + if (line == null) |
| 42 | + { |
| 43 | + break; |
| 44 | + } |
| 45 | + |
| 46 | + var formattedLine = Named.Format( |
| 47 | + CultureInfo.InvariantCulture, |
| 48 | + line, |
| 49 | + keyValues, |
| 50 | + key => string.Empty, |
| 51 | + new(context.BracketStart, context.BracketEnd)); |
| 52 | + |
| 53 | + tw.WriteLine(formattedLine); |
| 54 | + } |
| 55 | + |
| 56 | + tw.Flush(); |
| 57 | + } |
| 58 | + |
| 59 | + if (!string.IsNullOrWhiteSpace(context.OutputPath)) |
| 60 | + { |
| 61 | + if (context.IsDryRun) |
| 62 | + { |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + Processor.WriteSafeTransacted( |
| 67 | + context.OutputPath, |
| 68 | + stream => |
| 69 | + { |
| 70 | + using var tr = context.ReplaceInputPath is { } rip ? |
| 71 | + new StreamReader(rip, Encoding.UTF8, true) : |
| 72 | + Console.In; |
| 73 | + var tw = new StreamWriter(stream, Encoding.UTF8); |
| 74 | + |
| 75 | + Replace(tr, tw); |
| 76 | + }); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + using var tr = context.ReplaceInputPath is { } rip ? |
| 81 | + new StreamReader(rip, Encoding.UTF8, true) : |
| 82 | + Console.In; |
| 83 | + Replace(tr, Console.Out); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments