Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
32 changes: 26 additions & 6 deletions src/NuGet.Core/NuGet.Build.Tasks.Pack/GetPackOutputItemsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ public class GetPackOutputItemsTask : Microsoft.Build.Utilities.Task
[Required]
public string NuspecOutputPath { get; set; }

public string NuspecInputFilePath { get; set; }

public string[] NuspecProperties { get; set; }

public bool IncludeSymbols { get; set; }

public bool IncludeSource { get; set; }

public string SymbolPackageFormat { get; set; }

public bool OutputFileNamesWithoutVersion { get; set; }

/// <summary>
/// Output items
/// </summary>
Expand All @@ -43,27 +49,41 @@ public class GetPackOutputItemsTask : Microsoft.Build.Utilities.Task

public override bool Execute()
{
var packageId = PackageId;
var packageVersion = PackageVersion;

if (!string.IsNullOrWhiteSpace(NuspecInputFilePath))
{
var nuspecReader = new NuGet.Packaging.NuspecReader(NuspecInputFilePath);
packageId = nuspecReader.GetId();
packageVersion = nuspecReader.GetVersion().ToNormalizedString();

PackArgs packArgs = new PackArgs() { Version = packageVersion };
PackTaskLogic.SetPackArgsPropertiesFromNuspecProperties(packArgs, MSBuildStringUtility.TrimAndExcludeNullOrEmpty(NuspecProperties), NuspecInputFilePath);
packageVersion = packArgs.Version;
}

NuGetVersion version;
if (!NuGetVersion.TryParse(PackageVersion, out version))
if (!NuGetVersion.TryParse(packageVersion, out version))
{
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
Strings.InvalidPackageVersion,
PackageVersion));
packageVersion));
}

var symbolPackageFormat = PackArgs.GetSymbolPackageFormat(MSBuildStringUtility.TrimAndGetNullForEmpty(SymbolPackageFormat));
var nupkgFileName = PackCommandRunner.GetOutputFileName(PackageId, version, isNupkg: true, symbols: false, symbolPackageFormat: symbolPackageFormat);
var nuspecFileName = PackCommandRunner.GetOutputFileName(PackageId, version, isNupkg: false, symbols: false, symbolPackageFormat: symbolPackageFormat);
var nupkgFileName = PackCommandRunner.GetOutputFileName(packageId, version, isNupkg: true, symbols: false, symbolPackageFormat: symbolPackageFormat, excludeVersion: OutputFileNamesWithoutVersion);
var nuspecFileName = PackCommandRunner.GetOutputFileName(packageId, version, isNupkg: false, symbols: false, symbolPackageFormat: symbolPackageFormat, excludeVersion: OutputFileNamesWithoutVersion);

var outputs = new List<ITaskItem>();
outputs.Add(new TaskItem(Path.Combine(PackageOutputPath, nupkgFileName)));
outputs.Add(new TaskItem(Path.Combine(NuspecOutputPath, nuspecFileName)));

