-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSemanticVersioning.targets
54 lines (52 loc) · 3.17 KB
/
SemanticVersioning.targets
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<!-- We are loaded *before* NuGet.Build.Tasks.Pack.targets, so we can't use the $(NuGetPackTaskAssemblyFile) and $(NuGetBuildTasksPackTargets) properties.
Thus we re-define some of the things here based on $(MSBuildToolsPath). Let's hope it doesn't break. -->
<_NuGetVersioningAssemblyFile Condition="'$(MSBuildRuntimeType)' == 'Core'">$(MSBuildToolsPath)/Sdks/NuGet.Build.Tasks.Pack/CoreCLR/NuGet.Versioning.dll</_NuGetVersioningAssemblyFile>
<_NuGetVersioningAssemblyFile Condition="'$(MSBuildRuntimeType)' != 'Core'">$(MSBuildToolsPath)/Sdks/NuGet.Build.Tasks.Pack/Desktop/NuGet.Versioning.dll</_NuGetVersioningAssemblyFile>
<_NuGetVersioningAssemblyFile Condition="!$([System.IO.Path]::Exists('$(_NuGetVersioningAssemblyFile)'))">$(MSBuildToolsPath)/NuGet.Versioning.dll</_NuGetVersioningAssemblyFile>
</PropertyGroup>
<UsingTask TaskName="GenerateSemanticVersionRanges" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<ProjectReferencesWithVersions ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
<ProjectReferencesWithExactVersions ParameterType="Microsoft.Build.Framework.ITaskItem[]" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="$(_NuGetVersioningAssemblyFile)" />
<Using Namespace="NuGet.Versioning" />
<!-- language=C# -->
<Code Type="Fragment" Language="cs"><![CDATA[
foreach (var projectReference in ProjectReferencesWithVersions)
{
var versionString = projectReference.GetMetadata("ProjectVersion");
if (!SemanticVersion.TryParse(versionString, out var version)) {
Log.LogError("'{0}' is not a valid semantic version", versionString);
return false;
}
var range = version switch
{
// No SemVer guarantees for pre-release (e.g. nightly) versions, so we use an exact version.
{ IsPrerelease: true } => $"[{version}]",
// We use Cargo's convention for 0.x versions i.e. minor = breaking, patch = feature or patch.
{ Major: 0, Minor: var minor, Patch: var patch } => $"[0.{minor}.{patch}, 0.{minor + 1})",
// 1.x versions follow regular SemVer rules.
{ Major: var major, Minor: var minor, Patch: var patch } => $"[{major}.{minor}.{patch}, {major + 1})",
};
projectReference.SetMetadata("ProjectVersion", range);
}
ProjectReferencesWithExactVersions = ProjectReferencesWithVersions;
]]></Code>
</Task>
</UsingTask>
<!-- This workaround is based on comments in: https://github.com/NuGet/Home/issues/5556#issuecomment-753526765 -->
<Target Name="_ExactProjectReferencesVersion" AfterTargets="_GetProjectReferenceVersions">
<GenerateSemanticVersionRanges ProjectReferencesWithVersions="@(_ProjectReferencesWithVersions)">
<Output ItemName="_ProjectReferencesWithExactVersions" TaskParameter="ProjectReferencesWithExactVersions" />
</GenerateSemanticVersionRanges>
<ItemGroup>
<_ProjectReferencesWithVersions Remove="@(_ProjectReferencesWithVersions)" />
<_ProjectReferencesWithVersions Include="@(_ProjectReferencesWithExactVersions)" />
</ItemGroup>
</Target>
</Project>