Add support for exact version NuGet dependencies based on Maven dependency info from POM. (#770)

This commit is contained in:
Jonathan Pobst 2023-10-18 14:21:56 -10:00 коммит произвёл GitHub
Родитель bf08825d20
Коммит cd26694543
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 69 добавлений и 3 удалений

18
Directory.Build.targets Normal file
Просмотреть файл

@ -0,0 +1,18 @@
<Project>
<!-- Enables "ExactVersion" attribute on "ProjectReference" to allow an exact NuGet reference version to be used.
https://github.com/NuGet/Home/issues/5556#issuecomment-1179526189 -->
<Target Name="UseExplicitPackageVersions" BeforeTargets="GenerateNuspec">
<ItemGroup>
<_ProjectReferenceWithExplicitPackageVersion Include="@(ProjectReference->'%(FullPath)')" Condition="'%(ProjectReference.PackageVersion)' != ''" />
<_ProjectReferenceWithExactPackageVersion Include="@(ProjectReference->'%(FullPath)')" Condition="'%(ProjectReference.ExactVersion)' == 'true'" />
<_ProjectReferenceWithReassignedVersion Include="@(_ProjectReferencesWithVersions)" Condition="'%(Identity)' != '' And '@(_ProjectReferenceWithExplicitPackageVersion)' == '@(_ProjectReferencesWithVersions)'">
<ProjectVersion>@(_ProjectReferenceWithExplicitPackageVersion->'%(PackageVersion)')</ProjectVersion>
</_ProjectReferenceWithReassignedVersion>
<_ProjectReferenceWithReassignedVersion Include="@(_ProjectReferencesWithVersions)" Condition="'%(Identity)' != '' And '@(_ProjectReferenceWithExactPackageVersion)' == '@(_ProjectReferencesWithVersions)'">
<ProjectVersion>[@(_ProjectReferencesWithVersions->'%(ProjectVersion)')]</ProjectVersion>
</_ProjectReferenceWithReassignedVersion>
<_ProjectReferencesWithVersions Remove="@(_ProjectReferenceWithReassignedVersion)" />
<_ProjectReferencesWithVersions Include="@(_ProjectReferenceWithReassignedVersion)" />
</ItemGroup>
</Target>
</Project>

Просмотреть файл

@ -173,8 +173,7 @@
<!-- ProjectReference --> <!-- ProjectReference -->
@foreach (var dep in @Model.NuGetDependencies) { @foreach (var dep in @Model.NuGetDependencies) {
if (dep.IsProjectReference) { if (dep.IsProjectReference) {
<ProjectReference Include="..\..\generated\@(dep.MavenArtifact.MavenGroupId).@(dep.MavenArtifact.MavenArtifactId)\@(dep.MavenArtifact.MavenGroupId).@(dep.MavenArtifact.MavenArtifactId).csproj" PrivateAssets="none" /> <ProjectReference Include="..\..\generated\@(dep.MavenArtifact.MavenGroupId).@(dep.MavenArtifact.MavenArtifactId)\@(dep.MavenArtifact.MavenGroupId).@(dep.MavenArtifact.MavenArtifactId).csproj" PrivateAssets="none" PackageVersion="@GetProjectVersionString(dep.MavenArtifact.MavenArtifactVersion, dep.NuGetVersion)" />
} }
} }
</ItemGroup> </ItemGroup>
@ -184,7 +183,7 @@
<!-- PackageReference --> <!-- PackageReference -->
@foreach (var dep in @Model.NuGetDependencies) { @foreach (var dep in @Model.NuGetDependencies) {
if (!dep.IsProjectReference) { if (!dep.IsProjectReference) {
<PackageReference Include="@(dep.NuGetPackageId)" Version="@(dep.NuGetVersion)" PrivateAssets="none" /> <PackageReference Include="@(dep.NuGetPackageId)" Version="@GetVersionString(dep.MavenArtifact.MavenArtifactVersion, dep.NuGetVersion)" PrivateAssets="none" />
} }
} }
</ItemGroup> </ItemGroup>
@ -205,4 +204,53 @@
</PropertyGroup> </PropertyGroup>
} }
@{
string GetProjectVersionString (string mavenVersion, string nugetVersion)
{
var adjusted_string = GetVersionString (mavenVersion, nugetVersion);
// If nothing changed, return empty string
if (adjusted_string == nugetVersion)
return null;
return adjusted_string;
}
string GetVersionString (string mavenVersion, string nugetVersion)
{
// If this isn't an exact version we don't use this code
if (!mavenVersion.StartsWith ('[') || !mavenVersion.EndsWith (']') || mavenVersion.Contains (','))
return nugetVersion;
// An exact version is requested like "1.2.0", however we want to let the revision (4th NuGet number) float,
// so that our updates that don't change the Java artifact can still be used.
// That is, if the POM specifies "1.2.0" and the dependency NuGet is already "1.2.0.4", we want to allow:
// 1.2.0.4 >= x < 1.2.1
// NuGet expresses that as "[1.2.0.4, 1.2.1)", so that's what we need to create.
var lower_bound = nugetVersion;
var nuget_first = 0;
var nuget_second = 0;
var nuget_third = 0;
var nuget_parts = nugetVersion.Split ('.');
if (nuget_parts.Length > 0 && int.TryParse (nuget_parts [0], out var p1))
nuget_first = p1;
if (nuget_parts.Length > 1 && int.TryParse (nuget_parts [1], out var p2))
nuget_second = p2;
if (nuget_parts.Length > 2 && int.TryParse (nuget_parts [2], out var p3))
nuget_third = p3;
// Bump the third number
nuget_third++;
var upper_bound = $"{nuget_first}.{nuget_second}.{nuget_third}";
// Put it all together
return $"[{lower_bound}, {upper_bound})";
}
}
</Project> </Project>