Skip to content

Commit a7213ac

Browse files
authored
Merge pull request #4750 from arturcic/feature/msbuild-task
Bring back support for msbuild and Visual Studio
2 parents 2c0df38 + 9aa4c70 commit a7213ac

File tree

5 files changed

+47
-16
lines changed

5 files changed

+47
-16
lines changed

build/artifacts/Tasks/ArtifactsMsBuildFullTest.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ public override void Run(BuildContext context)
1919
if (context.Version == null)
2020
return;
2121
var version = context.Version.NugetVersion;
22+
var fullSemVer = context.Version.GitVersion.FullSemVer;
2223

2324
var nugetSource = context.MakeAbsolute(Paths.Nuget).FullPath;
2425

26+
const int toolVersionValue = 11; // Workaround for now. It should be removed when https://github.com/cake-build/cake/issues/4658 is merged
27+
var isMsBuildToolVersionValid = Enum.IsDefined(typeof(MSBuildToolVersion), toolVersionValue);
28+
2529
context.Information("\nTesting msbuild task with dotnet build\n");
2630
foreach (var netVersion in Constants.DotnetVersions)
2731
{
@@ -40,7 +44,27 @@ public override void Run(BuildContext context)
4044
});
4145

4246
var exe = Paths.Integration.Combine("build").Combine(framework).CombineWithFilePath("app.dll");
43-
context.ValidateOutput("dotnet", exe.FullPath, context.Version.GitVersion.FullSemVer);
47+
context.ValidateOutput("dotnet", exe.FullPath, fullSemVer);
48+
49+
if (!isMsBuildToolVersionValid) continue;
50+
51+
const MSBuildToolVersion toolVersion = (MSBuildToolVersion)toolVersionValue;
52+
context.Information("\nTesting msbuild task with msbuild (for full framework)\n");
53+
54+
var msBuildSettings = new MSBuildSettings
55+
{
56+
Verbosity = Verbosity.Minimal,
57+
ToolVersion = toolVersion,
58+
Restore = true
59+
};
60+
61+
msBuildSettings.WithProperty("GitVersionMsBuildVersion", version);
62+
msBuildSettings.WithProperty("RestoreSource", nugetSource);
63+
64+
context.MSBuild(projPath.FullPath, msBuildSettings);
65+
66+
var fullExe = Paths.Integration.Combine("build").CombineWithFilePath("app.exe");
67+
context.ValidateOutput(fullExe.FullPath, null, fullSemVer);
4468
}
4569
}
4670
}

src/GitVersion.Core/Extensions/ReferenceNameExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ public bool TryGetSemanticVersion(IGitVersionConfiguration configuration, out Se
1717

1818
private bool TryGetSemanticVersion(string? versionPatternPattern,
1919
string? tagPrefix,
20-
SemanticVersionFormat format, out SemanticVersionResult result)
20+
SemanticVersionFormat format,
21+
// ReSharper disable once OutParameterValueIsAlwaysDiscarded.Local
22+
out SemanticVersionResult result)
2123
{
2224
var versionPatternRegex = RegexPatterns.Cache.GetOrAdd(GetVersionInBranchPattern(versionPatternPattern));
2325
result = default;

src/GitVersion.MsBuild/GitVersionTasks.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ public static bool Execute(GitVersionTaskBase task)
1717
var executor = serviceProvider.GetRequiredService<IGitVersionTaskExecutor>();
1818
return task switch
1919
{
20-
GetVersion getVersion => ExecuteGitVersionTask(getVersion, () => executor.GetVersion(getVersion)),
21-
UpdateAssemblyInfo updateAssemblyInfo => ExecuteGitVersionTask(updateAssemblyInfo, () => executor.UpdateAssemblyInfo(updateAssemblyInfo)),
22-
GenerateGitVersionInformation generateGitVersionInformation => ExecuteGitVersionTask(generateGitVersionInformation, () => executor.GenerateGitVersionInformation(generateGitVersionInformation)),
23-
WriteVersionInfoToBuildLog writeVersionInfoToBuildLog => ExecuteGitVersionTask(writeVersionInfoToBuildLog, () => executor.WriteVersionInfoToBuildLog(writeVersionInfoToBuildLog)),
20+
GetVersion getVersion => ExecuteGitVersionTask(getVersion, executor.GetVersion),
21+
UpdateAssemblyInfo updateAssemblyInfo => ExecuteGitVersionTask(updateAssemblyInfo, executor.UpdateAssemblyInfo),
22+
GenerateGitVersionInformation generateGitVersionInformation => ExecuteGitVersionTask(generateGitVersionInformation, executor.GenerateGitVersionInformation),
23+
WriteVersionInfoToBuildLog writeVersionInfoToBuildLog => ExecuteGitVersionTask(writeVersionInfoToBuildLog, executor.WriteVersionInfoToBuildLog),
2424
_ => throw new NotSupportedException($"Task type {task.GetType().Name} is not supported")
2525
};
2626
}
2727

