Skip to content
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
32 changes: 31 additions & 1 deletion docs/usage/command-line.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,34 @@ It will update the following assembly attributes:

Note that contrary to when using the [MSBuild Task](msbuild-task.md) the attributes must already exist in the `AssemblyInfo.cs` or `AssemblyInfo.vb` files prior to calling GitVersion.

By adding `/updateassemblyinfofilename` the name of AssemblyInfo file to update can be set.
By adding `/updateassemblyinfo <filenames>` the name of AssemblyInfo file to update can be set. This switch can accept multiple files with the path to the file specified relative to the working directory.

GitVersion can generate an assembly info source file for you if it does not already exist. Use the `/ensureassemblyinfo` switch alongside `/updateassemblyinfo <filename>`, if the filename specified does not exist it will be generated based on a known template that adds:

* `AssemblyVersion` will be set to the `AssemblySemVer` variable.
* `AssemblyFileVersion` will be set to the `MajorMinorPatch` variable with a appended `.0`.
* `AssemblyInformationalVersion` will be set to the `InformationalVersion` variable.

This can be done for *.cs, *.vb and *.fs files.

When requesting that GitVersion generate an assembly info file you are limited to only specifying a single `<filename>` within the `/updateassemblyinfo` switch, this is to prevent the creation of mulitple assembly info files with the same assembly version attributes. If this occurs your build will fail.

### Example: When AssemblyInfo.cs does not exist
`GitVersion.exe /updateassemblyinfo AssemblyInfo.cs /ensureassemblyinfo`

A file is generated that contains version attributes (`AssemblyVersion`, `AssemblyFileVersion`, `AssemblyInformationalVersion`)

### Example: When AssemblyInfo.cs already exists
`GitVersion.exe /updateassemblyinfo AssemblyInfo.cs /ensureassemblyinfo`

All known attributes (`AssemblyVersion`, `AssemblyFileVersion`, `AssemblyInformationalVersion`) will be updated

### Example: When AssemblyInfo.cs and AssemblyVersionInfo.cs do not exist
`GitVersion.exe /updateassemblyinfo AssemblyInfo.cs AssemblyVersionInfo.cs /ensureassemblyinfo`

Will result in command line argument error

### Example: When AssemblyInfo.cs and AssemblyVersionInfo.cs already exist
`GitVersion.exe /updateassemblyinfo AssemblyInfo.cs AssemblyVersionInfo.cs`

Will iterate through each file and update known attributes (`AssemblyVersion`, `AssemblyFileVersion`, `AssemblyInformationalVersion`).
13 changes: 13 additions & 0 deletions src/GitVersionCore.Tests/TestFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ public Stream OpenRead(string path)

public void CreateDirectory(string path)
{
if (fileSystem.ContainsKey(path))
{
fileSystem[path] = "";
}
else
{
fileSystem.Add(path, "");
}
}

public bool DirectoryExists(string path)
{
return fileSystem.ContainsKey(path);
}

