Skip to content

Commit

Permalink
Introducing variable provider concept, decoupling version generation …
Browse files Browse the repository at this point in the history
…from output a bit
  • Loading branch information
Jake Ginnivan committed Jan 30, 2014
1 parent c30a2be commit f2daf60
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 110 deletions.
21 changes: 0 additions & 21 deletions GitFlowVersion/BuildServers/BuildOutputGenerator.cs

This file was deleted.

48 changes: 48 additions & 0 deletions GitFlowVersion/GitFlow/GitFlowVariableProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
namespace GitFlowVersion
{
using System;
using System.Collections.Generic;

public class GitFlowVariableProvider
{
public static string SemVer = "SemVer";
public static string LongVersion = "LongVersion";
public static string NugetVersion = "NugetVersion";
public static string Major = "Major";
public static string Minor = "Minor";
public static string Patch = "Patch";

public Dictionary<string, string> GetVariables(VersionAndBranch versionAndBranch)
{
var variables = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase)
{
{Major, versionAndBranch.Version.Major.ToString()},
{Minor, versionAndBranch.Version.Minor.ToString()},
{Patch, versionAndBranch.Version.Patch.ToString()},
{"Suffix", versionAndBranch.Version.Suffix.JsonEncode()},
{LongVersion, versionAndBranch.ToLongString().JsonEncode()},
{NugetVersion, versionAndBranch.GenerateNugetVersion().JsonEncode()},
{"ShortVersion", versionAndBranch.ToShortString().JsonEncode()},
{"BranchName", versionAndBranch.BranchName.JsonEncode()},
{"BranchType", versionAndBranch.BranchType == null ? null : versionAndBranch.BranchType.ToString()},
{"Sha", versionAndBranch.Sha},
{SemVer, versionAndBranch.GenerateSemVer()}
};

var releaseInformation = ReleaseInformationCalculator.Calculate(versionAndBranch.BranchType, versionAndBranch.Version.Tag);
if (releaseInformation.ReleaseNumber.HasValue)
{
variables.Add("PreReleasePartOne", releaseInformation.ReleaseNumber.ToString());
}
if (versionAndBranch.Version.PreReleasePartTwo != null)
{
variables.Add("PreReleasePartTwo", versionAndBranch.Version.PreReleasePartTwo.ToString());
}
if (releaseInformation.Stability.HasValue)
{
variables.Add("Stability", releaseInformation.Stability.ToString());
}
return variables;
}
}
}
File renamed without changes.
File renamed without changes.
9 changes: 5 additions & 4 deletions GitFlowVersion/GitFlowVersion.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,19 @@
<Compile Include="BranchFinders\FeatureVersionFinder.cs" />
<Compile Include="BranchFinders\DevelopVersionFinder.cs" />
<Compile Include="BranchFinders\HotfixVersionFinder.cs" />
<Compile Include="BuildServers\BuildOutputGenerator.cs" />
<Compile Include="OutputFormatters\BuildOutputFormatter.cs" />
<Compile Include="BuildServers\BuildServerList.cs" />
<Compile Include="BuildServers\ContinuaCi.cs" />
<Compile Include="BuildServers\GitHelper.cs" />
<Compile Include="BuildServers\IBuildServer.cs" />
<Compile Include="BuildServers\TeamCity.cs" />
<Compile Include="GitFlow\GitFlowVariableProvider.cs" />
<Compile Include="GitVersionContext.cs" />
<Compile Include="HelpWriter.cs" />
<Compile Include="GitFlow\ReleaseInformation.cs" />
<Compile Include="GitFlow\ReleaseInformationCalculator.cs" />
<Compile Include="SemanticVersionTag.cs" />
<Compile Include="VersionBuilders\JsonVersionBuilder.cs" />
<Compile Include="OutputFormatters\JsonOutputFormatter.cs" />
<Compile Include="VersionBuilders\NugetVersionBuilder.cs" />
<Compile Include="ExtensionMethods.cs" />
<Compile Include="ShortVersionParser.cs" />
Expand All @@ -93,8 +94,8 @@
<Compile Include="SemanticVersionParser.cs" />
<Compile Include="VersionBuilders\SemVerVersionBuilder.cs" />
<Compile Include="SemanticVersion.cs" />
<Compile Include="Stability.cs" />
<Compile Include="VersionAndBranch.cs" />
<Compile Include="GitFlow\Stability.cs" />
<Compile Include="GitFlow\VersionAndBranch.cs" />
<Compile Include="VersionCache.cs" />
<Compile Include="VersionForDirectoryFinder.cs" />
<Compile Include="VersionPoint.cs" />
Expand Down
13 changes: 13 additions & 0 deletions GitFlowVersion/OutputFormatters/BuildOutputFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace GitFlowVersion
{
using System.Collections.Generic;
using System.Linq;

public static class BuildOutputFormatter
{
public static IEnumerable<string> GenerateBuildLogOutput(Dictionary<string, string> variables, IBuildServer buildServer)
{
return variables.Select(variable => buildServer.GenerateSetParameterMessage(variable.Key, variable.Value));
}
}
}
28 changes: 28 additions & 0 deletions GitFlowVersion/OutputFormatters/JsonOutputFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace GitFlowVersion
{
using System.Collections.Generic;
using System.Linq;
using System.Text;

public static class JsonOutputFormatter
{
public static string ToJson(Dictionary<string, string> variables)
{
var builder = new StringBuilder();
builder.AppendLine("{");
var last = variables.Last().Key;
foreach (var variable in variables)
{
var isLast = (variable.Key == last);
int value;
if (int.TryParse(variable.Value, out value))
builder.AppendLineFormat(" \"{0}\":{1}{2}", variable.Key, value, isLast ? string.Empty : ",");
else
builder.AppendLineFormat(" \"{0}\":\"{1}\"{2}", variable.Key, variable.Value, isLast ? string.Empty : ",");
}

builder.Append("}");
return builder.ToString();
}
}
}
23 changes: 4 additions & 19 deletions GitFlowVersion/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,15 @@ static void Main()
Environment.Exit(1);
}

