Skip to content

Commit

Permalink
Fixed unprinted version when default usage of cli.
Browse files Browse the repository at this point in the history
  • Loading branch information
kekyo committed Oct 15, 2024
1 parent 39990b4 commit a97277f
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 53 deletions.
15 changes: 12 additions & 3 deletions RelaxVersioner.Core/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#nullable enable

using System;
using System.ComponentModel;
using System.IO;

namespace RelaxVersioner;
Expand All @@ -18,7 +19,9 @@ public enum LogImportance
{
Low = 1,
Normal = 2,
High =3
High = 3,
[EditorBrowsable(EditorBrowsableState.Never)]
Ignore = 100,
}

public abstract class Logger
Expand All @@ -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) =>
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
}
Expand Down
21 changes: 2 additions & 19 deletions RelaxVersioner.Core/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public sealed class ProcessorContext
public string BracketStart;
public string BracketEnd;
public bool IsDryRun;
public bool IsQuietOnStandardOutput;
public string[] NpmPrefixes;
}

Expand All @@ -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<Result> WriteVersionSourceFileAsync(
Logger logger,
WriteProviderBase writeProvider,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions RelaxVersioner.Core/Writers/NpmReplaceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions RelaxVersioner.Core/Writers/SourceCodeWriteProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
5 changes: 5 additions & 0 deletions RelaxVersioner.Core/Writers/TextReplaceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions RelaxVersioner.Core/Writers/TextWriteProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ void Write(TextWriter tw, bool emitEol)
}
else
{
if (context.IsQuietOnStandardOutput)
{
return;
}

var tw = Console.Out;
Write(tw, true);
}
Expand Down
51 changes: 28 additions & 23 deletions RelaxVersioner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ public static async Task<int> 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}",
Expand All @@ -42,7 +44,12 @@ public static async Task<int> 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 },
Expand Down Expand Up @@ -90,7 +97,9 @@ public static async Task<int> 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 },
};
Expand All @@ -112,6 +121,11 @@ public static async Task<int> Main(string[] args)
logger.Error("");
return 1;
}

if (!verbose)
{
logger.SetImportance(LogImportance.Ignore);
}

context.ProjectDirectory = trails[0];

Expand All @@ -122,29 +136,20 @@ public static async Task<int> 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)
{
Expand Down
4 changes: 2 additions & 2 deletions RelaxVersioner/build/RelaxVersioner.targets
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
</ItemGroup>

<Exec WorkingDirectory="$(RelaxVersionerToolingDir)"
Command="$(RelaxVersionerToolingRuntimeName)&quot;$(RelaxVersionerToolingPath)&quot; $(_RVB_LaunchDebuggerOption) --language=&quot;$(RelaxVersionerLanguage)&quot; --namespace=&quot;$(RelaxVersionerNamespace)&quot; --tfm=&quot;$(_RVB_TargetFramework)&quot; --tfid=&quot;$(_RVB_TargetFrameworkIdentifier)&quot; --tfv=&quot;$(_RVB_TargetFrameworkVersion)&quot; --tfp=&quot;$(_RVB_TargetFrameworkProfile)&quot; --genStatic=&quot;$(RelaxVersionerGenerateStatic)&quot; --buildIdentifier=&quot;$(RelaxVersionerBuildIdentifier)&quot; --propertiesPath=&quot;$(RelaxVersionerPropertiesPath)&quot; --outputPath=@(_RVB_OutputPath->'&quot;%(FullPath)&quot;',' ') --resultPath=&quot;$(RelaxVersionerResultPath)&quot; &quot;$(MSBuildProjectFullPath)&quot;" />
Command="$(RelaxVersionerToolingRuntimeName)&quot;$(RelaxVersionerToolingPath)&quot; $(_RVB_LaunchDebuggerOption) --verbose --language=&quot;$(RelaxVersionerLanguage)&quot; --namespace=&quot;$(RelaxVersionerNamespace)&quot; --tfm=&quot;$(_RVB_TargetFramework)&quot; --tfid=&quot;$(_RVB_TargetFrameworkIdentifier)&quot; --tfv=&quot;$(_RVB_TargetFrameworkVersion)&quot; --tfp=&quot;$(_RVB_TargetFrameworkProfile)&quot; --genStatic=&quot;$(RelaxVersionerGenerateStatic)&quot; --buildIdentifier=&quot;$(RelaxVersionerBuildIdentifier)&quot; --propertiesPath=&quot;$(RelaxVersionerPropertiesPath)&quot; --outputPath=@(_RVB_OutputPath->'&quot;%(FullPath)&quot;',' ') --resultPath=&quot;$(RelaxVersionerResultPath)&quot; &quot;$(MSBuildProjectFullPath)&quot;" />

<ItemGroup>
<FileWrites Include="$(RelaxVersionerResultPath)" />
Expand Down Expand Up @@ -328,7 +328,7 @@
</ItemGroup>

<Exec WorkingDirectory="$(RelaxVersionerToolingDir)"
Command="$(RelaxVersionerToolingRuntimeName)&quot;$(RelaxVersionerToolingPath)&quot; $(_RVB_LaunchDebuggerOption) --buildIdentifier=&quot;$(RelaxVersionerBuildIdentifier)&quot; --namespace=&quot;$(RelaxVersionerNamespace)&quot; --tfm=&quot;$(_RVB_TargetFramework)&quot; --tfid=&quot;$(_RVB_TargetFrameworkIdentifier)&quot; --tfv=&quot;$(_RVB_TargetFrameworkVersion)&quot; --tfp=&quot;$(_RVB_TargetFrameworkProfile)&quot; --genStatic=&quot;$(RelaxVersionerGenerateStatic)&quot; --propertiesPath=&quot;$(RelaxVersionerPropertiesPath)&quot; --resultPath=&quot;$(RelaxVersionerResultPath)&quot; &quot;$(MSBuildProjectFullPath)&quot;" />
Command="$(RelaxVersionerToolingRuntimeName)&quot;$(RelaxVersionerToolingPath)&quot; $(_RVB_LaunchDebuggerOption) --quiet --buildIdentifier=&quot;$(RelaxVersionerBuildIdentifier)&quot; --namespace=&quot;$(RelaxVersionerNamespace)&quot; --tfm=&quot;$(_RVB_TargetFramework)&quot; --tfid=&quot;$(_RVB_TargetFrameworkIdentifier)&quot; --tfv=&quot;$(_RVB_TargetFrameworkVersion)&quot; --tfp=&quot;$(_RVB_TargetFrameworkProfile)&quot; --genStatic=&quot;$(RelaxVersionerGenerateStatic)&quot; --propertiesPath=&quot;$(RelaxVersionerPropertiesPath)&quot; --resultPath=&quot;$(RelaxVersionerResultPath)&quot; &quot;$(MSBuildProjectFullPath)&quot;" />

<ItemGroup>
<FileWrites Include="$(RelaxVersionerResultPath)" />
Expand Down

0 comments on commit a97277f

Please sign in to comment.