Skip to content

Commit

Permalink
Merge pull request #3975 from arturcic/main
Browse files Browse the repository at this point in the history
cleanups & refactorings
  • Loading branch information
arturcic authored Mar 21, 2024
2 parents 7f147ed + e88f431 commit 1affea1
Show file tree
Hide file tree
Showing 193 changed files with 836 additions and 943 deletions.
1 change: 1 addition & 0 deletions new-cli/GitVersion.Calculation/CalculateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using GitVersion.Extensions;
using GitVersion.Git;
using GitVersion.Infrastructure;

namespace GitVersion.Commands;
Expand Down
1 change: 1 addition & 0 deletions new-cli/GitVersion.Cli/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GitVersion;
using GitVersion.Extensions;
using GitVersion.Generated;
using GitVersion.Git;
using GitVersion.Infrastructure;

var modules = new IGitVersionModule[]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GitVersion;
namespace GitVersion.Git;

internal sealed partial class GitRepository : IGitRepository
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GitVersion.Infrastructure;

namespace GitVersion;
namespace GitVersion.Git;

public class LibGit2SharpCoreModule : IGitVersionModule
{
Expand Down
1 change: 1 addition & 0 deletions new-cli/GitVersion.Core.Tester/GitVersionApp.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GitVersion.Git;
using GitVersion.Infrastructure;

namespace GitVersion;
Expand Down
1 change: 1 addition & 0 deletions new-cli/GitVersion.Core.Tester/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GitVersion;
using GitVersion.Extensions;
using GitVersion.Git;
using GitVersion.Infrastructure;

var assemblies = new IGitVersionModule[]
Expand Down
32 changes: 21 additions & 11 deletions schemas/6.0/GitVersion.configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,8 @@
"pre-release-weight": {
"$ref": "#/$defs/nullableOfInt32"
},
"prevent-increment-of-merged-branch-version": {
"$ref": "#/$defs/nullableOfBoolean2"
},
"prevent-increment-when-current-commit-tagged": {
"$ref": "#/$defs/nullableOfBoolean3"
"prevent-increment": {
"$ref": "#/$defs/preventIncrementConfiguration"
},
"regex": {
"$ref": "#/$defs/string2"
Expand Down Expand Up @@ -186,6 +183,7 @@
"description": "Specifies which version strategies (one or more) will be used to determine the next version. Following values are available: 'ConfiguredNextVersion', 'MergeMessage', 'TaggedCommit', 'TrackReleaseBranches', 'VersionInBranchName' and 'TrunkBased'.",
"enum": [
"None",
"Fallback",
"ConfiguredNextVersion",
"MergeMessage",
"TaggedCommit",
Expand Down Expand Up @@ -231,11 +229,8 @@
"pre-release-weight": {
"$ref": "#/$defs/nullableOfInt32"
},
"prevent-increment-of-merged-branch-version": {
"$ref": "#/$defs/nullableOfBoolean2"
},
"prevent-increment-when-current-commit-tagged": {
"$ref": "#/$defs/nullableOfBoolean3"
"prevent-increment": {
"$ref": "#/$defs/preventIncrementConfiguration"
},
"regex": {
"$ref": "#/$defs/string2"
Expand Down Expand Up @@ -319,8 +314,23 @@
"null"
]
},
"preventIncrementConfiguration": {
"description": "The prevent increment configuration section.",
"type": "object",
"properties": {
"of-merged-branch": {
"$ref": "#/$defs/nullableOfBoolean2"
},
"when-branch-merged": {
"$ref": "#/$defs/nullableOfBoolean2"
},
"when-current-commit-tagged": {
"$ref": "#/$defs/nullableOfBoolean3"
}
}
},
"nullableOfBoolean2": {
"description": "Prevent increment of merged branch version.",
"description": "Prevent increment when branch merged.",
"type": [
"boolean",
"null"
Expand Down
1 change: 0 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json"/>
<PackageReference Include="Roslynator.Analyzers">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
1 change: 0 additions & 1 deletion src/GitVersion.App.Tests/ArgumentParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using GitVersion.Configuration;
using GitVersion.Core.Tests.Helpers;
using GitVersion.Extensions;
using GitVersion.Helpers;
using GitVersion.Logging;
using GitVersion.VersionCalculation;
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.App.Tests/Helpers/ProgramFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace GitVersion.App.Tests;
public sealed class ProgramFixture
{
private readonly IEnvironment environment;
private List<Action<IServiceCollection>> Overrides { get; } = new();
private List<Action<IServiceCollection>> Overrides { get; } = [];
private readonly Lazy<string> logger;
private readonly Lazy<string?> output;

Expand Down
1 change: 1 addition & 0 deletions src/GitVersion.App.Tests/PullRequestInBuildAgentTest.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GitVersion.Agents;
using GitVersion.Core.Tests.Helpers;
using GitVersion.Extensions;
using GitVersion.Git;
using GitVersion.Helpers;
using GitVersion.Output;
using LibGit2Sharp;
Expand Down
78 changes: 78 additions & 0 deletions src/GitVersion.App/ArgumentParserExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System.Text.RegularExpressions;
using GitVersion.Helpers;

namespace GitVersion;

internal static class ArgumentParserExtensions
{
private static readonly string[] TrueValues = ["1", "true"];
private static readonly string[] FalseValues = ["0", "false"];

public static bool IsTrue(this string? value) => TrueValues.Contains(value, StringComparer.OrdinalIgnoreCase);

public static bool IsFalse(this string? value) => FalseValues.Contains(value, StringComparer.OrdinalIgnoreCase);

public static bool IsValidPath(this string? path)
{
if (path == null)
return false;

try
{
_ = PathHelper.GetFullPath(path);
}
catch
{
path = PathHelper.Combine(SysEnv.CurrentDirectory, path);

try
{
_ = PathHelper.GetFullPath(path);
}
catch
{
return false;
}
}

return Directory.Exists(path);
}

public static bool IsSwitchArgument(this string? value)
=> value != null
&& (value.StartsWith('-') || value.StartsWith('/'))
&& !Regex.Match(value, @"/\w+:").Success; //Exclude msbuild & project parameters in form /blah:, which should be parsed as values, not switch names.

public static bool IsSwitch(this string? value, string switchName)
{
if (value == null)
return false;

if (value.StartsWith('-'))
{
value = value[1..];
}

if (value.StartsWith('/'))
{
value = value[1..];
}

return string.Equals(switchName, value, StringComparison.OrdinalIgnoreCase);
}

public static bool IsHelp(this string singleArgument) => (singleArgument == "?") || singleArgument.IsSwitch("h") || singleArgument.IsSwitch("help") || singleArgument.IsSwitch("?");

public static bool ArgumentRequiresValue(this string argument, int argumentIndex)
{
var booleanArguments = new[] { "init", "updateassemblyinfo", "ensureassemblyinfo", "nofetch", "nonormalize", "nocache" };

var argumentMightRequireValue = !booleanArguments.Contains(argument[1..], StringComparer.OrdinalIgnoreCase);

// If this is the first argument that might be a target path, the argument starts with slash and we're on an OS that supports paths with slashes, the argument does not require a value.
if (argumentMightRequireValue && argumentIndex == 0 && argument.StartsWith('/') && Path.DirectorySeparatorChar == '/' && argument.IsValidPath())
return false;

return argumentMightRequireValue;
}
}
1 change: 1 addition & 0 deletions src/GitVersion.App/Arguments.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GitVersion.Git;
using GitVersion.Logging;

namespace GitVersion;
Expand Down
7 changes: 6 additions & 1 deletion src/GitVersion.App/GitVersionExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GitVersion.Configuration;
using GitVersion.Extensions;
using GitVersion.Git;
using GitVersion.Helpers;
using GitVersion.Logging;

Expand All @@ -10,6 +11,7 @@ internal class GitVersionExecutor(
IConsole console,
IConfigurationFileLocator configurationFileLocator,
IConfigurationProvider configurationProvider,
IConfigurationSerializer configurationSerializer,
IGitVersionCalculateTool gitVersionCalculateTool,
IGitVersionOutputTool gitVersionOutputTool,
IVersionWriter versionWriter,
Expand All @@ -21,6 +23,8 @@ internal class GitVersionExecutor(
private readonly IConsole console = console.NotNull();
private readonly IConfigurationFileLocator configurationFileLocator = configurationFileLocator.NotNull();
private readonly IConfigurationProvider configurationProvider = configurationProvider.NotNull();
private readonly IConfigurationSerializer configurationSerializer = configurationSerializer.NotNull();

private readonly IGitVersionCalculateTool gitVersionCalculateTool = gitVersionCalculateTool.NotNull();
private readonly IGitVersionOutputTool gitVersionOutputTool = gitVersionOutputTool.NotNull();
private readonly IVersionWriter versionWriter = versionWriter.NotNull();
Expand Down Expand Up @@ -143,7 +147,8 @@ private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int e
this.configurationFileLocator.Verify(workingDirectory, this.repositoryInfo.ProjectRootDirectory);
}
var configuration = this.configurationProvider.Provide();
this.console.WriteLine(configuration.ToJsonString());
var configurationString = configurationSerializer.Serialize(configuration);
this.console.WriteLine(configurationString);
exitCode = 0;
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.App/OverrideConfigurationOptionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace GitVersion;

internal class OverrideConfigurationOptionParser
{
private readonly Dictionary<object, object?> overrideConfiguration = new();
private readonly Dictionary<object, object?> overrideConfiguration = [];

private static readonly Lazy<ILookup<string?, PropertyInfo>> _lazySupportedProperties =
new(GetSupportedProperties, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void BuildNumberIsFullSemVer()

writes[1].ShouldBe("1.2.3-beta.1+5");

writes = new List<string?>();
writes = [];
buildAgent.WriteIntegration(writes.Add, variables, false);
writes.ShouldNotContain(x => x != null && x.StartsWith("Executing GenerateSetVersionMessage for "));
}
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.BuildAgents/Agents/BuildKite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override string[] GenerateSetParameterMessage(string name, string? value)

// For pull requests BUILDKITE_BRANCH refers to the head, so adjust the
// branch name for pull request versioning to function as expected
return string.Format("refs/pull/{0}/head", pullRequest);
return $"refs/pull/{pullRequest}/head";
}

public override bool PreventFetch() => true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GitVersion.Configuration;
using GitVersion.Core.Tests.Helpers;
using GitVersion.Git;

namespace GitVersion.Core.Tests.Configuration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using GitVersion.Configuration;
using GitVersion.Configuration.Tests.Configuration;
using GitVersion.Core.Tests.Helpers;
using GitVersion.Extensions;
using GitVersion.Helpers;
using GitVersion.Logging;
using GitVersion.VersionCalculation;
Expand Down Expand Up @@ -183,7 +182,7 @@ public void CanWriteOutEffectiveConfiguration()
{
var configuration = this.configurationProvider.ProvideForDirectory(this.repoPath);

configuration.ToJsonString().ShouldMatchApproved();
new ConfigurationSerializer().Serialize(configuration).ShouldMatchApproved();
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,54 @@ namespace GitVersion.Core.Tests.Configuration;
[TestFixture]
public class IgnoreConfigurationTests : TestBase
{
private readonly ConfigurationSerializer serializer = new();

[Test]
public void CanDeserialize()
{
const string yaml = @"
ignore:
sha: [b6c0c9fda88830ebcd563e500a5a7da5a1658e98]
commits-before: 2015-10-23T12:23:15
";
const string yaml =
"""
ignore:
sha: [b6c0c9fda88830ebcd563e500a5a7da5a1658e98]
commits-before: 2015-10-23T12:23:15
""";

using var reader = new StringReader(yaml);
var configuration = ConfigurationSerializer.Read(reader);
var configuration = serializer.ReadConfiguration(yaml);

configuration.ShouldNotBeNull();
configuration.Ignore.ShouldNotBeNull();
configuration.Ignore.Shas.ShouldNotBeEmpty();
configuration.Ignore.Shas.ShouldBe(new[] { "b6c0c9fda88830ebcd563e500a5a7da5a1658e98" });
configuration.Ignore.Shas.ShouldBe(["b6c0c9fda88830ebcd563e500a5a7da5a1658e98"]);
configuration.Ignore.Before.ShouldBe(DateTimeOffset.Parse("2015-10-23T12:23:15"));
}

[Test]
public void ShouldSupportsOtherSequenceFormat()
{
const string yaml = @"
ignore:
sha:
- b6c0c9fda88830ebcd563e500a5a7da5a1658e98
- 6c19c7c219ecf8dbc468042baefa73a1b213e8b1
";
const string yaml =
"""
ignore:
sha:
- b6c0c9fda88830ebcd563e500a5a7da5a1658e98
- 6c19c7c219ecf8dbc468042baefa73a1b213e8b1
""";

using var reader = new StringReader(yaml);
var configuration = ConfigurationSerializer.Read(reader);
var configuration = serializer.ReadConfiguration(yaml);

configuration.ShouldNotBeNull();
configuration.Ignore.ShouldNotBeNull();
configuration.Ignore.Shas.ShouldNotBeEmpty();
configuration.Ignore.Shas.ShouldBe(new[] { "b6c0c9fda88830ebcd563e500a5a7da5a1658e98", "6c19c7c219ecf8dbc468042baefa73a1b213e8b1" });
configuration.Ignore.Shas.ShouldBe(["b6c0c9fda88830ebcd563e500a5a7da5a1658e98", "6c19c7c219ecf8dbc468042baefa73a1b213e8b1"]);
}

[Test]
public void WhenNotInConfigShouldHaveDefaults()
{
const string yaml = @"
next-version: 1.0
";
const string yaml = "next-version: 1.0";

using var reader = new StringReader(yaml);
var configuration = ConfigurationSerializer.Read(reader);
var configuration = serializer.ReadConfiguration(yaml);

configuration.ShouldNotBeNull();
configuration.Ignore.ShouldNotBeNull();
configuration.Ignore.Shas.ShouldBeEmpty();
configuration.Ignore.Before.ShouldBe(null);
Expand All @@ -61,13 +63,13 @@ public void WhenNotInConfigShouldHaveDefaults()
[Test]
public void WhenBadDateFormatShouldFail()
{
const string yaml = @"
ignore:
commits-before: bad format date
";
const string yaml =
"""
ignore:
commits-before: bad format date
""";

using var reader = new StringReader(yaml);
Should.Throw<YamlException>(() => ConfigurationSerializer.Read(reader));
Should.Throw<YamlException>(() => serializer.ReadConfiguration(yaml));
}

[Test]
Expand Down
Loading

0 comments on commit 1affea1

Please sign in to comment.