var versionAndBranch = VersionCache.GetVersion(gitDirectory);
var variables = VersionCache.GetVersion(gitDirectory);

switch (arguments.VersionPart)
{
case null:
Console.WriteLine(versionAndBranch.ToJson());
Console.WriteLine(JsonOutputFormatter.ToJson(variables));
break;
case "nuget":
Console.WriteLine(versionAndBranch.GenerateNugetVersion());
break;
case "major":
Console.WriteLine(versionAndBranch.Version.Major);
break;
case "minor":
Console.WriteLine(versionAndBranch.Version.Minor);
break;
case "patch":
Console.WriteLine(versionAndBranch.Version.Patch);
break;
case "long":
Console.WriteLine(versionAndBranch.ToLongString());
break;
case "short":
Console.WriteLine(versionAndBranch.ToShortString());
default:
Console.WriteLine(variables[arguments.VersionPart]);
break;
}

Expand Down
38 changes: 0 additions & 38 deletions GitFlowVersion/VersionBuilders/JsonVersionBuilder.cs

This file was deleted.

2 changes: 1 addition & 1 deletion GitFlowVersion/VersionBuilders/SemVerVersionBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace GitFlowVersion.VersionBuilders
namespace GitFlowVersion
{
using System;

Expand Down
5 changes: 3 additions & 2 deletions GitFlowVersion/VersionCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class VersionCache
{
static Dictionary<string, CachedVersion> versionCacheVersions = new Dictionary<string, CachedVersion>();

public static VersionAndBranch GetVersion(string gitDirectory)
public static Dictionary<string, string> GetVersion(string gitDirectory)
{
using (var repo = RepositoryLoader.GetRepo(gitDirectory))
{
Expand Down Expand Up @@ -44,7 +44,8 @@ public static VersionAndBranch GetVersion(string gitDirectory)
Timestamp = ticks
};
}
return versionAndBranch;

return new GitFlowVariableProvider().GetVariables(versionAndBranch);
}
}

Expand Down
19 changes: 8 additions & 11 deletions GitFlowVersionTask/AssemblyInfoBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
namespace GitFlowVersionTask
{
using System.Collections.Generic;
using GitFlowVersion;
using GitFlowVersion.VersionBuilders;

public class AssemblyInfoBuilder
public class AssemblyInfoBuilder
{

public VersionAndBranch VersionAndBranch;
public Dictionary<string, string> Variables;
public bool SignAssembly;
public string AssemblyName;

Expand Down Expand Up @@ -45,28 +45,25 @@ static class GitFlowVersionInformation
}}
}}
", GetAssemblyVersion(), GetAssemblyFileVersion(), VersionAndBranch.ToLongString(), VersionAndBranch.GenerateNugetVersion(), AssemblyName, VersionAndBranch.GenerateSemVer());
", GetAssemblyVersion(), GetAssemblyFileVersion(), Variables[GitFlowVariableProvider.LongVersion], Variables[GitFlowVariableProvider.NugetVersion], AssemblyName, Variables[GitFlowVariableProvider.SemVer]);

return assemblyInfo;
}

string GetAssemblyVersion()
{
var semanticVersion = VersionAndBranch.Version;
if (SignAssembly)
{
// for strong named we don't want to include the patch to avoid binding redirect issues
return string.Format("{0}.{1}.0", semanticVersion.Major, semanticVersion.Minor);
return string.Format("{0}.{1}.0", Variables[GitFlowVariableProvider.Major], Variables[GitFlowVariableProvider.Minor]);
}
// for non strong named we want to include the patch
return string.Format("{0}.{1}.{2}", semanticVersion.Major, semanticVersion.Minor, semanticVersion.Patch);
return string.Format("{0}.{1}.{2}", Variables[GitFlowVariableProvider.Major], Variables[GitFlowVariableProvider.Minor], Variables[GitFlowVariableProvider.Patch]);
}

