diff --git a/RelaxVersioner.Core/Logger.cs b/RelaxVersioner.Core/Logger.cs index e068701..74792e2 100644 --- a/RelaxVersioner.Core/Logger.cs +++ b/RelaxVersioner.Core/Logger.cs @@ -10,6 +10,7 @@ #nullable enable using System; +using System.ComponentModel; using System.IO; namespace RelaxVersioner; @@ -18,7 +19,9 @@ public enum LogImportance { Low = 1, Normal = 2, - High =3 + High = 3, + [EditorBrowsable(EditorBrowsableState.Never)] + Ignore = 100, } public abstract class Logger @@ -28,6 +31,8 @@ public abstract class Logger protected Logger(string header) => this.Header = header; + public abstract void SetImportance(LogImportance lowerImportance); + public abstract void Message(LogImportance importance, string message); public virtual void Message(LogImportance importance, string format, params object?[] args) => @@ -55,11 +60,12 @@ public static Logger Create(string header, LogImportance lowerImportance, TextWr internal sealed class TextWriterLogger : Logger { - private readonly LogImportance lowerImportance; private readonly TextWriter @out; private readonly TextWriter warning; private readonly TextWriter error; + private LogImportance lowerImportance; + public TextWriterLogger(string header, LogImportance lowerImportance, TextWriter @out, TextWriter warning, TextWriter error) : base(header) { @@ -69,9 +75,12 @@ public TextWriterLogger(string header, LogImportance lowerImportance, TextWriter this.error = error; } + public override void SetImportance(LogImportance lowerImportance) => + this.lowerImportance = lowerImportance; + public override void Message(LogImportance importance, string message) { - if (importance >= lowerImportance) + if (importance >= this.lowerImportance) { @out.WriteLine(message); } diff --git a/RelaxVersioner.Core/Processor.cs b/RelaxVersioner.Core/Processor.cs index 0123a35..066fb53 100644 --- a/RelaxVersioner.Core/Processor.cs +++ b/RelaxVersioner.Core/Processor.cs @@ -41,6 +41,7 @@ public sealed class ProcessorContext public string BracketStart; public string BracketEnd; public bool IsDryRun; + public bool IsQuietOnStandardOutput; public string[] NpmPrefixes; } @@ -60,21 +61,6 @@ public Processor(Logger logger) public string[] Languages { get; } - private readonly struct TargetCommit - { - public readonly int StartDepth; - public readonly Commit Commit; - - public TargetCommit(int startDepth, Commit commit) - { - this.StartDepth = startDepth; - this.Commit = commit; - } - - public override string ToString() => - $"StartDepth={this.StartDepth}, {this.Commit}"; - } - private static async Task WriteVersionSourceFileAsync( Logger logger, WriteProviderBase writeProvider, @@ -155,10 +141,7 @@ static string FormatSignature(Signature? sig) => sig is { } s ? keyValues[entry.key] = entry.value; } - if (!string.IsNullOrWhiteSpace(context.OutputPath)) - { - writeProvider.Write(context, keyValues, generated); - } + writeProvider.Write(context, keyValues, generated); return new Result( versionLabel, diff --git a/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs b/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs index 76054f6..b98fad2 100644 --- a/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs +++ b/RelaxVersioner.Core/Writers/NpmReplaceProvider.cs @@ -102,6 +102,11 @@ void ReplaceSubKey(string key) } else { + if (context.IsQuietOnStandardOutput) + { + return; + } + using var tr = context.ReplaceInputPath is { } rip ? new StreamReader(rip, Encoding.UTF8, true) : Console.In; diff --git a/RelaxVersioner.Core/Writers/SourceCodeWriteProviderBase.cs b/RelaxVersioner.Core/Writers/SourceCodeWriteProviderBase.cs index 767142b..ca98b11 100644 --- a/RelaxVersioner.Core/Writers/SourceCodeWriteProviderBase.cs +++ b/RelaxVersioner.Core/Writers/SourceCodeWriteProviderBase.cs @@ -129,16 +129,12 @@ public override sealed void Write( var importSet = Utilities.AggregateImports(elementSet); var ruleSet = Utilities.AggregateRules(elementSet); - if (context.IsDryRun) + if (context.IsDryRun || + string.IsNullOrWhiteSpace(context.OutputPath)) { return; } - if (string.IsNullOrWhiteSpace(context.OutputPath)) - { - throw new ArgumentException("Output path required."); - } - this.Write(context, keyValues, generated, ruleSet, importSet); } diff --git a/RelaxVersioner.Core/Writers/TextReplaceProvider.cs b/RelaxVersioner.Core/Writers/TextReplaceProvider.cs index 890c294..d7b80b1 100644 --- a/RelaxVersioner.Core/Writers/TextReplaceProvider.cs +++ b/RelaxVersioner.Core/Writers/TextReplaceProvider.cs @@ -72,6 +72,11 @@ void Replace(TextReader tr, TextWriter tw) } else { + if (context.IsQuietOnStandardOutput) + { + return; + } + using var tr = context.ReplaceInputPath is { } rip ? new StreamReader(rip, Encoding.UTF8, true) : Console.In; diff --git a/RelaxVersioner.Core/Writers/TextWriteProvider.cs b/RelaxVersioner.Core/Writers/TextWriteProvider.cs index 6574bc1..f4d4af4 100644 --- a/RelaxVersioner.Core/Writers/TextWriteProvider.cs +++ b/RelaxVersioner.Core/Writers/TextWriteProvider.cs @@ -64,6 +64,11 @@ void Write(TextWriter tw, bool emitEol) } else { + if (context.IsQuietOnStandardOutput) + { + return; + } + var tw = Console.Out; Write(tw, true); } diff --git a/RelaxVersioner/Program.cs b/RelaxVersioner/Program.cs index a611192..bc6b64a 100644 --- a/RelaxVersioner/Program.cs +++ b/RelaxVersioner/Program.cs @@ -27,9 +27,11 @@ public static async Task Main(string[] args) { var processor = new Processor(logger); var languages = string.Join("|", processor.Languages); + var verbose = false; var context = new ProcessorContext { + IsDryRun = false, Language = "Text", GenerateStatic = true, TextFormat = "{versionLabel}", @@ -42,7 +44,12 @@ public static async Task Main(string[] args) var options = new OptionSet { - { "language=", $"target language [{languages}]", v => context.Language = v }, + { "l|language=", $"target language [{languages}]", v => + { + context.Language = v; + verbose = true; + } + }, { "namespace=", "applying namespace", v => context.Namespace = v }, { "tfm=", "target framework moniker definition (TargetFramework)", v => context.TargetFramework = v }, { "tfid=", "target framework identity definition (TargetFrameworkIdentifier)", v => context.TargetFrameworkIdentity = v }, @@ -90,7 +97,9 @@ public static async Task Main(string[] args) context.OutputPath = "package.json"; } }, + { "quiet", "quiet on stdout", _ => context.IsQuietOnStandardOutput = true }, { "dryrun", "dryrun mode", _ => context.IsDryRun = true }, + { "verbose", "verbose mode", _ => verbose = true }, { "launchDebugger", "Launch debugger", _ => launchDebugger = true }, { "help", "help", v => help = v != null }, }; @@ -112,6 +121,11 @@ public static async Task Main(string[] args) logger.Error(""); return 1; } + + if (!verbose) + { + logger.SetImportance(LogImportance.Ignore); + } context.ProjectDirectory = trails[0]; @@ -122,29 +136,20 @@ public static async Task Main(string[] args) ResultWriter.Write(resultPath!, result); } - if (context.Language switch - { - "Text" => context.IsDryRun, - "Replace" => context.IsDryRun, - "NPM" => context.IsDryRun, - _ => true, - }) - { - var dryrunDisplay = context.IsDryRun ? - " (dryrun)" : string.Empty; - var languageDisplay = context.IsDryRun ? - string.Empty : $"Language={context.Language}, "; - var tfmDisplay = context.IsDryRun ? - string.Empty : $"TFM={context.TargetFramework}, "; + var dryrunDisplay = context.IsDryRun ? + " (dryrun)" : string.Empty; + var languageDisplay = context.IsDryRun ? + string.Empty : $"Language={context.Language}, "; + var tfmDisplay = (context.IsDryRun || string.IsNullOrWhiteSpace(context.TargetFramework)) ? + string.Empty : $"TFM={context.TargetFramework}, "; - logger.Message( - LogImportance.High, - "Generated versions code{0}: {1}{2}Version={3}", - dryrunDisplay, - languageDisplay, - tfmDisplay, - result.Version); - } + logger.Message( + LogImportance.High, + "Generated versions code{0}: {1}{2}Version={3}", + dryrunDisplay, + languageDisplay, + tfmDisplay, + result.Version); } catch (Exception ex) { diff --git a/RelaxVersioner/build/RelaxVersioner.targets b/RelaxVersioner/build/RelaxVersioner.targets index d8e3920..2342ab2 100644 --- a/RelaxVersioner/build/RelaxVersioner.targets +++ b/RelaxVersioner/build/RelaxVersioner.targets @@ -156,7 +156,7 @@ + Command="$(RelaxVersionerToolingRuntimeName)"$(RelaxVersionerToolingPath)" $(_RVB_LaunchDebuggerOption) --verbose --language="$(RelaxVersionerLanguage)" --namespace="$(RelaxVersionerNamespace)" --tfm="$(_RVB_TargetFramework)" --tfid="$(_RVB_TargetFrameworkIdentifier)" --tfv="$(_RVB_TargetFrameworkVersion)" --tfp="$(_RVB_TargetFrameworkProfile)" --genStatic="$(RelaxVersionerGenerateStatic)" --buildIdentifier="$(RelaxVersionerBuildIdentifier)" --propertiesPath="$(RelaxVersionerPropertiesPath)" --outputPath=@(_RVB_OutputPath->'"%(FullPath)"',' ') --resultPath="$(RelaxVersionerResultPath)" "$(MSBuildProjectFullPath)"" /> @@ -328,7 +328,7 @@ + Command="$(RelaxVersionerToolingRuntimeName)"$(RelaxVersionerToolingPath)" $(_RVB_LaunchDebuggerOption) --quiet --buildIdentifier="$(RelaxVersionerBuildIdentifier)" --namespace="$(RelaxVersionerNamespace)" --tfm="$(_RVB_TargetFramework)" --tfid="$(_RVB_TargetFrameworkIdentifier)" --tfv="$(_RVB_TargetFrameworkVersion)" --tfp="$(_RVB_TargetFrameworkProfile)" --genStatic="$(RelaxVersionerGenerateStatic)" --propertiesPath="$(RelaxVersionerPropertiesPath)" --resultPath="$(RelaxVersionerResultPath)" "$(MSBuildProjectFullPath)"" />