Skip to content

Commit f2daf60

Browse files
author
Jake Ginnivan
committed
Introducing variable provider concept, decoupling version generation from output a bit
1 parent c30a2be commit f2daf60

16 files changed

+125
-110
lines changed

GitFlowVersion/BuildServers/BuildOutputGenerator.cs

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace GitFlowVersion
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
6+
public class GitFlowVariableProvider
7+
{
8+
public static string SemVer = "SemVer";
9+
public static string LongVersion = "LongVersion";
10+
public static string NugetVersion = "NugetVersion";
11+
public static string Major = "Major";
12+
public static string Minor = "Minor";
13+
public static string Patch = "Patch";
14+
15+
public Dictionary<string, string> GetVariables(VersionAndBranch versionAndBranch)
16+
{
17+
var variables = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
18+
{
19+
{Major, versionAndBranch.Version.Major.ToString()},
20+
{Minor, versionAndBranch.Version.Minor.ToString()},
21+
{Patch, versionAndBranch.Version.Patch.ToString()},
22+
{"Suffix", versionAndBranch.Version.Suffix.JsonEncode()},
23+
{LongVersion, versionAndBranch.ToLongString().JsonEncode()},
24+
{NugetVersion, versionAndBranch.GenerateNugetVersion().JsonEncode()},
25+
{"ShortVersion", versionAndBranch.ToShortString().JsonEncode()},
26+
{"BranchName", versionAndBranch.BranchName.JsonEncode()},
27+
{"BranchType", versionAndBranch.BranchType == null ? null : versionAndBranch.BranchType.ToString()},
28+
{"Sha", versionAndBranch.Sha},
29+
{SemVer, versionAndBranch.GenerateSemVer()}
30+
};
31+
32+
var releaseInformation = ReleaseInformationCalculator.Calculate(versionAndBranch.BranchType, versionAndBranch.Version.Tag);
33+
if (releaseInformation.ReleaseNumber.HasValue)
34+
{
35+
variables.Add("PreReleasePartOne", releaseInformation.ReleaseNumber.ToString());
36+
}
37+
if (versionAndBranch.Version.PreReleasePartTwo != null)
38+
{
39+
variables.Add("PreReleasePartTwo", versionAndBranch.Version.PreReleasePartTwo.ToString());
40+
}
41+
if (releaseInformation.Stability.HasValue)
42+
{
43+
variables.Add("Stability", releaseInformation.Stability.ToString());
44+
}
45+
return variables;
46+
}
47+
}
48+
}
File renamed without changes.
File renamed without changes.

GitFlowVersion/GitFlowVersion.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,19 @@
5757
<Compile Include="BranchFinders\FeatureVersionFinder.cs" />
5858
<Compile Include="BranchFinders\DevelopVersionFinder.cs" />
5959
<Compile Include="BranchFinders\HotfixVersionFinder.cs" />
60-
<Compile Include="BuildServers\BuildOutputGenerator.cs" />
60+
<Compile Include="OutputFormatters\BuildOutputFormatter.cs" />
6161
<Compile Include="BuildServers\BuildServerList.cs" />
6262
<Compile Include="BuildServers\ContinuaCi.cs" />
6363
<Compile Include="BuildServers\GitHelper.cs" />
6464
<Compile Include="BuildServers\IBuildServer.cs" />
6565
<Compile Include="BuildServers\TeamCity.cs" />
66+
<Compile Include="GitFlow\GitFlowVariableProvider.cs" />
6667
<Compile Include="GitVersionContext.cs" />
6768
<Compile Include="HelpWriter.cs" />
6869
<Compile Include="GitFlow\ReleaseInformation.cs" />
6970
<Compile Include="GitFlow\ReleaseInformationCalculator.cs" />
7071
<Compile Include="SemanticVersionTag.cs" />
71-
<Compile Include="VersionBuilders\JsonVersionBuilder.cs" />
72+
<Compile Include="OutputFormatters\JsonOutputFormatter.cs" />
7273
<Compile Include="VersionBuilders\NugetVersionBuilder.cs" />
7374
<Compile Include="ExtensionMethods.cs" />
7475
<Compile Include="ShortVersionParser.cs" />
@@ -93,8 +94,8 @@
9394
<Compile Include="SemanticVersionParser.cs" />
9495
<Compile Include="VersionBuilders\SemVerVersionBuilder.cs" />
9596
<Compile Include="SemanticVersion.cs" />
96-
<Compile Include="Stability.cs" />
97-
<Compile Include="VersionAndBranch.cs" />
97+
<Compile Include="GitFlow\Stability.cs" />
98+
<Compile Include="GitFlow\VersionAndBranch.cs" />
9899
<Compile Include="VersionCache.cs" />
99100
<Compile Include="VersionForDirectoryFinder.cs" />
100101
<Compile Include="VersionPoint.cs" />
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace GitFlowVersion
2+
{
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
public static class BuildOutputFormatter
7+
{
8+
public static IEnumerable<string> GenerateBuildLogOutput(Dictionary<string, string> variables, IBuildServer buildServer)
9+
{
10+
return variables.Select(variable => buildServer.GenerateSetParameterMessage(variable.Key, variable.Value));
11+
}
12+
}
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace GitFlowVersion
2+
{
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
public static class JsonOutputFormatter
8+
{
9+
public static string ToJson(Dictionary<string, string> variables)
10+
{
11+
var builder = new StringBuilder();
12+
builder.AppendLine("{");
13+
var last = variables.Last().Key;
14+
foreach (var variable in variables)
15+
{
16+
var isLast = (variable.Key == last);
17+
int value;
18+
if (int.TryParse(variable.Value, out value))
19+
builder.AppendLineFormat(" \"{0}\":{1}{2}", variable.Key, value, isLast ? string.Empty : ",");
20+
else
21+
builder.AppendLineFormat(" \"{0}\":\"{1}\"{2}", variable.Key, variable.Value, isLast ? string.Empty : ",");
22+
}
23+
24+
builder.Append("}");
25+
return builder.ToString();
26+
}
27+
}
28+
}

GitFlowVersion/Program.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,15 @@ static void Main()
2727
Environment.Exit(1);
2828
}
2929

30-
var versionAndBranch = VersionCache.GetVersion(gitDirectory);
30+
var variables = VersionCache.GetVersion(gitDirectory);
3131

3232
switch (arguments.VersionPart)
3333
{
3434
case null:
35-
Console.WriteLine(versionAndBranch.ToJson());
35+
Console.WriteLine(JsonOutputFormatter.ToJson(variables));
3636
break;
37-
case "nuget":
38-
Console.WriteLine(versionAndBranch.GenerateNugetVersion());
39-
break;
40-
case "major":
41-
Console.WriteLine(versionAndBranch.Version.Major);
42-
break;
43-
case "minor":
44-
Console.WriteLine(versionAndBranch.Version.Minor);
45-
break;
46-
case "patch":
47-
Console.WriteLine(versionAndBranch.Version.Patch);
48-
break;
49-
case "long":
50-
Console.WriteLine(versionAndBranch.ToLongString());
51-
break;
52-
case "short":
53-
Console.WriteLine(versionAndBranch.ToShortString());
37+
default:
38+
Console.WriteLine(variables[arguments.VersionPart]);
5439
break;
5540
}
5641

GitFlowVersion/VersionBuilders/JsonVersionBuilder.cs

Lines changed: 0 additions & 38 deletions
This file was deleted.

GitFlowVersion/VersionBuilders/SemVerVersionBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace GitFlowVersion.VersionBuilders
1+
namespace GitFlowVersion
22
{
33
using System;
44

0 commit comments

Comments
 (0)