Skip to content

Commit 0b726f1

Browse files
committed
replaced BuildServerList with BuildServerResolver
1 parent 24486e4 commit 0b726f1

File tree

11 files changed

+98
-96
lines changed

11 files changed

+98
-96
lines changed

src/GitVersionCore.Tests/DynamicRepositoryTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO;
22
using GitVersion;
3+
using GitVersion.Common;
34
using GitVersion.Configuration;
45
using NUnit.Framework;
56
using GitVersion.Logging;
@@ -76,11 +77,10 @@ public void FindsVersionInDynamicRepo(string name, string url, string targetBran
7677
Directory.CreateDirectory(workingDirectory);
7778

7879
var testFileSystem = new TestFileSystem();
79-
var testEnvironment = new TestEnvironment();
8080
var log = new NullLog();
8181
var configFileLocator = new DefaultConfigFileLocator(testFileSystem, log);
8282

83-
var executeCore = new ExecuteCore(testFileSystem, testEnvironment, log, configFileLocator);
83+
var executeCore = new ExecuteCore(testFileSystem, log, configFileLocator, new BuildServerResolver(null, log));
8484

8585
var versionVariables = executeCore.ExecuteGitVersion(arguments);
8686

src/GitVersionCore.Tests/ExecuteCoreTests.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ExecuteCoreTests : TestBase
2525
private IEnvironment environment;
2626
private ILog log;
2727
private IConfigFileLocator configFileLocator;
28+
private IBuildServerResolver buildServerResolver;
2829

2930
[SetUp]
3031
public void SetUp()
@@ -33,12 +34,13 @@ public void SetUp()
3334
environment = new TestEnvironment();
3435
log = new NullLog();
3536
configFileLocator = new DefaultConfigFileLocator(fileSystem, log);
37+
buildServerResolver = new BuildServerResolver(null, log);
3638
}
3739

