Skip to content

Commit 50d6f03

Browse files
committed
Merge pull request #320 from JakeGinnivan/GitVersionInit
Added 'GitVersion init' command which creates a sample GitVersionConfig....
2 parents 2c902c3 + dc8e2a1 commit 50d6f03

31 files changed

+276
-71
lines changed

GitVersionConfig.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
assembly-versioning-scheme: MajorMinor
2+
next-version: 2.0.0
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System.IO;
2+
using System.Linq;
3+
using System.Reflection;
4+
using GitVersion;
5+
using GitVersion.Helpers;
6+
using GitVersionCore.Tests;
7+
using NUnit.Framework;
8+
using Shouldly;
9+
using YamlDotNet.Serialization;
10+
11+
[TestFixture]
12+
public class ConfigProviderTests
13+
{
14+
string gitDirectory;
15+
IFileSystem fileSystem;
16+
17+
[SetUp]
18+
public void Setup()
19+
{
20+
fileSystem = new TestFileSystem();
21+
gitDirectory = "c:\\MyGitRepo\\.git";
22+
}
23+
24+
[Test]
25+
public void CanReadDocument()
26+
{
27+
const string text = @"
28+
assembly-versioning-scheme: MajorMinor
29+
develop-branch-tag: alpha
30+
release-branch-tag: rc
31+
next-version: 2.0.0
32+
tag-prefix: '[vV|version-]'
33+
";
34+
SetupConfigFileContent(text);
35+
36+
var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
37+
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinor);
38+
config.DevelopBranchTag.ShouldBe("alpha");
39+
config.ReleaseBranchTag.ShouldBe("rc");
40+
config.NextVersion.ShouldBe("2.0.0");
41+
config.TagPrefix.ShouldBe("[vV|version-]");
42+
}
43+
44+
[Test]
45+
public void CanReadOldDocument()
46+
{
47+
const string text = @"assemblyVersioningScheme: MajorMinor";
48+
SetupConfigFileContent(text);
49+
var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
50+
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinor);
51+
}
52+
53+
[Test]
54+
public void CanReadDefaultDocument()
55+
{
56+
const string text = "";
57+
SetupConfigFileContent(text);
58+
var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
59+
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinorPatch);
60+
config.DevelopBranchTag.ShouldBe("unstable");
61+
config.ReleaseBranchTag.ShouldBe("beta");
62+
config.TagPrefix.ShouldBe("[vV]");
63+
config.NextVersion.ShouldBe(null);
64+
}
65+
66+
[Test]
67+
public void VerifyInit()
68+
{
69+
var config = typeof(Config);
70+
var aliases = config.GetProperties().Select(p => ((YamlAliasAttribute) p.GetCustomAttribute(typeof(YamlAliasAttribute))).Alias);
71+
var writer = new StringWriter();
72+
73+
ConfigReader.WriteSample(writer);
74+
var initFile = writer.GetStringBuilder().ToString();
75+
76+
foreach (var alias in aliases)
77+
{
78+
initFile.ShouldContain(alias);
79+
}
80+
}
81+
82+
[Test]
83+
public void VerifyAliases()
84+
{
85+
var config = typeof(Config);
86+
var propertiesMissingAlias = config.GetProperties().Where(p => p.GetCustomAttribute(typeof(YamlAliasAttribute)) == null).Select(p => p.Name);
87+
88+
propertiesMissingAlias.ShouldBeEmpty();
89+
}
90+
91+
void SetupConfigFileContent(string text)
92+
{
93+
fileSystem.WriteAllText(Path.Combine(Directory.GetParent(gitDirectory).FullName, "GitVersionConfig.yaml"), text);
94+
}
95+
}

GitVersionCore.Tests/ConfigReaderTests.cs

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

GitVersionCore.Tests/GitVersionCore.Tests.csproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@
6060
<Reference Include="System.Data.DataSetExtensions" />
6161
<Reference Include="System.Data" />
6262
<Reference Include="System.Xml" />
63+
<Reference Include="YamlDotNet, Version=3.3.0.0, Culture=neutral, processorArchitecture=MSIL">
64+
<SpecificVersion>False</SpecificVersion>
65+
<HintPath>..\packages\YamlDotNet.3.3.1\lib\net35\YamlDotNet.dll</HintPath>
66+
</Reference>
6367
</ItemGroup>
6468
<ItemGroup>
6569
<Compile Include="..\GitVersionTask.Tests\Helpers\DirectoryHelper.cs">
6670
<Link>Helpers\DirectoryHelper.cs</Link>
6771
</Compile>
6872
<Compile Include="Fixtures\CommitCountingRepoFixture.cs" />
69-
<Compile Include="ConfigReaderTests.cs" />
73+
<Compile Include="ConfigProviderTests.cs" />
7074
<Compile Include="GitDirFinderTests.cs" />
7175
<Compile Include="Fixtures\BaseGitFlowRepositoryFixture.cs" />
7276
<Compile Include="IntegrationTests\GitFlow\DevelopScenarios.cs" />
@@ -85,12 +89,13 @@
8589
<Compile Include="JsonVersionBuilderTests.cs" />
8690
<Compile Include="ModuleInitializer.cs" />
8791
<Compile Include="Fixtures\EmptyRepositoryFixture.cs" />
88-
<Compile Include="Helpers\GitHelper.cs" />
92+
<Compile Include="Helpers\GitTestExtensions.cs" />
8993
<Compile Include="Helpers\PathHelper.cs" />
9094
<Compile Include="IntegrationTests\GitHubFlow\MasterTests.cs" />
9195
<Compile Include="ApprovalTestsConfig.cs" />
9296
<Compile Include="Fixtures\RepositoryFixtureBase.cs" />
9397
<Compile Include="SemanticVersionTests.cs" />
98+
<Compile Include="TestFileSystem.cs" />
9499
<Compile Include="VariableProviderTests.cs" />
95100
</ItemGroup>
96101
<ItemGroup>

