Skip to content

Commit

Permalink
decoupled modules from core and use abstractions instead
Browse files Browse the repository at this point in the history
  • Loading branch information
arturcic committed Mar 28, 2023
1 parent 683ca38 commit 07ba041
Show file tree
Hide file tree
Showing 24 changed files with 101 additions and 94 deletions.
40 changes: 20 additions & 20 deletions new-cli/GitVersion.Common/GitVersion.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,64 @@
<PackageReference Include="Polly" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\src\GitVersion.Core\Git\AuthenticationInfo.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\AuthenticationInfo.cs">
<Link>Git\AuthenticationInfo.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\CommitFilter.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\CommitFilter.cs">
<Link>Git\CommitFilter.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\IBranch.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\IBranch.cs">
<Link>Git\IBranch.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\IBranchCollection.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\IBranchCollection.cs">
<Link>Git\IBranchCollection.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\ICommit.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\ICommit.cs">
<Link>Git\ICommit.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\ICommitCollection.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\ICommitCollection.cs">
<Link>Git\ICommitCollection.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\IGitObject.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\IGitObject.cs">
<Link>Git\IGitObject.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\IGitRepository.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\IGitRepository.cs">
<Link>Git\IGitRepository.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\INamedReference.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\INamedReference.cs">
<Link>Git\INamedReference.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\IObjectId.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\IObjectId.cs">
<Link>Git\IObjectId.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\IReference.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\IReference.cs">
<Link>Git\IReference.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\IReferenceCollection.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\IReferenceCollection.cs">
<Link>Git\IReferenceCollection.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\IRefSpec.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\IRefSpec.cs">
<Link>Git\IRefSpec.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\IRefSpecCollection.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\IRefSpecCollection.cs">
<Link>Git\IRefSpecCollection.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\IRemote.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\IRemote.cs">
<Link>Git\IRemote.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\IRemoteCollection.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\IRemoteCollection.cs">
<Link>Git\IRemoteCollection.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\ITag.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\ITag.cs">
<Link>Git\ITag.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\ITagCollection.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\ITagCollection.cs">
<Link>Git\ITagCollection.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\ReferenceName.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\ReferenceName.cs">
<Link>Git\ReferenceName.cs</Link>
</Compile>
<Compile Include="..\..\src\GitVersion.Core\Git\RefSpecDirection.cs">
<Compile Include="..\..\src\GitVersion.Abstractions\Git\RefSpecDirection.cs">
<Link>Git\RefSpecDirection.cs</Link>
</Compile>
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace GitVersion.Agents;
public abstract class BuildAgentBase : ICurrentBuildAgent
{
protected readonly ILog Log;
protected IEnvironment Environment { get; }
protected IEnvironment Environment;

protected BuildAgentBase(IEnvironment environment, ILog log)
{
this.Log = log;
Environment = environment;
this.Environment = environment;
}

protected abstract string EnvironmentVariable { get; }
Expand Down
9 changes: 9 additions & 0 deletions src/GitVersion.Abstractions/Common/IGitVersionModule.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
using GitVersion.Extensions;
using Microsoft.Extensions.DependencyInjection;

namespace GitVersion;

public interface IGitVersionModule
{
void RegisterTypes(IServiceCollection services);

static IEnumerable<Type> FindAllDerivedTypes<T>(Assembly? assembly)
{
assembly.NotNull();

var derivedType = typeof(T);
return assembly.GetTypes().Where(t => t != derivedType && derivedType.IsAssignableFrom(t));
}
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Text.Encodings.Web;
using GitVersion.OutputVariables;

namespace GitVersion.Extensions;

public static class GitVersionVariablesExtensions
{
public static string ToJsonString(this GitVersionVariables gitVersionVariables)
{
var variablesType = typeof(VersionVariablesJsonModel);
var variables = new VersionVariablesJsonModel();

foreach (var (key, value) in gitVersionVariables.OrderBy(x => x.Key))
{
var propertyInfo = variablesType.GetProperty(key);
propertyInfo?.SetValue(variables, ChangeType(value, propertyInfo.PropertyType));
}

var serializeOptions = GetJsonSerializerOptions();

return JsonSerializer.Serialize(variables, serializeOptions);
}

public static JsonSerializerOptions GetJsonSerializerOptions()
{
var serializeOptions = new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
Converters =
{
new VersionVariablesJsonStringConverter()
}
};
return serializeOptions;
}

private static object? ChangeType(object? value, Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
if (value == null || value.ToString()?.Length == 0)
{
return null;
}

type = Nullable.GetUnderlyingType(type)!;
}

return Convert.ChangeType(value, type);
}
}
1 change: 1 addition & 0 deletions src/GitVersion.Abstractions/GitVersion.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="Microsoft.Extensions.Options" />
<PackageReference Include="Polly" />
</ItemGroup>

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/GitVersion.BuildAgents/GitVersion.BuildAgents.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\GitVersion.Core\GitVersion.Core.csproj"/>
<ProjectReference Include="..\GitVersion.Abstractions\GitVersion.Abstractions.csproj"/>
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="GitVersion.App.Tests"/>
Expand Down
6 changes: 3 additions & 3 deletions src/GitVersion.BuildAgents/GitVersionBuildAgentsModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace GitVersion.Agents;

