Skip to content

Commit 2ff757f

Browse files
GitVersion now writes results to console when a compatible build server is found so the build server can also pick up the version without using it as msbuild task
1 parent f3bde9c commit 2ff757f

File tree

9 files changed

+89
-18
lines changed

9 files changed

+89
-18
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace GitVersion
2+
{
3+
using System;
4+
5+
public abstract class BuildServerBase : IBuildServer
6+
{
7+
public abstract bool CanApplyToCurrentContext();
8+
public abstract void PerformPreProcessingSteps(string gitDirectory);
9+
public abstract string GenerateSetVersionMessage(string versionToUseForBuildNumber);
10+
public abstract string[] GenerateSetParameterMessage(string name, string value);
11+
12+
public virtual void WriteIntegration(VersionAndBranch versionAndBranch, Action<string> writer)
13+
{
14+
if (versionAndBranch == null)
15+
{
16+
return;
17+
}
18+
19+
if (writer == null)
20+
{
21+
return;
22+
}
23+
24+
writer(string.Format("Executing GenerateSetVersionMessage for '{0}'.", GetType().Name));
25+
writer(GenerateSetVersionMessage(versionAndBranch.GenerateSemVer()));
26+
writer(string.Format("Executing GenerateBuildLogOutput for '{0}'.", GetType().Name));
27+
foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(versionAndBranch, this))
28+
{
29+
writer(buildParameter);
30+
}
31+
}
32+
}
33+
}

GitVersion/BuildServers/BuildServerList.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,25 @@ static IEnumerable<IBuildServer> DefaultSelector(Arguments arguments)
3030
};
3131
}
3232

33+
var buildServices = new List<IBuildServer>();
34+
3335
foreach (var buildServer in BuildServers)
3436
{
35-
if (buildServer.CanApplyToCurrentContext())
37+
try
38+
{
39+
if (buildServer.CanApplyToCurrentContext())
40+
{
41+
Logger.WriteInfo(string.Format("Applicable build agent found: '{0}'.", buildServer.GetType().Name));
42+
buildServices.Add(buildServer);
43+
}
44+
}
45+
catch (Exception ex)
3646
{
37-
Logger.WriteInfo(string.Format("Applicable build agent found: '{0}'.", buildServer.GetType().Name));
38-
yield return buildServer;
47+
Logger.WriteWarning(string.Format("Failed to check build server '{0}': {1}", buildServer.GetType().Name, ex.Message));
3948
}
4049
}
50+
51+
return buildServices;
4152
}
4253
}
4354
}

GitVersion/BuildServers/ContinuaCi.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
using Microsoft.Win32;
44

5-
public class ContinuaCi : IBuildServer
5+
public class ContinuaCi : BuildServerBase
66
{
77
readonly Arguments _arguments;
88

@@ -11,15 +11,24 @@ public ContinuaCi(Arguments arguments)
1111
_arguments = arguments;
1212
}
1313

14-
public bool CanApplyToCurrentContext()
14+
public override bool CanApplyToCurrentContext()
1515
{
16-
using (var registryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\VSoft Technologies\\Continua CI Agent"))
16+
const string KeyName = @"Software\VSoft Technologies\Continua CI Agent";
17+
18+
if (RegistryKeyExists(KeyName, RegistryView.Registry32))
1719
{
18-
return registryKey != null;
20+
return true;
1921
}
22+
23+
if (RegistryKeyExists(KeyName, RegistryView.Registry64))
24+
{
25+
return true;
26+
}
27+
28+
return false;
2029
}
2130

22-
public void PerformPreProcessingSteps(string gitDirectory)
31+
public override void PerformPreProcessingSteps(string gitDirectory)
2332
{
2433
if (string.IsNullOrEmpty(gitDirectory))
2534
{
@@ -29,18 +38,25 @@ public void PerformPreProcessingSteps(string gitDirectory)
2938
GitHelper.NormalizeGitDirectory(gitDirectory, _arguments);
3039
}
3140

32-
public string[] GenerateSetParameterMessage(string name, string value)
41+
public override string[] GenerateSetParameterMessage(string name, string value)
3342
{
34-
return new []
43+
return new[]
3544
{
3645
string.Format("@@continua[setVariable name='GitVersion.{0}' value='{1}']", name, value)
3746
};
3847
}
3948

40-
public string GenerateSetVersionMessage(string versionToUseForBuildNumber)
49+
public override string GenerateSetVersionMessage(string versionToUseForBuildNumber)
4150
{
4251
return string.Format("@@continua[setBuildVersion value='{0}']", versionToUseForBuildNumber);
4352
}
44-
}
4553

54+
private static bool RegistryKeyExists(string keyName, RegistryView registryView)
55+
{
56+
var localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView);
57+
localKey = localKey.OpenSubKey(keyName);
58+
59+
return localKey != null;
60+
}
61+
}
4662
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
namespace GitVersion
22
{
3+
using System;
4+
35
public interface IBuildServer
46
{
57
bool CanApplyToCurrentContext();
68
void PerformPreProcessingSteps(string gitDirectory);
79
string GenerateSetVersionMessage(string versionToUseForBuildNumber);
810
string[] GenerateSetParameterMessage(string name, string value);
11+
12+
void WriteIntegration(VersionAndBranch versionAndBranch, Action<string> writer);
913
}
14+
1015
}

