Skip to content

Commit

Permalink
Cleanup and SDK update (#16)
Browse files Browse the repository at this point in the history
* Cleanup and SDK update

* Build fixes
  • Loading branch information
Turnerj authored Apr 29, 2024
1 parent 360998b commit 40360b3
Show file tree
Hide file tree
Showing 19 changed files with 395 additions and 422 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ jobs:
uses: actions/[email protected]
with:
dotnet-version: |
5.0.x
6.0.x
8.0.x
- name: .NET info
run: dotnet --info
- name: Install dependencies
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Simple build versioning for .NET, powered by Git tags.

![Build](https://img.shields.io/github/workflow/status/TurnerSoftware/buildversioning/Build)
![Build](https://img.shields.io/github/actions/workflow/status/TurnerSoftware/buildversioning/build.yml?branch=main)
[![Codecov](https://img.shields.io/codecov/c/github/turnersoftware/BuildVersioning/main.svg)](https://codecov.io/gh/TurnerSoftware/BuildVersioning)
[![NuGet](https://img.shields.io/nuget/v/TurnerSoftware.BuildVersioning.svg)](https://www.nuget.org/packages/TurnerSoftware.BuildVersioning/)
</div>
Expand Down Expand Up @@ -37,8 +37,8 @@ These support plans help fund our OSS commitments to provide better software for

- Your project must be using a modern SDK-style project file
- One of the following .NET runtimes must be installed:
- .NET 5
- .NET 6
- .NET 8

The runtime requirement is so that Build Versioning itself can run.
Your project though can target whatever version of .NET you want (Framework/Standard/Core etc).
Expand Down
8 changes: 4 additions & 4 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ jobs:
BUILD_ARTIFACT_PATH: $(Build.ArtifactStagingDirectory)

steps:
- task: UseDotNet@2
displayName: Install .NET 5 SDK
inputs:
version: 5.0.x
- task: UseDotNet@2
displayName: Install .NET 6 SDK
inputs:
version: 6.0.x
- task: UseDotNet@2
displayName: Install .NET 8 SDK
inputs:
version: 8.0.x

- script: dotnet --info
displayName: .NET info
Expand Down
13 changes: 6 additions & 7 deletions src/TurnerSoftware.BuildVersioning.Tool/BuildVersion.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace TurnerSoftware.BuildVersioning.Tool
namespace TurnerSoftware.BuildVersioning.Tool;

public record BuildVersion
{
public record BuildVersion
{
public string FullVersion { get; init; }
public string FileVersion { get; init; }
public string AssemblyVersion { get; init; }
}
public string FullVersion { get; init; }
public string FileVersion { get; init; }
public string AssemblyVersion { get; init; }
}
116 changes: 54 additions & 62 deletions src/TurnerSoftware.BuildVersioning.Tool/BuildVersioner.cs
Original file line number Diff line number Diff line change
@@ -1,82 +1,74 @@
namespace TurnerSoftware.BuildVersioning.Tool
namespace TurnerSoftware.BuildVersioning.Tool;

internal class BuildVersioner(IVersionDetailsProvider versionDetailsProvider)
{
internal class BuildVersioner
public BuildVersion GetBuildVersion(BuildVersioningOptions options)
{
private IVersionDetailsProvider VersionDetailsProvider { get; }

public BuildVersioner(IVersionDetailsProvider versionDetailsProvider)
var versionDetails = versionDetailsProvider.GetVersionDetails();
if (versionDetails is null)
{
VersionDetailsProvider = versionDetailsProvider;
return null;
}

public BuildVersion GetBuildVersion(BuildVersioningOptions options)
if (!versionDetails.IsTaggedRelease && versionDetails.PreRelease is null && options.PreReleaseFormat?.Length > 0)
{
var versionDetails = VersionDetailsProvider.GetVersionDetails();
if (versionDetails is null)
{
return null;
}

if (!versionDetails.IsTaggedRelease && versionDetails.PreRelease is null && options.PreReleaseFormat?.Length > 0)
{
versionDetails = versionDetails with
{
PreRelease = options.PreReleaseFormat
.Replace("{CommitHeight}", versionDetails.CommitHeight.ToString())
};
}

if (options.BuildMetadataFormat?.Length > 0)
versionDetails = versionDetails with
{
versionDetails = versionDetails with
{
BuildMetadata = options.BuildMetadataFormat
.Replace("{CommitHash}", versionDetails.CommitHash)
.Replace("{CommitHeight}", versionDetails.CommitHeight.ToString())
};
}

var fullVersion = FormatFullVersion(options.FullVersionFormat, versionDetails);
var fileVersion = FormatVersion(options.FileVersionFormat, versionDetails);
var assemblyVersion = FormatVersion(options.AssemblyVersionFormat, versionDetails);
PreRelease = options.PreReleaseFormat
.Replace("{CommitHeight}", versionDetails.CommitHeight.ToString())
};
}

return new BuildVersion
if (options.BuildMetadataFormat?.Length > 0)
{
versionDetails = versionDetails with
{
FullVersion = fullVersion,
FileVersion = fileVersion,
AssemblyVersion = assemblyVersion
BuildMetadata = options.BuildMetadataFormat
.Replace("{CommitHash}", versionDetails.CommitHash)
.Replace("{CommitHeight}", versionDetails.CommitHeight.ToString())
};
}

private static string FormatFullVersion(string format, VersionDetails versionDetails)
var fullVersion = FormatFullVersion(options.FullVersionFormat, versionDetails);
var fileVersion = FormatVersion(options.FileVersionFormat, versionDetails);
var assemblyVersion = FormatVersion(options.AssemblyVersionFormat, versionDetails);

return new BuildVersion
{
if (string.IsNullOrEmpty(format))
{
return format;
}
FullVersion = fullVersion,
FileVersion = fileVersion,
AssemblyVersion = assemblyVersion
};
}

return FormatVersion(format, versionDetails)
.Replace("{PreRelease}", versionDetails.PreRelease is null ? default : $"-{versionDetails.PreRelease}")
.Replace("{BuildMetadata}", versionDetails.BuildMetadata is null ? default : $"+{versionDetails.BuildMetadata}");
private static string FormatFullVersion(string format, VersionDetails versionDetails)
{
if (string.IsNullOrEmpty(format))
{
return format;
}

private static string FormatVersion(string format, VersionDetails versionDetails)
{
if (string.IsNullOrEmpty(format))
{
return format;
}
return FormatVersion(format, versionDetails)
.Replace("{PreRelease}", versionDetails.PreRelease is null ? default : $"-{versionDetails.PreRelease}")
.Replace("{BuildMetadata}", versionDetails.BuildMetadata is null ? default : $"+{versionDetails.BuildMetadata}");
}

var autoIncrement = versionDetails.IsTaggedRelease ? 0 : 1;
return format
.Replace("{Major}", versionDetails.MajorVersion.ToString())
.Replace("{Major++}", (versionDetails.MajorVersion + autoIncrement).ToString())
.Replace("{Minor}", versionDetails.MinorVersion.ToString())
.Replace("{Minor++}", (versionDetails.MinorVersion + autoIncrement).ToString())
.Replace("{Patch}", versionDetails.PatchVersion.ToString())
.Replace("{Patch++}", (versionDetails.PatchVersion + autoIncrement).ToString())
.Replace("{CommitHeight}", versionDetails.CommitHeight.ToString())
.Replace("{CommitHash}", versionDetails.CommitHash ?? "NOCANDO");
private static string FormatVersion(string format, VersionDetails versionDetails)
{
if (string.IsNullOrEmpty(format))
{
return format;
}

var autoIncrement = versionDetails.IsTaggedRelease ? 0 : 1;
return format
.Replace("{Major}", versionDetails.MajorVersion.ToString())
.Replace("{Major++}", (versionDetails.MajorVersion + autoIncrement).ToString())
.Replace("{Minor}", versionDetails.MinorVersion.ToString())
.Replace("{Minor++}", (versionDetails.MinorVersion + autoIncrement).ToString())
.Replace("{Patch}", versionDetails.PatchVersion.ToString())
.Replace("{Patch++}", (versionDetails.PatchVersion + autoIncrement).ToString())
.Replace("{CommitHeight}", versionDetails.CommitHeight.ToString())
.Replace("{CommitHash}", versionDetails.CommitHash ?? "NOCANDO");
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace TurnerSoftware.BuildVersioning.Tool
namespace TurnerSoftware.BuildVersioning.Tool;

public record BuildVersioningOptions
{
public record BuildVersioningOptions
{
public string FullVersionFormat { get; init; }
public string FileVersionFormat { get; init; }
public string AssemblyVersionFormat { get; init; }
public string PreReleaseFormat { get; init; }
public string BuildMetadataFormat { get; init; }
}
public string FullVersionFormat { get; init; }
public string FileVersionFormat { get; init; }
public string AssemblyVersionFormat { get; init; }
public string PreReleaseFormat { get; init; }
public string BuildMetadataFormat { get; init; }
}
69 changes: 33 additions & 36 deletions src/TurnerSoftware.BuildVersioning.Tool/GitCommandRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,43 @@
using System.Diagnostics;
using System.Threading.Tasks;

namespace TurnerSoftware.BuildVersioning.Tool
namespace TurnerSoftware.BuildVersioning.Tool;

internal class GitCommandRunner : IGitCommandRunner
{
internal class GitCommandRunner : IGitCommandRunner
private static string RunCommand(string command)
{
private string RunCommand(string command)
using var process = new Process();
process.StartInfo = new ProcessStartInfo("git", command)
{
RedirectStandardOutput = true
};

var waitOnExit = new TaskCompletionSource<object>();
process.Exited += (s, e) => waitOnExit.SetResult(default);
process.EnableRaisingEvents = true;

try
{
process.Start();
}
catch (Exception ex)
{
using (var process = new Process())
{
process.StartInfo = new ProcessStartInfo("git", command)
{
RedirectStandardOutput = true
};

var waitOnExit = new TaskCompletionSource<object>();
process.Exited += (s, e) => waitOnExit.SetResult(default);
process.EnableRaisingEvents = true;

try
{
process.Start();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return null;
}

var standardOutputTask = process.StandardOutput.ReadToEndAsync();

Task.WaitAll(waitOnExit.Task, standardOutputTask);

if (process.ExitCode != 0)
{
return null;
}

return standardOutputTask.Result;
}
Console.Error.WriteLine(ex.Message);
return null;
}

public string GitDescribe() => RunCommand("describe --tags --abbrev=7 --always --long");
var standardOutputTask = process.StandardOutput.ReadToEndAsync();

Task.WaitAll(waitOnExit.Task, standardOutputTask);

if (process.ExitCode != 0)
{
return null;
}

return standardOutputTask.Result;
}

public string GitDescribe() => RunCommand("describe --tags --abbrev=7 --always --long");
}
23 changes: 11 additions & 12 deletions src/TurnerSoftware.BuildVersioning.Tool/IGitCommandRunner.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
namespace TurnerSoftware.BuildVersioning.Tool
namespace TurnerSoftware.BuildVersioning.Tool;

public interface IGitCommandRunner
{
public interface IGitCommandRunner
{
/// <summary>
/// Returns a result from `git describe` containing the tag name, number of commits from the tag (commit height) and a 7-character commit hash.
/// </summary>
/// <remarks>
/// Format with tag: {tag}-{commitHeight}-{commitHash}<br />
/// Format without tag: {commitHash}
/// </remarks>
string GitDescribe();
}
/// <summary>
/// Returns a result from `git describe` containing the tag name, number of commits from the tag (commit height) and a 7-character commit hash.
/// </summary>
/// <remarks>
/// Format with tag: {tag}-{commitHeight}-{commitHash}<br />
/// Format without tag: {commitHash}
/// </remarks>
string GitDescribe();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace TurnerSoftware.BuildVersioning.Tool
namespace TurnerSoftware.BuildVersioning.Tool;

public interface IVersionDetailsProvider
{
public interface IVersionDetailsProvider
{
VersionDetails GetVersionDetails();
}
VersionDetails GetVersionDetails();
}
Loading

0 comments on commit 40360b3

Please sign in to comment.