public long GetLastDirectoryWrite(string path)
Expand Down
30 changes: 30 additions & 0 deletions src/GitVersionCore/Extensions/ReadEmbeddedResourceExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace GitVersionCore.Extensions
{
using System.IO;

public static class ReadEmbeddedResourceExtensions
{
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="resourceName">Should include Namespace separated path to resource in assembly referenced by <typeparamref name="T"/></param>
/// <returns></returns>
public static string ReadAsStringFromEmbeddedResource<T>(this string resourceName)
{
using (var stream = resourceName.ReadFromEmbeddedResource<T>())
{
using (var rdr = new StreamReader(stream))
{
return rdr.ReadToEnd();
}
}
}

public static Stream ReadFromEmbeddedResource<T>(this string resourceName)
{
var assembly = typeof(T).Assembly;
return assembly.GetManifestResourceStream(resourceName);
}
}
}
5 changes: 5 additions & 0 deletions src/GitVersionCore/GitVersionCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<Compile Include="Configuration\Init\StepResult.cs" />
<Compile Include="EffectiveConfiguration.cs" />
<Compile Include="ExecuteCore.cs" />
<Compile Include="Extensions\ReadEmbeddedResourceExtensions.cs" />
<Compile Include="GitPreparer.cs" />
<Compile Include="GitVersionCache.cs" />
<Compile Include="Helpers\FileSystem.cs" />
Expand All @@ -121,6 +122,8 @@
<Compile Include="SemanticVersionExtensions.cs" />
<Compile Include="SemanticVersionFormatValues.cs" />
<Compile Include="StringFormatWith.cs" />
<Compile Include="VersionAssemblyInfoResources\AssemblyVersionInfoTemplates.cs" />
<EmbeddedResource Include="VersionAssemblyInfoResources\VersionAssemblyInfo.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculator.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculators\BaseVersion.cs" />
<Compile Include="VersionCalculation\BaseVersionCalculators\ConfigNextVersionBaseVersionStrategy.cs" />
Expand Down Expand Up @@ -161,6 +164,8 @@
<Content Include="FodyWeavers.xml">
<SubType>Designer</SubType>
</Content>
<EmbeddedResource Include="VersionAssemblyInfoResources\VersionAssemblyInfo.vb" />
<EmbeddedResource Include="VersionAssemblyInfoResources\VersionAssemblyInfo.fs" />
</ItemGroup>
<ItemGroup>
<None Include="NugetAssets\GitVersion.nuspec">
Expand Down
5 changes: 5 additions & 0 deletions src/GitVersionCore/Helpers/FileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public void CreateDirectory(string path)
Directory.CreateDirectory(path);
}

public bool DirectoryExists(string path)
{
return Directory.Exists(path);
}