GitVersionCore.Tests/Helpers/GitHelper.cs renamed to GitVersionCore.Tests/Helpers/GitTestExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using GitVersion;
55
using LibGit2Sharp;
66

7-
public static class GitHelper
7+
public static class GitTestExtensions
88
{
99
public static Commit MakeACommit(this IRepository repository)
1010
{

GitVersionCore.Tests/SemanticVersionTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public class SemanticVersionTests
88

99
[TestCase("1.2.3", 1, 2, 3, null, null, null, null, null, null, null)]
1010
[TestCase("1.2", 1, 2, 0, null, null, null, null, null, null, "1.2.0")]
11-
[TestCase("1", 1, 0, 0, null, null, null, null, null, null, "1.0.0")]
1211
[TestCase("1.2.3-beta", 1, 2, 3, "beta", null, null, null, null, null, null)]
1312
[TestCase("1.2.3-beta3", 1, 2, 3, "beta", 3, null, null, null, null, "1.2.3-beta.3")]
1413
[TestCase("1.2.3-alpha", 1, 2, 3, "alpha", null, null, null, null, null, null)]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
namespace GitVersionCore.Tests
2+
{
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using GitVersion.Helpers;
6+
7+
public class TestFileSystem : IFileSystem
8+
{
9+
Dictionary<string, string> fileSystem = new Dictionary<string, string>();
10+
11+
public void Copy(string @from, string to, bool overwrite)
12+
{
13+
throw new System.NotImplementedException();
14+
}
15+
16+
public void Move(string @from, string to)
17+
{
18+
throw new System.NotImplementedException();
19+
}
20+
21+
public bool Exists(string file)
22+
{
23+
return fileSystem.ContainsKey(file);
24+
}
25+
26+
public void Delete(string path)
27+
{
28+
throw new System.NotImplementedException();
29+
}
30+
31+
public string ReadAllText(string path)
32+
{
33+
return fileSystem[path];
34+
}
35+
36+
public void WriteAllText(string file, string fileContents)
37+
{
38+
if (fileSystem.ContainsKey(file))
39+
fileSystem[file] = fileContents;
40+
else
41+
fileSystem.Add(file, fileContents);
42+
}
43+
44+
public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
45+
{
46+
throw new System.NotImplementedException();
47+
}
48+
49+
public Stream OpenWrite(string path)
50+
{
51+
throw new System.NotImplementedException();
52+
}
53+
}
54+
}

GitVersionCore.Tests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
<package id="LibGit2Sharp" version="0.19.0.0" targetFramework="net45" />
77
<package id="NUnit" version="2.6.3" targetFramework="net45" />
88
<package id="Shouldly" version="2.2.0" targetFramework="net45" />
9+
<package id="YamlDotNet" version="3.3.1" targetFramework="net45" />
910
</packages>

GitVersionCore/AssemblyVersioningScheme.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ public enum AssemblyVersioningScheme
55
MajorMinorPatchMetadata,
66
MajorMinorPatch,
77
MajorMinor,
8-
Major,
8+
Major
99
}
1010
}

GitVersionCore/Configuration/Config.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public Config()
1212
TagPrefix = "[vV]";
1313
}
1414

15+
[YamlAlias("assembly-versioning-scheme")]
1516
public AssemblyVersioningScheme AssemblyVersioningScheme { get; set; }
1617

