Skip to content

Added 'GitVersion init' command which creates a sample GitVersionConfig.... #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions GitVersionConfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
assembly-versioning-scheme: MajorMinor
next-version: 2.0.0
95 changes: 95 additions & 0 deletions GitVersionCore.Tests/ConfigProviderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.IO;
using System.Linq;
using System.Reflection;
using GitVersion;
using GitVersion.Helpers;
using GitVersionCore.Tests;
using NUnit.Framework;
using Shouldly;
using YamlDotNet.Serialization;

[TestFixture]
public class ConfigProviderTests
{
string gitDirectory;
IFileSystem fileSystem;

[SetUp]
public void Setup()
{
fileSystem = new TestFileSystem();
gitDirectory = "c:\\MyGitRepo\\.git";
}

[Test]
public void CanReadDocument()
{
const string text = @"
assembly-versioning-scheme: MajorMinor
develop-branch-tag: alpha
release-branch-tag: rc
next-version: 2.0.0
tag-prefix: '[vV|version-]'
";
SetupConfigFileContent(text);

var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinor);
config.DevelopBranchTag.ShouldBe("alpha");
config.ReleaseBranchTag.ShouldBe("rc");
config.NextVersion.ShouldBe("2.0.0");
config.TagPrefix.ShouldBe("[vV|version-]");
}

[Test]
public void CanReadOldDocument()
{
const string text = @"assemblyVersioningScheme: MajorMinor";
SetupConfigFileContent(text);
var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinor);
}

[Test]
public void CanReadDefaultDocument()
{
const string text = "";
SetupConfigFileContent(text);
var config = ConfigurationProvider.Provide(gitDirectory, fileSystem);
config.AssemblyVersioningScheme.ShouldBe(AssemblyVersioningScheme.MajorMinorPatch);
config.DevelopBranchTag.ShouldBe("unstable");
config.ReleaseBranchTag.ShouldBe("beta");
config.TagPrefix.ShouldBe("[vV]");
config.NextVersion.ShouldBe(null);
}

[Test]
public void VerifyInit()
{
var config = typeof(Config);
var aliases = config.GetProperties().Select(p => ((YamlAliasAttribute) p.GetCustomAttribute(typeof(YamlAliasAttribute))).Alias);
var writer = new StringWriter();

ConfigReader.WriteSample(writer);
var initFile = writer.GetStringBuilder().ToString();

foreach (var alias in aliases)
{
initFile.ShouldContain(alias);
}
}

[Test]
public void VerifyAliases()
{
var config = typeof(Config);
var propertiesMissingAlias = config.GetProperties().Where(p => p.GetCustomAttribute(typeof(YamlAliasAttribute)) == null).Select(p => p.Name);

propertiesMissingAlias.ShouldBeEmpty();
}

void SetupConfigFileContent(string text)
{
fileSystem.WriteAllText(Path.Combine(Directory.GetParent(gitDirectory).FullName, "GitVersionConfig.yaml"), text);
}
}
36 changes: 0 additions & 36 deletions GitVersionCore.Tests/ConfigReaderTests.cs

This file was deleted.

9 changes: 7 additions & 2 deletions GitVersionCore.Tests/GitVersionCore.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="YamlDotNet, Version=3.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\YamlDotNet.3.3.1\lib\net35\YamlDotNet.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\GitVersionTask.Tests\Helpers\DirectoryHelper.cs">
<Link>Helpers\DirectoryHelper.cs</Link>
</Compile>
<Compile Include="Fixtures\CommitCountingRepoFixture.cs" />
<Compile Include="ConfigReaderTests.cs" />
<Compile Include="ConfigProviderTests.cs" />
<Compile Include="GitDirFinderTests.cs" />
<Compile Include="Fixtures\BaseGitFlowRepositoryFixture.cs" />
<Compile Include="IntegrationTests\GitFlow\DevelopScenarios.cs" />
Expand All @@ -85,12 +89,13 @@
<Compile Include="JsonVersionBuilderTests.cs" />
<Compile Include="ModuleInitializer.cs" />
<Compile Include="Fixtures\EmptyRepositoryFixture.cs" />
<Compile Include="Helpers\GitHelper.cs" />
<Compile Include="Helpers\GitTestExtensions.cs" />
<Compile Include="Helpers\PathHelper.cs" />
<Compile Include="IntegrationTests\GitHubFlow\MasterTests.cs" />
<Compile Include="ApprovalTestsConfig.cs" />
<Compile Include="Fixtures\RepositoryFixtureBase.cs" />
<Compile Include="SemanticVersionTests.cs" />
<Compile Include="TestFileSystem.cs" />
<Compile Include="VariableProviderTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using GitVersion;
using LibGit2Sharp;