public long GetLastDirectoryWrite(string path)
{
return new DirectoryInfo(path)
Expand Down
1 change: 1 addition & 0 deletions src/GitVersionCore/Helpers/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public interface IFileSystem
Stream OpenWrite(string path);
Stream OpenRead(string path);
void CreateDirectory(string path);
bool DirectoryExists(string path);
long GetLastDirectoryWrite(string path);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace GitVersion.VersionAssemblyInfoResources
{
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GitVersionCore.Extensions;

public class AssemblyVersionInfoTemplates
{
static IDictionary<string, FileInfo> assemblyInfoSourceList;

static AssemblyVersionInfoTemplates()
{
var enclosingNamespace = typeof(AssemblyVersionInfoTemplates).Namespace;

var files = typeof(AssemblyVersionInfoTemplates)
.Assembly
.GetManifestResourceNames()
.Where(n => n.StartsWith(enclosingNamespace ?? string.Empty)).Select(f => new FileInfo(f));

assemblyInfoSourceList = files.ToDictionary(k => k.Extension, v => v);
}

public static string GetAssemblyInfoTemplateFor(string assemblyInfoFile)
{
var fi = new FileInfo(assemblyInfoFile);
if (assemblyInfoSourceList.ContainsKey(fi.Extension))
{
var template = assemblyInfoSourceList[fi.Extension];
if (template != null)
{
return template.Name.ReadAsStringFromEmbeddedResource<AssemblyVersionInfoTemplates>();
}
}
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by GitVersion.
//
// You can modify this code as we will not overwrite it when re-executing GitVersion
// </auto-generated>
//------------------------------------------------------------------------------

using System.Reflection;

[assembly: AssemblyFileVersion("0.0.0.0")]
[assembly: AssemblyVersion("0.0.0.0")]
[assembly: AssemblyInformationalVersion("0.0.0.0")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by GitVersion.
//
// You can modify this code as we will not overwrite it when re-executing GitVersion
// </auto-generated>
//------------------------------------------------------------------------------

open System.Reflection

[<assembly: AssemblyFileVersion("0.0.0.0")>]
[<assembly: AssemblyVersion("0.0.0.0")>]
[<assembly: AssemblyInformationalVersion("0.0.0.0")>]
()
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
''------------------------------------------------------------------------------
'' <auto-generated>
'' This code was generated by GitVersion.
''
'' You can modify this code as we will not overwrite it when re-executing GitVersion
'' </auto-generated>
''------------------------------------------------------------------------------

Imports System.Reflection

<assembly: AssemblyFileVersion("0.0.0.0")>
<assembly: AssemblyVersion("0.0.0.0")>
<assembly: AssemblyInformationalVersion("0.0.0.0")>
47 changes: 45 additions & 2 deletions src/GitVersionExe.Tests/ArgumentParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using GitVersion;
using NUnit.Framework;
using Shouldly;
Expand Down Expand Up @@ -175,6 +176,10 @@ public void Unknown_argument_should_throw()
[TestCase("-updateAssemblyInfo 1")]
[TestCase("-updateAssemblyInfo")]
[TestCase("-updateAssemblyInfo -proj foo.sln")]
[TestCase("-updateAssemblyInfo assemblyInfo.cs")]
[TestCase("-updateAssemblyInfo assemblyInfo.cs -ensureassemblyinfo")]
[TestCase("-updateAssemblyInfo assemblyInfo.cs otherAssemblyInfo.cs")]
[TestCase("-updateAssemblyInfo Assembly.cs Assembly.cs -ensureassemblyinfo")]
public void update_assembly_info_true(string command)
{
var arguments = ArgumentParser.ParseArguments(command);
Expand All @@ -189,22 +194,60 @@ public void update_assembly_info_false(string command)
arguments.UpdateAssemblyInfo.ShouldBe(false);
}

[TestCase("-updateAssemblyInfo Assembly.cs Assembly1.cs -ensureassemblyinfo")]
public void create_mulitple_assembly_info_protected(string command)
{
var exception = Assert.Throws<WarningException>(() => ArgumentParser.ParseArguments(command));
exception.Message.ShouldBe("Can't specify multiple assembly info files when using -ensureassemblyinfo switch, either use a single assembly info file or do not specify -ensureassemblyinfo and create assembly info files manually");
}

[Test]
public void update_assembly_info_with_filename()
{
var arguments = ArgumentParser.ParseArguments("-updateAssemblyInfo CommonAssemblyInfo.cs");
arguments.UpdateAssemblyInfo.ShouldBe(true);
arguments.UpdateAssemblyInfoFileName.ShouldBe("CommonAssemblyInfo.cs");
arguments.UpdateAssemblyInfoFileName.ShouldContain("CommonAssemblyInfo.cs");
}

[Test]
public void update_assembly_info_with_multiple_filenames()
{
var arguments = ArgumentParser.ParseArguments("-updateAssemblyInfo CommonAssemblyInfo.cs VersionAssemblyInfo.cs");
arguments.UpdateAssemblyInfo.ShouldBe(true);
arguments.UpdateAssemblyInfoFileName.Count.ShouldBe(2);
arguments.UpdateAssemblyInfoFileName.ShouldContain("CommonAssemblyInfo.cs");
arguments.UpdateAssemblyInfoFileName.ShouldContain("VersionAssemblyInfo.cs");
}

[Test]
public void update_assembly_info_with_relative_filename()
{
var arguments = ArgumentParser.ParseArguments("-updateAssemblyInfo ..\\..\\CommonAssemblyInfo.cs");
arguments.UpdateAssemblyInfo.ShouldBe(true);
arguments.UpdateAssemblyInfoFileName.ShouldBe("..\\..\\CommonAssemblyInfo.cs");
arguments.UpdateAssemblyInfoFileName.ShouldContain("..\\..\\CommonAssemblyInfo.cs");
}

[Test]
public void ensure_assembly_info_true_when_found()
{
var arguments = ArgumentParser.ParseArguments("-ensureAssemblyInfo");
arguments.EnsureAssemblyInfo.ShouldBe(true);
}

[Test]
public void ensure_assembly_info_true()
{
var arguments = ArgumentParser.ParseArguments("-ensureAssemblyInfo true");
arguments.EnsureAssemblyInfo.ShouldBe(true);
}

[Test]
public void ensure_assembly_info_false()
{
var arguments = ArgumentParser.ParseArguments("-ensureAssemblyInfo false");
arguments.EnsureAssemblyInfo.ShouldBe(false);
}

[Test]
public void dynamicRepoLocation()
{
Expand Down
Loading