From fb8a1623920835e89fca34bfb0e0536293ae32a3 Mon Sep 17 00:00:00 2001 From: apmoskevitz Date: Wed, 5 Jul 2023 12:49:43 -0400 Subject: [PATCH 1/8] added unique namespace generation (see issue #3606) --- .../Abstractions/IGitVersionOutputTool.cs | 2 + .../Core/GitVersionOutputTool.cs | 26 ++-- src/GitVersion.Core/Model/FileWriteInfo.cs | 1 + .../Model/GitVersionOptions.cs | 2 + src/GitVersion.Core/PublicAPI.Unshipped.txt | 6 + .../GitVersionInfo/GitVersionInfoContext.cs | 8 +- .../GitVersionInfo/GitVersionInfoGenerator.cs | 39 ++++-- .../Templates/GitVersionInformation.cs | 12 +- .../Templates/GitVersionInformation.fs | 2 +- .../Templates/GitVersionInformation.vb | 2 +- .../GenerateGitVersionInformationTest.cs | 114 +++++++++++++++++- src/GitVersion.MsBuild/GitVersionTaskBase.cs | 1 + .../GitVersionTaskExecutor.cs | 16 ++- .../PublicAPI.Unshipped.txt | 4 + .../Tasks/GenerateGitVersionInformation.cs | 4 + .../msbuild/tools/GitVersion.MsBuild.targets | 4 +- 16 files changed, 207 insertions(+), 36 deletions(-) diff --git a/src/GitVersion.Core/Core/Abstractions/IGitVersionOutputTool.cs b/src/GitVersion.Core/Core/Abstractions/IGitVersionOutputTool.cs index a20f5a9ecf..f53966267f 100644 --- a/src/GitVersion.Core/Core/Abstractions/IGitVersionOutputTool.cs +++ b/src/GitVersion.Core/Core/Abstractions/IGitVersionOutputTool.cs @@ -8,4 +8,6 @@ public interface IGitVersionOutputTool void UpdateAssemblyInfo(VersionVariables variables); void UpdateWixVersionFile(VersionVariables variables); void GenerateGitVersionInformation(VersionVariables variables, FileWriteInfo fileWriteInfo); + void GenerateGitVersionInformation(VersionVariables variables, FileWriteInfo fileWriteInfo, string? targetNamespace = null); + } diff --git a/src/GitVersion.Core/Core/GitVersionOutputTool.cs b/src/GitVersion.Core/Core/GitVersionOutputTool.cs index 2de67aeb6a..8ceec891cb 100644 --- a/src/GitVersion.Core/Core/GitVersionOutputTool.cs +++ b/src/GitVersion.Core/Core/GitVersionOutputTool.cs @@ -22,7 +22,7 @@ public GitVersionOutputTool(IOptions options, IGitVersionInfoGenerator gitVersionInfoGenerator, IAssemblyInfoFileUpdater assemblyInfoFileUpdater, IProjectFileUpdater projectFileUpdater) { - this.gitVersionOptions = options.Value.NotNull(); + gitVersionOptions = options.Value.NotNull(); this.outputGenerator = outputGenerator.NotNull(); @@ -34,9 +34,9 @@ public GitVersionOutputTool(IOptions options, public void OutputVariables(VersionVariables variables, bool updateBuildNumber) { - using (this.outputGenerator) + using (outputGenerator) { - this.outputGenerator.Execute(variables, new OutputContext(gitVersionOptions.WorkingDirectory, gitVersionOptions.OutputFile, updateBuildNumber)); + outputGenerator.Execute(variables, new OutputContext(gitVersionOptions.WorkingDirectory, gitVersionOptions.OutputFile, updateBuildNumber)); } } @@ -46,16 +46,16 @@ public void UpdateAssemblyInfo(VersionVariables variables) if (gitVersionOptions.AssemblyInfo.UpdateProjectFiles) { - using (this.projectFileUpdater) + using (projectFileUpdater) { - this.projectFileUpdater.Execute(variables, assemblyInfoContext); + projectFileUpdater.Execute(variables, assemblyInfoContext); } } else if (gitVersionOptions.AssemblyInfo.UpdateAssemblyInfo) { - using (this.assemblyInfoFileUpdater) + using (assemblyInfoFileUpdater) { - this.assemblyInfoFileUpdater.Execute(variables, assemblyInfoContext); + assemblyInfoFileUpdater.Execute(variables, assemblyInfoContext); } } } @@ -64,18 +64,20 @@ public void UpdateWixVersionFile(VersionVariables variables) { if (gitVersionOptions.WixInfo.ShouldUpdate) { - using (this.wixVersionFileUpdater) + using (wixVersionFileUpdater) { - this.wixVersionFileUpdater.Execute(variables, new WixVersionContext(gitVersionOptions.WorkingDirectory)); + wixVersionFileUpdater.Execute(variables, new WixVersionContext(gitVersionOptions.WorkingDirectory)); } } } - public void GenerateGitVersionInformation(VersionVariables variables, FileWriteInfo fileWriteInfo) + public void GenerateGitVersionInformation(VersionVariables variables, FileWriteInfo fileWriteInfo, string? targetNamespace = null) { - using (this.gitVersionInfoGenerator) + using (gitVersionInfoGenerator) { - this.gitVersionInfoGenerator.Execute(variables, new GitVersionInfoContext(gitVersionOptions.WorkingDirectory, fileWriteInfo.FileName, fileWriteInfo.FileExtension)); + gitVersionInfoGenerator.Execute(variables, new GitVersionInfoContext(gitVersionOptions.WorkingDirectory, fileWriteInfo.FileName, fileWriteInfo.FileExtension, targetNamespace)); } } + + public void GenerateGitVersionInformation(VersionVariables variables, FileWriteInfo fileWriteInfo) => GenerateGitVersionInformation(variables, fileWriteInfo, null); } diff --git a/src/GitVersion.Core/Model/FileWriteInfo.cs b/src/GitVersion.Core/Model/FileWriteInfo.cs index 976dd386c1..2653353c63 100644 --- a/src/GitVersion.Core/Model/FileWriteInfo.cs +++ b/src/GitVersion.Core/Model/FileWriteInfo.cs @@ -12,4 +12,5 @@ public FileWriteInfo(string workingDirectory, string fileName, string fileExtens public string WorkingDirectory { get; } public string FileName { get; } public string FileExtension { get; } + } diff --git a/src/GitVersion.Core/Model/GitVersionOptions.cs b/src/GitVersion.Core/Model/GitVersionOptions.cs index b0cba50076..d0179e04a4 100644 --- a/src/GitVersion.Core/Model/GitVersionOptions.cs +++ b/src/GitVersion.Core/Model/GitVersionOptions.cs @@ -14,6 +14,8 @@ public class GitVersionOptions public WixInfo WixInfo { get; } = new(); public Settings Settings { get; } = new(); + public bool UniqueNamespace { get; set; } + public bool Init; public bool Diag; public bool IsVersion; diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index e69de29bb2..854e0b7ffd 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -0,0 +1,6 @@ +GitVersion.GitVersionOptions.UniqueNamespace.get -> bool +GitVersion.GitVersionOptions.UniqueNamespace.set -> void +GitVersion.GitVersionOutputTool.GenerateGitVersionInformation(GitVersion.OutputVariables.VersionVariables! variables, GitVersion.FileWriteInfo! fileWriteInfo, string? targetNamespace = null) -> void +GitVersion.IGitVersionOutputTool.GenerateGitVersionInformation(GitVersion.OutputVariables.VersionVariables! variables, GitVersion.FileWriteInfo! fileWriteInfo, string? targetNamespace = null) -> void +GitVersion.VersionConverters.GitVersionInfo.GitVersionInfoContext.GitVersionInfoContext(string! workingDirectory, string! fileName, string! fileExtension, string? targetNamespace = null) -> void +GitVersion.VersionConverters.GitVersionInfo.GitVersionInfoContext.TargetNamespace.get -> string? diff --git a/src/GitVersion.Core/VersionConverters/GitVersionInfo/GitVersionInfoContext.cs b/src/GitVersion.Core/VersionConverters/GitVersionInfo/GitVersionInfoContext.cs index ddccc5b0ce..d27f00cb64 100644 --- a/src/GitVersion.Core/VersionConverters/GitVersionInfo/GitVersionInfoContext.cs +++ b/src/GitVersion.Core/VersionConverters/GitVersionInfo/GitVersionInfoContext.cs @@ -2,14 +2,20 @@ namespace GitVersion.VersionConverters.GitVersionInfo; public readonly struct GitVersionInfoContext : IConverterContext { - public GitVersionInfoContext(string workingDirectory, string fileName, string fileExtension) + public GitVersionInfoContext(string workingDirectory, string fileName, string fileExtension) : this(workingDirectory, fileName, fileExtension, null) + { + } + + public GitVersionInfoContext(string workingDirectory, string fileName, string fileExtension, string? targetNamespace = null) { WorkingDirectory = workingDirectory; FileName = fileName; FileExtension = fileExtension; + TargetNamespace = targetNamespace; } public string WorkingDirectory { get; } public string FileName { get; } public string FileExtension { get; } + public string? TargetNamespace { get; } } diff --git a/src/GitVersion.Core/VersionConverters/GitVersionInfo/GitVersionInfoGenerator.cs b/src/GitVersion.Core/VersionConverters/GitVersionInfo/GitVersionInfoGenerator.cs index e02971218e..956d9e2136 100644 --- a/src/GitVersion.Core/VersionConverters/GitVersionInfo/GitVersionInfoGenerator.cs +++ b/src/GitVersion.Core/VersionConverters/GitVersionInfo/GitVersionInfoGenerator.cs @@ -1,6 +1,7 @@ using GitVersion.Extensions; using GitVersion.Helpers; using GitVersion.OutputVariables; +using Polly.CircuitBreaker; namespace GitVersion.VersionConverters.GitVersionInfo; @@ -10,13 +11,14 @@ public interface IGitVersionInfoGenerator : IVersionConverter string.Format(indentation + addFormat, v.Key, v.Value))); - var fileContents = string.Format(template, members); + + var fileContents = string.Format(template, members, targetNamespace, openBracket, closeBracket, indent); if (fileContents != originalFileContents) { - this.fileSystem.WriteAllText(filePath, fileContents); + fileSystem.WriteAllText(filePath, fileContents); } + + string getTargetNamespace(string fileExtension) => fileExtension switch + { + ".vb" => context.TargetNamespace ?? "Global", + ".cs" => context.TargetNamespace != null ? $"{System.Environment.NewLine}namespace {context.TargetNamespace};" : "", + ".fs" => context.TargetNamespace ?? "global", + _ => targetNamespaceSentinelValue, + }; } public void Dispose() diff --git a/src/GitVersion.Core/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.cs b/src/GitVersion.Core/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.cs index 53e9b35ead..12deef9fa3 100644 --- a/src/GitVersion.Core/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.cs +++ b/src/GitVersion.Core/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.cs @@ -23,10 +23,10 @@ namespace System.Diagnostics.CodeAnalysis internal sealed class ExcludeFromCodeCoverageAttribute : global::System.Attribute {{ }} }} #endif - -[global::System.Runtime.CompilerServices.CompilerGenerated] -[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] -static class GitVersionInformation -{{ +{1}{2} +{4}[global::System.Runtime.CompilerServices.CompilerGenerated] +{4}[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] +{4}static class GitVersionInformation +{4}{{ {0} -}} +{4}}}{3} diff --git a/src/GitVersion.Core/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.fs b/src/GitVersion.Core/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.fs index 561bc56f75..0862986636 100644 --- a/src/GitVersion.Core/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.fs +++ b/src/GitVersion.Core/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.fs @@ -24,7 +24,7 @@ namespace System.Diagnostics.CodeAnalysis type ExcludeFromCodeCoverageAttribute() = inherit global.System.Attribute() #endif -namespace global +namespace {1} [] [] diff --git a/src/GitVersion.Core/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.vb b/src/GitVersion.Core/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.vb index d9cd68db27..b7f3b7b923 100644 --- a/src/GitVersion.Core/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.vb +++ b/src/GitVersion.Core/VersionConverters/GitVersionInfo/Templates/GitVersionInformation.vb @@ -24,7 +24,7 @@ Namespace Global.System.Diagnostics.CodeAnalysis End Namespace #End If -Namespace Global +Namespace {1} diff --git a/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs b/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs index cf571cb990..b8cc0ba255 100644 --- a/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs +++ b/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs @@ -31,6 +31,8 @@ public void GenerateGitVersionInformationTaskShouldCreateFile() fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.2.4+1"""); } + + [Test] public void GenerateGitVersionInformationTaskShouldCreateFileInBuildServer() { @@ -50,6 +52,52 @@ public void GenerateGitVersionInformationTaskShouldCreateFileInBuildServer() fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.0.1+1"""); } + [Test] + public void GenerateGitVersionInformationTaskShouldCreateFileWithUniqueNamespaceSetAndRootNamespaceUnSet() + { + var task = new GenerateGitVersionInformation(); + task.ProjectFile = "App.Project.csproj"; + task.GenerateGitVersionInformationInUniqueNamespace = "true"; + using var result = ExecuteMsBuildTask(task); + + result.Success.ShouldBe(true); + result.Errors.ShouldBe(0); + result.Task.GitVersionInformationFilePath.ShouldNotBeNull(); + + var fileContent = File.ReadAllText(result.Task.GitVersionInformationFilePath); + TestContext.WriteLine(fileContent); + fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""2"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""4"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.2.4"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.2.4+1"""); + fileContent.ShouldContain("namespace App.Project"); + } + + [Test] + public void GenerateGitVersionInformationTaskShouldCreateFileWithUniqueNamespaceSetAndRootNamespaceIsSet() + { + var task = new GenerateGitVersionInformation(); + task.ProjectFile = "App.Project.csproj"; + task.RootNamespace = "App.Project.RootNamespace"; + task.GenerateGitVersionInformationInUniqueNamespace = "true"; + using var result = ExecuteMsBuildTask(task); + + result.Success.ShouldBe(true); + result.Errors.ShouldBe(0); + result.Task.GitVersionInformationFilePath.ShouldNotBeNull(); + + var fileContent = File.ReadAllText(result.Task.GitVersionInformationFilePath); + TestContext.WriteLine(fileContent); + fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""2"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""4"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.2.4"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.2.4+1"""); + + fileContent.ShouldContain("namespace App.Project.RootNamespace"); + } + [Test] public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuild() { @@ -190,10 +238,66 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildA fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.0.1+1"""); } - private static void AddGenerateGitVersionInformationTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty, string intermediateOutputPath = "$(MSBuildProjectDirectory)") + [Test] + public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndUniqueNamespaceIsSpecifiedAndRootNamespaceIsNotSet() + { + const string taskName = nameof(GenerateGitVersionInformation); + const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath); + var randDir = Guid.NewGuid().ToString("N"); + + using var result = ExecuteMsBuildExeInAzurePipeline(project => AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, Path.Combine("$(MSBuildProjectDirectory)", randDir)).Property("GenerateGitVersionInformationInUniqueNamespace", "True")); + + result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); + result.MsBuild.Count.ShouldBeGreaterThan(0); + result.MsBuild.OverallSuccess.ShouldBe(true); + result.MsBuild.ShouldAllBe(x => x.Succeeded); + result.Output.ShouldNotBeNullOrWhiteSpace(); + + var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "GitVersionInformation.g.cs"); + result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}"); + + var fileContent = File.ReadAllText(generatedFilePath); + fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""0"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""1"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.0.1"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.0.1+1"""); + fileContent.ShouldContain($@"namespace App"); + } + + [Test] + public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndUniqueNamespaceIsSpecifiedAndRootNamespaceIsSet() + { + const string taskName = nameof(GenerateGitVersionInformation); + const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath); + var randDir = Guid.NewGuid().ToString("N"); + + using var result = ExecuteMsBuildExe(project => AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, Path.Combine("$(MSBuildProjectDirectory)", randDir)).Property("GenerateGitVersionInformationInUniqueNamespace", "True").Property("RootNamespace", "Test.Root")); + + result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); + result.MsBuild.Count.ShouldBeGreaterThan(0); + result.MsBuild.OverallSuccess.ShouldBe(true); + result.MsBuild.ShouldAllBe(x => x.Succeeded); + result.Output.ShouldNotBeNullOrWhiteSpace(); + + var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, "GitVersionInformation.g.cs"); + result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}"); + + var fileContent = File.ReadAllText(generatedFilePath); + TestContext.WriteLine(fileContent); + fileContent.ShouldContain($@"{nameof(VersionVariables.Major)} = ""1"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.Minor)} = ""2"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.Patch)} = ""4"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.MajorMinorPatch)} = ""1.2.4"""); + fileContent.ShouldContain($@"{nameof(VersionVariables.FullSemVer)} = ""1.2.4+1"""); + fileContent.ShouldContain($@"namespace Test.Root"); + + } + + private static ProjectCreator AddGenerateGitVersionInformationTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty, string intermediateOutputPath = "$(MSBuildProjectDirectory)") { var assemblyFileLocation = typeof(GitVersionTaskBase).Assembly.Location; - project.UsingTaskAssemblyFile(taskName, assemblyFileLocation) + return project.UsingTaskAssemblyFile(taskName, assemblyFileLocation) .Property("GenerateAssemblyInfo", "false") .Target(targetToRun, beforeTargets: "CoreCompile;GetAssemblyVersion;GenerateNuspec") .Task(taskName, parameters: new Dictionary @@ -202,7 +306,9 @@ private static void AddGenerateGitVersionInformationTask(ProjectCreator project, { "VersionFile", "$(MSBuildProjectDirectory)/gitversion.json" }, { "ProjectFile", "$(MSBuildProjectFullPath)" }, { "IntermediateOutputPath", intermediateOutputPath }, - { "Language", "$(Language)" } + { "Language", "$(Language)" }, + { "GenerateGitVersionInformationInUniqueNamespace", "$(GenerateGitVersionInformationInUniqueNamespace)" }, + { "RootNamespace", "$(RootNamespace)" }, }) .TaskOutputProperty(outputProperty, outputProperty) .ItemGroup() @@ -212,4 +318,6 @@ private static void AddGenerateGitVersionInformationTask(ProjectCreator project, .Target(MsBuildExeFixture.OutputTarget, dependsOnTargets: targetToRun) .TaskMessage($"{outputProperty}: $({outputProperty})", MessageImportance.High); } + + } diff --git a/src/GitVersion.MsBuild/GitVersionTaskBase.cs b/src/GitVersion.MsBuild/GitVersionTaskBase.cs index 4af337e7d7..ddcdf64937 100644 --- a/src/GitVersion.MsBuild/GitVersionTaskBase.cs +++ b/src/GitVersion.MsBuild/GitVersionTaskBase.cs @@ -15,6 +15,7 @@ public abstract class GitVersionTaskBase : ITask public string VersionFile { get; set; } + public TaskLoggingHelper Log { get; } public bool Execute() => OnExecute(); diff --git a/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs b/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs index 1f0e9dcebc..a89aec96ad 100644 --- a/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs +++ b/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs @@ -44,7 +44,7 @@ public void UpdateAssemblyInfo(UpdateAssemblyInfo task) var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "AssemblyInfo"); task.AssemblyInfoTempFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName); - var gitVersionOptions = this.options.Value; + var gitVersionOptions = options.Value; gitVersionOptions.AssemblyInfo.UpdateAssemblyInfo = true; gitVersionOptions.AssemblyInfo.EnsureAssemblyInfo = true; gitVersionOptions.WorkingDirectory = fileWriteInfo.WorkingDirectory; @@ -66,10 +66,18 @@ public void GenerateGitVersionInformation(GenerateGitVersionInformation task) var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "GitVersionInformation"); task.GitVersionInformationFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName); - var gitVersionOptions = this.options.Value; + var gitVersionOptions = options.Value; gitVersionOptions.WorkingDirectory = fileWriteInfo.WorkingDirectory; - - gitVersionOutputTool.GenerateGitVersionInformation(versionVariables, fileWriteInfo); + string? targetNamespace = null; + if (string.Equals(task.GenerateGitVersionInformationInUniqueNamespace, "true", StringComparison.OrdinalIgnoreCase)) + { + targetNamespace = task.RootNamespace; + if (string.IsNullOrWhiteSpace(targetNamespace)) + { + targetNamespace = Path.GetFileNameWithoutExtension(task.ProjectFile); + } + } + gitVersionOutputTool.GenerateGitVersionInformation(versionVariables, fileWriteInfo, targetNamespace); } public void WriteVersionInfoToBuildLog(WriteVersionInfoToBuildLog task) diff --git a/src/GitVersion.MsBuild/PublicAPI.Unshipped.txt b/src/GitVersion.MsBuild/PublicAPI.Unshipped.txt index e69de29bb2..789f4d5fbe 100644 --- a/src/GitVersion.MsBuild/PublicAPI.Unshipped.txt +++ b/src/GitVersion.MsBuild/PublicAPI.Unshipped.txt @@ -0,0 +1,4 @@ +GitVersion.MsBuild.Tasks.GenerateGitVersionInformation.GenerateGitVersionInformationInUniqueNamespace.get -> string? +GitVersion.MsBuild.Tasks.GenerateGitVersionInformation.GenerateGitVersionInformationInUniqueNamespace.set -> void +GitVersion.MsBuild.Tasks.GenerateGitVersionInformation.RootNamespace.get -> string! +GitVersion.MsBuild.Tasks.GenerateGitVersionInformation.RootNamespace.set -> void diff --git a/src/GitVersion.MsBuild/Tasks/GenerateGitVersionInformation.cs b/src/GitVersion.MsBuild/Tasks/GenerateGitVersionInformation.cs index ebebf91f66..84556f6451 100644 --- a/src/GitVersion.MsBuild/Tasks/GenerateGitVersionInformation.cs +++ b/src/GitVersion.MsBuild/Tasks/GenerateGitVersionInformation.cs @@ -13,6 +13,10 @@ public class GenerateGitVersionInformation : GitVersionTaskBase [Required] public string Language { get; set; } = "C#"; + public string? GenerateGitVersionInformationInUniqueNamespace { get; set; } + + public string RootNamespace { get; set; } + [Output] public string GitVersionInformationFilePath { get; set; } diff --git a/src/GitVersion.MsBuild/msbuild/tools/GitVersion.MsBuild.targets b/src/GitVersion.MsBuild/msbuild/tools/GitVersion.MsBuild.targets index 2597da08a1..abce230c18 100644 --- a/src/GitVersion.MsBuild/msbuild/tools/GitVersion.MsBuild.targets +++ b/src/GitVersion.MsBuild/msbuild/tools/GitVersion.MsBuild.targets @@ -92,7 +92,9 @@ + Language="$(Language)" + GenerateGitVersionInformationInUniqueNamespace="$(GenerateGitVersionInformationInUniqueNamespace)" + RootNamespace="$(RootNamespace)"> From 58c44dfa9200c0bfcef7512e159d3d210cc2074d Mon Sep 17 00:00:00 2001 From: apmoskevitz Date: Wed, 5 Jul 2023 13:24:47 -0400 Subject: [PATCH 2/8] fix merge issues --- src/GitVersion.Core/PublicAPI.Unshipped.txt | 4 - .../GenerateGitVersionInformationTest.cs | 127 +++++++++++++++++- src/GitVersion.Output/GitVersionOutputTool.cs | 4 +- .../IGitVersionOutputTool.cs | 1 + src/GitVersion.Output/PublicAPI.Unshipped.txt | 1 + 5 files changed, 129 insertions(+), 8 deletions(-) diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index 85aedaa5b7..54a26e841f 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -945,7 +945,3 @@ virtual GitVersion.Agents.BuildAgentBase.ShouldCleanUpRemotes() -> bool virtual GitVersion.Agents.BuildAgentBase.WriteIntegration(System.Action! writer, GitVersion.OutputVariables.GitVersionVariables! variables, bool updateBuildNumber = true) -> void GitVersion.GitVersionOptions.UniqueNamespace.get -> bool GitVersion.GitVersionOptions.UniqueNamespace.set -> void -GitVersion.GitVersionOutputTool.GenerateGitVersionInformation(GitVersion.OutputVariables.VersionVariables! variables, GitVersion.FileWriteInfo! fileWriteInfo, string? targetNamespace = null) -> void -GitVersion.IGitVersionOutputTool.GenerateGitVersionInformation(GitVersion.OutputVariables.VersionVariables! variables, GitVersion.FileWriteInfo! fileWriteInfo, string? targetNamespace = null) -> void -GitVersion.VersionConverters.GitVersionInfo.GitVersionInfoContext.GitVersionInfoContext(string! workingDirectory, string! fileName, string! fileExtension, string? targetNamespace = null) -> void -GitVersion.VersionConverters.GitVersionInfo.GitVersionInfoContext.TargetNamespace.get -> string? diff --git a/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs b/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs index 3c6a143d51..a2a809a615 100644 --- a/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs +++ b/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs @@ -218,12 +218,133 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildA fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.0.1-1")); } - private static void AddGenerateGitVersionInformationTask(ProjectCreator project, string targetToRun, string taskName, + [TestCaseSource(nameof(Languages))] + public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndUniqueNamespaceIsSpecifiedAndRootNamespaceIsSet(string language) + { + const string taskName = nameof(GenerateGitVersionInformation); + const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath); + var randDir = Guid.NewGuid().ToString("N"); + + var extension = FileHelper.GetFileExtension(language); + using var result = ExecuteMsBuildExe(project => + { + var intermediateOutputPath = Path.Combine("$(MSBuildProjectDirectory)", randDir); + AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, language, intermediateOutputPath).Property("GenerateGitVersionInformationInUniqueNamespace", "True").Property("RootNamespace", "Test.Root"); + }, language); + + result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); + result.MsBuild.Count.ShouldBeGreaterThan(0); + result.MsBuild.OverallSuccess.ShouldBe(true); + result.MsBuild.ShouldAllBe(x => x.Succeeded); + result.Output.ShouldNotBeNullOrWhiteSpace(); + + var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, $"GitVersionInformation.g.{extension}"); + result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}"); + + var fileContent = File.ReadAllText(generatedFilePath); + TestContext.WriteLine(fileContent); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Major), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Minor), "2")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Patch), "4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.MajorMinorPatch), "1.2.4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.2.4-1")); + fileContent.ShouldContain("namespace Test.Root", Case.Insensitive); + + } + + [TestCaseSource(nameof(Languages))] + public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndUniqueNamespaceIsSpecifiedAndRootNamespaceIsNotSet(string language) + { + const string taskName = nameof(GenerateGitVersionInformation); + const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath); + var randDir = Guid.NewGuid().ToString("N"); + + var extension = FileHelper.GetFileExtension(language); + using var result = ExecuteMsBuildExeInAzurePipeline(project => + { + var intermediateOutputPath = Path.Combine("$(MSBuildProjectDirectory)", randDir); + AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, language, intermediateOutputPath).Property("GenerateGitVersionInformationInUniqueNamespace", "True"); + }, language); + + result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); + result.MsBuild.Count.ShouldBeGreaterThan(0); + result.MsBuild.OverallSuccess.ShouldBe(true); + result.MsBuild.ShouldAllBe(x => x.Succeeded); + result.Output.ShouldNotBeNullOrWhiteSpace(); + + var generatedFilePath = PathHelper.Combine(Path.GetDirectoryName(result.ProjectPath), randDir, $"GitVersionInformation.g.{extension}"); + result.Output.ShouldContain($"{outputProperty}: {generatedFilePath}"); + + var fileContent = File.ReadAllText(generatedFilePath); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Major), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Minor), "0")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Patch), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.MajorMinorPatch), "1.0.1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.0.1-1")); + fileContent.ShouldContain("namespace App", Case.Insensitive); + } + + [TestCaseSource(nameof(Languages))] + public void GenerateGitVersionInformationTaskShouldCreateFileWithUniqueNamespaceSetAndRootNamespaceUnSet(string language) + { + var extension = FileHelper.GetFileExtension(language); + var task = new GenerateGitVersionInformation + { + Language = language, + GenerateGitVersionInformationInUniqueNamespace = "true", + ProjectFile = "App.Project.csproj", + }; + using var result = ExecuteMsBuildTask(task); + + result.Success.ShouldBe(true); + result.Errors.ShouldBe(0); + result.Task.GitVersionInformationFilePath.ShouldNotBeNull(); + result.Task.GitVersionInformationFilePath.ShouldMatch($@"GitVersionInformation.*\.g\.{extension}"); + + var fileContent = File.ReadAllText(result.Task.GitVersionInformationFilePath); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Major), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Minor), "2")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Patch), "4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.MajorMinorPatch), "1.2.4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.2.4-1")); + fileContent.ShouldContain("namespace App.Project", Case.Insensitive); + } + + [TestCaseSource(nameof(Languages))] + public void GenerateGitVersionInformationTaskShouldCreateFileWithUniqueNamespaceSetAndRootNamespaceIsSet(string language) + { + + var extension = FileHelper.GetFileExtension(language); + var task = new GenerateGitVersionInformation + { + Language = language, + GenerateGitVersionInformationInUniqueNamespace = "true", + ProjectFile = "App.Project.csproj", + RootNamespace = "App.Project.RootNamespace", + }; + using var result = ExecuteMsBuildTask(task); + + result.Success.ShouldBe(true); + result.Errors.ShouldBe(0); + result.Task.GitVersionInformationFilePath.ShouldNotBeNull(); + result.Task.GitVersionInformationFilePath.ShouldMatch($@"GitVersionInformation.*\.g\.{extension}"); + + var fileContent = File.ReadAllText(result.Task.GitVersionInformationFilePath); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Major), "1")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Minor), "2")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.Patch), "4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.MajorMinorPatch), "1.2.4")); + fileContent.ShouldMatch(string.Format(regexPattern, nameof(GitVersionVariables.FullSemVer), "1.2.4-1")); + + fileContent.ShouldContain("namespace App.Project.RootNamespace"); + } + + private static ProjectCreator AddGenerateGitVersionInformationTask(ProjectCreator project, string targetToRun, string taskName, string outputProperty, string language, string intermediateOutputPath = "$(MSBuildProjectDirectory)") { var assemblyFileLocation = typeof(GitVersionTaskBase).Assembly.Location; - project.UsingTaskAssemblyFile(taskName, assemblyFileLocation) + return project.UsingTaskAssemblyFile(taskName, assemblyFileLocation) .Property("ManagePackageVersionsCentrally", "false") .Property("GenerateAssemblyInfo", "false") .Property("Language", language) @@ -235,6 +356,8 @@ private static void AddGenerateGitVersionInformationTask(ProjectCreator project, { "ProjectFile", "$(MSBuildProjectFullPath)" }, { "Language", "$(Language)" }, { "IntermediateOutputPath", intermediateOutputPath }, + { "GenerateGitVersionInformationInUniqueNamespace", "$(GenerateGitVersionInformationInUniqueNamespace)" }, + { "RootNamespace", "$(RootNamespace)" }, }) .TaskOutputProperty(outputProperty, outputProperty) .ItemGroup() diff --git a/src/GitVersion.Output/GitVersionOutputTool.cs b/src/GitVersion.Output/GitVersionOutputTool.cs index 306a0e21b7..e2b30b59d0 100644 --- a/src/GitVersion.Output/GitVersionOutputTool.cs +++ b/src/GitVersion.Output/GitVersionOutputTool.cs @@ -72,7 +72,7 @@ public void UpdateWixVersionFile(GitVersionVariables variables) } } - public void GenerateGitVersionInformation(GitVersionVariables variables, FileWriteInfo fileWriteInfo, string? targetNamespace = null)) + public void GenerateGitVersionInformation(GitVersionVariables variables, FileWriteInfo fileWriteInfo, string? targetNamespace = null) { using (gitVersionInfoGenerator) { @@ -80,5 +80,5 @@ public void GenerateGitVersionInformation(GitVersionVariables variables, FileWri } } - public void GenerateGitVersionInformation(VersionVariables variables, FileWriteInfo fileWriteInfo) => GenerateGitVersionInformation(variables, fileWriteInfo, null); + public void GenerateGitVersionInformation(GitVersionVariables variables, FileWriteInfo fileWriteInfo) => GenerateGitVersionInformation(variables, fileWriteInfo, null); } diff --git a/src/GitVersion.Output/IGitVersionOutputTool.cs b/src/GitVersion.Output/IGitVersionOutputTool.cs index 514c878150..f56248fe5e 100644 --- a/src/GitVersion.Output/IGitVersionOutputTool.cs +++ b/src/GitVersion.Output/IGitVersionOutputTool.cs @@ -8,4 +8,5 @@ public interface IGitVersionOutputTool void UpdateAssemblyInfo(GitVersionVariables variables); void UpdateWixVersionFile(GitVersionVariables variables); void GenerateGitVersionInformation(GitVersionVariables variables, FileWriteInfo fileWriteInfo); + void GenerateGitVersionInformation(GitVersionVariables variables, FileWriteInfo fileWriteInfo, string? targetNamespace = null); } diff --git a/src/GitVersion.Output/PublicAPI.Unshipped.txt b/src/GitVersion.Output/PublicAPI.Unshipped.txt index 5a492ac2d7..4920c0a9cc 100644 --- a/src/GitVersion.Output/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Output/PublicAPI.Unshipped.txt @@ -1,6 +1,7 @@ #nullable enable GitVersion.IGitVersionOutputTool GitVersion.IGitVersionOutputTool.GenerateGitVersionInformation(GitVersion.OutputVariables.GitVersionVariables! variables, GitVersion.FileWriteInfo! fileWriteInfo) -> void +GitVersion.IGitVersionOutputTool.GenerateGitVersionInformation(GitVersion.OutputVariables.GitVersionVariables! variables, GitVersion.FileWriteInfo! fileWriteInfo, string? targetNamespace = null) -> void GitVersion.IGitVersionOutputTool.OutputVariables(GitVersion.OutputVariables.GitVersionVariables! variables, bool updateBuildNumber) -> void GitVersion.IGitVersionOutputTool.UpdateAssemblyInfo(GitVersion.OutputVariables.GitVersionVariables! variables) -> void GitVersion.IGitVersionOutputTool.UpdateWixVersionFile(GitVersion.OutputVariables.GitVersionVariables! variables) -> void From 7330f8647153087900b5b434e7eb6fa6db6fbe22 Mon Sep 17 00:00:00 2001 From: apmoskevitz Date: Wed, 5 Jul 2023 13:57:31 -0400 Subject: [PATCH 3/8] cleanup code --- global.json | 2 +- src/Directory.Packages.props | 4 +-- src/GitVersion.Core/Options/FileWriteInfo.cs | 1 - src/GitVersion.MsBuild/GitVersionTaskBase.cs | 1 - .../GitVersionTaskExecutor.cs | 26 ++++++++++++------- .../GitVersionInfo/GitVersionInfoGenerator.cs | 10 +++---- src/GitVersion.Output/GitVersionOutputTool.cs | 23 ++++++++-------- 7 files changed, 34 insertions(+), 33 deletions(-) diff --git a/global.json b/global.json index ad12120c01..8960f9efff 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "7.0.103" + "version": "7.0.304" } } \ No newline at end of file diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index fbe6a8a882..1c8c0fac85 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -7,8 +7,8 @@ - - + + diff --git a/src/GitVersion.Core/Options/FileWriteInfo.cs b/src/GitVersion.Core/Options/FileWriteInfo.cs index 2653353c63..976dd386c1 100644 --- a/src/GitVersion.Core/Options/FileWriteInfo.cs +++ b/src/GitVersion.Core/Options/FileWriteInfo.cs @@ -12,5 +12,4 @@ public FileWriteInfo(string workingDirectory, string fileName, string fileExtens public string WorkingDirectory { get; } public string FileName { get; } public string FileExtension { get; } - } diff --git a/src/GitVersion.MsBuild/GitVersionTaskBase.cs b/src/GitVersion.MsBuild/GitVersionTaskBase.cs index 5f2d946d13..cb74cc0c5f 100644 --- a/src/GitVersion.MsBuild/GitVersionTaskBase.cs +++ b/src/GitVersion.MsBuild/GitVersionTaskBase.cs @@ -16,7 +16,6 @@ public abstract class GitVersionTaskBase : ITask public string VersionFile { get; set; } - public TaskLoggingHelper Log { get; } public bool Execute() => OnExecute(); diff --git a/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs b/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs index 307bb4db5e..2af2f804a8 100644 --- a/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs +++ b/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs @@ -3,7 +3,6 @@ using GitVersion.Helpers; using GitVersion.MsBuild.Tasks; using GitVersion.OutputVariables; - using Microsoft.Extensions.Options; namespace GitVersion.MsBuild; @@ -49,7 +48,7 @@ public void UpdateAssemblyInfo(UpdateAssemblyInfo task) var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "AssemblyInfo"); task.AssemblyInfoTempFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName); - var gitVersionOptions = this.options.Value; + var gitVersionOptions = options.Value; gitVersionOptions.WorkingDirectory = fileWriteInfo.WorkingDirectory; gitVersionOptions.AssemblySettingsInfo.UpdateAssemblyInfo = true; gitVersionOptions.AssemblySettingsInfo.EnsureAssemblyInfo = true; @@ -73,24 +72,31 @@ public void GenerateGitVersionInformation(GenerateGitVersionInformation task) var gitVersionOptions = options.Value; gitVersionOptions.WorkingDirectory = fileWriteInfo.WorkingDirectory; - string? targetNamespace = null; - if (string.Equals(task.GenerateGitVersionInformationInUniqueNamespace, "true", StringComparison.OrdinalIgnoreCase)) + var targetNamespace = getTargetNamespace(task); + gitVersionOutputTool.GenerateGitVersionInformation(versionVariables, fileWriteInfo, targetNamespace); + + static string? getTargetNamespace(GenerateGitVersionInformation task) { - targetNamespace = task.RootNamespace; - if (string.IsNullOrWhiteSpace(targetNamespace)) + string? targetNamespace = null; + if (string.Equals(task.GenerateGitVersionInformationInUniqueNamespace, "true", StringComparison.OrdinalIgnoreCase)) { - targetNamespace = Path.GetFileNameWithoutExtension(task.ProjectFile); + targetNamespace = task.RootNamespace; + if (string.IsNullOrWhiteSpace(targetNamespace)) + { + targetNamespace = Path.GetFileNameWithoutExtension(task.ProjectFile); + } } + + return targetNamespace; } - gitVersionOutputTool.GenerateGitVersionInformation(versionVariables, fileWriteInfo, targetNamespace); } public void WriteVersionInfoToBuildLog(WriteVersionInfoToBuildLog task) { var versionVariables = VersionVariablesHelper.FromFile(task.VersionFile, fileSystem); - var gitVersionOptions = this.options.Value; - var configuration = this.configurationProvider.Provide(gitVersionOptions.ConfigurationInfo.OverrideConfiguration); + var gitVersionOptions = options.Value; + var configuration = configurationProvider.Provide(gitVersionOptions.ConfigurationInfo.OverrideConfiguration); gitVersionOutputTool.OutputVariables(versionVariables, configuration.UpdateBuildNumber); } diff --git a/src/GitVersion.Output/GitVersionInfo/GitVersionInfoGenerator.cs b/src/GitVersion.Output/GitVersionInfo/GitVersionInfoGenerator.cs index 35ca7b35e0..d8d953b948 100644 --- a/src/GitVersion.Output/GitVersionInfo/GitVersionInfoGenerator.cs +++ b/src/GitVersion.Output/GitVersionInfo/GitVersionInfoGenerator.cs @@ -2,8 +2,6 @@ using GitVersion.Helpers; using GitVersion.OutputVariables; -using Polly.CircuitBreaker; - namespace GitVersion.Output.GitVersionInfo; internal interface IGitVersionInfoGenerator : IVersionConverter @@ -19,7 +17,7 @@ internal sealed class GitVersionInfoGenerator : IGitVersionInfoGenerator public GitVersionInfoGenerator(IFileSystem fileSystem) { this.fileSystem = fileSystem.NotNull(); - templateManager = new TemplateManager(TemplateType.GitVersionInfo); + this.templateManager = new TemplateManager(TemplateType.GitVersionInfo); } public void Execute(GitVersionVariables variables, GitVersionInfoContext context) @@ -37,8 +35,8 @@ public void Execute(GitVersionVariables variables, GitVersionInfoContext context } var fileExtension = Path.GetExtension(filePath); - var template = templateManager.GetTemplateFor(fileExtension); - var addFormat = templateManager.GetAddFormatFor(fileExtension); + var template = this.templateManager.GetTemplateFor(fileExtension); + var addFormat = this.templateManager.GetAddFormatFor(fileExtension); var targetNamespace = getTargetNamespace(fileExtension); if (string.IsNullOrWhiteSpace(template) || string.IsNullOrWhiteSpace(addFormat) || targetNamespace == targetNamespaceSentinelValue) @@ -67,7 +65,7 @@ public void Execute(GitVersionVariables variables, GitVersionInfoContext context if (fileContents != originalFileContents) { - fileSystem.WriteAllText(filePath, fileContents); + this.fileSystem.WriteAllText(filePath, fileContents); } string getTargetNamespace(string fileExtension) => fileExtension switch diff --git a/src/GitVersion.Output/GitVersionOutputTool.cs b/src/GitVersion.Output/GitVersionOutputTool.cs index e2b30b59d0..6d19222437 100644 --- a/src/GitVersion.Output/GitVersionOutputTool.cs +++ b/src/GitVersion.Output/GitVersionOutputTool.cs @@ -4,7 +4,6 @@ using GitVersion.Output.OutputGenerator; using GitVersion.Output.WixUpdater; using GitVersion.OutputVariables; - using Microsoft.Extensions.Options; namespace GitVersion; @@ -23,7 +22,7 @@ public GitVersionOutputTool(IOptions options, IGitVersionInfoGenerator gitVersionInfoGenerator, IAssemblyInfoFileUpdater assemblyInfoFileUpdater, IProjectFileUpdater projectFileUpdater) { - gitVersionOptions = options.Value.NotNull(); + this.gitVersionOptions = options.Value.NotNull(); this.outputGenerator = outputGenerator.NotNull(); @@ -35,9 +34,9 @@ public GitVersionOutputTool(IOptions options, public void OutputVariables(GitVersionVariables variables, bool updateBuildNumber) { - using (outputGenerator) + using (this.outputGenerator) { - outputGenerator.Execute(variables, new OutputContext(gitVersionOptions.WorkingDirectory, gitVersionOptions.OutputFile, updateBuildNumber)); + this.outputGenerator.Execute(variables, new OutputContext(gitVersionOptions.WorkingDirectory, gitVersionOptions.OutputFile, updateBuildNumber)); } } @@ -47,16 +46,16 @@ public void UpdateAssemblyInfo(GitVersionVariables variables) if (gitVersionOptions.AssemblySettingsInfo.UpdateProjectFiles) { - using (projectFileUpdater) + using (this.projectFileUpdater) { - projectFileUpdater.Execute(variables, assemblyInfoContext); + this.projectFileUpdater.Execute(variables, assemblyInfoContext); } } else if (gitVersionOptions.AssemblySettingsInfo.UpdateAssemblyInfo) { - using (assemblyInfoFileUpdater) + using (this.assemblyInfoFileUpdater) { - assemblyInfoFileUpdater.Execute(variables, assemblyInfoContext); + this.assemblyInfoFileUpdater.Execute(variables, assemblyInfoContext); } } } @@ -65,18 +64,18 @@ public void UpdateWixVersionFile(GitVersionVariables variables) { if (gitVersionOptions.WixInfo.UpdateWixVersionFile) { - using (wixVersionFileUpdater) + using (this.wixVersionFileUpdater) { - wixVersionFileUpdater.Execute(variables, new WixVersionContext(gitVersionOptions.WorkingDirectory)); + this.wixVersionFileUpdater.Execute(variables, new WixVersionContext(gitVersionOptions.WorkingDirectory)); } } } public void GenerateGitVersionInformation(GitVersionVariables variables, FileWriteInfo fileWriteInfo, string? targetNamespace = null) { - using (gitVersionInfoGenerator) + using (this.gitVersionInfoGenerator) { - gitVersionInfoGenerator.Execute(variables, new GitVersionInfoContext(gitVersionOptions.WorkingDirectory, fileWriteInfo.FileName, fileWriteInfo.FileExtension, targetNamespace)); + this.gitVersionInfoGenerator.Execute(variables, new GitVersionInfoContext(gitVersionOptions.WorkingDirectory, fileWriteInfo.FileName, fileWriteInfo.FileExtension, targetNamespace)); } } From ecd27ff59a180397a87e708f6019305b52d96041 Mon Sep 17 00:00:00 2001 From: apmoskevitz Date: Wed, 5 Jul 2023 13:59:48 -0400 Subject: [PATCH 4/8] add back removed this. --- src/GitVersion.MsBuild/GitVersionTaskExecutor.cs | 8 ++++---- .../GitVersionInfo/GitVersionInfoGenerator.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs b/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs index 2af2f804a8..f8bfff0c40 100644 --- a/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs +++ b/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs @@ -48,7 +48,7 @@ public void UpdateAssemblyInfo(UpdateAssemblyInfo task) var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "AssemblyInfo"); task.AssemblyInfoTempFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName); - var gitVersionOptions = options.Value; + var gitVersionOptions = this.options.Value; gitVersionOptions.WorkingDirectory = fileWriteInfo.WorkingDirectory; gitVersionOptions.AssemblySettingsInfo.UpdateAssemblyInfo = true; gitVersionOptions.AssemblySettingsInfo.EnsureAssemblyInfo = true; @@ -70,7 +70,7 @@ public void GenerateGitVersionInformation(GenerateGitVersionInformation task) var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "GitVersionInformation"); task.GitVersionInformationFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName); - var gitVersionOptions = options.Value; + var gitVersionOptions = this.options.Value; gitVersionOptions.WorkingDirectory = fileWriteInfo.WorkingDirectory; var targetNamespace = getTargetNamespace(task); gitVersionOutputTool.GenerateGitVersionInformation(versionVariables, fileWriteInfo, targetNamespace); @@ -95,8 +95,8 @@ public void WriteVersionInfoToBuildLog(WriteVersionInfoToBuildLog task) { var versionVariables = VersionVariablesHelper.FromFile(task.VersionFile, fileSystem); - var gitVersionOptions = options.Value; - var configuration = configurationProvider.Provide(gitVersionOptions.ConfigurationInfo.OverrideConfiguration); + var gitVersionOptions = this.options.Value; + var configuration = this.configurationProvider.Provide(gitVersionOptions.ConfigurationInfo.OverrideConfiguration); gitVersionOutputTool.OutputVariables(versionVariables, configuration.UpdateBuildNumber); } diff --git a/src/GitVersion.Output/GitVersionInfo/GitVersionInfoGenerator.cs b/src/GitVersion.Output/GitVersionInfo/GitVersionInfoGenerator.cs index d8d953b948..97970f5a1e 100644 --- a/src/GitVersion.Output/GitVersionInfo/GitVersionInfoGenerator.cs +++ b/src/GitVersion.Output/GitVersionInfo/GitVersionInfoGenerator.cs @@ -31,7 +31,7 @@ public void Execute(GitVersionVariables variables, GitVersionInfoContext context if (File.Exists(filePath)) { - originalFileContents = fileSystem.ReadAllText(filePath); + originalFileContents = this.fileSystem.ReadAllText(filePath); } var fileExtension = Path.GetExtension(filePath); From e0802f132e6f13fb0080160e0a488f076217f1a1 Mon Sep 17 00:00:00 2001 From: apmoskevitz Date: Wed, 5 Jul 2023 14:00:56 -0400 Subject: [PATCH 5/8] revert changing SDK version --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index 8960f9efff..ad12120c01 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "7.0.304" + "version": "7.0.103" } } \ No newline at end of file From 27c03acb1a7613577a3f4a94ec861f768bd59603 Mon Sep 17 00:00:00 2001 From: apmoskevitz Date: Wed, 5 Jul 2023 14:02:50 -0400 Subject: [PATCH 6/8] revert package version of microsoft.build --- src/Directory.Packages.props | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index 1c8c0fac85..fbe6a8a882 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -7,8 +7,8 @@ - - + + From e89aedf390c9fe80df557ac11ec3a80cf1550146 Mon Sep 17 00:00:00 2001 From: Andrew Moskevitz <49752377+Applesauce314@users.noreply.github.com> Date: Thu, 13 Jul 2023 12:23:29 -0400 Subject: [PATCH 7/8] Update msbuild.md with description on how to use the new build property --- docs/input/docs/usage/msbuild.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/docs/input/docs/usage/msbuild.md b/docs/input/docs/usage/msbuild.md index 64e06e9b9b..b3ee074ad1 100644 --- a/docs/input/docs/usage/msbuild.md +++ b/docs/input/docs/usage/msbuild.md @@ -258,6 +258,18 @@ For SDK-style projects, `UpdateVersionProperties` controls setting the default variables: `Version`, `VersionPrefix`, `VersionSuffix`, `PackageVersion`, `InformationalVersion`, `AssemblyVersion` and `FileVersion`. +### Namespace generation + +You can configure GitVersion to generate the `GitVersionInformation` class in a namespace that matches the current assembly. By default this class is created in the global namespace. If `GenerateGitVersionInformationInUniqueNamespace` is set to true, the `GitVersionInfomation` class will instead be generated in a namespace matching the current project. If the property `` is set that value will be used, otherwise the name of the project file is used. + +```xml + + ... + true + ... + +``` + ## Extra properties There are properties that correspont to certain From ad52bec1095d47d17cbfb0fd8709ce6909b15a93 Mon Sep 17 00:00:00 2001 From: Andrew Moskevitz <49752377+Applesauce314@users.noreply.github.com> Date: Sun, 23 Jul 2023 15:20:14 +0000 Subject: [PATCH 8/8] update to better property name per review --- docs/input/docs/usage/msbuild.md | 12 ++++++------ .../Options/GitVersionOptions.cs | 2 +- src/GitVersion.Core/PublicAPI.Unshipped.txt | 2 -- .../Tasks/GenerateGitVersionInformationTest.cs | 18 +++++++++--------- .../GitVersionTaskExecutor.cs | 2 +- src/GitVersion.MsBuild/PublicAPI.Unshipped.txt | 4 ++-- .../Tasks/GenerateGitVersionInformation.cs | 2 +- .../msbuild/tools/GitVersion.MsBuild.targets | 2 +- 8 files changed, 21 insertions(+), 23 deletions(-) diff --git a/docs/input/docs/usage/msbuild.md b/docs/input/docs/usage/msbuild.md index b3ee074ad1..b91d38f17a 100644 --- a/docs/input/docs/usage/msbuild.md +++ b/docs/input/docs/usage/msbuild.md @@ -260,24 +260,24 @@ variables: `Version`, `VersionPrefix`, `VersionSuffix`, `PackageVersion`, ### Namespace generation -You can configure GitVersion to generate the `GitVersionInformation` class in a namespace that matches the current assembly. By default this class is created in the global namespace. If `GenerateGitVersionInformationInUniqueNamespace` is set to true, the `GitVersionInfomation` class will instead be generated in a namespace matching the current project. If the property `` is set that value will be used, otherwise the name of the project file is used. +You can configure GitVersion to generate the `GitVersionInformation` class in a namespace that matches the current assembly. By default this class is created in the global namespace. If `UseProjectNamespaceForGitVersionInformation` is set to true, the `GitVersionInfomation` class will instead be generated in a namespace matching the current project. If the property `` is set that value will be used, otherwise the name of the project file is used. ```xml ... - true + true ... ``` ## Extra properties -There are properties that correspont to certain +There are properties that correspont to certain [command line arguments](/docs/usage/cli/arguments) for GetVersion task. -In particular, setting `GitVersion_NoFetchEnabled` to `true` disables `git fetch` -during version calculation, setting `GitVersion_NoNormalizeEnabled` to `true` disables +In particular, setting `GitVersion_NoFetchEnabled` to `true` disables `git fetch` +during version calculation, setting `GitVersion_NoNormalizeEnabled` to `true` disables normalize step on a build server, setting `GitVersion_NoCacheEnabled` to `true` -makes GetVersion ignore cache. All the rest command line arguments can be passed via +makes GetVersion ignore cache. All the rest command line arguments can be passed via `GitVersion_CommandLineArguments` variable. ## My Git repository requires authentication. What should I do? diff --git a/src/GitVersion.Core/Options/GitVersionOptions.cs b/src/GitVersion.Core/Options/GitVersionOptions.cs index cec30412e5..0a26d69ef2 100644 --- a/src/GitVersion.Core/Options/GitVersionOptions.cs +++ b/src/GitVersion.Core/Options/GitVersionOptions.cs @@ -13,7 +13,7 @@ public class GitVersionOptions public WixInfo WixInfo { get; } = new(); public Settings Settings { get; } = new(); - public bool UniqueNamespace { get; set; } + public bool Init; public bool Diag; diff --git a/src/GitVersion.Core/PublicAPI.Unshipped.txt b/src/GitVersion.Core/PublicAPI.Unshipped.txt index 54a26e841f..7c0d531d89 100644 --- a/src/GitVersion.Core/PublicAPI.Unshipped.txt +++ b/src/GitVersion.Core/PublicAPI.Unshipped.txt @@ -943,5 +943,3 @@ virtual GitVersion.Agents.BuildAgentBase.IsDefault.get -> bool virtual GitVersion.Agents.BuildAgentBase.PreventFetch() -> bool virtual GitVersion.Agents.BuildAgentBase.ShouldCleanUpRemotes() -> bool virtual GitVersion.Agents.BuildAgentBase.WriteIntegration(System.Action! writer, GitVersion.OutputVariables.GitVersionVariables! variables, bool updateBuildNumber = true) -> void -GitVersion.GitVersionOptions.UniqueNamespace.get -> bool -GitVersion.GitVersionOptions.UniqueNamespace.set -> void diff --git a/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs b/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs index a2a809a615..871c22176e 100644 --- a/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs +++ b/src/GitVersion.MsBuild.Tests/Tasks/GenerateGitVersionInformationTest.cs @@ -219,7 +219,7 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildA } [TestCaseSource(nameof(Languages))] - public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndUniqueNamespaceIsSpecifiedAndRootNamespaceIsSet(string language) + public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndUseProjectNamespaceIsSpecifiedAndRootNamespaceIsSet(string language) { const string taskName = nameof(GenerateGitVersionInformation); const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath); @@ -229,7 +229,7 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildA using var result = ExecuteMsBuildExe(project => { var intermediateOutputPath = Path.Combine("$(MSBuildProjectDirectory)", randDir); - AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, language, intermediateOutputPath).Property("GenerateGitVersionInformationInUniqueNamespace", "True").Property("RootNamespace", "Test.Root"); + AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, language, intermediateOutputPath).Property("UseProjectNamespaceForGitVersionInformation", "True").Property("RootNamespace", "Test.Root"); }, language); result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); @@ -253,7 +253,7 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildA } [TestCaseSource(nameof(Languages))] - public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndUniqueNamespaceIsSpecifiedAndRootNamespaceIsNotSet(string language) + public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildAndUseProjectNamespaceIsSpecifiedAndRootNamespaceIsNotSet(string language) { const string taskName = nameof(GenerateGitVersionInformation); const string outputProperty = nameof(GenerateGitVersionInformation.GitVersionInformationFilePath); @@ -263,7 +263,7 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildA using var result = ExecuteMsBuildExeInAzurePipeline(project => { var intermediateOutputPath = Path.Combine("$(MSBuildProjectDirectory)", randDir); - AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, language, intermediateOutputPath).Property("GenerateGitVersionInformationInUniqueNamespace", "True"); + AddGenerateGitVersionInformationTask(project, taskName, taskName, outputProperty, language, intermediateOutputPath).Property("UseProjectNamespaceForGitVersionInformation", "True"); }, language); result.ProjectPath.ShouldNotBeNullOrWhiteSpace(); @@ -285,13 +285,13 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWhenRunWithMsBuildA } [TestCaseSource(nameof(Languages))] - public void GenerateGitVersionInformationTaskShouldCreateFileWithUniqueNamespaceSetAndRootNamespaceUnSet(string language) + public void GenerateGitVersionInformationTaskShouldCreateFileWithUseProjectNamespaceSetAndRootNamespaceUnSet(string language) { var extension = FileHelper.GetFileExtension(language); var task = new GenerateGitVersionInformation { Language = language, - GenerateGitVersionInformationInUniqueNamespace = "true", + UseProjectNamespaceForGitVersionInformation = "true", ProjectFile = "App.Project.csproj", }; using var result = ExecuteMsBuildTask(task); @@ -311,14 +311,14 @@ public void GenerateGitVersionInformationTaskShouldCreateFileWithUniqueNamespace } [TestCaseSource(nameof(Languages))] - public void GenerateGitVersionInformationTaskShouldCreateFileWithUniqueNamespaceSetAndRootNamespaceIsSet(string language) + public void GenerateGitVersionInformationTaskShouldCreateFileWithUseProjectNamespaceSetAndRootNamespaceIsSet(string language) { var extension = FileHelper.GetFileExtension(language); var task = new GenerateGitVersionInformation { Language = language, - GenerateGitVersionInformationInUniqueNamespace = "true", + UseProjectNamespaceForGitVersionInformation = "true", ProjectFile = "App.Project.csproj", RootNamespace = "App.Project.RootNamespace", }; @@ -356,7 +356,7 @@ private static ProjectCreator AddGenerateGitVersionInformationTask(ProjectCreato { "ProjectFile", "$(MSBuildProjectFullPath)" }, { "Language", "$(Language)" }, { "IntermediateOutputPath", intermediateOutputPath }, - { "GenerateGitVersionInformationInUniqueNamespace", "$(GenerateGitVersionInformationInUniqueNamespace)" }, + { "UseProjectNamespaceForGitVersionInformation", "$(UseProjectNamespaceForGitVersionInformation)" }, { "RootNamespace", "$(RootNamespace)" }, }) .TaskOutputProperty(outputProperty, outputProperty) diff --git a/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs b/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs index f8bfff0c40..6e4ff9dc03 100644 --- a/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs +++ b/src/GitVersion.MsBuild/GitVersionTaskExecutor.cs @@ -78,7 +78,7 @@ public void GenerateGitVersionInformation(GenerateGitVersionInformation task) static string? getTargetNamespace(GenerateGitVersionInformation task) { string? targetNamespace = null; - if (string.Equals(task.GenerateGitVersionInformationInUniqueNamespace, "true", StringComparison.OrdinalIgnoreCase)) + if (string.Equals(task.UseProjectNamespaceForGitVersionInformation, "true", StringComparison.OrdinalIgnoreCase)) { targetNamespace = task.RootNamespace; if (string.IsNullOrWhiteSpace(targetNamespace)) diff --git a/src/GitVersion.MsBuild/PublicAPI.Unshipped.txt b/src/GitVersion.MsBuild/PublicAPI.Unshipped.txt index fb2d6e18aa..56f89d7613 100644 --- a/src/GitVersion.MsBuild/PublicAPI.Unshipped.txt +++ b/src/GitVersion.MsBuild/PublicAPI.Unshipped.txt @@ -98,7 +98,7 @@ override GitVersion.MsBuild.Tasks.GenerateGitVersionInformation.OnExecute() -> b override GitVersion.MsBuild.Tasks.GetVersion.OnExecute() -> bool override GitVersion.MsBuild.Tasks.UpdateAssemblyInfo.OnExecute() -> bool override GitVersion.MsBuild.Tasks.WriteVersionInfoToBuildLog.OnExecute() -> bool -GitVersion.MsBuild.Tasks.GenerateGitVersionInformation.GenerateGitVersionInformationInUniqueNamespace.get -> string? -GitVersion.MsBuild.Tasks.GenerateGitVersionInformation.GenerateGitVersionInformationInUniqueNamespace.set -> void +GitVersion.MsBuild.Tasks.GenerateGitVersionInformation.UseProjectNamespaceForGitVersionInformation.get -> string? +GitVersion.MsBuild.Tasks.GenerateGitVersionInformation.UseProjectNamespaceForGitVersionInformation.set -> void GitVersion.MsBuild.Tasks.GenerateGitVersionInformation.RootNamespace.get -> string! GitVersion.MsBuild.Tasks.GenerateGitVersionInformation.RootNamespace.set -> void diff --git a/src/GitVersion.MsBuild/Tasks/GenerateGitVersionInformation.cs b/src/GitVersion.MsBuild/Tasks/GenerateGitVersionInformation.cs index d0b66845c5..92c0fb95af 100644 --- a/src/GitVersion.MsBuild/Tasks/GenerateGitVersionInformation.cs +++ b/src/GitVersion.MsBuild/Tasks/GenerateGitVersionInformation.cs @@ -13,7 +13,7 @@ public class GenerateGitVersionInformation : GitVersionTaskBase [Required] public string Language { get; set; } = "C#"; - public string? GenerateGitVersionInformationInUniqueNamespace { get; set; } + public string? UseProjectNamespaceForGitVersionInformation { get; set; } public string RootNamespace { get; set; } diff --git a/src/GitVersion.MsBuild/msbuild/tools/GitVersion.MsBuild.targets b/src/GitVersion.MsBuild/msbuild/tools/GitVersion.MsBuild.targets index d31e4d1b69..e7d1a30e58 100644 --- a/src/GitVersion.MsBuild/msbuild/tools/GitVersion.MsBuild.targets +++ b/src/GitVersion.MsBuild/msbuild/tools/GitVersion.MsBuild.targets @@ -101,7 +101,7 @@ ProjectFile="$(MSBuildProjectFullPath)" IntermediateOutputPath="$(IntermediateOutputPath)" Language="$(Language)" - GenerateGitVersionInformationInUniqueNamespace="$(GenerateGitVersionInformationInUniqueNamespace)" + UseProjectNamespaceForGitVersionInformation="$(UseProjectNamespaceForGitVersionInformation)" RootNamespace="$(RootNamespace)">