public static class GitHelper
public static class GitTestExtensions
{
public static Commit MakeACommit(this IRepository repository)
{
Expand Down
1 change: 0 additions & 1 deletion GitVersionCore.Tests/SemanticVersionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class SemanticVersionTests

[TestCase("1.2.3", 1, 2, 3, null, null, null, null, null, null, null)]
[TestCase("1.2", 1, 2, 0, null, null, null, null, null, null, "1.2.0")]
[TestCase("1", 1, 0, 0, null, null, null, null, null, null, "1.0.0")]
[TestCase("1.2.3-beta", 1, 2, 3, "beta", null, null, null, null, null, null)]
[TestCase("1.2.3-beta3", 1, 2, 3, "beta", 3, null, null, null, null, "1.2.3-beta.3")]
[TestCase("1.2.3-alpha", 1, 2, 3, "alpha", null, null, null, null, null, null)]
Expand Down
54 changes: 54 additions & 0 deletions GitVersionCore.Tests/TestFileSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace GitVersionCore.Tests
{
using System.Collections.Generic;
using System.IO;
using GitVersion.Helpers;

public class TestFileSystem : IFileSystem
{
Dictionary<string, string> fileSystem = new Dictionary<string, string>();

public void Copy(string @from, string to, bool overwrite)
{
throw new System.NotImplementedException();
}

public void Move(string @from, string to)
{
throw new System.NotImplementedException();
}

public bool Exists(string file)
{
return fileSystem.ContainsKey(file);
}

public void Delete(string path)
{
throw new System.NotImplementedException();
}

public string ReadAllText(string path)
{
return fileSystem[path];
}

public void WriteAllText(string file, string fileContents)
{
if (fileSystem.ContainsKey(file))
fileSystem[file] = fileContents;
else
fileSystem.Add(file, fileContents);
}

public IEnumerable<string> DirectoryGetFiles(string directory, string searchPattern, SearchOption searchOption)
{
throw new System.NotImplementedException();
}

public Stream OpenWrite(string path)
{
throw new System.NotImplementedException();
}
}
}
1 change: 1 addition & 0 deletions GitVersionCore.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<package id="LibGit2Sharp" version="0.19.0.0" targetFramework="net45" />
<package id="NUnit" version="2.6.3" targetFramework="net45" />
<package id="Shouldly" version="2.2.0" targetFramework="net45" />
<package id="YamlDotNet" version="3.3.1" targetFramework="net45" />
</packages>
2 changes: 1 addition & 1 deletion GitVersionCore/AssemblyVersioningScheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public enum AssemblyVersioningScheme
MajorMinorPatchMetadata,
MajorMinorPatch,
MajorMinor,
Major,
Major
}
}
1 change: 1 addition & 0 deletions GitVersionCore/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public Config()
TagPrefix = "[vV]";
}

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

[YamlAlias("develop-branch-tag")]
Expand Down
21 changes: 15 additions & 6 deletions GitVersionCore/Configuration/ConfigReader.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
using System.IO;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

