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 version-suffix to P2P references for dotnet pack #1688

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion src/NuGet.Core/NuGet.Build.Tasks.Pack/IPackTaskRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.Build.Framework;
Expand Down Expand Up @@ -59,5 +59,6 @@ public interface IPackTaskRequest<TItem>
string[] TargetFrameworks { get; }
TItem[] TargetPathsToSymbols { get; }
string Title { get; }
string VersionSuffix { get; }
}
}
1 change: 1 addition & 0 deletions src/NuGet.Core/NuGet.Build.Tasks.Pack/Pack.targets
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ Copyright (c) .NET Foundation. All rights reserved.
PackageVersion="$(PackageVersion)"
PackageId="$(PackageId)"
Title="$(Title)"
VersionSuffix="$(VersionSuffix)"
Authors="$(Authors)"
Description="$(PackageDescription)"
Copyright="$(Copyright)"
Expand Down
4 changes: 3 additions & 1 deletion src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class PackTask : Microsoft.Build.Utilities.Task, IPackTaskRequest<ITaskIt
public ITaskItem[] BuildOutputInPackage { get; set; }
public string PackageId { get; set; }
public string PackageVersion { get; set; }
public string VersionSuffix { get; set; }
public string Title { get; set; }
public string[] Authors { get; set; }
public string Description { get; set; }
Expand Down Expand Up @@ -164,7 +165,8 @@ private IPackTaskRequest<IMSBuildItem> GetRequest()
Tags = MSBuildStringUtility.TrimAndExcludeNullOrEmpty(Tags),
TargetFrameworks = MSBuildStringUtility.TrimAndExcludeNullOrEmpty(TargetFrameworks),
TargetPathsToSymbols = MSBuildUtility.WrapMSBuildItem(TargetPathsToSymbols),
Title = MSBuildStringUtility.TrimAndGetNullForEmpty(Title)
Title = MSBuildStringUtility.TrimAndGetNullForEmpty(Title),
VersionSuffix = MSBuildStringUtility.TrimAndGetNullForEmpty(VersionSuffix)
};
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTaskLogic.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -183,7 +183,7 @@ public PackageBuilder GetPackageBuilder(IPackTaskRequest<IMSBuildItem> request)
assetsFilePath));
}

PopulateProjectAndPackageReferences(builder, assetsFile);
PopulateProjectAndPackageReferences(builder, assetsFile, request.VersionSuffix);
PopulateFrameworkAssemblyReferences(builder, request);
return builder;
}
Expand Down Expand Up @@ -570,11 +570,11 @@ private IDictionary<string, string> GetSourceFiles(IPackTaskRequest<IMSBuildItem
return sourceFiles;
}