3840
[Test]
3941
public void CacheKeySameAfterReNormalizing()
4042
{
41-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
43+
var versionAndBranchFinder = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
4244

4345
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
4446
{
@@ -60,7 +62,7 @@ public void CacheKeySameAfterReNormalizing()
6062
[Description("LibGit2Sharp fails here when running under Mono")]
6163
public void CacheKeyForWorktree()
6264
{
63-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
65+
var versionAndBranchFinder = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
6466

6567
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
6668
{
@@ -126,7 +128,7 @@ public void CacheFileExistsOnDisk()
126128
var logAppender = new TestLogAppender(Action);
127129
log = new Log(logAppender);
128130

129-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
131+
var versionAndBranchFinder = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
130132

131133
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
132134
{
@@ -175,7 +177,7 @@ public void CacheFileExistsOnDiskWhenOverrideConfigIsSpecifiedVersionShouldBeDyn
175177
CommitDate: 2015-11-10
176178
";
177179

178-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
180+
var versionAndBranchFinder = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
179181

180182
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
181183
{
@@ -206,7 +208,7 @@ public void CacheFileIsMissing()
206208
var logAppender = new TestLogAppender(Action);
207209
log = new Log(logAppender);
208210

209-
var executeCore = new ExecuteCore(fileSystem, environment, log, configFileLocator);
211+
var executeCore = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
210212

211213
RepositoryScope(executeCore);
212214
var logsMessages = stringBuilder.ToString();
@@ -250,7 +252,7 @@ public void ConfigChangeInvalidatesCache()
250252
CommitDate: 2015-11-10
251253
";
252254

253-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
255+
var versionAndBranchFinder = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
254256

255257
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
256258
{
@@ -303,7 +305,7 @@ public void NoCacheBypassesCache()
303305
CommitDate: 2015-11-10
304306
";
305307

306-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
308+
var versionAndBranchFinder = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
307309

308310
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
309311
{
@@ -323,7 +325,7 @@ public void NoCacheBypassesCache()
323325
[Test]
324326
public void WorkingDirectoryWithoutGit()
325327
{
326-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
328+
var versionAndBranchFinder = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
327329

328330
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
329331
{
@@ -339,7 +341,7 @@ public void WorkingDirectoryWithoutGit()
339341
[Description("LibGit2Sharp fails when running under Mono")]
340342
public void GetProjectRootDirectory_WorkingDirectoryWithWorktree()
341343
{
342-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
344+
var versionAndBranchFinder = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
343345

344346
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
345347
{
@@ -364,7 +366,7 @@ public void GetProjectRootDirectory_WorkingDirectoryWithWorktree()
364366
[Test]
365367
public void GetProjectRootDirectory_NoWorktree()
366368
{
367-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
369+
var versionAndBranchFinder = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
368370

369371
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
370372
{
@@ -378,7 +380,7 @@ public void GetProjectRootDirectory_NoWorktree()
378380
[Test]
379381
public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
380382
{
381-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
383+
var versionAndBranchFinder = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
382384

383385
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
384386
{
@@ -396,7 +398,7 @@ public void DynamicRepositoriesShouldNotErrorWithFailedToFindGitDirectory()
396398
[Test]
397399
public void GetDotGitDirectory_NoWorktree()
398400
{
399-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
401+
var versionAndBranchFinder = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
400402

401403
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
402404
{
@@ -412,7 +414,7 @@ public void GetDotGitDirectory_NoWorktree()
412414
[Description("LibGit2Sharp fails when running under Mono")]
413415
public void GetDotGitDirectory_Worktree()
414416
{
415-
var versionAndBranchFinder = new ExecuteCore(fileSystem, environment, log, configFileLocator);
417+
var versionAndBranchFinder = new ExecuteCore(fileSystem, log, configFileLocator, buildServerResolver);
416418

417419
RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
418420
{

src/GitVersionCore/BuildServers/BuildServerList.cs

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using GitVersion.Logging;
4+
5+
namespace GitVersion.Common
6+
{
7+
public class BuildServerResolver : IBuildServerResolver
8+
{
9+
private readonly IEnumerable<IBuildServer> buildServers;
10+
private readonly ILog log;
11+
public BuildServerResolver(IEnumerable<IBuildServer> buildServers, ILog log)
12+
{
13+
this.log = log;
14+
this.buildServers = buildServers ?? Array.Empty<IBuildServer>();
15+
}
16+
17+
public IBuildServer GetCurrentBuildServer()
18+
{
19+
IBuildServer instance = null;
20+
foreach (var buildServer in buildServers)
21+
{
22+
try
23+
{
24+
if (buildServer.CanApplyToCurrentContext())
25+
{
26+
log.Info($"Applicable build agent found: '{buildServer.GetType().Name}'.");
27+
instance = buildServer;
28+
}
29+
}
30+
catch (Exception ex)
31+
{
32+
log.Warning($"Failed to check build server '{buildServer.GetType().Name}': {ex.Message}");
33+
}
34+
}
35+
36+
return instance;
37+
}
38+
}
39+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace GitVersion.Common
2+
{
3+
public interface IBuildServerResolver
4+
{
5+
IBuildServer GetCurrentBuildServer();
6+
}
7+
}

src/GitVersionCore/CoreModule.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.BuildServers;
12
using Microsoft.Extensions.DependencyInjection;
23
using GitVersion.Common;
34
using GitVersion.Logging;
@@ -13,6 +14,20 @@ public void RegisterTypes(IServiceCollection services)
1314
services.AddSingleton<ILog, Log>();
1415

1516
services.AddSingleton<IExecuteCore, ExecuteCore>();
17+
18+
services.AddSingleton<IBuildServer, ContinuaCi>();
19+
services.AddSingleton<IBuildServer, TeamCity>();
20+
services.AddSingleton<IBuildServer, AppVeyor>();
21+
services.AddSingleton<IBuildServer, MyGet>();
22+
services.AddSingleton<IBuildServer, Jenkins>();
23+
services.AddSingleton<IBuildServer, GitLabCi>();
24+
services.AddSingleton<IBuildServer, AzurePipelines>();
25+
services.AddSingleton<IBuildServer, TravisCI>();
26+
services.AddSingleton<IBuildServer, EnvRun>();
27+
services.AddSingleton<IBuildServer, Drone>();
28+
services.AddSingleton<IBuildServer, CodeBuild>();
29+
30+
services.AddSingleton<IBuildServerResolver, BuildServerResolver>();
1631
}
1732
}
1833
}

src/GitVersionCore/ExecuteCore.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Linq;
3-
using GitVersion.BuildServers;
42
using GitVersion.Configuration;
53
using GitVersion.OutputVariables;
64
using GitVersion.Cache;
@@ -13,17 +11,17 @@ namespace GitVersion
1311
public class ExecuteCore : IExecuteCore
1412
{
1513
private readonly IFileSystem fileSystem;
16-
private readonly IEnvironment environment;
1714
private readonly ILog log;
1815
private readonly IConfigFileLocator configFileLocator;
16+
private readonly IBuildServerResolver buildServerResolver;
1917
private readonly GitVersionCache gitVersionCache;
2018

21-
public ExecuteCore(IFileSystem fileSystem, IEnvironment environment, ILog log, IConfigFileLocator configFileLocator)
19+
public ExecuteCore(IFileSystem fileSystem, ILog log, IConfigFileLocator configFileLocator, IBuildServerResolver buildServerResolver)
2220
{
2321
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
24-
this.environment = environment;
2522
this.log = log;
2623
this.configFileLocator = configFileLocator;
24+
this.buildServerResolver = buildServerResolver;
2725
gitVersionCache = new GitVersionCache(fileSystem, log);
2826
}
2927

@@ -52,11 +50,8 @@ public bool TryGetVersion(string directory, out VersionVariables versionVariable
5250

5351
private VersionVariables ExecuteGitVersion(string targetUrl, string dynamicRepositoryLocation, Authentication authentication, string targetBranch, bool noFetch, string workingDirectory, string commitId, Config overrideConfig = null, bool noCache = false, bool noNormalize = false)
5452
{
55-
BuildServerList.Init(environment, log);
56-
5753
// Normalize if we are running on build server
58-
var applicableBuildServers = BuildServerList.GetApplicableBuildServers(log);
59-
var buildServer = applicableBuildServers.FirstOrDefault();
54+
var buildServer = buildServerResolver.GetCurrentBuildServer();
6055
var normalizeGitDirectory = !noNormalize && buildServer != null;
6156
var fetch = noFetch || buildServer != null && buildServer.PreventFetch();
6257
var shouldCleanUpRemotes = buildServer != null && buildServer.ShouldCleanUpRemotes();

src/GitVersionCore/GitVersionCore.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<Description>Derives SemVer information from a repository following GitFlow or GitHubFlow. This is the Core library which both GitVersion cli and Task use allowing programatic usage of GitVersion.</Description>
1313

1414
<Product>$(AssemblyName)</Product>
15+
16+
<LangVersion>8</LangVersion>
1517
</PropertyGroup>
1618

1719
<ItemGroup>

src/GitVersionExe/ExecCommand.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Runtime.InteropServices;
6-
using GitVersion.BuildServers;
76
using GitVersion.Exceptions;
87
using GitVersion.OutputFormatters;
98
using GitVersion.OutputVariables;
@@ -20,16 +19,16 @@ public class ExecCommand : IExecCommand
2019
private static readonly bool runningOnUnix = !RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
2120

2221
private readonly IFileSystem fileSystem;
23-
private readonly IEnvironment environment;
22+
private readonly IBuildServerResolver buildServerResolver;
2423
private readonly ILog log;
2524
private readonly IExecuteCore executeCore;
2625
private readonly Arguments arguments;
2726
public static readonly string BuildTool = GetMsBuildToolPath();
2827

29-
public ExecCommand(IFileSystem fileSystem, IEnvironment environment, ILog log, IExecuteCore executeCore, IOptions<Arguments> arguments)
28+
public ExecCommand(IFileSystem fileSystem, IBuildServerResolver buildServerResolver, ILog log, IExecuteCore executeCore, IOptions<Arguments> arguments)
3029
{
3130
this.fileSystem = fileSystem;
32-
this.environment = environment;
31+
this.buildServerResolver = buildServerResolver;
3332
this.log = log;
3433
this.executeCore = executeCore;
3534
this.arguments = arguments.Value;
@@ -45,11 +44,8 @@ public void Execute()
4544
{
4645
case OutputType.BuildServer:
4746
{
48-
BuildServerList.Init(environment, log);
49-
foreach (var buildServer in BuildServerList.GetApplicableBuildServers(log))
50-
{
51-
buildServer.WriteIntegration(Console.WriteLine, variables);
52-
}
47+
var buildServer = buildServerResolver.GetCurrentBuildServer();
48+
buildServer?.WriteIntegration(Console.WriteLine, variables);
5349

5450
break;
5551
}

src/GitVersionTask.Tests/GitVersionTaskDirectoryTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.IO;
33
using GitVersion;
4+
using GitVersion.Common;
45
using GitVersion.Configuration;
56
using GitVersion.Logging;
67
using LibGit2Sharp;
@@ -24,11 +25,10 @@ public void CreateTemporaryRepository()
2425
.TrimEnd(Path.DirectorySeparatorChar);
2526

2627
var testFileSystem = new TestFileSystem();
27-
var testEnvironment = new TestEnvironment();
2828
var log = new NullLog();
2929
var configFileLocator = new DefaultConfigFileLocator(testFileSystem, log);
3030

31-
executeCore = new ExecuteCore(testFileSystem, testEnvironment, log, configFileLocator);
31+
executeCore = new ExecuteCore(testFileSystem, log, configFileLocator, new BuildServerResolver(null, log));
3232
Assert.NotNull(gitDirectory);
3333
}
3434

0 commit comments

Comments
 (0)