Skip to content

Commit 35e272c

Browse files
committed
(build) Added ArgumentNullException checks for context.Version in various files.
Improved error messaging and updated some syntax for consistency and clarity
1 parent 297004b commit 35e272c

File tree

16 files changed

+63
-36
lines changed

16 files changed

+63
-36
lines changed

build/artifacts/Tasks/ArtifactsExecutableTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ private static void PackageTest(BuildContextBase context, string packageToTest)
3636

3737
context.NuGetInstall(packageToTest, new NuGetInstallSettings
3838
{
39-
Source = new[]
40-
{
39+
Source =
40+
[
4141
context.MakeAbsolute(Paths.Nuget).FullPath
42-
},
42+
],
4343
ExcludeVersion = true,
4444
Prerelease = true,
4545
OutputDirectory = outputDirectory

build/artifacts/Tasks/ArtifactsMsBuildFullTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override void Run(BuildContext context)
3636
Verbosity = DotNetVerbosity.Minimal,
3737
Configuration = context.MsBuildConfiguration,
3838
MSBuildSettings = dotnetMsBuildSettings,
39-
Sources = new[] { nugetSource }
39+
Sources = [nugetSource]
4040
});
4141

4242
var exe = Paths.Integration.Combine("build").Combine(framework).CombineWithFilePath("app.dll");

build/build/BuildLifetime.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public override void Setup(BuildContext context, ISetupContext info)
2929
private static void SetMsBuildSettingsVersion(BuildContext context)
3030
{
3131
var msBuildSettings = context.MsBuildSettings;
32-
var version = context.Version!;
32+
ArgumentNullException.ThrowIfNull(context.Version);
33+
var version = context.Version;
3334

3435
msBuildSettings.SetVersion(version.SemVersion);
3536
msBuildSettings.SetAssemblyVersion(version.Version);

build/build/Tasks/Build.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public override void Run(BuildContext context)
1616
context.DotNetRestore(sln, new DotNetRestoreSettings
1717
{
1818
Verbosity = DotNetVerbosity.Minimal,
19-
Sources = new[] { Constants.NugetOrgUrl },
19+
Sources = [Constants.NugetOrgUrl],
2020
MSBuildSettings = context.MsBuildSettings
2121
});
2222

build/build/Tasks/BuildPrepare.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public override void Run(BuildContext context)
1616
new()
1717
{
1818
Verbosity = DotNetVerbosity.Minimal,
19-
Sources = new[] { Constants.NugetOrgUrl },
19+
Sources = [Constants.NugetOrgUrl],
2020
});
2121

2222
context.DotNetBuild("./src/GitVersion.App/GitVersion.App.csproj",

build/build/Tasks/Package/PackageChocolatey.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public override void Run(BuildContext context)
3535
.Select(file => new ChocolateyNuSpecContent { Source = file.FullPath, Target = file.FullPath.Replace(artifactPath, "") })
3636
.ToArray();
3737

38-
metaPackageSettings.Dependencies = new[]
39-
{
38+
metaPackageSettings.Dependencies =
39+
[
4040
new ChocolateyNuSpecDependency { Id = "GitVersion.Portable", Version = context.Version?.ChocolateyVersion }
41-
};
41+
];
4242

4343
context.ChocolateyPack(metaPackageSettings);
4444
}
@@ -51,17 +51,18 @@ private static ChocolateyPackSettings GetChocolateyPackSettings(BuildContextBase
5151
Version = context.Version?.ChocolateyVersion,
5252
Title = "GitVersion",
5353
Description = "Derives SemVer information from a repository following GitFlow or GitHubFlow.",
54-
Authors = new[] { "GitTools and Contributors" },
55-
Owners = new[] { "GitTools and Contributors" },
54+
Authors = ["GitTools and Contributors"],
55+
Owners = ["GitTools and Contributors"],
5656
Copyright = $"Copyright GitTools {DateTime.Now.Year}",
5757
DocsUrl = new Uri("https://gitversion.net/docs/"),
5858
LicenseUrl = new Uri("https://opensource.org/license/mit/"),
5959
ProjectUrl = new Uri("https://github.com/GitTools/GitVersion"),
6060
ProjectSourceUrl = new Uri("https://github.com/GitTools/GitVersion"),
6161
IconUrl = new Uri("https://raw.githubusercontent.com/GitTools/graphics/master/GitVersion/Color/icon_100x100.png"),
6262
RequireLicenseAcceptance = false,
63-
Tags = new[] { "Git", "Versioning", "GitVersion", "GitFlowVersion", "GitFlow", "GitHubFlow", "SemVer" },
64-
ReleaseNotes = new[] { $"https://github.com/GitTools/GitVersion/releases/tag/{context.Version?.ChocolateyVersion}" },
63+
Tags = ["Git", "Versioning", "GitVersion", "GitFlowVersion", "GitFlow", "GitHubFlow", "SemVer"],
64+
ReleaseNotes = [$"https://github.com/GitTools/GitVersion/releases/tag/{context.Version?.ChocolateyVersion}"
65+
],
6566
OutputDirectory = Paths.Nuget,
6667
LimitOutput = true,
6768
};

build/build/Tasks/Test/UnitTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ private static void TestProjectForTarget(BuildContext context, FilePath project,
7373
};
7474

7575
var resultsPath = context.MakeAbsolute(testResultsPath.CombineWithFilePath($"{projectName}.results.xml"));
76-
settings.Loggers = new[] { $"junit;LogFilePath={resultsPath}" };
76+
settings.Loggers = [$"junit;LogFilePath={resultsPath}"];
7777

