Skip to content

Commit 40360b3

Browse files
authored
Cleanup and SDK update (#16)
* Cleanup and SDK update * Build fixes
1 parent 360998b commit 40360b3

19 files changed

+395
-422
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ jobs:
3131
uses: actions/[email protected]
3232
with:
3333
dotnet-version: |
34-
5.0.x
3534
6.0.x
35+
8.0.x
3636
- name: .NET info
3737
run: dotnet --info
3838
- name: Install dependencies

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

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

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

3838
- Your project must be using a modern SDK-style project file
3939
- One of the following .NET runtimes must be installed:
40-
- .NET 5
4140
- .NET 6
41+
- .NET 8
4242

4343
The runtime requirement is so that Build Versioning itself can run.
4444
Your project though can target whatever version of .NET you want (Framework/Standard/Core etc).

azure-pipelines.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ jobs:
1010
BUILD_ARTIFACT_PATH: $(Build.ArtifactStagingDirectory)
1111

1212
steps:
13-
- task: UseDotNet@2
14-
displayName: Install .NET 5 SDK
15-
inputs:
16-
version: 5.0.x
1713
- task: UseDotNet@2
1814
displayName: Install .NET 6 SDK
1915
inputs:
2016
version: 6.0.x
17+
- task: UseDotNet@2
18+
displayName: Install .NET 8 SDK
19+
inputs:
20+
version: 8.0.x
2121

2222
- script: dotnet --info
2323
displayName: .NET info
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
namespace TurnerSoftware.BuildVersioning.Tool
1+
namespace TurnerSoftware.BuildVersioning.Tool;
2+
3+
public record BuildVersion
24
{
3-
public record BuildVersion
4-
{
5-
public string FullVersion { get; init; }
6-
public string FileVersion { get; init; }
7-
public string AssemblyVersion { get; init; }
8-
}
5+
public string FullVersion { get; init; }
6+
public string FileVersion { get; init; }
7+
public string AssemblyVersion { get; init; }
98
}
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,74 @@
1-
namespace TurnerSoftware.BuildVersioning.Tool
1+
namespace TurnerSoftware.BuildVersioning.Tool;
2+
3+
internal class BuildVersioner(IVersionDetailsProvider versionDetailsProvider)
24
{
3-
internal class BuildVersioner
5+
public BuildVersion GetBuildVersion(BuildVersioningOptions options)
46
{
5-
private IVersionDetailsProvider VersionDetailsProvider { get; }
6-
7-
public BuildVersioner(IVersionDetailsProvider versionDetailsProvider)
7+
var versionDetails = versionDetailsProvider.GetVersionDetails();
8+
if (versionDetails is null)
89
{
9-
VersionDetailsProvider = versionDetailsProvider;
10+
return null;
1011
}
1112

12-
public BuildVersion GetBuildVersion(BuildVersioningOptions options)
13+
if (!versionDetails.IsTaggedRelease && versionDetails.PreRelease is null && options.PreReleaseFormat?.Length > 0)
1314
{
14-
var versionDetails = VersionDetailsProvider.GetVersionDetails();
15-
if (versionDetails is null)
16-
{
17-
return null;
18-
}
19-
20-
if (!versionDetails.IsTaggedRelease && versionDetails.PreRelease is null && options.PreReleaseFormat?.Length > 0)
21-
{
22-
versionDetails = versionDetails with
23-
{
24-
PreRelease = options.PreReleaseFormat
25-
.Replace("{CommitHeight}", versionDetails.CommitHeight.ToString())
26-
};
27-
}
28-
29-
if (options.BuildMetadataFormat?.Length > 0)
15+
versionDetails = versionDetails with
3016
{
31-
versionDetails = versionDetails with
32-
{
33-
BuildMetadata = options.BuildMetadataFormat
34-
.Replace("{CommitHash}", versionDetails.CommitHash)
35-
.Replace("{CommitHeight}", versionDetails.CommitHeight.ToString())
36-
};
37-
}
38-
39-
var fullVersion = FormatFullVersion(options.FullVersionFormat, versionDetails);
40-
var fileVersion = FormatVersion(options.FileVersionFormat, versionDetails);
41-
var assemblyVersion = FormatVersion(options.AssemblyVersionFormat, versionDetails);
17+
PreRelease = options.PreReleaseFormat
18+
.Replace("{CommitHeight}", versionDetails.CommitHeight.ToString())
19+
};
20+
}
4221

43-
return new BuildVersion
22+
if (options.BuildMetadataFormat?.Length > 0)
23+
{
24+
versionDetails = versionDetails with
4425
{
45-
FullVersion = fullVersion,
46-
FileVersion = fileVersion,
47-
AssemblyVersion = assemblyVersion
26+
BuildMetadata = options.BuildMetadataFormat
27+
.Replace("{CommitHash}", versionDetails.CommitHash)
28+
.Replace("{CommitHeight}", versionDetails.CommitHeight.ToString())
4829
};
4930
}
5031

51-
private static string FormatFullVersion(string format, VersionDetails versionDetails)
32+
var fullVersion = FormatFullVersion(options.FullVersionFormat, versionDetails);
33+
var fileVersion = FormatVersion(options.FileVersionFormat, versionDetails);
34+
var assemblyVersion = FormatVersion(options.AssemblyVersionFormat, versionDetails);
35+
36+
return new BuildVersion
5237
{
53-
if (string.IsNullOrEmpty(format))
54-
{
55-
return format;
56-
}
38+
FullVersion = fullVersion,
39+
FileVersion = fileVersion,
40+
AssemblyVersion = assemblyVersion
41+
};
42+
}
5743

58-
return FormatVersion(format, versionDetails)
59-
.Replace("{PreRelease}", versionDetails.PreRelease is null ? default : $"-{versionDetails.PreRelease}")
60-
.Replace("{BuildMetadata}", versionDetails.BuildMetadata is null ? default : $"+{versionDetails.BuildMetadata}");
44+
private static string FormatFullVersion(string format, VersionDetails versionDetails)
45+
{
46+
if (string.IsNullOrEmpty(format))
47+
{
48+
return format;
6149
}
6250

63-
private static string FormatVersion(string format, VersionDetails versionDetails)
64-
{
65-
if (string.IsNullOrEmpty(format))
66-
{
67-
return format;
68-
}
51+
return FormatVersion(format, versionDetails)
52+
.Replace("{PreRelease}", versionDetails.PreRelease is null ? default : $"-{versionDetails.PreRelease}")
53+
.Replace("{BuildMetadata}", versionDetails.BuildMetadata is null ? default : $"+{versionDetails.BuildMetadata}");
54+
}
6955

70-
var autoIncrement = versionDetails.IsTaggedRelease ? 0 : 1;
71-
return format
72-
.Replace("{Major}", versionDetails.MajorVersion.ToString())
73-
.Replace("{Major++}", (versionDetails.MajorVersion + autoIncrement).ToString())
74-
.Replace("{Minor}", versionDetails.MinorVersion.ToString())
75-
.Replace("{Minor++}", (versionDetails.MinorVersion + autoIncrement).ToString())
76-
.Replace("{Patch}", versionDetails.PatchVersion.ToString())
77-
.Replace("{Patch++}", (versionDetails.PatchVersion + autoIncrement).ToString())
78-
.Replace("{CommitHeight}", versionDetails.CommitHeight.ToString())
79-
.Replace("{CommitHash}", versionDetails.CommitHash ?? "NOCANDO");
56+
private static string FormatVersion(string format, VersionDetails versionDetails)
57+
{
58+
if (string.IsNullOrEmpty(format))
59+
{
60+
return format;
8061
}
62+
63+
var autoIncrement = versionDetails.IsTaggedRelease ? 0 : 1;
64+
return format
65+
.Replace("{Major}", versionDetails.MajorVersion.ToString())
66+
.Replace("{Major++}", (versionDetails.MajorVersion + autoIncrement).ToString())
67+
.Replace("{Minor}", versionDetails.MinorVersion.ToString())
68+
.Replace("{Minor++}", (versionDetails.MinorVersion + autoIncrement).ToString())
69+
.Replace("{Patch}", versionDetails.PatchVersion.ToString())
70+
.Replace("{Patch++}", (versionDetails.PatchVersion + autoIncrement).ToString())
71+
.Replace("{CommitHeight}", versionDetails.CommitHeight.ToString())
72+
.Replace("{CommitHash}", versionDetails.CommitHash ?? "NOCANDO");
8173
}
8274
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
namespace TurnerSoftware.BuildVersioning.Tool
1+
namespace TurnerSoftware.BuildVersioning.Tool;
2+
3+
public record BuildVersioningOptions
24
{
3-
public record BuildVersioningOptions
4-
{
5-
public string FullVersionFormat { get; init; }
6-
public string FileVersionFormat { get; init; }
7-
public string AssemblyVersionFormat { get; init; }
8-
public string PreReleaseFormat { get; init; }
9-
public string BuildMetadataFormat { get; init; }
10-
}
5+
public string FullVersionFormat { get; init; }
6+
public string FileVersionFormat { get; init; }
7+
public string AssemblyVersionFormat { get; init; }
8+
public string PreReleaseFormat { get; init; }
9+
public string BuildMetadataFormat { get; init; }
1110
}

src/TurnerSoftware.BuildVersioning.Tool/GitCommandRunner.cs

+33-36
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,43 @@
22
using System.Diagnostics;
33
using System.Threading.Tasks;
44

5-
namespace TurnerSoftware.BuildVersioning.Tool
5+
namespace TurnerSoftware.BuildVersioning.Tool;
6+
7+
internal class GitCommandRunner : IGitCommandRunner
68
{
7-
internal class GitCommandRunner : IGitCommandRunner
9+
private static string RunCommand(string command)
810
{
9-
private string RunCommand(string command)
11+
using var process = new Process();
12+
process.StartInfo = new ProcessStartInfo("git", command)
13+
{
14+
RedirectStandardOutput = true
15+
};
16+
17+
var waitOnExit = new TaskCompletionSource<object>();
18+
process.Exited += (s, e) => waitOnExit.SetResult(default);
19+
process.EnableRaisingEvents = true;
20+
21+
try
22+
{
23+
process.Start();
24+
}
25+
catch (Exception ex)
1026
{
11-
using (var process = new Process())
12-
{
13-
process.StartInfo = new ProcessStartInfo("git", command)
14-
{
15-
RedirectStandardOutput = true
16-
};
17-
18-
var waitOnExit = new TaskCompletionSource<object>();
19-
process.Exited += (s, e) => waitOnExit.SetResult(default);
20-
process.EnableRaisingEvents = true;
21-
22-
try
23-
{
24-
process.Start();
25-
}
26-
catch (Exception ex)
27-
{
28-
Console.Error.WriteLine(ex.Message);
29-
return null;
30-
}
31-
32-
var standardOutputTask = process.StandardOutput.ReadToEndAsync();
33-
34-
Task.WaitAll(waitOnExit.Task, standardOutputTask);
35-
36-
if (process.ExitCode != 0)
37-
{
38-
return null;
39-
}
40-
41-
return standardOutputTask.Result;
42-
}
27+
Console.Error.WriteLine(ex.Message);
28+
return null;
4329
}
4430

45-
public string GitDescribe() => RunCommand("describe --tags --abbrev=7 --always --long");
31+
var standardOutputTask = process.StandardOutput.ReadToEndAsync();
32+
33+
Task.WaitAll(waitOnExit.Task, standardOutputTask);
34+
35+
if (process.ExitCode != 0)
36+
{
37+
return null;
38+
}
39+
40+
return standardOutputTask.Result;
4641
}
42+
43+
public string GitDescribe() => RunCommand("describe --tags --abbrev=7 --always --long");
4744
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
namespace TurnerSoftware.BuildVersioning.Tool
1+
namespace TurnerSoftware.BuildVersioning.Tool;
2+
3+
public interface IGitCommandRunner
24
{
3-
public interface IGitCommandRunner
4-
{
5-
/// <summary>
6-
/// Returns a result from `git describe` containing the tag name, number of commits from the tag (commit height) and a 7-character commit hash.
7-
/// </summary>
8-
/// <remarks>
9-
/// Format with tag: {tag}-{commitHeight}-{commitHash}<br />
10-
/// Format without tag: {commitHash}
11-
/// </remarks>
12-
string GitDescribe();
13-
}
5+
/// <summary>
6+
/// Returns a result from `git describe` containing the tag name, number of commits from the tag (commit height) and a 7-character commit hash.
7+
/// </summary>
8+
/// <remarks>
9+
/// Format with tag: {tag}-{commitHeight}-{commitHash}<br />
10+
/// Format without tag: {commitHash}
11+
/// </remarks>
12+
string GitDescribe();
1413
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
namespace TurnerSoftware.BuildVersioning.Tool
1+
namespace TurnerSoftware.BuildVersioning.Tool;
2+
3+
public interface IVersionDetailsProvider
24
{
3-
public interface IVersionDetailsProvider
4-
{
5-
VersionDetails GetVersionDetails();
6-
}
5+
VersionDetails GetVersionDetails();
76
}

0 commit comments

Comments
 (0)