Skip to content

Commit

Permalink
Change *VersionCalculator implement a single common interface IVersio…
Browse files Browse the repository at this point in the history
…nModeCalculator
  • Loading branch information
arturcic committed Oct 6, 2023
1 parent 7970077 commit 8c8b08e
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@
// null
// );
// var mainlineVersionCalculatorMock = Substitute.For<IMainlineVersionCalculator>();
// mainlineVersionCalculatorMock.FindMainlineModeVersion(Arg.Any<NextVersion>()).Returns(lowerVersion.SemanticVersion);
// mainlineVersionCalculatorMock.Calculate(Arg.Any<NextVersion>()).Returns(lowerVersion.SemanticVersion);
// var continuousDeploymentVersionCalculatorMock = Substitute.For<IContinuousDeploymentVersionCalculator>();

// var versionStrategies = new IVersionStrategy[] { new TestVersionStrategy(preReleaseVersion, lowerVersion) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace GitVersion.Core.Tests.VersionCalculation;

public class TestMainlineVersionCalculator : IMainlineVersionCalculator
public class TestMainlineVersionCalculator : IVersionModeCalculator
{
private readonly SemanticVersionBuildMetaData metaData;

public TestMainlineVersionCalculator(SemanticVersionBuildMetaData metaData) => this.metaData = metaData;

public SemanticVersion FindMainlineModeVersion(NextVersion nextVersion) => throw new NotImplementedException();
public SemanticVersion Calculate(NextVersion nextVersion) => throw new NotImplementedException();

public SemanticVersionBuildMetaData CreateVersionBuildMetaData(ICommit? baseVersionSource) => this.metaData;

Check warning on line 13 in src/GitVersion.Core.Tests/VersionCalculation/TestMainlineVersionCalculator.cs

View workflow job for this annotation

GitHub Actions / DotNet Format

Remove unused parameter 'baseVersionSource' if it is not part of a shipped public API

Check warning on line 13 in src/GitVersion.Core.Tests/VersionCalculation/TestMainlineVersionCalculator.cs

View workflow job for this annotation

GitHub Actions / DotNet Format

Remove unused parameter 'baseVersionSource' if it is not part of a shipped public API

Check warning on line 13 in src/GitVersion.Core.Tests/VersionCalculation/TestMainlineVersionCalculator.cs

View workflow job for this annotation

GitHub Actions / DotNet Format

Remove unused parameter 'baseVersionSource' if it is not part of a shipped public API

Check warning on line 13 in src/GitVersion.Core.Tests/VersionCalculation/TestMainlineVersionCalculator.cs

View workflow job for this annotation

GitHub Actions / DotNet Format

Remove unused parameter 'baseVersionSource' if it is not part of a shipped public API
}
10 changes: 10 additions & 0 deletions src/GitVersion.Core/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ public static class EnumerableExtensions
var current = e.Current;
return !e.MoveNext() ? current : default;
}

public static T SingleOfType<T>(this IEnumerable source)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}