if (IncludeSource || IncludeSymbols)
{
var nupkgSymbolsFileName = PackCommandRunner.GetOutputFileName(PackageId, version, isNupkg: true, symbols: true, symbolPackageFormat: symbolPackageFormat);
var nuspecSymbolsFileName = PackCommandRunner.GetOutputFileName(PackageId, version, isNupkg: false, symbols: true, symbolPackageFormat: symbolPackageFormat);
var nupkgSymbolsFileName = PackCommandRunner.GetOutputFileName(packageId, version, isNupkg: true, symbols: true, symbolPackageFormat: symbolPackageFormat, excludeVersion: OutputFileNamesWithoutVersion);
var nuspecSymbolsFileName = PackCommandRunner.GetOutputFileName(packageId, version, isNupkg: false, symbols: true, symbolPackageFormat: symbolPackageFormat, excludeVersion: OutputFileNamesWithoutVersion);

outputs.Add(new TaskItem(Path.Combine(PackageOutputPath, nupkgSymbolsFileName)));
outputs.Add(new TaskItem(Path.Combine(NuspecOutputPath, nuspecSymbolsFileName)));
Expand Down
32 changes: 24 additions & 8 deletions src/NuGet.Core/NuGet.Build.Tasks.Pack/PackTaskLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,7 @@ public PackArgs GetPackArgs(IPackTaskRequest<IMSBuildItem> request)

if (!string.IsNullOrEmpty(request.NuspecFile))
{
if (request.NuspecProperties != null && request.NuspecProperties.Any())
{
packArgs.Properties.AddRange(ParsePropertiesAsDictionary(request.NuspecProperties));
if (packArgs.Properties.TryGetValue("version", out var version))
{
packArgs.Version = version;
}
}
SetPackArgsPropertiesFromNuspecProperties(packArgs, request.NuspecProperties, request.NuspecFile);
}
else
{
Expand Down Expand Up @@ -1091,6 +1084,29 @@ private static IDictionary<string, string> ParsePropertiesAsDictionary(string[]
return dictionary;
}

internal static void SetPackArgsPropertiesFromNuspecProperties(PackArgs packArgs, string[] nuspecProperties, string nuspecInputFilePath)
{
if (string.IsNullOrWhiteSpace(nuspecInputFilePath) || nuspecProperties == null || !nuspecProperties.Any())
{
return;
}

packArgs.Properties.AddRange(ParsePropertiesAsDictionary(nuspecProperties));
if (packArgs.Properties.TryGetValue("version", out var packageVersion))
{
if (!NuGetVersion.TryParse(packageVersion, out var version))
{
throw new ArgumentException(string.Format(
CultureInfo.CurrentCulture,
Strings.InvalidPackageVersion,
packageVersion));
}
packArgs.Version = version.ToNormalizedString();
}


}

private HashSet<string> InitOutputExtensions(IEnumerable<string> outputExtensions)
{
return new HashSet<string>(outputExtensions.Distinct(StringComparer.OrdinalIgnoreCase));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@ Copyright (c) .NET Foundation. All rights reserved.
<GetPackOutputItemsTask
PackageOutputPath="$(PackageOutputAbsolutePath)"
NuspecOutputPath="$(NuspecOutputAbsolutePath)"
NuspecInputFilePath="$(NuspecFile)"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PackTask already takes a bunch of these properties as input.
I think for ease for maintenance, the property names should be the same.

So instead of NuspecInputFilePath, use NuspecFile.

Something else, we should use the NuspecFileAbsolutePath as https://github.com/NuGet/NuGet.Client/blob/3a2648cbab3528f4105e0dedbe60be26289379eb/src/NuGet.Core/NuGet.Build.Tasks/NuGet.Build.Tasks.Pack.targets#L265C29-L265C51 is using it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change the property name from NuspecInputFilePath to NuspecFile

$(AbsolutePath) doesn’t exist when GetPackOutputItemsTask is called, I changed the logic to generate it from $(NuspecFile) and pass it in.

Ideally, for maintainability, PackTask should use the output filename produced by GetPackOutputItemsTask directly, which would avoid inconsistencies from generating filenames separately. However, that would require larger changes to PackTask, so this pull request applies the minimal fix.

NuspecProperties="$(NuspecProperties)"
PackageId="$(PackageId)"
PackageVersion="$(PackageVersion)"
IncludeSymbols="$(IncludeSymbols)"
IncludeSource="$(IncludeSource)"
SymbolPackageFormat="$(SymbolPackageFormat)">

SymbolPackageFormat="$(SymbolPackageFormat)"
OutputFileNamesWithoutVersion="$(OutputFileNamesWithoutVersion)">
<Output
TaskParameter="OutputPackItems"
ItemName="_OutputPackItems" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\NuGet.Core\NuGet.Build.Tasks\NuGet.Build.Tasks.csproj" />
<ProjectReference Include="..\..\TestUtilities\Test.Utility\Test.Utility.csproj" />
<ProjectReference Include="..\..\..\src\NuGet.Core\NuGet.Build.Tasks.Pack\NuGet.Build.Tasks.Pack.csproj" />
</ItemGroup>
Expand Down
Loading
Loading