Skip to content

Commit 5d8356f

Browse files
authored
Merge pull request #3878 from arturcic/main
Use C# 12 features in the codebase
2 parents 628b8ef + 1b328dd commit 5d8356f

File tree

162 files changed

+668
-1167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+668
-1167
lines changed

src/GitVersion.App.Tests/Helpers/ExecutionResults.cs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,11 @@
44

55
namespace GitVersion.App.Tests;
66

7-
public class ExecutionResults
7+
public class ExecutionResults(int exitCode, string? output, string? logContents = null)
88
{
9-
public ExecutionResults(int exitCode, string? output, string? logContents = null)
10-
{
11-
ExitCode = exitCode;
12-
Output = output;
13-
Log = logContents;
14-
}
15-
16-
public int ExitCode { get; init; }
17-
public string? Output { get; init; }
18-
public string? Log { get; init; }
9+
public int ExitCode { get; init; } = exitCode;
10+
public string? Output { get; init; } = output;
11+
public string? Log { get; init; } = logContents;
1912

2013
public GitVersionVariables? OutputVariables
2114
{

src/GitVersion.App.Tests/Helpers/GitVersionHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private static ExecutionResults ExecuteIn(ArgumentBuilder arguments,
7171
executable,
7272
args,
7373
arguments.WorkingDirectory,
74-
environmentalVariables.ToArray());
74+
[.. environmentalVariables]);
7575
}
7676
catch (Exception exception)
7777
{

src/GitVersion.App.Tests/Helpers/ProgramFixture.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ public ProgramFixture(string workingDirectory = "")
2222
ILog log = new Log(logAppender);
2323

2424
var consoleBuilder = new StringBuilder();
25-
IConsole consoleAdapter = new TestConsoleAdapter(consoleBuilder);
25+
var consoleAdapter = new TestConsoleAdapter(consoleBuilder);
2626

2727
this.environment = new TestEnvironment();
2828

2929
WithOverrides(services =>
3030
{
3131
services.AddSingleton(log);
32-
services.AddSingleton(consoleAdapter);
32+
services.AddSingleton<IConsole>(consoleAdapter);
3333
services.AddSingleton(this.environment);
3434
});
3535

36-
this.logger = new Lazy<string>(() => logBuilder.ToString());
37-
this.output = new Lazy<string?>(() => consoleAdapter.ToString());
36+
this.logger = new(() => logBuilder.ToString());
37+
this.output = new(() => consoleAdapter.ToString());
3838
}
3939

4040
public void WithEnv(params KeyValuePair<string, string>[] envs)
@@ -60,7 +60,7 @@ public async Task<ExecutionResults> Run(params string[] args)
6060

6161
if (!this.workingDirectory.IsNullOrWhiteSpace())
6262
{
63-
args = new[] { "-targetpath", this.workingDirectory }.Concat(args).ToArray();
63+
args = ["-targetpath", this.workingDirectory, .. args];
6464
}
6565
await program.RunAsync(args);
6666

src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ private static async Task VerifyPullRequestVersionIsCalculatedProperly(string pu
163163
remoteRepository.Refs.Add(pullRequestRef, new ObjectId(mergeCommitSha));
164164

165165
// Checkout PR commit
166-
Commands.Fetch(fixture.Repository, "origin", Array.Empty<string>(), new FetchOptions(), null);
166+
Commands.Fetch(fixture.Repository, "origin", [], new FetchOptions(), null);
167167
Commands.Checkout(fixture.Repository, mergeCommitSha);
168168
}
169169

@@ -173,7 +173,7 @@ private static async Task VerifyPullRequestVersionIsCalculatedProperly(string pu
173173
services.AddModule(new GitVersionBuildAgentsModule());
174174
services.AddModule(new GitVersionOutputModule());
175175
});
176-
programFixture.WithEnv(env.ToArray());
176+
programFixture.WithEnv([.. env]);
177177

178178
var result = await programFixture.Run();
179179