private void PopulateProjectAndPackageReferences(PackageBuilder packageBuilder, LockFile assetsFile)
private void PopulateProjectAndPackageReferences(PackageBuilder packageBuilder, LockFile assetsFile, string versionSuffix)
{
var dependenciesByFramework = new Dictionary<NuGetFramework, HashSet<LibraryDependency>>();

InitializeProjectDependencies(assetsFile, dependenciesByFramework);
InitializeProjectDependencies(assetsFile, dependenciesByFramework, versionSuffix);
InitializePackageDependencies(assetsFile, dependenciesByFramework);

foreach (var pair in dependenciesByFramework)
Expand All @@ -585,7 +585,8 @@ private void PopulateProjectAndPackageReferences(PackageBuilder packageBuilder,

private static void InitializeProjectDependencies(
LockFile assetsFile,
Dictionary<NuGetFramework, HashSet<LibraryDependency>> dependenciesByFramework)
Dictionary<NuGetFramework, HashSet<LibraryDependency>> dependenciesByFramework,
string versionSuffix)
{
// From the package spec, all we know is each absolute path to the project reference the the target
// framework that project reference applies to.
Expand Down Expand Up @@ -640,6 +641,10 @@ private static void InitializeProjectDependencies(
continue;
}

if(!string.IsNullOrEmpty(versionSuffix) && !string.Equals(versionSuffix, targetLibrary.Version.Release, StringComparison.Ordinal))
{
targetLibrary.Version = new NuGetVersion(targetLibrary.Version.Version, versionSuffix);
}
// TODO: Implement <TreatAsPackageReference>false</TreatAsPackageReference>
// https://github.com/NuGet/Home/issues/3891
//
Expand Down
3 changes: 2 additions & 1 deletion src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTaskRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -53,5 +53,6 @@ public class PackTaskRequest : IPackTaskRequest<IMSBuildItem>
public string[] TargetFrameworks { get; set; }
public IMSBuildItem[] TargetPathsToSymbols { get; set; }
public string Title { get; set; }
public string VersionSuffix { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,108 @@ public void PackCommand_PackProject_AddsProjectRefsAsPackageRefs()
}
}

[PlatformTheory(Platform.Windows)]
[InlineData("1.0.0", "alpha2", "1.0.0-alpha2")]
[InlineData("1.0.0-alpha1", "alpha2", "1.0.0-alpha2")]
[InlineData("1.0.0-1.0.0", "alpha2.abc", "1.0.0-alpha2.abc")]
[InlineData("1.2.3.4", "alpha2", "1.2.3.4-alpha2")]
[InlineData("1.0.0-alpha-beta", "gamma", "1.0.0-gamma")]
[InlineData("1.0.0-alpha2", "alpha2", "1.0.0-alpha2")]
[InlineData("1.0.0.0", "alpha2", "1.0.0.0-alpha2")]
[InlineData("1.0.0" , "alpha2+asdafdfsdfgdfsdf", "1.0.0.0-alpha2+asdafdfsdfgdfsdf")]
[InlineData("1.0.0-beta+asdasdasdasasd", "alpha2+asdafdfsdfgdfsdf", "1.0.0.0-alpha2+asdafdfsdfgdfsdf")]
[InlineData("1.0.0", "0-abc-123", "1.0.0-0-abc-123")]
public void PackCommand_PackProject_AddsProjectRefsAsPackageRefsWithVersionSuffix(string projectRefVersion, string versionSuffix, string expectedVersion)
{
// Arrange
using (var testDirectory = TestDirectory.Create())
{
var projectName = "ClassLibrary1";
var referencedProject = "ClassLibrary2";
var workingDirectory = Path.Combine(testDirectory, projectName);
var projectFile = Path.Combine(workingDirectory, $"{projectName}.csproj");
var referencedProjectFile = Path.Combine(testDirectory, referencedProject, $"{referencedProject}.csproj");


msbuildFixture.CreateDotnetNewProject(testDirectory.Path, projectName);
msbuildFixture.CreateDotnetNewProject(testDirectory.Path, referencedProject, "classlib");

using (var stream = new FileStream(referencedProjectFile, FileMode.Open, FileAccess.ReadWrite))
{
var xml = XDocument.Load(stream);

ProjectFileUtils.AddProperty(xml, "Version", projectRefVersion);
ProjectFileUtils.WriteXmlToFile(xml, stream);
}

using (var stream = new FileStream(projectFile, FileMode.Open, FileAccess.ReadWrite))
{
var xml = XDocument.Load(stream);

var attributes = new Dictionary<string, string>();

var properties = new Dictionary<string, string>();
ProjectFileUtils.AddItem(
xml,
"ProjectReference",
@"..\ClassLibrary2\ClassLibrary2.csproj",
string.Empty,
properties,
attributes);

ProjectFileUtils.WriteXmlToFile(xml, stream);
}

msbuildFixture.RestoreProject(workingDirectory, projectName, string.Empty);

// Act
msbuildFixture.PackProject(workingDirectory, projectName, $"-o {workingDirectory} --version-suffix {versionSuffix}");

var nupkgPath = Path.Combine(workingDirectory, $"{projectName}.1.0.0-{versionSuffix}.nupkg");
var nuspecPath = Path.Combine(workingDirectory, "obj", $"{projectName}.1.0.0-{versionSuffix}.nuspec");
Assert.True(File.Exists(nupkgPath), "The output .nupkg is not in the expected place");
Assert.True(File.Exists(nuspecPath), "The intermediate nuspec file is not in the expected place");

// Assert
using (var nupkgReader = new PackageArchiveReader(nupkgPath))
{
var nuspecReader = nupkgReader.NuspecReader;

var dependencyGroups = nuspecReader
.GetDependencyGroups()
.OrderBy(x => x.TargetFramework,
new NuGetFrameworkSorter())
.ToList();

Assert.Equal(1,
dependencyGroups.Count);

Assert.Equal(FrameworkConstants.CommonFrameworks.NetCoreApp20,
dependencyGroups[0].TargetFramework);
var packagesA = dependencyGroups[0].Packages.ToList();
Assert.Equal(2,
packagesA.Count);
Assert.Equal("Microsoft.NETCore.App", packagesA[1].Id);
Assert.Equal(new List<string> { "Analyzers", "Build" }, packagesA[1].Exclude);
Assert.Empty(packagesA[1].Include);

Assert.Equal("ClassLibrary2", packagesA[0].Id);
Assert.Equal(new VersionRange(new NuGetVersion(expectedVersion)), packagesA[0].VersionRange);
Assert.Equal(new List<string> { "Analyzers", "Build" }, packagesA[0].Exclude);
Assert.Empty(packagesA[0].Include);

// Validate the assets.
var libItems = nupkgReader.GetLibItems().ToList();
Assert.Equal(1, libItems.Count);
Assert.Equal(FrameworkConstants.CommonFrameworks.NetCoreApp20, libItems[0].TargetFramework);
Assert.Equal(
new[]
{"lib/netcoreapp2.0/ClassLibrary1.dll", "lib/netcoreapp2.0/ClassLibrary1.runtimeconfig.json"},
libItems[0].Items);
}
}
}

[PlatformFact(Platform.Windows)]
public void PackCommand_PackProject_PacksFromNuspec()
{
Expand Down