Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for exact version NuGet dependencies #770

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -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>
54 changes: 51 additions & 3 deletions source/AndroidXProject.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@
<!-- ProjectReference -->
@foreach (var dep in @Model.NuGetDependencies) {
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>
Expand All @@ -184,7 +183,7 @@
<!-- PackageReference -->
@foreach (var dep in @Model.NuGetDependencies) {
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>
Expand All @@ -205,4 +204,53 @@
</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>