return source.OfType<T>().Single();
}
}
14 changes: 3 additions & 11 deletions src/GitVersion.Core/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -718,28 +718,19 @@ GitVersion.VersionCalculation.CommitMessageIncrementMode
GitVersion.VersionCalculation.CommitMessageIncrementMode.Disabled = 1 -> GitVersion.VersionCalculation.CommitMessageIncrementMode
GitVersion.VersionCalculation.CommitMessageIncrementMode.Enabled = 0 -> GitVersion.VersionCalculation.CommitMessageIncrementMode
GitVersion.VersionCalculation.CommitMessageIncrementMode.MergeMessageOnly = 2 -> GitVersion.VersionCalculation.CommitMessageIncrementMode
GitVersion.VersionCalculation.IContinuousDeliveryVersionCalculator
GitVersion.VersionCalculation.IContinuousDeliveryVersionCalculator.Calculate(GitVersion.VersionCalculation.NextVersion! nextVersion) -> GitVersion.SemanticVersion!
GitVersion.VersionCalculation.IContinuousDeploymentVersionCalculator
GitVersion.VersionCalculation.IContinuousDeploymentVersionCalculator.Calculate(GitVersion.VersionCalculation.NextVersion! nextVersion) -> GitVersion.SemanticVersion!
GitVersion.VersionCalculation.IEffectiveBranchConfigurationFinder
GitVersion.VersionCalculation.IEffectiveBranchConfigurationFinder.GetConfigurations(GitVersion.IBranch! branch, GitVersion.Configuration.IGitVersionConfiguration! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.Configuration.EffectiveBranchConfiguration!>!
GitVersion.VersionCalculation.IIncrementStrategyFinder
GitVersion.VersionCalculation.IIncrementStrategyFinder.DetermineIncrementedField(GitVersion.ICommit? currentCommit, GitVersion.VersionCalculation.BaseVersion! baseVersion, GitVersion.Configuration.EffectiveConfiguration! configuration) -> GitVersion.VersionField
GitVersion.VersionCalculation.IIncrementStrategyFinder.GetIncrementForCommits(string? majorVersionBumpMessage, string? minorVersionBumpMessage, string? patchVersionBumpMessage, string? noBumpMessage, System.Collections.Generic.IEnumerable<GitVersion.ICommit!>! commits) -> GitVersion.VersionField?
GitVersion.VersionCalculation.IMainlineVersionCalculator
GitVersion.VersionCalculation.IMainlineVersionCalculator.CreateVersionBuildMetaData(GitVersion.ICommit? baseVersionSource) -> GitVersion.SemanticVersionBuildMetaData!
GitVersion.VersionCalculation.IMainlineVersionCalculator.FindMainlineModeVersion(GitVersion.VersionCalculation.NextVersion! nextVersion) -> GitVersion.SemanticVersion!
GitVersion.VersionCalculation.IManualDeploymentVersionCalculator
GitVersion.VersionCalculation.IManualDeploymentVersionCalculator.Calculate(GitVersion.VersionCalculation.NextVersion! nextVersion) -> GitVersion.SemanticVersion!
GitVersion.VersionCalculation.INextVersionCalculator
GitVersion.VersionCalculation.INextVersionCalculator.FindVersion() -> GitVersion.VersionCalculation.NextVersion!
GitVersion.VersionCalculation.ITrunkBasedVersionCalculator
GitVersion.VersionCalculation.ITrunkBasedVersionCalculator.Calculate(GitVersion.VersionCalculation.NextVersion! nextVersion) -> GitVersion.SemanticVersion!
GitVersion.VersionCalculation.IVariableProvider
GitVersion.VersionCalculation.IVariableProvider.GetVariablesFor(GitVersion.SemanticVersion! semanticVersion, GitVersion.Configuration.EffectiveConfiguration! configuration, GitVersion.SemanticVersion? currentCommitTaggedVersion) -> GitVersion.OutputVariables.GitVersionVariables!
GitVersion.VersionCalculation.IVersionFilter
GitVersion.VersionCalculation.IVersionFilter.Exclude(GitVersion.VersionCalculation.BaseVersion! version, out string? reason) -> bool
GitVersion.VersionCalculation.IVersionModeCalculator
GitVersion.VersionCalculation.IVersionModeCalculator.Calculate(GitVersion.VersionCalculation.NextVersion! nextVersion) -> GitVersion.SemanticVersion!
GitVersion.VersionCalculation.IVersionStrategy
GitVersion.VersionCalculation.IVersionStrategy.GetBaseVersions(GitVersion.Configuration.EffectiveBranchConfiguration! configuration) -> System.Collections.Generic.IEnumerable<GitVersion.VersionCalculation.BaseVersion!>!
GitVersion.VersionCalculation.NextVersion
Expand Down Expand Up @@ -834,6 +825,7 @@ static GitVersion.Extensions.CommonExtensions.NotNullOrEmpty(this string? value,
static GitVersion.Extensions.CommonExtensions.NotNullOrWhitespace(this string? value, string! name = "") -> string!
static GitVersion.Extensions.DictionaryExtensions.GetOrAdd<TKey, TValue>(this System.Collections.Generic.IDictionary<TKey, TValue>! dict, TKey key, System.Func<TValue>! getValue) -> TValue
static GitVersion.Extensions.EnumerableExtensions.OnlyOrDefault<T>(this System.Collections.Generic.IEnumerable<T>! source) -> T?
static GitVersion.Extensions.EnumerableExtensions.SingleOfType<T>(this System.Collections.IEnumerable! source) -> T
static GitVersion.Extensions.GitExtensions.CreateGitLogArgs(int? maxCommits) -> string!
static GitVersion.Extensions.GitExtensions.DumpGraph(string! workingDirectory, System.Action<string!>? writer = null, int? maxCommits = null) -> void
static GitVersion.Extensions.ObjectExtensions.Deconstruct<TKey, TValue>(this System.Collections.Generic.KeyValuePair<TKey, TValue> kvp, out TKey key, out TValue value) -> void
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace GitVersion.VersionCalculation;

public interface ITrunkBasedVersionCalculator
public interface IVersionModeCalculator
{
SemanticVersion Calculate(NextVersion nextVersion);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GitVersion.VersionCalculation;

internal sealed class ContinuousDeliveryVersionCalculator : NonTrunkBasedVersionCalculatorBase, IContinuousDeliveryVersionCalculator
internal sealed class ContinuousDeliveryVersionCalculator : NonTrunkBasedVersionCalculatorBase, IVersionModeCalculator
{
public ContinuousDeliveryVersionCalculator(ILog log, IRepositoryStore repositoryStore, Lazy<GitVersionContext> versionContext)
: base(log, repositoryStore, versionContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GitVersion.VersionCalculation;

internal sealed class ContinuousDeploymentVersionCalculator : NonTrunkBasedVersionCalculatorBase, IContinuousDeploymentVersionCalculator
internal sealed class ContinuousDeploymentVersionCalculator : NonTrunkBasedVersionCalculatorBase, IVersionModeCalculator
{
public ContinuousDeploymentVersionCalculator(ILog log, IRepositoryStore repositoryStore, Lazy<GitVersionContext> versionContext)
: base(log, repositoryStore, versionContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace GitVersion.VersionCalculation;

internal class MainlineVersionCalculator : IMainlineVersionCalculator
internal class MainlineVersionCalculator : IVersionModeCalculator
{
private readonly ILog log;
private readonly IRepositoryStore repositoryStore;
Expand All @@ -21,7 +21,7 @@ public MainlineVersionCalculator(ILog log, IRepositoryStore repositoryStore, Laz
this.incrementStrategyFinder = incrementStrategyFinder.NotNull();
}

public SemanticVersion FindMainlineModeVersion(NextVersion nextVersion)
public SemanticVersion Calculate(NextVersion nextVersion)
{
var baseVersion = nextVersion.BaseVersion;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace GitVersion.VersionCalculation;

internal sealed class ManualDeploymentVersionCalculator : NonTrunkBasedVersionCalculatorBase, IManualDeploymentVersionCalculator
internal sealed class ManualDeploymentVersionCalculator : NonTrunkBasedVersionCalculatorBase, IVersionModeCalculator
{
public ManualDeploymentVersionCalculator(ILog log, IRepositoryStore repositoryStore, Lazy<GitVersionContext> versionContext)
: base(log, repositoryStore, versionContext)
Expand Down
36 changes: 15 additions & 21 deletions src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ namespace GitVersion.VersionCalculation;
internal class NextVersionCalculator : INextVersionCalculator
{
private readonly ILog log;
private readonly IMainlineVersionCalculator mainlineVersionCalculator;
private readonly IContinuousDeploymentVersionCalculator continuousDeploymentVersionCalculator;
private readonly IContinuousDeliveryVersionCalculator continuousDeliveryVersionCalculator;
private readonly IManualDeploymentVersionCalculator manualDeploymentVersionCalculator;
private readonly Lazy<GitVersionContext> versionContext;
private readonly IEnumerable<IVersionModeCalculator> versionModeCalculators;
private readonly IVersionStrategy[] versionStrategies;
private readonly IEffectiveBranchConfigurationFinder effectiveBranchConfigurationFinder;
private readonly IIncrementStrategyFinder incrementStrategyFinder;
Expand All @@ -22,21 +19,14 @@ internal class NextVersionCalculator : INextVersionCalculator

public NextVersionCalculator(ILog log,
Lazy<GitVersionContext> versionContext,
IMainlineVersionCalculator mainlineVersionCalculator,
IContinuousDeploymentVersionCalculator continuousDeploymentVersionCalculator,
IContinuousDeliveryVersionCalculator continuousDeliveryVersionCalculator,
IManualDeploymentVersionCalculator manualDeploymentVersionCalculator,
IEnumerable<IVersionModeCalculator> versionModeCalculators,
IEnumerable<IVersionStrategy> versionStrategies,
IEffectiveBranchConfigurationFinder effectiveBranchConfigurationFinder,
IIncrementStrategyFinder incrementStrategyFinder)
{
this.log = log.NotNull();
this.versionContext = versionContext.NotNull();
this.mainlineVersionCalculator = mainlineVersionCalculator.NotNull();
this.continuousDeploymentVersionCalculator = continuousDeploymentVersionCalculator.NotNull();
this.continuousDeliveryVersionCalculator = continuousDeliveryVersionCalculator.NotNull();
this.manualDeploymentVersionCalculator = manualDeploymentVersionCalculator.NotNull();
this.incrementStrategyFinder = incrementStrategyFinder.NotNull();
this.versionModeCalculators = versionModeCalculators;
this.versionStrategies = versionStrategies.NotNull().ToArray();
this.effectiveBranchConfigurationFinder = effectiveBranchConfigurationFinder.NotNull();
this.incrementStrategyFinder = incrementStrategyFinder.NotNull();
Expand All @@ -55,15 +45,19 @@ public virtual NextVersion FindVersion()
return new(incrementedVersion, nextVersion.BaseVersion, new(nextVersion.Branch, nextVersion.Configuration));
}

private SemanticVersion CalculateIncrementedVersion(VersioningMode versioningMode, NextVersion nextVersion) => versioningMode switch
private SemanticVersion CalculateIncrementedVersion(VersioningMode versioningMode, NextVersion nextVersion)
{
VersioningMode.ContinuousDelivery => this.manualDeploymentVersionCalculator.Calculate(nextVersion),
VersioningMode.ContinuousDeployment => nextVersion.Configuration is { IsMainline: true, Label: null }
? this.continuousDeploymentVersionCalculator.Calculate(nextVersion)
: this.continuousDeliveryVersionCalculator.Calculate(nextVersion),
VersioningMode.Mainline => this.mainlineVersionCalculator.FindMainlineModeVersion(nextVersion),
_ => throw new InvalidEnumArgumentException(nameof(versioningMode), (int)versioningMode, typeof(VersioningMode)),
};
IVersionModeCalculator calculator = versioningMode switch
{
VersioningMode.ContinuousDelivery => this.versionModeCalculators.SingleOfType<ManualDeploymentVersionCalculator>(),
VersioningMode.ContinuousDeployment => nextVersion.Configuration is { IsMainline: true, Label: null }
? this.versionModeCalculators.SingleOfType<ContinuousDeploymentVersionCalculator>()
: this.versionModeCalculators.SingleOfType<ContinuousDeliveryVersionCalculator>(),
VersioningMode.Mainline => this.versionModeCalculators.SingleOfType<MainlineVersionCalculator>(),
_ => throw new InvalidEnumArgumentException(nameof(versioningMode), (int)versioningMode, typeof(VersioningMode)),
};
return calculator.Calculate(nextVersion);
}

private NextVersion CalculateNextVersion(IBranch branch, IGitVersionConfiguration configuration)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace GitVersion.VersionCalculation;

internal sealed class TrunkBasedVersionCalculator : ITrunkBasedVersionCalculator
internal sealed class TrunkBasedVersionCalculator : IVersionModeCalculator
{
// TODO: Please implement trunk based version here and remove MainlineVersionCalculator.
public SemanticVersion Calculate(NextVersion nextVersion) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public void RegisterTypes(IServiceCollection services)
services.AddModule(new VersionStrategyModule());

services.AddSingleton<IVariableProvider, VariableProvider>();
services.AddSingleton<IMainlineVersionCalculator, MainlineVersionCalculator>();
services.AddSingleton<ITrunkBasedVersionCalculator, TrunkBasedVersionCalculator>();
services.AddSingleton<IContinuousDeploymentVersionCalculator, ContinuousDeploymentVersionCalculator>();
services.AddSingleton<IContinuousDeliveryVersionCalculator, ContinuousDeliveryVersionCalculator>();
services.AddSingleton<IManualDeploymentVersionCalculator, ManualDeploymentVersionCalculator>();
services.AddSingleton<IVersionModeCalculator, MainlineVersionCalculator>();
services.AddSingleton<IVersionModeCalculator, TrunkBasedVersionCalculator>();
services.AddSingleton<IVersionModeCalculator, ContinuousDeploymentVersionCalculator>();
services.AddSingleton<IVersionModeCalculator, ContinuousDeliveryVersionCalculator>();
services.AddSingleton<IVersionModeCalculator, ManualDeploymentVersionCalculator>();
services.AddSingleton<INextVersionCalculator, NextVersionCalculator>();
services.AddSingleton<IIncrementStrategyFinder, IncrementStrategyFinder>();
services.AddSingleton<IEffectiveBranchConfigurationFinder, EffectiveBranchConfigurationFinder>();
Expand Down

0 comments on commit 8c8b08e

Please sign in to comment.