28-
private static bool ExecuteGitVersionTask<T>(T task, Action action)
28+
private static bool ExecuteGitVersionTask<T>(T task, Action<T> action)
2929
where T : GitVersionTaskBase
3030
{
3131
var taskLog = task.Log;
3232
try
3333
{
34-
action();
34+
action(task);
3535
}
3636
catch (WarningException errorException)
3737
{

src/GitVersion.MsBuild/msbuild/tools/GitVersion.MsBuild.props

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
</PropertyGroup>
2222

2323
<PropertyGroup>
24-
<!-- The GitVersion task is explicitly disabled when running on the .NET Framework because it's no longer supported.
25-
If a project that uses GitVersion.MsBuild is opened in Visual Studio,
24+
<!-- The GitVersion task is explicitly disabled when running on the .NET Framework with MSBuild version lower than 18
25+
or in Visual Studio 2022 or earlier (which uses MSBuild version 17 and below). In these scenarios,
2626
the task will be turned off because Visual Studio operates on the .NET Framework's version of MSBuild.
27-
However, you can still execute GitVersion.MsBuild as part of the `dotnet build` or `dotnet msbuild` commands. -->
28-
<DisableGitVersionTask Condition=" '$(MSBuildRuntimeType)' != 'Core' ">true</DisableGitVersionTask>
27+
However, it is supported from msbuild version 18 or Visual Studio 2026, or using the .NET version `dotnet build` -->
28+
<DisableGitVersionTask Condition=" '$(MSBuildRuntimeType)' == 'Full' AND $([System.Version]::Parse('$(MSBuildVersion)').Major) &lt; 18 ">true</DisableGitVersionTask>
2929

3030
<DisableGitVersionTask Condition=" '$(DisableGitVersionTask)' == '' ">false</DisableGitVersionTask>
3131

src/GitVersion.MsBuild/msbuild/tools/GitVersion.MsBuild.targets

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@
1818
<GitVersionAssemblyFile Condition="'$(GitVersionAssemblyFile)' == ''">$([MSBuild]::EnsureTrailingSlash($(MSBuildThisFileDirectory)$(GitVersionTargetFramework)))GitVersion.MsBuild.dll</GitVersionAssemblyFile>
1919
</PropertyGroup>
2020

21-
<UsingTask TaskName="GetVersion" AssemblyFile="$(GitVersionAssemblyFile)" />
22-
<UsingTask TaskName="GenerateGitVersionInformation" AssemblyFile="$(GitVersionAssemblyFile)" />
23-
<UsingTask TaskName="WriteVersionInfoToBuildLog" AssemblyFile="$(GitVersionAssemblyFile)" />
24-
<UsingTask TaskName="UpdateAssemblyInfo" AssemblyFile="$(GitVersionAssemblyFile)" />
21+
<UsingTask TaskName="GetVersion" AssemblyFile="$(GitVersionAssemblyFile)" Runtime="NET" TaskFactory="TaskHostFactory" Condition=" '$(MSBuildRuntimeType)' == 'Full' " />
22+
<UsingTask TaskName="GenerateGitVersionInformation" AssemblyFile="$(GitVersionAssemblyFile)" Runtime="NET" TaskFactory="TaskHostFactory" Condition=" '$(MSBuildRuntimeType)' == 'Full' " />
23+
<UsingTask TaskName="WriteVersionInfoToBuildLog" AssemblyFile="$(GitVersionAssemblyFile)" Runtime="NET" TaskFactory="TaskHostFactory" Condition=" '$(MSBuildRuntimeType)' == 'Full' " />
24+
<UsingTask TaskName="UpdateAssemblyInfo" AssemblyFile="$(GitVersionAssemblyFile)" Runtime="NET" TaskFactory="TaskHostFactory" Condition=" '$(MSBuildRuntimeType)' == 'Full' " />
25+
26+
<UsingTask TaskName="GetVersion" AssemblyFile="$(GitVersionAssemblyFile)" Runtime="NET" Condition=" '$(MSBuildRuntimeType)' == 'Core' " />
27+
<UsingTask TaskName="GenerateGitVersionInformation" AssemblyFile="$(GitVersionAssemblyFile)" Runtime="NET" Condition=" '$(MSBuildRuntimeType)' == 'Core' " />
28+
<UsingTask TaskName="WriteVersionInfoToBuildLog" AssemblyFile="$(GitVersionAssemblyFile)" Runtime="NET" Condition=" '$(MSBuildRuntimeType)' == 'Core' " />
29+
<UsingTask TaskName="UpdateAssemblyInfo" AssemblyFile="$(GitVersionAssemblyFile)" Runtime="NET" Condition=" '$(MSBuildRuntimeType)' == 'Core' " />
2530

2631
<Target Name="RunGitVersion" Condition="'$(DisableGitVersionTask)' == 'false'">
2732
<Exec Command="$(GitVersionFileExe) &quot;$(MSBuildProjectDirectory)&quot; $(GitVersion_ToolArgments)" />

0 commit comments

Comments
 (0)