Skip to content

Commit 297004b

Browse files
committed
(build) Refactor task arguments for readability and maintainability
This commit improves the arguments presented by each task in our system, switching from directly using TaskArgument with constant data to an attribute approach. This change will make our code easier to read and maintain, whilst preserving the functionality of the tasks. The attribute classes provide a more explicit specification of the argument options, leading to a better understanding of the system requirements.
1 parent c82b524 commit 297004b

16 files changed

+69
-64
lines changed

build/artifacts/Tasks/ArtifactsDotnetToolTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Artifacts.Tasks;
44

55
[TaskName(nameof(ArtifactsDotnetToolTest))]
66
[TaskDescription("Tests the dotnet global tool in docker container")]
7-
[TaskArgument(Arguments.DockerRegistry, Constants.DockerHub, Constants.GitHub)]
8-
[TaskArgument(Arguments.DockerDotnetVersion, Constants.VersionCurrent, Constants.VersionLatest)]
9-
[TaskArgument(Arguments.DockerDistro, Constants.AlpineLatest, Constants.DebianLatest, Constants.UbuntuLatest)]
7+
[DockerRegistryArgument]
8+
[DockerDotnetArgument]
9+
[DockerDistroArgument]
1010
[IsDependentOn(typeof(ArtifactsPrepare))]
1111
public class ArtifactsDotnetToolTest : FrostingTask<BuildContext>
1212
{

build/artifacts/Tasks/ArtifactsMsBuildCoreTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Artifacts.Tasks;
44

55
[TaskName(nameof(ArtifactsMsBuildCoreTest))]
66
[TaskDescription("Tests the msbuild package in docker container")]
7-
[TaskArgument(Arguments.DockerRegistry, Constants.DockerHub, Constants.GitHub)]
8-
[TaskArgument(Arguments.DockerDotnetVersion, Constants.VersionCurrent, Constants.VersionLatest)]
9-
[TaskArgument(Arguments.DockerDistro, Constants.AlpineLatest, Constants.DebianLatest, Constants.UbuntuLatest)]
7+
[DockerRegistryArgument]
8+
[DockerDotnetArgument]
9+
[DockerDistroArgument]
1010
[IsDependentOn(typeof(ArtifactsPrepare))]
1111
public class ArtifactsMsBuildCoreTest : FrostingTask<BuildContext>
1212
{

build/artifacts/Tasks/ArtifactsNativeTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Artifacts.Tasks;
44

55
[TaskName(nameof(ArtifactsNativeTest))]
66
[TaskDescription("Tests the native executables in docker container")]
7-
[TaskArgument(Arguments.DockerRegistry, Constants.DockerHub, Constants.GitHub)]
8-
[TaskArgument(Arguments.DockerDotnetVersion, Constants.VersionCurrent, Constants.VersionLatest)]
9-
[TaskArgument(Arguments.DockerDistro, Constants.AlpineLatest, Constants.DebianLatest, Constants.UbuntuLatest)]
7+
[DockerRegistryArgument]
8+
[DockerDotnetArgument]
9+
[DockerDistroArgument]
1010
[IsDependentOn(typeof(ArtifactsPrepare))]
1111
public class ArtifactsNativeTest : FrostingTask<BuildContext>
1212
{

build/artifacts/Tasks/ArtifactsPrepare.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Artifacts.Tasks;
44

55
[TaskName(nameof(ArtifactsPrepare))]
66
[TaskDescription("Pulls the docker images needed for testing the artifacts")]
7-
[TaskArgument(Arguments.DockerRegistry, Constants.DockerHub, Constants.GitHub)]
8-
[TaskArgument(Arguments.DockerDotnetVersion, Constants.VersionCurrent, Constants.VersionLatest)]
9-
[TaskArgument(Arguments.DockerDistro, Constants.AlpineLatest, Constants.DebianLatest, Constants.UbuntuLatest)]
7+
[DockerRegistryArgument]
8+
[DockerDotnetArgument]
9+
[DockerDistroArgument]
1010
public class ArtifactsPrepare : FrostingTask<BuildContext>
1111
{
1212
public override bool ShouldRun(BuildContext context)

build/artifacts/Tasks/ArtifactsTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ namespace Artifacts.Tasks;
44

55
[TaskName(nameof(ArtifactsTest))]
66
[TaskDescription("Tests packages in docker container")]
7-
[TaskArgument(Arguments.DockerRegistry, Constants.DockerHub, Constants.GitHub)]
8-
[TaskArgument(Arguments.DockerDotnetVersion, Constants.VersionCurrent, Constants.VersionLatest)]
9-
[TaskArgument(Arguments.DockerDistro, Constants.AlpineLatest, Constants.DebianLatest, Constants.UbuntuLatest)]
7+
[DockerRegistryArgument]
8+
[DockerDotnetArgument]
9+
[DockerDistroArgument]
1010
[IsDependentOn(typeof(ArtifactsNativeTest))]
1111
[IsDependentOn(typeof(ArtifactsDotnetToolTest))]
1212
[IsDependentOn(typeof(ArtifactsMsBuildCoreTest))]

build/build/Tasks/Test/UnitTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Build.Tasks;
88

99
[TaskName(nameof(UnitTest))]
1010
[TaskDescription("Run the unit tests")]
11-
[TaskArgument(Arguments.DotnetTarget, Constants.VersionCurrent, Constants.VersionLatest)]
11+
[DotnetArgument]
1212
[IsDependentOn(typeof(Build))]
1313
public class UnitTest : FrostingTask<BuildContext>
1414
{

build/common/Addins/GitVersion/GitVersionAliases.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ public static class GitVersionAliases
5454
[CakeMethodAlias]
5555
public static GitVersion GitVersion(this ICakeContext context)
5656
{
57-
if (context == null)
58-
{
59-
throw new ArgumentNullException(nameof(context));
60-
}
57+
ArgumentNullException.ThrowIfNull(context);
6158

6259
return GitVersion(context, new GitVersionSettings());
6360
}
@@ -104,10 +101,7 @@ public static GitVersion GitVersion(this ICakeContext context)
104101
[CakeMethodAlias]
105102
public static GitVersion GitVersion(this ICakeContext context, GitVersionSettings settings)
106103
{
107-
if (context == null)
108-
{
109-
throw new ArgumentNullException(nameof(context));
110-
}
104+
ArgumentNullException.ThrowIfNull(context);
111105

112106
var gitVersionRunner = new GitVersionRunner(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools, context.Log);
113107
return gitVersionRunner.Run(settings);

build/common/Addins/GitVersion/GitVersionRunner.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Common.Addins.GitVersion;
88
/// </summary>
99
public sealed class GitVersionRunner : Tool<GitVersionSettings>
1010
{
11-
private readonly ICakeLog log;
11+
private readonly ICakeLog _log;
1212

1313
/// <summary>
1414
/// Initializes a new instance of the <see cref="GitVersionRunner"/> class.
@@ -23,7 +23,7 @@ public GitVersionRunner(
2323
ICakeEnvironment environment,
2424
IProcessRunner processRunner,
2525
IToolLocator tools,
26-
ICakeLog log) : base(fileSystem, environment, processRunner, tools) => this.log = log;
26+
ICakeLog log) : base(fileSystem, environment, processRunner, tools) => this._log = log;
2727

2828
/// <summary>
2929
/// Runs GitVersion and processes the results.
@@ -32,31 +32,28 @@ public GitVersionRunner(
3232
/// <returns>A task with the GitVersion results.</returns>
3333
public GitVersion Run(GitVersionSettings settings)
3434
{
35-
if (settings == null)
36-
{
37-
throw new ArgumentNullException(nameof(settings));
38-
}
35+
ArgumentNullException.ThrowIfNull(settings);
3936

4037
var output = string.Empty;
4138
Run(settings, GetArguments(settings), new ProcessSettings { RedirectStandardOutput = true }, process =>
4239
{
4340
output = string.Join("\n", process.GetStandardOutput());
44-
if (log.Verbosity < Verbosity.Diagnostic)
41+
if (this._log.Verbosity < Verbosity.Diagnostic)
4542
{
4643
var errors = Regex.Matches(output, @"( *ERROR:? [^\n]*)\n([^\n]*)")
4744
.SelectMany(match => new[] { match.Groups[1].Value, match.Groups[2].Value });
4845
foreach (var error in errors)
4946
{
50-
log.Error(error);
47+
this._log.Error(error);
5148
}
5249
}
5350
});
5451

5552
if (!settings.OutputTypes.Contains(GitVersionOutput.Json))
5653
return new GitVersion();
5754

58-
var jsonStartIndex = output.IndexOf("{", StringComparison.Ordinal);
59-
var jsonEndIndex = output.IndexOf("}", StringComparison.Ordinal);
55+
var jsonStartIndex = output.IndexOf('{');
56+
var jsonEndIndex = output.IndexOf('}');
6057
var json = output.Substring(jsonStartIndex, jsonEndIndex - jsonStartIndex + 1);
6158

6259
return JsonConvert.DeserializeObject<GitVersion>(json) ?? new GitVersion();
@@ -119,7 +116,7 @@ private ProcessArgumentBuilder GetArguments(GitVersionSettings settings)
119116
}
120117
else
121118
{
122-
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("If you leave the branch name for GitVersion unset, it will fallback to the default branch for the repository.");
123120
}
124121

125122
if (!string.IsNullOrWhiteSpace(settings.Commit))
@@ -146,7 +143,7 @@ private ProcessArgumentBuilder GetArguments(GitVersionSettings settings)
146143
builder.Append("-nofetch");
147144
}
148145

149-
var verbosity = settings.Verbosity ?? log.Verbosity;
146+
var verbosity = settings.Verbosity ?? this._log.Verbosity;
150147

151148
if (verbosity != Verbosity.Normal)
152149
{

build/common/Utilities/Constants.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@ public static class Constants
2121
public const string GitHub = "github";
2222
public const string DockerHubRegistry = "docker.io";
2323
public const string GitHubContainerRegistry = "ghcr.io";
24+
public static readonly string[] DockerRegistries = [DockerHub, GitHub];
2425

2526
public const string Arm64 = "arm64";
2627
public const string Amd64 = "amd64";
2728

29+
public static readonly string[] Architectures = [Amd64, Arm64];
30+
2831
public const string AlpineLatest = "alpine.3.20";
2932
public const string CentosStreamLatest = "centos.stream.9";
3033
public const string DebianLatest = "debian.12";
3134
public const string FedoraLatest = "fedora.40";
3235
public const string Ubuntu2004 = "ubuntu.20.04";
3336
public const string Ubuntu2204 = "ubuntu.22.04";
3437
public const string Ubuntu2404 = "ubuntu.24.04";
35-
public const string UbuntuLatest = Ubuntu2404;
3638

3739
public const string DockerDistroLatest = DebianLatest;
3840

build/common/Utilities/Extensions.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,23 @@ where baseType.IsAssignableFrom(type) && info.IsClass && !info.IsAbstract
1313

1414
public static string GetTaskDescription(this Type task)
1515
{
16-
if (task is null)
17-
{
18-
throw new ArgumentNullException(nameof(task));
19-
}
16+
ArgumentNullException.ThrowIfNull(task);
2017

2118
var attribute = task.GetCustomAttribute<TaskDescriptionAttribute>();
2219
return attribute != null ? attribute.Description : string.Empty;
2320
}
2421

2522
public static string GetTaskName(this Type task)
2623
{
27-
if (task is null)
28-
{
29-
throw new ArgumentNullException(nameof(task));
30-
}
24+
ArgumentNullException.ThrowIfNull(task);
3125

3226
var attribute = task.GetCustomAttribute<TaskNameAttribute>();
3327
return attribute != null ? attribute.Name : task.Name;
3428
}
3529

3630
public static string GetTaskArguments(this Type task)
3731
{
38-
if (task is null)
39-
{
40-
throw new ArgumentNullException(nameof(task));
41-
}
32+
ArgumentNullException.ThrowIfNull(task);
4233

4334
var attributes = task.GetCustomAttributes<TaskArgumentAttribute>().ToArray();
4435
if (attributes.Length != 0)

0 commit comments

Comments
 (0)