7878
var coverletSettings = new CoverletSettings
7979
{

build/build/Tasks/ValidateVersion.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public class ValidateVersion : FrostingTask<BuildContext>
99
{
1010
public override void Run(BuildContext context)
1111
{
12+
ArgumentNullException.ThrowIfNull(context.Version);
1213
var gitVersionTool = context.GetGitVersionToolLocation();
13-
context.ValidateOutput("dotnet", $"\"{gitVersionTool}\" -version", context.Version!.GitVersion!.InformationalVersion!);
14+
context.ValidateOutput("dotnet", $"\"{gitVersionTool}\" -version", context.Version.GitVersion.InformationalVersion);
1415
}
1516
}

build/common/Addins/GitVersion/GitVersionRunner.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Common.Addins.GitVersion;
66
/// <summary>
77
/// The GitVersion runner.
88
/// </summary>
9-
public sealed class GitVersionRunner : Tool<GitVersionSettings>
9+
public sealed partial class GitVersionRunner : Tool<GitVersionSettings>
1010
{
1111
private readonly ICakeLog _log;
1212

@@ -38,14 +38,13 @@ public GitVersion Run(GitVersionSettings settings)
3838
Run(settings, GetArguments(settings), new ProcessSettings { RedirectStandardOutput = true }, process =>
3939
{
4040
output = string.Join("\n", process.GetStandardOutput());
41-
if (this._log.Verbosity < Verbosity.Diagnostic)
41+
if (this._log.Verbosity >= Verbosity.Diagnostic) return;
42+
var regex = ParseErrorRegex();
43+
var errors = regex.Matches(output)
44+
.SelectMany(match => new[] { match.Groups[1].Value, match.Groups[2].Value });
45+
foreach (var error in errors)
4246
{
43-
var errors = Regex.Matches(output, @"( *ERROR:? [^\n]*)\n([^\n]*)")
44-
.SelectMany(match => new[] { match.Groups[1].Value, match.Groups[2].Value });
45-
foreach (var error in errors)
46-
{
47-
this._log.Error(error);
48-
}
47+
this._log.Error(error);
4948
}
5049
});
5150

@@ -68,6 +67,7 @@ private ProcessArgumentBuilder GetArguments(GitVersionSettings settings)
6867
builder.Append("-output");
6968
builder.Append("json");
7069
}
70+
7171
if (settings.OutputTypes.Contains(GitVersionOutput.BuildServer))
7272
{
7373
builder.Append("-output");
@@ -116,7 +116,8 @@ private ProcessArgumentBuilder GetArguments(GitVersionSettings settings)
116116
}
117117
else
118118
{
119-
this._log.Warning("If you leave the branch name for GitVersion unset, it will fallback to the default branch for the repository.");
119+
this._log.Warning(
120+
"If you leave the branch name for GitVersion unset, it will fallback to the default branch for the repository.");
120121
}
121122

122123
if (!string.IsNullOrWhiteSpace(settings.Commit))
@@ -150,6 +151,7 @@ private ProcessArgumentBuilder GetArguments(GitVersionSettings settings)
150151
builder.Append("-verbosity");
151152
builder.Append(verbosity.ToString());
152153
}
154+
153155
return builder;
154156
}
155157

@@ -163,5 +165,14 @@ private ProcessArgumentBuilder GetArguments(GitVersionSettings settings)
163165
/// Gets the possible names of the tool executable.
164166
/// </summary>
165167
/// <returns>The tool executable name.</returns>
166-
protected override IEnumerable<string> GetToolExecutableNames() => new[] { "GitVersion.exe", "dotnet-gitversion", "dotnet-gitversion.exe", "gitversion" };
168+
protected override IEnumerable<string> GetToolExecutableNames() =>
169+
[
170+
"GitVersion.exe",
171+
"dotnet-gitversion",
172+
"dotnet-gitversion.exe",
173+
"gitversion"
174+
];
175+
176+
[GeneratedRegex(@"( *ERROR:? [^\n]*)\n([^\n]*)")]
177+
private static partial Regex ParseErrorRegex();
167178
}

build/common/Lifetime/BuildLifetimeBase.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,18 @@ public override void Setup(T context, ISetupContext info)
3030
context.Information("Running BuildPrepare...");
3131
return;
3232
}
33+
34+
var gitVersionPath = context.GetGitVersionDotnetToolLocation();
35+
if (gitVersionPath is null || context.FileExists(gitVersionPath) is false)
36+
{
37+
throw new FileNotFoundException("Failed to locate the Release build of gitversion.dll in ./tools/gitversion. Try running \"./build.ps1 -Stage build -Target BuildPrepare\"");
38+
}
39+
3340
var gitVersionSettings = new GitVersionSettings
3441
{
3542
OutputTypes = [GitVersionOutput.Json, GitVersionOutput.BuildServer],
3643
ToolPath = context.Tools.Resolve(["dotnet.exe", "dotnet"]),
37-
ArgumentCustomization = args => args.Prepend(context.GetGitVersionDotnetToolLocation()?.FullPath ?? throw new FileNotFoundException("Failed to locate the Release build of gitversion.dll in ./tools/gitversion. Try running \"./build.ps1 -Stage build -Target BuildPrepare\""))
44+
ArgumentCustomization = args => args.Prepend(gitVersionPath.FullPath)
3845
};
3946

4047
var gitVersion = context.GitVersion(gitVersionSettings);

0 commit comments

Comments
 (0)