GitVersion/BuildServers/TeamCity.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
using System;
44

5-
public class TeamCity : IBuildServer
5+
public class TeamCity : BuildServerBase
66
{
77
readonly Arguments _arguments;
88

@@ -12,12 +12,12 @@ public TeamCity(Arguments arguments)
1212
}
1313

1414

15-
public bool CanApplyToCurrentContext()
15+
public override bool CanApplyToCurrentContext()
1616
{
1717
return !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TEAMCITY_VERSION"));
1818
}
1919

20-
public void PerformPreProcessingSteps(string gitDirectory)
20+
public override void PerformPreProcessingSteps(string gitDirectory)
2121
{
2222
if (string.IsNullOrEmpty(gitDirectory))
2323
{
@@ -27,7 +27,7 @@ public void PerformPreProcessingSteps(string gitDirectory)
2727
GitHelper.NormalizeGitDirectory(gitDirectory, _arguments);
2828
}
2929

30-
public string[] GenerateSetParameterMessage(string name, string value)
30+
public override string[] GenerateSetParameterMessage(string name, string value)
3131
{
3232
return new[]
3333
{
@@ -37,7 +37,7 @@ public string[] GenerateSetParameterMessage(string name, string value)
3737
};
3838
}
3939

40-
public string GenerateSetVersionMessage(string versionToUseForBuildNumber)
40+
public override string GenerateSetVersionMessage(string versionToUseForBuildNumber)
4141
{
4242
return string.Format("##teamcity[buildNumber '{0}']", EscapeValue(versionToUseForBuildNumber));
4343
}

GitVersion/GitVersion.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<ItemGroup>
5555
<Compile Include="ArgumentParser.cs" />
5656
<Compile Include="Arguments.cs" />
57+
<Compile Include="BuildServers\BuildServerBase.cs" />
5758
<Compile Include="GitFlow\GitFlowVersionFinder.cs" />
5859
<Compile Include="GitFlow\BranchClassifier.cs" />
5960
<Compile Include="GitFlow\BranchFinders\DevelopBasedVersionFinderBase.cs" />

GitVersion/Program.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ static void Main()
4040

4141
var versionAndBranch = VersionCache.GetVersion(gitDirectory);
4242

43+
foreach (var buildServer in applicableBuildServers)
44+
{
45+
buildServer.WriteIntegration(versionAndBranch, Logger.WriteInfo);
46+
}
47+
4348
var versionAsKeyValue = versionAndBranch.ToKeyValue();
4449
switch (arguments.VersionPart)
4550
{

GitVersion/VersionCache.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public static VersionAndBranchAndDate GetVersion(string gitDirectory)
1616
{
1717
throw new ErrorException("No Tip found. Has repo been initialize?");
1818
}
19+
1920
var ticks = DirectoryDateFinder.GetLastDirectoryWrite(gitDirectory);
2021
var key = string.Format("{0}:{1}:{2}", repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks);
2122
CachedVersion cachedVersion;

GitVersionTask/WriteVersionInfoToBuildLog.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,5 @@ public void WriteIntegrationParameters(VersionAndBranch versionAndBranch, IEnume
7070
}
7171
}
7272
}
73-
7473
}
7574
}

0 commit comments

Comments
 (0)