diff --git a/src/GitVersionTask.Tests/GetVersionTaskTests.cs b/src/GitVersionTask.Tests/GetVersionTaskTests.cs index a3f6d5c7d1..2038b891cd 100644 --- a/src/GitVersionTask.Tests/GetVersionTaskTests.cs +++ b/src/GitVersionTask.Tests/GetVersionTaskTests.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using GitVersion; using GitVersionTask; using Microsoft.Build.Framework; @@ -11,9 +11,8 @@ public class GetVersionTaskTests : TestBase [Test] public void OutputsShouldMatchVariableProvider() { - var taskProperties = typeof(GetVersion) + var taskProperties = typeof(GetVersion.Output) .GetProperties() - .Where(p => p.GetCustomAttributes(typeof(OutputAttribute), false).Any()) .Select(p => p.Name); var variablesProperties = VersionVariables.AvailableVariables; diff --git a/src/GitVersionTask.Tests/InvalidFileCheckerTests.cs b/src/GitVersionTask.Tests/InvalidFileCheckerTests.cs index e8877d2f94..13758c167c 100644 --- a/src/GitVersionTask.Tests/InvalidFileCheckerTests.cs +++ b/src/GitVersionTask.Tests/InvalidFileCheckerTests.cs @@ -1,7 +1,6 @@ using System; using System.IO; using GitVersion; -using GitVersionTask.Tests.Mocks; using Microsoft.Build.Framework; using NUnit.Framework; @@ -41,7 +40,7 @@ public void VerifyIgnoreNonAssemblyInfoFile() "); } - InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "SomeOtherFile.cs" } }, projectFile); + InvalidFileChecker.CheckForInvalidFiles(new[] { "SomeOtherFile.cs" }, projectFile); } [Test] @@ -57,7 +56,7 @@ public void VerifyAttributeFoundCSharp([Values("AssemblyVersion", "AssemblyFileV ", attribute); } - var ex = Assert.Throws(() => InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile), attribute); + var ex = Assert.Throws(() => InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile), attribute); Assert.That(ex.Message, Is.EqualTo("File contains assembly version attributes which conflict with the attributes generated by GitVersion AssemblyInfo.cs")); } @@ -75,7 +74,7 @@ public void VerifyUnformattedAttributeFoundCSharp([Values("AssemblyVersion", "As ", attribute); } - var ex = Assert.Throws(() => InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile), attribute); + var ex = Assert.Throws(() => InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile), attribute); Assert.That(ex.Message, Is.EqualTo("File contains assembly version attributes which conflict with the attributes generated by GitVersion AssemblyInfo.cs")); } @@ -92,7 +91,7 @@ public void VerifyCommentWorksCSharp([Values("AssemblyVersion", "AssemblyFileVer ", attribute); } - InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile); + InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile); } [Test] @@ -111,7 +110,7 @@ public class Temp ", attribute); } - InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile); + InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile); } [Test] @@ -129,7 +128,7 @@ public class {0} ", attribute); } - InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.cs" } }, projectFile); + InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.cs" }, projectFile); } [Test] @@ -145,7 +144,7 @@ Imports System.Reflection ", attribute); } - var ex = Assert.Throws(() => InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.vb" } }, projectFile), attribute); + var ex = Assert.Throws(() => InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile), attribute); Assert.That(ex.Message, Is.EqualTo("File contains assembly version attributes which conflict with the attributes generated by GitVersion AssemblyInfo.vb")); } @@ -163,7 +162,7 @@ Imports System.Reflection ", attribute); } - var ex = Assert.Throws(() => InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.vb" } }, projectFile), attribute); + var ex = Assert.Throws(() => InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile), attribute); Assert.That(ex.Message, Is.EqualTo("File contains assembly version attributes which conflict with the attributes generated by GitVersion AssemblyInfo.vb")); } @@ -180,7 +179,7 @@ Imports System.Reflection ", attribute); } - InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.vb" } }, projectFile); + InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile); } [Test] @@ -198,7 +197,7 @@ End Class ", attribute); } - InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.vb" } }, projectFile); + InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile); } [Test] @@ -215,6 +214,6 @@ End Class ", attribute); } - InvalidFileChecker.CheckForInvalidFiles(new ITaskItem[] { new MockTaskItem { ItemSpec = "AssemblyInfo.vb" } }, projectFile); + InvalidFileChecker.CheckForInvalidFiles(new[] { "AssemblyInfo.vb" }, projectFile); } } diff --git a/src/GitVersionTask.Tests/Mocks/MockBuildEngine.cs b/src/GitVersionTask.Tests/Mocks/MockBuildEngine.cs deleted file mode 100644 index 710462eff5..0000000000 --- a/src/GitVersionTask.Tests/Mocks/MockBuildEngine.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections; -using Microsoft.Build.Framework; - -class MockBuildEngine : IBuildEngine -{ - public void LogErrorEvent(BuildErrorEventArgs e) - { } - - public void LogWarningEvent(BuildWarningEventArgs e) - { } - - public void LogMessageEvent(BuildMessageEventArgs e) - { } - - public void LogCustomEvent(CustomBuildEventArgs e) - { - throw new NotImplementedException(); - } - - public bool BuildProjectFile(string projectFileName, string[] targetNames, IDictionary globalProperties, IDictionary targetOutputs) - { - throw new NotImplementedException(); - } - - public bool ContinueOnError - { - get { throw new NotImplementedException(); } - } - - public int LineNumberOfTaskNode - { - get { throw new NotImplementedException(); } - } - - public int ColumnNumberOfTaskNode - { - get { throw new NotImplementedException(); } - } - - public string ProjectFileOfTaskNode - { - get { throw new NotImplementedException(); } - } -} \ No newline at end of file diff --git a/src/GitVersionTask.Tests/Mocks/MockTaskItem.cs b/src/GitVersionTask.Tests/Mocks/MockTaskItem.cs deleted file mode 100644 index 45a98850b9..0000000000 --- a/src/GitVersionTask.Tests/Mocks/MockTaskItem.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using System.Collections; -using Microsoft.Build.Framework; - -namespace GitVersionTask.Tests.Mocks -{ - class MockTaskItem : ITaskItem - { - public string ItemSpec { get; set; } - - public int MetadataCount { get; private set; } - - public ICollection MetadataNames { get; private set; } - - public IDictionary CloneCustomMetadata() - { - throw new NotImplementedException(); - } - - public void CopyMetadataTo(ITaskItem destinationItem) - { - throw new NotImplementedException(); - } - - public string GetMetadata(string metadataName) - { - throw new NotImplementedException(); - } - - public void RemoveMetadata(string metadataName) - { - throw new NotImplementedException(); - } - - public void SetMetadata(string metadataName, string metadataValue) - { - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff --git a/src/GitVersionTask/GenerateGitVersionInformation.cs b/src/GitVersionTask/GenerateGitVersionInformation.cs index 6cb61e7034..64c083e5d5 100644 --- a/src/GitVersionTask/GenerateGitVersionInformation.cs +++ b/src/GitVersionTask/GenerateGitVersionInformation.cs @@ -1,42 +1,52 @@ namespace GitVersionTask { + using System; using System.IO; using GitVersion; using GitVersion.Helpers; - using Microsoft.Build.Framework; - public class GenerateGitVersionInformation : GitVersionTaskBase + public static class GenerateGitVersionInformation { - [Required] - public string ProjectFile { get; set; } - - [Required] - public string IntermediateOutputPath { get; set; } - - [Required] - public string Language { get; set; } - - [Output] - public string GitVersionInformationFilePath { get; set; } + // This method is entrypoint for the task declared in .props file + public static Output Execute(Input input) + { + return GitVersionTaskCommonFunctionality.ExecuteGitVersionTask(input, InnerExecute); + } - protected override void InnerExecute() + private static Output InnerExecute(Input input, TaskLogger logger) { - if (GetVersionVariables(out var versionVariables)) return; + var execute = GitVersionTaskCommonFunctionality.CreateExecuteCore(); + if (!execute.TryGetVersion(input.SolutionDirectory, out var versionVariables, input.NoFetch, new Authentication())) + { + return null; + } - var fileExtension = TaskUtils.GetFileExtension(Language); - var fileName = $"GitVersionInformation.g.{fileExtension}"; + var fileWriteInfo = input.IntermediateOutputPath.GetFileWriteInfo( + input.Language, + input.ProjectFile, + (pf, ext) => $"GitVersionInformation.g.{ext}", + (pf, ext) => $"GitVersionInformation_{Path.GetFileNameWithoutExtension(pf)}_{Path.GetRandomFileName()}.g.{ext}" + ); - if (IntermediateOutputPath == null) + var output = new Output() { - fileName = $"GitVersionInformation_{Path.GetFileNameWithoutExtension(ProjectFile)}_{Path.GetRandomFileName()}.g.{fileExtension}"; - } + GitVersionInformationFilePath = Path.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName) + }; + var generator = new GitVersionInformationGenerator(fileWriteInfo.FileName, fileWriteInfo.WorkingDirectory, versionVariables, new FileSystem()); + generator.Generate(); - var workingDirectory = IntermediateOutputPath ?? TempFileTracker.TempPath; + return output; + } - GitVersionInformationFilePath = Path.Combine(workingDirectory, fileName); - var generator = new GitVersionInformationGenerator(fileName, workingDirectory, versionVariables, new FileSystem()); - generator.Generate(); + public sealed class Input : InputWithCommonAdditionalProperties + { } + + public sealed class Output + { + public string GitVersionInformationFilePath { get; set; } + } + } } diff --git a/src/GitVersionTask/GetVersion.cs b/src/GitVersionTask/GetVersion.cs index 5b8dcb0244..8dd66e960d 100644 --- a/src/GitVersionTask/GetVersion.cs +++ b/src/GitVersionTask/GetVersion.cs @@ -1,108 +1,99 @@ namespace GitVersionTask { - using Microsoft.Build.Framework; + using System; - public class GetVersion : GitVersionTaskBase + public static class GetVersion { - [Output] - public string Major { get; set; } - [Output] - public string Minor { get; set; } + public static Output Execute(Input input) + { + return GitVersionTaskCommonFunctionality.ExecuteGitVersionTask(input, InnerExecute); + } - [Output] - public string Patch { get; set; } + private static Output InnerExecute(Input input, TaskLogger logger) + { + if (!GitVersionTaskCommonFunctionality.CreateExecuteCore().TryGetVersion(input.SolutionDirectory, out var versionVariables, input.NoFetch, new GitVersion.Authentication())) + { + return null; + } - [Output] - public string PreReleaseTag { get; set; } + var outputType = typeof(Output); + var output = new Output(); + foreach (var variable in versionVariables) + { + outputType.GetProperty(variable.Key).SetValue(output, variable.Value, null); + } - [Output] - public string PreReleaseTagWithDash { get; set; } + return output; + } - [Output] - public string PreReleaseLabel { get; set; } + public sealed class Input : InputBase + { - [Output] - public string PreReleaseNumber { get; set; } + } - [Output] - public string WeightedPreReleaseNumber { get; set; } + public sealed class Output + { + public string Major { get; set; } - [Output] - public string BuildMetaData { get; set; } + public string Minor { get; set; } - [Output] - public string BuildMetaDataPadded { get; set; } + public string Patch { get; set; } - [Output] - public string FullBuildMetaData { get; set; } + public string PreReleaseTag { get; set; } - [Output] - public string MajorMinorPatch { get; set; } + public string PreReleaseTagWithDash { get; set; } - [Output] - public string SemVer { get; set; } + public string PreReleaseLabel { get; set; } - [Output] - public string LegacySemVer { get; set; } + public string PreReleaseNumber { get; set; } - [Output] - public string LegacySemVerPadded { get; set; } + public string WeightedPreReleaseNumber { get; set; } - [Output] - public string AssemblySemVer { get; set; } + public string BuildMetaData { get; set; } - [Output] - public string AssemblySemFileVer { get; set; } + public string BuildMetaDataPadded { get; set; } - [Output] - public string FullSemVer { get; set; } + public string FullBuildMetaData { get; set; } - [Output] - public string InformationalVersion { get; set; } + public string MajorMinorPatch { get; set; } - [Output] - public string BranchName { get; set; } + public string SemVer { get; set; } - [Output] - public string Sha { get; set; } + public string LegacySemVer { get; set; } - [Output] - public string ShortSha { get; set; } + public string LegacySemVerPadded { get; set; } - [Output] - public string NuGetVersionV2 { get; set; } + public string AssemblySemVer { get; set; } - [Output] - public string NuGetVersion { get; set; } + public string AssemblySemFileVer { get; set; } - [Output] - public string NuGetPreReleaseTagV2 { get; set; } + public string FullSemVer { get; set; } - [Output] - public string NuGetPreReleaseTag { get; set; } + public string InformationalVersion { get; set; } - [Output] - public string CommitDate { get; set; } + public string BranchName { get; set; } - [Output] - public string VersionSourceSha { get; set; } + public string Sha { get; set; } - [Output] - public string CommitsSinceVersionSource { get; set; } + public string ShortSha { get; set; } - [Output] - public string CommitsSinceVersionSourcePadded { get; set; } + public string NuGetVersionV2 { get; set; } - protected override void InnerExecute() - { - if (GetVersionVariables(out var versionVariables)) return; - - var thisType = typeof(GetVersion); - foreach (var variable in versionVariables) - { - thisType.GetProperty(variable.Key)?.SetValue(this, variable.Value, null); - } + public string NuGetVersion { get; set; } + + public string NuGetPreReleaseTagV2 { get; set; } + + public string NuGetPreReleaseTag { get; set; } + + public string CommitDate { get; set; } + + public string VersionSourceSha { get; set; } + + public string CommitsSinceVersionSource { get; set; } + + public string CommitsSinceVersionSourcePadded { get; set; } } + } } diff --git a/src/GitVersionTask/GitVersionTask.csproj b/src/GitVersionTask/GitVersionTask.csproj index 4aa0a34c77..2bd1428419 100644 --- a/src/GitVersionTask/GitVersionTask.csproj +++ b/src/GitVersionTask/GitVersionTask.csproj @@ -1,7 +1,7 @@ - + net461;netstandard2.0 @@ -17,16 +17,16 @@ NugetAssets\GitVersionTask.nuspec 0.0.1-alpha-0001 - version=$(PackageVersion);configuration=$(Configuration);utilpackversion=$(PackageVersion_UtilPackNuGetMSBuild);libgit2sharpversion=$(PackageVersion_LibGit2Sharp);yamldotnetversion=$(PackageVersion_YamlDotNet) + version=$(PackageVersion);configuration=$(Configuration);nugetutilsversion=$(PackageVersion_NuGetUtilsMSBuildExec);libgit2sharpversion=$(PackageVersion_LibGit2Sharp);yamldotnetversion=$(PackageVersion_YamlDotNet) $(AssemblyName) - + - + build\ @@ -38,8 +38,8 @@ All - - + + all runtime; build; native; contentfiles; analyzers diff --git a/src/GitVersionTask/GitVersionTaskBase.cs b/src/GitVersionTask/GitVersionTaskBase.cs deleted file mode 100644 index 37805297b5..0000000000 --- a/src/GitVersionTask/GitVersionTaskBase.cs +++ /dev/null @@ -1,72 +0,0 @@ -namespace GitVersionTask -{ - using System; - using GitVersion; - using GitVersion.Helpers; - - using Microsoft.Build.Framework; - using Microsoft.Build.Utilities; - - public abstract class GitVersionTaskBase : Task - { - protected GitVersionTaskBase() - { - var fileSystem = new FileSystem(); - ExecuteCore = new ExecuteCore(fileSystem); - GitVersion.Logger.SetLoggers(LogDebug, LogInfo, LogWarning, s => LogError(s)); - } - - public override bool Execute() - { - try - { - InnerExecute(); - return true; - } - catch (WarningException errorException) - { - LogWarning(errorException.Message); - return true; - } - catch (Exception exception) - { - LogError("Error occurred: " + exception); - return false; - } - } - - protected abstract void InnerExecute(); - - protected ExecuteCore ExecuteCore { get; } - - [Required] - public string SolutionDirectory { get; set; } - - public bool NoFetch { get; set; } - - public void LogDebug(string message) - { - BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Low)); - } - - public void LogWarning(string message) - { - BuildEngine.LogWarningEvent(new BuildWarningEventArgs(string.Empty, string.Empty, null, 0, 0, 0, 0, message, string.Empty, "GitVersionTask")); - } - - public void LogInfo(string message) - { - BuildEngine.LogMessageEvent(new BuildMessageEventArgs(message, string.Empty, "GitVersionTask", MessageImportance.Normal)); - } - - public void LogError(string message, string file = null) - { - BuildEngine.LogErrorEvent(new BuildErrorEventArgs(string.Empty, string.Empty, file, 0, 0, 0, 0, message, string.Empty, "GitVersionTask")); - } - - protected bool GetVersionVariables(out VersionVariables versionVariables) - { - return !ExecuteCore.TryGetVersion(SolutionDirectory, out versionVariables, NoFetch, new Authentication()); - } - } -} diff --git a/src/GitVersionTask/GitVersionTaskCommonFunctionality.cs b/src/GitVersionTask/GitVersionTaskCommonFunctionality.cs new file mode 100644 index 0000000000..7851a51783 --- /dev/null +++ b/src/GitVersionTask/GitVersionTaskCommonFunctionality.cs @@ -0,0 +1,151 @@ +namespace GitVersionTask +{ + using GitVersion; + using GitVersion.Helpers; + using System; + using System.IO; + + public static class GitVersionTaskCommonFunctionality + { + internal static TOutput ExecuteGitVersionTask(TInput input, Func execute) + where TInput : InputBase + where TOutput : class, new() + { + + input.ValidateInputOrThrowException(); + + var logger = new TaskLogger(); + Logger.SetLoggers(logger.LogInfo, logger.LogInfo, logger.LogWarning, s => logger.LogError(s)); + + + TOutput output = null; + try + { + output = execute(input, logger); + } + catch (WarningException errorException) + { + logger.LogWarning(errorException.Message); + output = new TOutput(); + } + catch (Exception exception) + { + logger.LogError("Error occurred: " + exception); + throw; + } + finally + { + Logger.Reset(); + } + + return output; + } + + + public static ExecuteCore CreateExecuteCore() + => new ExecuteCore(new FileSystem()); + + private static string GetFileExtension(this string language) + { + switch (language) + { + case "C#": + return "cs"; + + case "F#": + return "fs"; + + case "VB": + return "vb"; + + default: + throw new ArgumentException($"Unknown language detected: '{language}'"); + } + } + + public static FileWriteInfo GetFileWriteInfo( + this string intermediateOutputPath, + string language, + string projectFile, + Func fileNameWithIntermediatePath, + Func fileNameNoIntermediatePath + ) + { + var fileExtension = language.GetFileExtension(); + string workingDirectory, fileName; + if (intermediateOutputPath == null) + { + fileName = fileNameWithIntermediatePath(projectFile, fileExtension); + workingDirectory = TempFileTracker.TempPath; + } + else + { + workingDirectory = intermediateOutputPath; + fileName = fileNameNoIntermediatePath(projectFile, fileExtension); + } + return new FileWriteInfo(workingDirectory, fileName, fileExtension); + } + + } + + public abstract class InputBase + { + public string SolutionDirectory { get; set; } + + public Boolean NoFetch { get; set; } + + public void ValidateInputOrThrowException() + { + if (!this.ValidateInput()) + { + throw new InputValidationException($"Invalid input for {this.GetType()}."); + } + } + + protected virtual Boolean ValidateInput() + { + return !String.IsNullOrEmpty(this.SolutionDirectory); + } + } + + public abstract class InputWithCommonAdditionalProperties : InputBase + { + public string ProjectFile { get; set; } + + public string IntermediateOutputPath { get; set; } + + public string Language { get; set; } + + protected override Boolean ValidateInput() + { + return base.ValidateInput() + && !String.IsNullOrEmpty(this.ProjectFile) + && !String.IsNullOrEmpty(this.IntermediateOutputPath) + && !String.IsNullOrEmpty(this.Language); + } + } + + public sealed class InputValidationException : Exception + { + public InputValidationException(string msg, Exception inner = null) + : base(msg, inner) + { + + } + } + + public sealed class FileWriteInfo + { + public FileWriteInfo(string workingDirectory, string fileName, string fileExtension) + { + this.WorkingDirectory = workingDirectory; + this.FileName = fileName; + this.FileExtension = fileExtension; + } + + public string WorkingDirectory { get; } + public string FileName { get; } + public string FileExtension { get; } + } + +} diff --git a/src/GitVersionTask/InvalidFileChecker.cs b/src/GitVersionTask/InvalidFileChecker.cs index d20be03e56..019f502208 100644 --- a/src/GitVersionTask/InvalidFileChecker.cs +++ b/src/GitVersionTask/InvalidFileChecker.cs @@ -4,7 +4,6 @@ using System.Linq; using System.Text.RegularExpressions; using GitVersion; -using Microsoft.Build.Framework; public static class InvalidFileChecker { @@ -14,7 +13,7 @@ public static class InvalidFileChecker { ".vb", VisualBasicFileContainsVersionAttribute } }; - public static void CheckForInvalidFiles(IEnumerable compileFiles, string projectFile) + public static void CheckForInvalidFiles(IEnumerable compileFiles, string projectFile) { foreach (var compileFile in GetInvalidFiles(compileFiles, projectFile)) { @@ -98,9 +97,9 @@ static bool VisualBasicFileContainsVersionAttribute(string compileFile, string p \s*\(\s*\)\s*\> # End brackets ()>"); } - static IEnumerable GetInvalidFiles(IEnumerable compileFiles, string projectFile) + static IEnumerable GetInvalidFiles(IEnumerable compileFiles, string projectFile) { - return compileFiles.Select(x => x.ItemSpec) + return compileFiles .Where(compileFile => compileFile.Contains("AssemblyInfo")) .Where(s => FileContainsVersionAttribute(s, projectFile)); } diff --git a/src/GitVersionTask/UtilPack.Version.props b/src/GitVersionTask/NuGetUtils.MSBuild.Exec.Version.props similarity index 57% rename from src/GitVersionTask/UtilPack.Version.props rename to src/GitVersionTask/NuGetUtils.MSBuild.Exec.Version.props index 109fbf9d1e..a00890c752 100644 --- a/src/GitVersionTask/UtilPack.Version.props +++ b/src/GitVersionTask/NuGetUtils.MSBuild.Exec.Version.props @@ -1,6 +1,6 @@ - 2.9.1 + 2.0.5 diff --git a/src/GitVersionTask/NugetAssets/GitVersionTask.nuspec b/src/GitVersionTask/NugetAssets/GitVersionTask.nuspec index b3436fdce8..2c6d00d55a 100644 --- a/src/GitVersionTask/NugetAssets/GitVersionTask.nuspec +++ b/src/GitVersionTask/NugetAssets/GitVersionTask.nuspec @@ -1,4 +1,4 @@ - + GitVersionTask @@ -17,15 +17,15 @@ Git Versioning GitVersion GitFlowVersion GitFlow GitHubFlow SemVer - + - + - + @@ -33,6 +33,6 @@ - + diff --git a/src/GitVersionTask/NugetAssets/build/GitVersionTask.targets b/src/GitVersionTask/NugetAssets/build/GitVersionTask.targets index d816d04563..fc1e76270d 100644 --- a/src/GitVersionTask/NugetAssets/build/GitVersionTask.targets +++ b/src/GitVersionTask/NugetAssets/build/GitVersionTask.targets @@ -1,4 +1,4 @@ - + false diff --git a/src/GitVersionTask/NugetAssets/build/Infrastructure.props b/src/GitVersionTask/NugetAssets/build/Infrastructure.props index e5ff8092e1..dbdb4e8f01 100644 --- a/src/GitVersionTask/NugetAssets/build/Infrastructure.props +++ b/src/GitVersionTask/NugetAssets/build/Infrastructure.props @@ -1,5 +1,5 @@ - - + + @@ -9,33 +9,32 @@ $(GitVersionTaskBuildTools_FunctionalityDir)obj/ - - $(PackageVersion_UtilPackNuGetMSBuild) - $(MSBuildThisFileDirectory)../../../utilpack.nuget.msbuild/$(UtilPackVersion)/build/UtilPack.NuGet.MSBuild.props - $([System.IO.Path]::GetFullPath('$(UtilPackNuGetMSBuildPropsPath)')) + + $(PackageVersion_NuGetUtilsMSBuildExec) + $(MSBuildThisFileDirectory)../../../nugetutils.msbuild.exec/$(NuGetUtilsVersion)/build/NuGetUtils.MSBuild.Exec.props + $([System.IO.Path]::GetFullPath('$(NuGetUtilsMSBuildPropsPath)')) - + - true + true - - .NETFramework - 4.6.1 - - - $(UtilPackTaskFactoryParametersXML) + + net461 + + + $(NuGetUtilsTaskFactoryParametersXML) true GitVersionTask.dll - + - + - + diff --git a/src/GitVersionTask/NugetAssets/build/functionality/GitVersionBuild.targets b/src/GitVersionTask/NugetAssets/build/functionality/GitVersionBuild.targets index ff6c8923cf..86871475b1 100644 --- a/src/GitVersionTask/NugetAssets/build/functionality/GitVersionBuild.targets +++ b/src/GitVersionTask/NugetAssets/build/functionality/GitVersionBuild.targets @@ -1,4 +1,4 @@ - + diff --git a/src/GitVersionTask/NugetAssets/build/functionality/GitVersionCommon.props b/src/GitVersionTask/NugetAssets/build/functionality/GitVersionCommon.props index ebf736a940..cef2a6fce6 100644 --- a/src/GitVersionTask/NugetAssets/build/functionality/GitVersionCommon.props +++ b/src/GitVersionTask/NugetAssets/build/functionality/GitVersionCommon.props @@ -1,4 +1,4 @@ - + $(MSBuildProjectDirectory)\..\ @@ -43,43 +43,55 @@ - $(UtilPackTaskFactoryParametersXML) + + $(NuGetUtilsTaskFactoryParametersXML) + Execute + - $(UtilPackTaskFactoryParametersXML) + + $(NuGetUtilsTaskFactoryParametersXML) + Execute + - $(UtilPackTaskFactoryParametersXML) + + $(NuGetUtilsTaskFactoryParametersXML) + Execute + - $(UtilPackTaskFactoryParametersXML) + + $(NuGetUtilsTaskFactoryParametersXML) + Execute + True - \ No newline at end of file + diff --git a/src/GitVersionTask/NugetAssets/build/functionality/GitVersionMultiTargetBuild.targets b/src/GitVersionTask/NugetAssets/build/functionality/GitVersionMultiTargetBuild.targets index 379ff192cb..6441c4d259 100644 --- a/src/GitVersionTask/NugetAssets/build/functionality/GitVersionMultiTargetBuild.targets +++ b/src/GitVersionTask/NugetAssets/build/functionality/GitVersionMultiTargetBuild.targets @@ -1,4 +1,4 @@ - + diff --git a/src/GitVersionTask/TaskLogger.cs b/src/GitVersionTask/TaskLogger.cs new file mode 100644 index 0000000000..6eb1e92e56 --- /dev/null +++ b/src/GitVersionTask/TaskLogger.cs @@ -0,0 +1,29 @@ +using System; +using System.IO; + +class TaskLogger +{ + private readonly TextWriter stdout; + private readonly TextWriter stderr; + + public TaskLogger(TextWriter paramStdout = null, TextWriter paramStderr = null) + { + this.stdout = paramStdout ?? Console.Out; + this.stderr = paramStderr ?? Console.Error; + } + + public void LogWarning(string message) + { + this.stdout.WriteLine(message); + } + + public void LogInfo(string message) + { + this.stdout.WriteLine(message); + } + + public void LogError(string message) + { + this.stderr.WriteLine(message); + } +} diff --git a/src/GitVersionTask/TaskUtils.cs b/src/GitVersionTask/TaskUtils.cs deleted file mode 100644 index ad0be50887..0000000000 --- a/src/GitVersionTask/TaskUtils.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace GitVersionTask -{ - using System; - - public class TaskUtils - { - public static string GetFileExtension(string language) - { - switch(language) - { - case "C#": - return "cs"; - - case "F#": - return "fs"; - - case "VB": - return "vb"; - - default: - throw new Exception($"Unknown language detected: '{language}'"); - } - } - } -} \ No newline at end of file diff --git a/src/GitVersionTask/UpdateAssemblyInfo.cs b/src/GitVersionTask/UpdateAssemblyInfo.cs index e386bbae8f..c3f2e72481 100644 --- a/src/GitVersionTask/UpdateAssemblyInfo.cs +++ b/src/GitVersionTask/UpdateAssemblyInfo.cs @@ -1,58 +1,72 @@ namespace GitVersionTask { + using System; using System.IO; using GitVersion; using GitVersion.Helpers; - using Microsoft.Build.Framework; - public class UpdateAssemblyInfo : GitVersionTaskBase + public static class UpdateAssemblyInfo { - [Required] - public string ProjectFile { get; set; } - - [Required] - public string IntermediateOutputPath { get; set; } - - [Required] - public ITaskItem[] CompileFiles { get; set; } - - [Required] - public string Language { get; set; } - - [Output] - public string AssemblyInfoTempFilePath { get; set; } + public static Output Execute(Input input) + { + return GitVersionTaskCommonFunctionality.ExecuteGitVersionTask(input, InnerExecute); + } - protected override void InnerExecute() + private static Output InnerExecute(Input input, TaskLogger logger) { + var execute = GitVersionTaskCommonFunctionality.CreateExecuteCore(); + TempFileTracker.DeleteTempFiles(); - InvalidFileChecker.CheckForInvalidFiles(CompileFiles, ProjectFile); + InvalidFileChecker.CheckForInvalidFiles(input.CompileFiles, input.ProjectFile); - if (GetVersionVariables(out var versionVariables)) return; + if (!execute.TryGetVersion(input.SolutionDirectory, out var versionVariables, input.NoFetch, new Authentication())) + { + return null; + } - CreateTempAssemblyInfo(versionVariables); + return CreateTempAssemblyInfo(input, versionVariables); } - private void CreateTempAssemblyInfo(VersionVariables versionVariables) + private static Output CreateTempAssemblyInfo(Input input, VersionVariables versionVariables) { - var fileExtension = TaskUtils.GetFileExtension(Language); - var assemblyInfoFileName = $"GitVersionTaskAssemblyInfo.g.{fileExtension}"; + var fileWriteInfo = input.IntermediateOutputPath.GetFileWriteInfo( + input.Language, + input.ProjectFile, + (pf, ext) => $"GitVersionTaskAssemblyInfo.g.{ext}", + (pf, ext) => $"AssemblyInfo_{Path.GetFileNameWithoutExtension(pf)}_{Path.GetRandomFileName()}.g.{ext}" + ); + + var output = new Output() + { + AssemblyInfoTempFilePath = Path.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName) + }; - if (IntermediateOutputPath == null) + using (var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(fileWriteInfo.FileName, fileWriteInfo.WorkingDirectory, versionVariables, new FileSystem(), true)) { - assemblyInfoFileName = $"AssemblyInfo_{Path.GetFileNameWithoutExtension(ProjectFile)}_{Path.GetRandomFileName()}.g.{fileExtension}"; + assemblyInfoFileUpdater.Update(); + assemblyInfoFileUpdater.CommitChanges(); } - var workingDirectory = IntermediateOutputPath ?? TempFileTracker.TempPath; + return output; + } - AssemblyInfoTempFilePath = Path.Combine(workingDirectory, assemblyInfoFileName); + public sealed class Input : InputWithCommonAdditionalProperties + { + public string[] CompileFiles { get; set; } - using (var assemblyInfoFileUpdater = new AssemblyInfoFileUpdater(assemblyInfoFileName, workingDirectory, versionVariables, new FileSystem(), true)) + protected override Boolean ValidateInput() { - assemblyInfoFileUpdater.Update(); - assemblyInfoFileUpdater.CommitChanges(); + return base.ValidateInput() + && this.CompileFiles != null; } + + } + + public sealed class Output + { + public string AssemblyInfoTempFilePath { get; set; } } } } diff --git a/src/GitVersionTask/WriteVersionInfoToBuildLog.cs b/src/GitVersionTask/WriteVersionInfoToBuildLog.cs index 03340b7947..6f6b0d19b2 100644 --- a/src/GitVersionTask/WriteVersionInfoToBuildLog.cs +++ b/src/GitVersionTask/WriteVersionInfoToBuildLog.cs @@ -3,25 +3,46 @@ namespace GitVersionTask using System.Collections.Generic; using GitVersion; - public class WriteVersionInfoToBuildLog : GitVersionTaskBase + public class WriteVersionInfoToBuildLog { - protected override void InnerExecute() + public static Output Execute(Input input) { - if (GetVersionVariables(out var versionVariables)) return; + return GitVersionTaskCommonFunctionality.ExecuteGitVersionTask(input, InnerExecute); + } + + public sealed class Input : InputBase + { + // No additional inputs for this task + } + + public sealed class Output + { + // No output for this task + } + + private static Output InnerExecute(Input input, TaskLogger logger) + { + var execute = GitVersionTaskCommonFunctionality.CreateExecuteCore(); + if (!execute.TryGetVersion(input.SolutionDirectory, out var result, input.NoFetch, new Authentication())) + { + return null; + } + + WriteIntegrationParameters(logger, BuildServerList.GetApplicableBuildServers(), result); - WriteIntegrationParameters(BuildServerList.GetApplicableBuildServers(), versionVariables); + return new Output(); } - private void WriteIntegrationParameters(IEnumerable applicableBuildServers, VersionVariables versionVariables) + private static void WriteIntegrationParameters(TaskLogger logger, IEnumerable applicableBuildServers, VersionVariables versionVariables) { foreach (var buildServer in applicableBuildServers) { - LogInfo($"Executing GenerateSetVersionMessage for '{buildServer.GetType().Name}'."); - LogInfo(buildServer.GenerateSetVersionMessage(versionVariables)); - LogInfo($"Executing GenerateBuildLogOutput for '{buildServer.GetType().Name}'."); + logger.LogInfo($"Executing GenerateSetVersionMessage for '{ buildServer.GetType().Name }'."); + logger.LogInfo(buildServer.GenerateSetVersionMessage(versionVariables)); + logger.LogInfo($"Executing GenerateBuildLogOutput for '{ buildServer.GetType().Name }'."); foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(buildServer, versionVariables)) { - LogInfo(buildParameter); + logger.LogInfo(buildParameter); } } }