Skip to content

Commit

Permalink
Merge pull request #94 from AshleighAdams/additional-output-options
Browse files Browse the repository at this point in the history
Add additional outputs
  • Loading branch information
AshleighAdams authored Jun 24, 2022
2 parents dddeff9 + 7e00cd5 commit cc155d6
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<MinVerTagPrefix>v</MinVerTagPrefix>
<!-- delete/set to patch the line below once almost out of v0.x.y (preferably once on a beta or rc). -->
<MinVerAutoIncrement>patch</MinVerAutoIncrement>
<MinVerMinimumMajorMinor>2.2</MinVerMinimumMajorMinor>
<MinVerMinimumMajorMinor>2.3</MinVerMinimumMajorMinor>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MinVer" Version="4.0.0">
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ See [docs/VersionCalculation.md](docs/VersionCalculation.md) for further reading
| Set the build data to this value. | -b, --build-metadata, VerliteBuildMetadata | |
| Part of the version to print. | -s, --show | all |
| Automatically fetch commits and a tag for shallow clones. | --auto-fetch | false |
| Create a lightweight tag instead of fetching the remote's. | --enable-lightweight-tags | false |
| Create a lightweight tag instead of fetching the remote's. | --enable-lightweight-tags | false |
| Set which version part should be bumped after an RTM release. | -a, --auto-increment, VerliteAutoIncrement | patch |
| A command to test whether a tag should be ignored via exit code. | -f, --filter-tags, VerliteFilterTags | |
| The remote endpoint to use when fetching tags and commits. | -r, --remote, VerliteRemote | origin |
Expand Down Expand Up @@ -243,6 +243,8 @@ namespace Verlite
public const string Patch;
public const string Prerelease;
public const string BuildMetadata;
public const string Commit;
public const string Height;
}
}
```
Expand Down
67 changes: 67 additions & 0 deletions src/Verlite.CLI/JsonOutput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System.Globalization;
using System.Text;

namespace Verlite.CLI
{
internal static class JsonOutput
{
public static string GenerateOutput(
SemVer version,
Commit? commit,
TaggedVersion? lastTag,
int? height)
{
var sb = new StringBuilder();
sb.AppendLine("{");
sb.AppendString(1, "commit", commit?.Id);
sb.AppendString(1, "full", version.ToString());
sb.AppendInteger(1, "major", version.Major);
sb.AppendInteger(1, "minor", version.Minor);
sb.AppendInteger(1, "patch", version.Patch);
sb.AppendString(1, "prerelease", version.Prerelease);
sb.AppendString(1, "meta", version.BuildMetadata);
sb.AppendInteger(1, "height", height);
if (lastTag is null)
{
sb.AppendLine("\t" + @"""lastTag"": null");
}
else
{
sb.AppendLine("\t" + @"""lastTag"": {");
sb.AppendString(2, "tag", lastTag.Tag.Name);
sb.AppendString(2, "commit", lastTag.Tag.PointsTo.Id);
sb.AppendString(2, "full", lastTag.Version.ToString());
sb.AppendInteger(2, "major", lastTag.Version.Major);
sb.AppendInteger(2, "minor", lastTag.Version.Minor);
sb.AppendInteger(2, "patch", lastTag.Version.Patch);
sb.AppendString(2, "prerelease", lastTag.Version.Prerelease);
sb.AppendString(2, "meta", lastTag.Version.BuildMetadata, final: true);
sb.AppendLine("\t}");
}
sb.AppendLine("}");

return sb.ToString();
}

private static void AppendString(this StringBuilder sb, int indentation, string key, string? value, bool final = false)
{
value = value is null ? "null" : $@"""{System.Web.HttpUtility.JavaScriptStringEncode(value)}""";
sb.Append(new string('\t', indentation));
sb.Append($@"""{key}"": {value}");
if (final)
sb.AppendLine("");
else
sb.AppendLine(",");
}
private static void AppendInteger(this StringBuilder sb, int indentation, string key, int? value, bool final = false)
{
var valuestr = value is null ? "null" : value.Value.ToString(CultureInfo.InvariantCulture);
sb.Append(new string('\t', indentation));
sb.Append($@"""{key}"": ""{valuestr}""");
if (final)
sb.AppendLine("");
else
sb.AppendLine(",");
}
}
}
9 changes: 8 additions & 1 deletion src/Verlite.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ private async static Task RootCommandAsync(
};

var version = opts.VersionOverride ?? new SemVer();
Commit? commit = null;
TaggedVersion? lastTag = null;
int? height = null;

if (opts.VersionOverride is null)
{
using var repo = await GitRepoInspector.FromPath(sourceDirectory, opts.Remote, log, commandRunner);
Expand All @@ -151,7 +155,8 @@ private async static Task RootCommandAsync(
if (!string.IsNullOrWhiteSpace(filterTags))
tagFilter = new CommandTagFilter(commandRunner, log, filterTags, sourceDirectory);

version = await VersionCalculator.FromRepository(repo, opts, log, tagFilter);
commit = await repo.GetHead();
(version, lastTag, height) = await VersionCalculator.FromRepository2(repo, opts, log, tagFilter);
}

string toShow = show switch
Expand All @@ -162,6 +167,8 @@ private async static Task RootCommandAsync(
Show.patch => version.Patch.ToString(CultureInfo.InvariantCulture),
Show.prerelease => version.Prerelease?.ToString(CultureInfo.InvariantCulture) ?? string.Empty,
Show.metadata => version.BuildMetadata?.ToString(CultureInfo.InvariantCulture) ?? string.Empty,
Show.height => height?.ToString(CultureInfo.InvariantCulture) ?? string.Empty,
Show.json => JsonOutput.GenerateOutput(version, commit, lastTag, height),
_ => throw new NotImplementedException(),
};

Expand Down
4 changes: 4 additions & 0 deletions src/Verlite.CLI/Show.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ internal enum Show
patch = 3,
prerelease = 4,
metadata = 5,
height = 6,
json = 7,
}
internal static partial class Parsers
{
Expand All @@ -34,6 +36,8 @@ Show invalid()
"PATCH" => Show.patch,
"PRERELEASE" => Show.prerelease,
"METADATA" => Show.metadata,
"HEIGHT" => Show.height,
"JSON" => Show.json,
_ => invalid(),
};
}
Expand Down
1 change: 1 addition & 0 deletions src/Verlite.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
static Verlite.VersionCalculator.FromRepository2(Verlite.IRepoInspector! repo, Verlite.VersionCalculationOptions! options, Verlite.ILogger? log, Verlite.ITagFilter? tagFilter) -> System.Threading.Tasks.Task<(Verlite.SemVer version, Verlite.TaggedVersion? lastTag, int? height)>!
30 changes: 30 additions & 0 deletions src/Verlite.Core/VersionCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public static SemVer FromTagInfomation(SemVer? lastTag, VersionCalculationOption
/// <param name="log">A log for diagnostics.</param>
/// <param name="tagFilter">A filter to ignore tags. When <c>null</c> no filter is used.</param>
/// <returns>The version for the state of the repository.</returns>
[Obsolete("Use FromRepository2()")]
public static async Task<SemVer> FromRepository(
IRepoInspector repo,
VersionCalculationOptions options,
Expand All @@ -120,5 +121,34 @@ public static async Task<SemVer> FromRepository(

return FromTagInfomation(lastTagVer?.Version, options, height);
}

/// <summary>
/// Calculate the next version from a repository.
/// </summary>
/// <param name="repo">The repo to use.</param>
/// <param name="options">The options.</param>
/// <param name="log">A log for diagnostics.</param>
/// <param name="tagFilter">A filter to ignore tags. When <c>null</c> no filter is used.</param>
/// <returns>The version for the state of the repository, and the associated tag information.</returns>
public static async Task<(SemVer version, TaggedVersion? lastTag, int? height)> FromRepository2(
IRepoInspector repo,
VersionCalculationOptions options,
ILogger? log,
ITagFilter? tagFilter)
{
if (options.VersionOverride.HasValue)
return (options.VersionOverride.Value, null, null);

var (height, lastTag) = await HeightCalculator.FromRepository(
repo: repo,
tagPrefix: options.TagPrefix,
queryRemoteTags: options.QueryRemoteTags,
fetchTags: options.QueryRemoteTags,
log: log,
tagFilter: tagFilter);

var version = FromTagInfomation(lastTag?.Version, options, height);
return (version, lastTag, height);
}
}
}
7 changes: 6 additions & 1 deletion src/Verlite.MsBuild/GetVersionTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ private async Task<bool> ExecuteAsync()

var version = opts.VersionOverride ?? new SemVer();
var commit = string.Empty;
var height = string.Empty;
if (opts.VersionOverride is null)
{
using var repo = await GitRepoInspector.FromPath(ProjectDirectory, opts.Remote, log, commandRunner);
Expand All @@ -117,8 +118,10 @@ private async Task<bool> ExecuteAsync()
if (!string.IsNullOrWhiteSpace(FilterTags))
tagFilter = new CommandTagFilter(commandRunner, log, FilterTags, ProjectDirectory);

version = await VersionCalculator.FromRepository(repo, opts, log, tagFilter);
int? heightInt;
(version, _, heightInt) = await VersionCalculator.FromRepository2(repo, opts, log, tagFilter);
commit = (await repo.GetHead())?.Id ?? string.Empty;
height = heightInt?.ToString(CultureInfo.InvariantCulture) ?? string.Empty;
}

Version = version.ToString();
Expand All @@ -128,6 +131,7 @@ private async Task<bool> ExecuteAsync()
VersionPrerelease = version.Prerelease ?? string.Empty;
VersionBuildMetadata = version.BuildMetadata ?? string.Empty;
Commit = commit;
Height = height;

return true;
}
Expand All @@ -138,5 +142,6 @@ private async Task<bool> ExecuteAsync()
[Output] public string VersionPrerelease { get; private set; } = "";
[Output] public string VersionBuildMetadata { get; private set; } = "";
[Output] public string Commit { get; private set; } = "";
[Output] public string Height { get; private set; } = "";
}
}
3 changes: 3 additions & 0 deletions src/Verlite.MsBuild/VersionEmbedGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void Execute(GeneratorExecutionContext context)
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.VerlitePrerelease", out var prerelease);
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.VerliteBuildMetadata", out var meta);
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.VerliteCommit", out var commit);
context.AnalyzerConfigOptions.GlobalOptions.TryGetValue("build_property.VerliteHeight", out var height);

version ??= string.Empty;
major ??= string.Empty;
Expand All @@ -32,6 +33,7 @@ public void Execute(GeneratorExecutionContext context)
prerelease ??= string.Empty;
meta ??= string.Empty;
commit ??= string.Empty;
height ??= string.Empty;

var coreVersion = string.IsNullOrEmpty(version) ? string.Empty : $"{major}.{minor}.{patch}";

Expand All @@ -54,6 +56,7 @@ internal static class Version
public const string Prerelease = ""{prerelease}"";
public const string BuildMetadata = ""{meta}"";
public const string Commit = ""{commit}"";
public const string Height = ""{height}"";
}}
}}
";
Expand Down
2 changes: 2 additions & 0 deletions src/Verlite.MsBuild/build/Verlite.MsBuild.targets
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ This file has been modified by Ashleigh Adams and adapted for Verlite -->
<CompilerVisibleProperty Include="VerlitePrerelease" />
<CompilerVisibleProperty Include="VerliteBuildMetadata" />
<CompilerVisibleProperty Include="VerliteCommit" />
<CompilerVisibleProperty Include="VerliteHeight" />
</ItemGroup>

<PropertyGroup>
Expand Down Expand Up @@ -77,6 +78,7 @@ This file has been modified by Ashleigh Adams and adapted for Verlite -->
<Output TaskParameter="VersionPrerelease" PropertyName="VerlitePrerelease" />
<Output TaskParameter="VersionBuildMetadata" PropertyName="VerliteBuildMetadata" />
<Output TaskParameter="Commit" PropertyName="VerliteCommit" />
<Output TaskParameter="Height" PropertyName="VerliteHeight" />
</Verlite.MsBuild.GetVersionTask>

<PropertyGroup>
Expand Down
Loading

0 comments on commit cc155d6

Please sign in to comment.