1718
[YamlAlias("develop-branch-tag")]
Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
using System.IO;
2-
using YamlDotNet.Serialization;
3-
using YamlDotNet.Serialization.NamingConventions;
4-
5-
namespace GitVersion
1+
namespace GitVersion
62
{
3+
using System.IO;
4+
using YamlDotNet.Serialization;
5+
using YamlDotNet.Serialization.NamingConventions;
6+
77
public class ConfigReader
88
{
99
public static Config Read(TextReader reader)
1010
{
11-
var deserializer = new Deserializer(null, new CamelCaseNamingConvention());
11+
var deserializer = new Deserializer(null, new HyphenatedNamingConvention());
1212
var deserialize = deserializer.Deserialize<Config>(reader);
1313
if (deserialize == null)
1414
{
1515
return new Config();
1616
}
1717
return deserialize;
1818
}
19+
20+
public static void WriteSample(TextWriter writer)
21+
{
22+
writer.WriteLine("# assembly-versioning-scheme: MajorMinorPatchMetadata | MajorMinorPatch | MajorMinor | Major");
23+
writer.WriteLine("# develop-branch-tag: alpha");
24+
writer.WriteLine("# release-branch-tag: rc");
25+
writer.WriteLine("# tag-prefix: '[vV|version-] # regex to match git tag prefix");
26+
writer.WriteLine("# next-version: 1.0.0");
27+
}
1928
}
2029
}
Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,54 @@
11
namespace GitVersion
22
{
33
using System.IO;
4+
using System.Text.RegularExpressions;
5+
using GitVersion.Helpers;
46

57
public class ConfigurationProvider
68
{
7-
public static Config Provide(string gitDirectory)
9+
static Regex oldAssemblyVersioningScheme = new Regex("assemblyVersioningScheme", RegexOptions.Compiled | RegexOptions.IgnoreCase);
10+
11+
public static Config Provide(string gitDirectory, IFileSystem fileSystem)
812
{
9-
var configFilePath = Path.Combine(Directory.GetParent(gitDirectory).FullName, "GitVersionConfig.yaml");
10-
if (File.Exists(configFilePath))
13+
var configFilePath = GetConfigFilePath(gitDirectory);
14+
15+
if (fileSystem.Exists(configFilePath))
1116
{
12-
using (var reader = File.OpenText(configFilePath))
17+
var readAllText = fileSystem.ReadAllText(configFilePath);
18+
if (oldAssemblyVersioningScheme.IsMatch(readAllText))
1319
{
14-
return ConfigReader.Read(reader);
20+
readAllText = oldAssemblyVersioningScheme.Replace(readAllText, "assembly-versioning-scheme");
21+
fileSystem.WriteAllText(configFilePath, readAllText);
22+
Logger.WriteWarning("Found legacy configuration value 'assemblyVersioningScheme', replaced with 'assembly-versioning-scheme");
1523
}
24+
25+
return ConfigReader.Read(new StringReader(readAllText));
1626
}
1727

1828
return new Config();
1929
}
30+
31+
public static void WriteSample(string gitDirectory, IFileSystem fileSystem)
32+
{
33+
var configFilePath = GetConfigFilePath(gitDirectory);
34+
35+
if (!fileSystem.Exists(configFilePath))
36+
{
37+
using (var stream = fileSystem.OpenWrite(configFilePath))
38+
using (var writer = new StreamWriter(stream))
39+
{
40+
ConfigReader.WriteSample(writer);
41+
}
42+
}
43+
else
44+
{
45+
Logger.WriteError("Cannot write sample, GitVersionConfig.yaml already exists");
46+
}
47+
}
48+
49+
static string GetConfigFilePath(string gitDirectory)
50+
{
51+
return Path.Combine(Directory.GetParent(gitDirectory).FullName, "GitVersionConfig.yaml");
52+
}
2053
}
2154
}

GitVersionCore/GitFlow/BranchFinders/VersionOnMasterFinder.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,5 @@ public VersionPoint Execute(GitVersionContext context, DateTimeOffset olderThan)
3939
};
4040
}
4141

42-
43-
44-
45-
4642
}
4743
}

GitVersionCore/GitVersionCore.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,9 @@
5353
<SpecificVersion>False</SpecificVersion>
5454
<HintPath>..\Packages\Visualize.Fody.0.4.0.0\Lib\portable-net4+sl4+wp7+win8+MonoAndroid16+MonoTouch40\Visualize.dll</HintPath>
5555
</Reference>
56-
<Reference Include="YamlDotNet">
57-
<HintPath>..\packages\YamlDotNet.3.3.0\lib\net35\YamlDotNet.dll</HintPath>
56+
<Reference Include="YamlDotNet, Version=3.3.0.0, Culture=neutral, processorArchitecture=MSIL">
57+
<SpecificVersion>False</SpecificVersion>
58+
<HintPath>..\packages\YamlDotNet.3.3.1\lib\net35\YamlDotNet.dll</HintPath>
5859
</Reference>
5960
</ItemGroup>
6061
<ItemGroup>
@@ -74,6 +75,8 @@
7475
<Compile Include="Configuration\ConfigurationProvider.cs" />
7576
<Compile Include="GitFlow\BranchFinders\BranchCommitDifferenceFinder.cs" />
7677
<Compile Include="GitFlow\BranchFinders\RecentTagVersionExtractor.cs" />
78+
<Compile Include="Helpers\FileSystem.cs" />
79+
<Compile Include="Helpers\IFileSystem.cs" />
7780
<Compile Include="LastMinorVersionFinder.cs" />
7881
<Compile Include="SemanticVersionExtensions.cs" />
7982
<Compile Include="WarningException.cs" />

0 commit comments

Comments
 (0)