public class GitVersionBuildAgentsModule : GitVersionModule
public class GitVersionBuildAgentsModule : IGitVersionModule
{
public override void RegisterTypes(IServiceCollection services)
public void RegisterTypes(IServiceCollection services)
{
var buildAgents = FindAllDerivedTypes<BuildAgentBase>(Assembly.GetAssembly(GetType()));
var buildAgents = IGitVersionModule.FindAllDerivedTypes<BuildAgentBase>(Assembly.GetAssembly(GetType()));

foreach (var buildAgent in buildAgents)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using GitVersion.Core.Tests.Helpers;
using GitVersion.OutputVariables;
using GitVersion.Extensions;
using GitVersion.VersionCalculation;
using Microsoft.Extensions.DependencyInjection;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using GitVersion.Core.Tests.Helpers;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.OutputVariables;
using GitVersion.VersionCalculation;
using Microsoft.Extensions.DependencyInjection;

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

namespace GitVersion.Configuration.Init;

public class GitVersionInitModule : GitVersionModule
public class GitVersionInitModule : IGitVersionModule
{
public override void RegisterTypes(IServiceCollection services)
public void RegisterTypes(IServiceCollection services)
{
services.AddTransient<IConfigInitWizard, ConfigInitWizard>();
services.AddTransient<IConfigInitStepFactory, ConfigInitStepFactory>();

var steps = FindAllDerivedTypes<ConfigInitWizardStep>(Assembly.GetAssembly(GetType()));
var steps = IGitVersionModule.FindAllDerivedTypes<ConfigInitWizardStep>(Assembly.GetAssembly(GetType()));

foreach (var step in steps)
{
Expand Down
17 changes: 0 additions & 17 deletions src/GitVersion.Core/Core/GitVersionModule.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Text.Encodings.Web;
using GitVersion.Extensions;
using GitVersion.Helpers;
using YamlDotNet.Serialization;
Expand All @@ -9,7 +8,7 @@ public static class VersionVariablesHelper
{
public static GitVersionVariables FromJson(string json)
{
var serializeOptions = JsonSerializerOptions();
var serializeOptions = GitVersionVariablesExtensions.GetJsonSerializerOptions();
var variablePairs = JsonSerializer.Deserialize<Dictionary<string, string>>(json, serializeOptions);
return FromDictionary(variablePairs);
}
Expand All @@ -33,22 +32,6 @@ public static GitVersionVariables FromFile(string filePath, IFileSystem fileSyst
}
}

public static string ToJsonString(this GitVersionVariables gitVersionVariables)
{
var variablesType = typeof(VersionVariablesJsonModel);
var variables = new VersionVariablesJsonModel();

foreach (var (key, value) in gitVersionVariables.OrderBy(x => x.Key))
{
var propertyInfo = variablesType.GetProperty(key);
propertyInfo?.SetValue(variables, ChangeType(value, propertyInfo.PropertyType));
}

var serializeOptions = JsonSerializerOptions();

return JsonSerializer.Serialize(variables, serializeOptions);
}

private static GitVersionVariables FromDictionary(IEnumerable<KeyValuePair<string, string>>? properties)
{
var type = typeof(GitVersionVariables);
Expand All @@ -71,25 +54,4 @@ private static GitVersionVariables FromFileInternal(string filePath, IFileSystem
var versionVariables = FromDictionary(dictionary);
return versionVariables;
}

private static JsonSerializerOptions JsonSerializerOptions()
{
var serializeOptions = new JsonSerializerOptions { WriteIndented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, Converters = { new VersionVariablesJsonStringConverter() } };
return serializeOptions;
}

private static object? ChangeType(object? value, Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
if (value == null || value.ToString()?.Length == 0)
{
return null;
}

type = Nullable.GetUnderlyingType(type)!;
}

return Convert.ChangeType(value, type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace GitVersion.VersionCalculation;

public class VersionStrategyModule : GitVersionModule
public class VersionStrategyModule : IGitVersionModule
{
public override void RegisterTypes(IServiceCollection services)
public void RegisterTypes(IServiceCollection services)
{
var versionStrategies = FindAllDerivedTypes<IVersionStrategy>(Assembly.GetAssembly(GetType()))
var versionStrategies = IGitVersionModule.FindAllDerivedTypes<IVersionStrategy>(Assembly.GetAssembly(GetType()))
.Where(x => x is { IsAbstract: false, IsInterface: false });

foreach (var versionStrategy in versionStrategies)
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.LibGit2Sharp/Git/GitRepository.extended.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private DirectReference GetPullRequestReference(AuthenticationInfo auth, LibGit2
: network.ListReferences(remote))
.Select(r => r.ResolveToDirectReference()).ToList();

this.log.Info($"Remote Refs:{System.Environment.NewLine}" + string.Join(System.Environment.NewLine, remoteTips.Select(r => r.CanonicalName)));
this.log.Info($"Remote Refs:{Environment.NewLine}" + string.Join(Environment.NewLine, remoteTips.Select(r => r.CanonicalName)));
var refs = remoteTips.Where(r => r.TargetIdentifier == headTipSha).ToList();

switch (refs.Count)
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.LibGit2Sharp/GitVersion.LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\GitVersion.Core\GitVersion.Core.csproj" />
<ProjectReference Include="..\GitVersion.Abstractions\GitVersion.Abstractions.csproj"/>
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.Output/GitVersion.Output.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\GitVersion.Core\GitVersion.Core.csproj" />
<ProjectReference Include="..\GitVersion.Abstractions\GitVersion.Abstractions.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Remove="*\AddFormats\**\*.*" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void Execute(GitVersionVariables variables, GitVersionInfoContext context
var indentation = GetIndentation(fileExtension);

var lines = variables.OrderBy(x => x.Key).Select(v => string.Format(indentation + addFormat, v.Key, v.Value));
var members = string.Join(System.Environment.NewLine, lines);
var members = string.Join(Environment.NewLine, lines);

var fileContents = string.Format(template, members);

Expand Down

0 comments on commit 07ba041

Please sign in to comment.