src/GitVersion.App.Tests/TagCheckoutInBuildAgentTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ private static async Task VerifyTagCheckoutVersionIsCalculatedProperly(Dictionar
5252
remoteRepository.MergeNoFF("release/0.2.0", Generate.SignatureNow());
5353
remoteRepository.MakeATaggedCommit("0.2.0");
5454

55-
Commands.Fetch(fixture.Repository, "origin", Array.Empty<string>(), new FetchOptions(), null);
55+
Commands.Fetch(fixture.Repository, "origin", [], new FetchOptions(), null);
5656
Commands.Checkout(fixture.Repository, "0.2.0");
5757
}
5858

5959
var programFixture = new ProgramFixture(fixture.RepositoryPath);
60-
programFixture.WithEnv(env.ToArray());
60+
programFixture.WithEnv([.. env]);
6161

6262
var result = await programFixture.Run();
6363

src/GitVersion.App/ArgumentParser.cs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,17 @@
66

77
namespace GitVersion;
88

9-
internal class ArgumentParser : IArgumentParser
9+
internal class ArgumentParser(IEnvironment environment, ICurrentBuildAgent buildAgent, IConsole console, IGlobbingResolver globbingResolver)
10+
: IArgumentParser
1011
{
11-
private readonly IEnvironment environment;
12-
private readonly ICurrentBuildAgent buildAgent;
13-
private readonly IConsole console;
14-
private readonly IGlobbingResolver globbingResolver;
12+
private readonly IEnvironment environment = environment.NotNull();
13+
private readonly ICurrentBuildAgent buildAgent = buildAgent.NotNull();
14+
private readonly IConsole console = console.NotNull();
15+
private readonly IGlobbingResolver globbingResolver = globbingResolver.NotNull();
16+
1517
private const string defaultOutputFileName = "GitVersion.json";
1618
private static readonly IEnumerable<string> availableVariables = GitVersionVariables.AvailableVariables;
1719

18-
public ArgumentParser(IEnvironment environment, ICurrentBuildAgent buildAgent, IConsole console, IGlobbingResolver globbingResolver)
19-
{
20-
this.environment = environment.NotNull();
21-
this.console = console.NotNull();
22-
this.globbingResolver = globbingResolver.NotNull();
23-
this.buildAgent = buildAgent.NotNull();
24-
}
25-
2620
public Arguments ParseArguments(string commandLineArguments)
2721
{
2822
var arguments = QuotedStringHelpers.SplitUnquoted(commandLineArguments, ' ');

src/GitVersion.App/GitVersionApp.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@
55

66
namespace GitVersion;
77

8-
internal class GitVersionApp : IHostedService
8+
internal class GitVersionApp(ILog log, IHostApplicationLifetime applicationLifetime, IGitVersionExecutor gitVersionExecutor, IOptions<GitVersionOptions> options)
9+
: IHostedService
910
{
10-
private readonly IHostApplicationLifetime applicationLifetime;
11-
private readonly IGitVersionExecutor gitVersionExecutor;
12-
private readonly ILog log;
13-
private readonly IOptions<GitVersionOptions> options;
11+
private readonly ILog log = log.NotNull();
12+
private readonly IHostApplicationLifetime applicationLifetime = applicationLifetime.NotNull();
13+
private readonly IGitVersionExecutor gitVersionExecutor = gitVersionExecutor.NotNull();
14+
private readonly IOptions<GitVersionOptions> options = options.NotNull();
1415

15-
public GitVersionApp(IHostApplicationLifetime applicationLifetime, IGitVersionExecutor gitVersionExecutor, ILog log, IOptions<GitVersionOptions> options)
16-
{
17-
this.options = options.NotNull();
18-
this.applicationLifetime = applicationLifetime.NotNull();
19-
this.gitVersionExecutor = gitVersionExecutor.NotNull();
20-
this.log = log.NotNull();
21-
}
2216
public Task StartAsync(CancellationToken cancellationToken)
2317
{
2418
try

src/GitVersion.App/GitVersionExecutor.cs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,27 @@
55

66
namespace GitVersion;
77

8-
internal class GitVersionExecutor : IGitVersionExecutor
8+
internal class GitVersionExecutor(
9+
ILog log,
10+
IConsole console,
11+
IConfigurationFileLocator configurationFileLocator,
12+
IConfigurationProvider configurationProvider,
13+
IGitVersionCalculateTool gitVersionCalculateTool,
14+
IGitVersionOutputTool gitVersionOutputTool,
15+
IVersionWriter versionWriter,
16+
IHelpWriter helpWriter,
17+
IGitRepositoryInfo repositoryInfo)
18+
: IGitVersionExecutor
919
{
10-
private readonly ILog log;
11-
private readonly IConsole console;
12-
private readonly IConfigurationFileLocator configurationFileLocator;
13-
private readonly IConfigurationProvider configurationProvider;
14-
private readonly IGitVersionCalculateTool gitVersionCalculateTool;
15-
private readonly IGitVersionOutputTool gitVersionOutputTool;
16-
private readonly IVersionWriter versionWriter;
17-
private readonly IHelpWriter helpWriter;
18-
private readonly IGitRepositoryInfo repositoryInfo;
19-
20-
public GitVersionExecutor(ILog log, IConsole console,
21-
IConfigurationFileLocator configurationFileLocator, IConfigurationProvider configurationProvider,
22-
IGitVersionCalculateTool gitVersionCalculateTool, IGitVersionOutputTool gitVersionOutputTool,
23-
IVersionWriter versionWriter, IHelpWriter helpWriter, IGitRepositoryInfo repositoryInfo)
24-
{
25-
this.log = log.NotNull();
26-
this.console = console.NotNull();
27-
this.configurationFileLocator = configurationFileLocator.NotNull();
28-
this.configurationProvider = configurationProvider.NotNull();
29-
30-
this.gitVersionCalculateTool = gitVersionCalculateTool.NotNull();
31-
this.gitVersionOutputTool = gitVersionOutputTool.NotNull();
32-
33-
this.versionWriter = versionWriter.NotNull();
34-
this.helpWriter = helpWriter.NotNull();
35-
this.repositoryInfo = repositoryInfo.NotNull();
36-
}
20+
private readonly ILog log = log.NotNull();
21+
private readonly IConsole console = console.NotNull();
22+
private readonly IConfigurationFileLocator configurationFileLocator = configurationFileLocator.NotNull();
23+
private readonly IConfigurationProvider configurationProvider = configurationProvider.NotNull();
24+
private readonly IGitVersionCalculateTool gitVersionCalculateTool = gitVersionCalculateTool.NotNull();
25+
private readonly IGitVersionOutputTool gitVersionOutputTool = gitVersionOutputTool.NotNull();
26+
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
27+
private readonly IHelpWriter helpWriter = helpWriter.NotNull();
28+
private readonly IGitRepositoryInfo repositoryInfo = repositoryInfo.NotNull();
3729

3830
public int Execute(GitVersionOptions gitVersionOptions)
3931
{

src/GitVersion.App/HelpWriter.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,10 @@
44

55
namespace GitVersion;
66

7-
internal class HelpWriter : IHelpWriter
7+
internal class HelpWriter(IVersionWriter versionWriter, IConsole console) : IHelpWriter
88
{
9-
private readonly IVersionWriter versionWriter;
10-
private readonly IConsole console;
11-
12-
public HelpWriter(IVersionWriter versionWriter, IConsole console)
13-
{
14-
this.versionWriter = versionWriter.NotNull();
15-
this.console = console.NotNull();
16-
}
9+
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
10+
private readonly IConsole console = console.NotNull();
1711

1812
public void Write() => WriteTo(this.console.WriteLine);
1913

src/GitVersion.App/QuotedStringHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ internal static class QuotedStringHelpers
2323
public static string[] SplitUnquoted(string? input, char splitChar)
2424
{
2525
if (input == null)
26-
return Array.Empty<string>();
26+
return [];
2727

2828
var split = new List<string>();
2929
bool isPreviousCharBackslash = false;
@@ -50,7 +50,7 @@ public static string[] SplitUnquoted(string? input, char splitChar)
5050
isPreviousCharBackslash = current == '\\';
5151
}
5252

53-
split.Add(input.Substring(startIndex, input.Length - startIndex));
53+
split.Add(input[startIndex..]);
5454

5555
return split.Where(argument => !argument.IsNullOrEmpty()).ToArray();
5656
}

0 commit comments

Comments
 (0)