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
33 changes: 33 additions & 0 deletions src/GitVersionTask.Tests/AssemblyInfoBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using GitVersion;
using GitVersionCore.Tests;
using Microsoft.Build.Framework;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.VisualBasic;
using NSubstitute;
using NUnit.Framework;
using Shouldly;

Expand Down Expand Up @@ -212,6 +215,36 @@ public void VerifyAssemblyVersion_MajorMinorPatchTag_NugetAssemblyInfo([ValueSou
VerifyAssemblyVersion(compiler, AssemblyVersioningScheme.MajorMinorPatchTag, "{NugetVersion}");
}

[Test]
public void GetAssemblyInfoBuilder_Empty_ThrowsWarningException()
{
var taskItems = Substitute.For<IEnumerable<ITaskItem>>();
var exception = Assert.Throws<GitTools.WarningException>(() => AssemblyInfoBuilder.GetAssemblyInfoBuilder(taskItems));
exception.Message.ShouldBe("Unable to determine which AssemblyBuilder required to generate GitVersion assembly information");
}

[Test]
public void GetAssemblyInfoBuilder_Null_ThrowsArgumentNullException()
{
var exception = Assert.Throws<ArgumentNullException>(() => AssemblyInfoBuilder.GetAssemblyInfoBuilder(null));
exception.ParamName.ShouldBe("compileFiles");
}

[TestCase("Class1.cs", typeof(CSharpAssemblyInfoBuilder))]
[TestCase("Class1.vb", typeof(VisualBasicAssemblyInfoBuilder))]
[TestCase("AssemblyInfo.cs", typeof(CSharpAssemblyInfoBuilder))]
[TestCase("AssemblyInfo.vb", typeof(VisualBasicAssemblyInfoBuilder))]
public void GetAssemblyInfoBuilder_ShouldReturnAppropriateAssemblyInfoBuilder(string fileName, Type assemblyInfoBuilderType)
{
var taskItem = Substitute.For<ITaskItem>();
taskItem.ItemSpec.Returns(fileName);

var assemblyInfoBuilder = AssemblyInfoBuilder.GetAssemblyInfoBuilder(new[] { taskItem });

assemblyInfoBuilder.ShouldNotBeNull();
assemblyInfoBuilder.ShouldBeOfType(assemblyInfoBuilderType);
}

static void VerifyAssemblyVersion(ICompiler compiler, AssemblyVersioningScheme avs, string assemblyInformationalFormat = null)
{
var semanticVersion = new SemanticVersion
Expand Down
4 changes: 4 additions & 0 deletions src/GitVersionTask.Tests/GitVersionTask.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
<HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NSubstitute, Version=1.10.0.0, Culture=neutral, PublicKeyToken=92dd2e9066daa5ca, processorArchitecture=MSIL">
<HintPath>..\packages\NSubstitute.1.10.0.0\lib\net45\NSubstitute.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="nunit.core, Version=2.6.4.14350, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77, processorArchitecture=MSIL">
<HintPath>..\packages\NUnitTestAdapter.2.0.0\lib\nunit.core.dll</HintPath>
<Private>True</Private>
Expand Down
1 change: 1 addition & 0 deletions src/GitVersionTask.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<package id="Microsoft.Web.Xdt" version="2.1.1" targetFramework="net45" />
<package id="ModuleInit.Fody" version="1.5.9.0" targetFramework="net45" developmentDependency="true" />
<package id="Newtonsoft.Json" version="8.0.3" targetFramework="net45" />
<package id="NSubstitute" version="1.10.0.0" targetFramework="net45" />
<package id="NUnit" version="3.2.1" targetFramework="net45" />
<package id="NUnitTestAdapter" version="2.0.0" targetFramework="net45" />
<package id="ObjectApproval" version="1.3.0" targetFramework="net45" />
Expand Down
31 changes: 19 additions & 12 deletions src/GitVersionTask/AssemblyInfoBuilder/AssemblyInfoBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using GitTools;
using GitVersion;
using Microsoft.Build.Framework;
using System.IO;
using GitTools;

public abstract class AssemblyInfoBuilder
{
private static readonly Dictionary<string, Type> assemblyInfoBuilders = new Dictionary<string, Type>()
private static readonly Dictionary<string, Type> assemblyInfoBuilders = new Dictionary<string, Type>
{
{ ".cs", typeof(CSharpAssemblyInfoBuilder) },
{ ".vb", typeof(VisualBasicAssemblyInfoBuilder) }
{".cs", typeof(CSharpAssemblyInfoBuilder)},
{".vb", typeof(VisualBasicAssemblyInfoBuilder)}
// TODO: Missing FSharpAssemblyInfoBuilder
};

public abstract string AssemblyInfoExtension { get; }

public static AssemblyInfoBuilder GetAssemblyInfoBuilder(IEnumerable<ITaskItem> compileFiles)
{
if (compileFiles == null)
{
throw new ArgumentNullException("compileFiles");
}

Type builderType;

var assemblyInfoExtension = compileFiles.Select(x => x.ItemSpec)
.Where(compileFile => compileFile.Contains("AssemblyInfo"))
.Select(Path.GetExtension).FirstOrDefault();
var assemblyInfoExtension = compileFiles
.Select(x => x.ItemSpec)
.Select(Path.GetExtension)
// TODO: While it works, this seems like a bad way to discover the language is being compiled. @asbjornu
.FirstOrDefault(extension => assemblyInfoBuilders.ContainsKey(extension.ToLowerInvariant()));

if (assemblyInfoBuilders.TryGetValue(assemblyInfoExtension, out builderType))
if (assemblyInfoExtension != null && assemblyInfoBuilders.TryGetValue(assemblyInfoExtension, out builderType))
{
return Activator.CreateInstance(builderType) as AssemblyInfoBuilder;
}

throw new WarningException("Unable to determine which AssemblyBuilder required to generate GitVersion assembly information");
}

public abstract string AssemblyInfoExtension { get; }

public abstract string GetAssemblyInfoText(VersionVariables vars, string rootNamespace);
}
}