-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbf5ee4
commit f410ea8
Showing
4 changed files
with
102 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
<Project> | ||
|
||
<PropertyGroup> | ||
<NuGetAuditMode Condition="'$(PackAsTool)' == 'true'">all</NuGetAuditMode> | ||
</PropertyGroup> | ||
<Import Project="msbuild\AutomaticVersionRanges.targets" Condition="Exists('msbuild\AutomaticVersionRanges.targets')" /> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<Project> | ||
|
||
<PropertyGroup> | ||
<AutomaticVersionRangesEnabled Condition="'$(AutomaticVersionRangesEnabled)' == '' And '$(Configuration)' == 'Debug'">false</AutomaticVersionRangesEnabled> | ||
<AutomaticVersionRangesEnabled Condition="'$(AutomaticVersionRangesEnabled)' == '' And '$(IsPackable)' == 'false'">false</AutomaticVersionRangesEnabled> | ||
<AutomaticVersionRangesEnabled Condition="'$(AutomaticVersionRangesEnabled)' == '' And '$(ManagePackageVersionsCentrally)' == 'true'">false</AutomaticVersionRangesEnabled> | ||
<AutomaticVersionRangesEnabled Condition="'$(AutomaticVersionRangesEnabled)' == ''">true</AutomaticVersionRangesEnabled> | ||
</PropertyGroup> | ||
|
||
<UsingTask TaskName="ConvertToVersionRange" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll"> | ||
<Task> | ||
<Code Source="$(MSBuildThisFileDirectory)ConvertToVersionRange.cs" /> | ||
</Task> | ||
</UsingTask> | ||
|
||
<Target Name="ConvertProjectReferenceVersionsToVersionRanges" AfterTargets="_GetProjectReferenceVersions" Condition="'$(AutomaticVersionRangesEnabled)' == 'true'"> | ||
<PropertyGroup> | ||
<NumberOfProjectReferences>@(_ProjectReferencesWithVersions->Count())</NumberOfProjectReferences> | ||
</PropertyGroup> | ||
<ConvertToVersionRange Condition="$(NumberOfProjectReferences) > 0" References="@(_ProjectReferencesWithVersions)" VersionProperty="ProjectVersion"> | ||
<Output TaskParameter="ReferencesWithVersionRanges" ItemName="_ProjectReferencesWithVersionRanges" /> | ||
</ConvertToVersionRange> | ||
<ItemGroup Condition="$(NumberOfProjectReferences) > 0"> | ||
<_ProjectReferencesWithVersions Remove="@(_ProjectReferencesWithVersions)" /> | ||
<_ProjectReferencesWithVersions Include="@(_ProjectReferencesWithVersionRanges)" /> | ||
</ItemGroup> | ||
</Target> | ||
|
||
<Target Name="ConvertPackageReferenceVersionsToVersionRanges" BeforeTargets="CollectPackageReferences" Condition="'$(AutomaticVersionRangesEnabled)' == 'true'"> | ||
<PropertyGroup> | ||
<NumberOfPackageReferences>@(PackageReference->Count())</NumberOfPackageReferences> | ||
</PropertyGroup> | ||
<ConvertToVersionRange Condition="$(NumberOfPackageReferences) > 0" References="@(PackageReference)" VersionProperty="Version"> | ||
<Output TaskParameter="ReferencesWithVersionRanges" ItemName="_PackageReferencesWithVersionRanges" /> | ||
</ConvertToVersionRange> | ||
<ItemGroup Condition="$(NumberOfPackageReferences) > 0"> | ||
<PackageReference Remove="@(PackageReference)" /> | ||
<PackageReference Include="@(_PackageReferencesWithVersionRanges)" /> | ||
</ItemGroup> | ||
</Target> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.Build.Framework; | ||
using Microsoft.Build.Utilities; | ||
|
||
public class ConvertToVersionRange : Task | ||
{ | ||
[Required] | ||
public ITaskItem[] References { get; set; } = []; | ||
|
||
[Required] | ||
public string VersionProperty { get; set; } = string.Empty; | ||
|
||
[Output] | ||
public ITaskItem[] ReferencesWithVersionRanges { get; private set; } = []; | ||
|
||
public override bool Execute() | ||
{ | ||
var success = true; | ||
|
||
foreach (var reference in References) | ||
{ | ||
var automaticVersionRange = reference.GetMetadata("AutomaticVersionRange"); | ||
|
||
if (automaticVersionRange.Equals("false", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
continue; | ||
} | ||
|
||
var privateAssets = reference.GetMetadata("PrivateAssets"); | ||
|
||
if (privateAssets.Equals("All", StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
continue; | ||
} | ||
|
||
var version = reference.GetMetadata(VersionProperty); | ||
var match = Regex.Match(version, @"^\d+"); | ||
|
||
if (match.Value.Equals(string.Empty, StringComparison.Ordinal)) | ||
{ | ||
Log.LogError("Reference '{0}' with version '{1}' is not valid for automatic version range conversion. Fix the version or exclude the reference from conversion by setting 'AutomaticVersionRange=\"false\"' on the reference.", reference.ItemSpec, version); | ||
success = false; | ||
continue; | ||
} | ||
|
||
var nextMajor = Convert.ToInt32(match.Value) + 1; | ||
|
||
var versionRange = $"[{version}, {nextMajor}.0.0)"; | ||
reference.SetMetadata(VersionProperty, versionRange); | ||
} | ||
|
||
ReferencesWithVersionRanges = References; | ||
|
||
return success; | ||
} | ||
} |