namespace GitVersion
namespace GitVersion
{
using System.IO;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;

public class ConfigReader
{
public static Config Read(TextReader reader)
{
var deserializer = new Deserializer(null, new CamelCaseNamingConvention());
var deserializer = new Deserializer(null, new HyphenatedNamingConvention());
var deserialize = deserializer.Deserialize<Config>(reader);
if (deserialize == null)
{
return new Config();
}
return deserialize;
}

public static void WriteSample(TextWriter writer)
{
writer.WriteLine("# assembly-versioning-scheme: MajorMinorPatchMetadata | MajorMinorPatch | MajorMinor | Major");
writer.WriteLine("# develop-branch-tag: alpha");
writer.WriteLine("# release-branch-tag: rc");
writer.WriteLine("# tag-prefix: '[vV|version-] # regex to match git tag prefix");
writer.WriteLine("# next-version: 1.0.0");
}
}
}
43 changes: 38 additions & 5 deletions GitVersionCore/Configuration/ConfigurationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
namespace GitVersion
{
using System.IO;
using System.Text.RegularExpressions;
using GitVersion.Helpers;

public class ConfigurationProvider
{
public static Config Provide(string gitDirectory)
static Regex oldAssemblyVersioningScheme = new Regex("assemblyVersioningScheme", RegexOptions.Compiled | RegexOptions.IgnoreCase);

public static Config Provide(string gitDirectory, IFileSystem fileSystem)
{
var configFilePath = Path.Combine(Directory.GetParent(gitDirectory).FullName, "GitVersionConfig.yaml");
if (File.Exists(configFilePath))
var configFilePath = GetConfigFilePath(gitDirectory);

if (fileSystem.Exists(configFilePath))
{
using (var reader = File.OpenText(configFilePath))
var readAllText = fileSystem.ReadAllText(configFilePath);
if (oldAssemblyVersioningScheme.IsMatch(readAllText))
{
return ConfigReader.Read(reader);
readAllText = oldAssemblyVersioningScheme.Replace(readAllText, "assembly-versioning-scheme");
fileSystem.WriteAllText(configFilePath, readAllText);
Logger.WriteWarning("Found legacy configuration value 'assemblyVersioningScheme', replaced with 'assembly-versioning-scheme");
}

return ConfigReader.Read(new StringReader(readAllText));
}

return new Config();
}

public static void WriteSample(string gitDirectory, IFileSystem fileSystem)
{
var configFilePath = GetConfigFilePath(gitDirectory);

if (!fileSystem.Exists(configFilePath))
{
using (var stream = fileSystem.OpenWrite(configFilePath))
using (var writer = new StreamWriter(stream))
{
ConfigReader.WriteSample(writer);
}
}
else
{
Logger.WriteError("Cannot write sample, GitVersionConfig.yaml already exists");
}
}

static string GetConfigFilePath(string gitDirectory)
{
return Path.Combine(Directory.GetParent(gitDirectory).FullName, "GitVersionConfig.yaml");
}
}
}
4 changes: 0 additions & 4 deletions GitVersionCore/GitFlow/BranchFinders/VersionOnMasterFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,5 @@ public VersionPoint Execute(GitVersionContext context, DateTimeOffset olderThan)
};
}





}
}
7 changes: 5 additions & 2 deletions GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Packages\Visualize.Fody.0.4.0.0\Lib\portable-net4+sl4+wp7+win8+MonoAndroid16+MonoTouch40\Visualize.dll</HintPath>
</Reference>
<Reference Include="YamlDotNet">
<HintPath>..\packages\YamlDotNet.3.3.0\lib\net35\YamlDotNet.dll</HintPath>
<Reference Include="YamlDotNet, Version=3.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\YamlDotNet.3.3.1\lib\net35\YamlDotNet.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -74,6 +75,8 @@
<Compile Include="Configuration\ConfigurationProvider.cs" />
<Compile Include="GitFlow\BranchFinders\BranchCommitDifferenceFinder.cs" />
<Compile Include="GitFlow\BranchFinders\RecentTagVersionExtractor.cs" />
<Compile Include="Helpers\FileSystem.cs" />
<Compile Include="Helpers\IFileSystem.cs" />
<Compile Include="LastMinorVersionFinder.cs" />
<Compile Include="SemanticVersionExtensions.cs" />
<Compile Include="WarningException.cs" />
Expand Down
Loading