string GetAssemblyFileVersion()
{
var semanticVersion = VersionAndBranch.Version;

return string.Format("{0}.{1}.{2}", semanticVersion.Major, semanticVersion.Minor, semanticVersion.Patch);
return string.Format("{0}.{1}.{2}", Variables[GitFlowVariableProvider.Major], Variables[GitFlowVariableProvider.Minor], Variables[GitFlowVariableProvider.Patch]);
}
}
}
17 changes: 8 additions & 9 deletions GitFlowVersionTask/UpdateAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.IO;
using System.Linq;
using GitFlowVersion;
using GitFlowVersion.VersionBuilders;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Logger = GitFlowVersion.Logger;
Expand Down Expand Up @@ -85,23 +84,23 @@ public bool InnerExecute()
logger.LogInfo(string.Format("Executing PerformPreProcessingSteps for '{0}'.", buildServer.GetType().Name));
buildServer.PerformPreProcessingSteps(gitDirectory);
}
var versionAndBranch = VersionCache.GetVersion(gitDirectory);
var variables = VersionCache.GetVersion(gitDirectory);

WriteIntegrationParameters(versionAndBranch, gitDirectory, applicableBuildServers);
WriteIntegrationParameters(variables, gitDirectory, applicableBuildServers);

CreateTempAssemblyInfo(versionAndBranch);
CreateTempAssemblyInfo(variables);

return true;
}

public void WriteIntegrationParameters(VersionAndBranch versionAndBranch, string gitDirectory, List<IBuildServer> applicableBuildServers)
public void WriteIntegrationParameters(Dictionary<string, string> versionAndBranch, string gitDirectory, List<IBuildServer> applicableBuildServers)
{
foreach (var buildServer in applicableBuildServers)
{
logger.LogInfo(string.Format("Executing GenerateSetVersionMessage for '{0}'.", buildServer.GetType().Name));
logger.LogInfo(buildServer.GenerateSetVersionMessage(versionAndBranch.GenerateSemVer()));
logger.LogInfo(buildServer.GenerateSetVersionMessage(versionAndBranch[GitFlowVariableProvider.SemVer]));
logger.LogInfo(string.Format("Executing GenerateBuildLogOutput for '{0}'.", buildServer.GetType().Name));
foreach (var buildParameter in BuildOutputGenerator.GenerateBuildLogOutput(versionAndBranch, buildServer))
foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(versionAndBranch, buildServer))
{
logger.LogInfo(buildParameter);
}
Expand All @@ -120,11 +119,11 @@ public virtual IEnumerable<IBuildServer> GetApplicableBuildServers(string gitDir
}


void CreateTempAssemblyInfo(VersionAndBranch versionAndBranch)
void CreateTempAssemblyInfo(Dictionary<string, string> variables)
{
var assemblyInfoBuilder = new AssemblyInfoBuilder
{
VersionAndBranch = versionAndBranch,
Variables = variables,
SignAssembly = SignAssembly,
AssemblyName = AssemblyName
};
Expand Down
2 changes: 1 addition & 1 deletion Tests/AssemblyInfoBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void VerifyCreatedCode()
};
var assemblyInfoBuilder = new AssemblyInfoBuilder
{
VersionAndBranch = semanticVersion,
Variables = new GitFlowVariableProvider().GetVariables(semanticVersion),
AssemblyName = "MyAssembly"
};
var assemblyInfoText = assemblyInfoBuilder.GetAssemblyInfoText();
Expand Down
7 changes: 4 additions & 3 deletions Tests/JsonVersionBuilderTests.Json.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
"Major":1,
"Minor":2,
"Patch":3,
"PreReleasePartOne":4,
"Stability":"Unstable",
"Suffix":"a682956d",
"LongVersion":"1.2.3-unstable.feature-a682956d Branch:'feature1' Sha:'a682956dc1a2752aa24597a0f5cd939f93614509'",
"NugetVersion":"1.2.3-Feature-feature1-a682956dc1a2752aa24597a0f5cd939f93614509",
"ShortVersion":"1.2.3-unstable.feature-a682956d",
"BranchName":"feature1",
"BranchType":"Feature",
"Sha":"a682956dc1a2752aa24597a0f5cd939f93614509"
"Sha":"a682956dc1a2752aa24597a0f5cd939f93614509",
"SemVer":"1.2.3-Feature-feature1-a682956dc1a2752aa24597a0f5cd939f93614509",
"PreReleasePartOne":4,
"Stability":"Unstable"
}
3 changes: 2 additions & 1 deletion Tests/JsonVersionBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public void Json()
Suffix = "a682956d",
}
};
var json = semanticVersion.ToJson();
var dictionary = new GitFlowVariableProvider().GetVariables(semanticVersion);
var json = JsonOutputFormatter.ToJson(dictionary);
Approvals.Verify(json);
}

Expand Down

0 comments on commit f2daf60

Please sign in to comment.