Skip to content

Implementing dotnet pack with nuspec files #49813

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
14 changes: 13 additions & 1 deletion src/Cli/dotnet/Commands/CliCommandStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2524,4 +2524,16 @@ Proceed?</value>
<value>Tool package download needs confirmation. Run in interactive mode or use the "--yes" command-line option to confirm.</value>
<comment>{Locked="--yes"}</comment>
</data>
</root>
<data name="PackCmdVersionDescription" xml:space="preserve">
<value>The version of the package to create</value>
</data>
<data name="PackCmdVersion" xml:space="preserve">
<value>VERSION</value>
</data>
<data name="PackCmdPropertiesDescription" xml:space="preserve">
<value>Sets properties for the pack operation</value>
</data>
<data name="PackCmdProperties" xml:space="preserve">
<value>PROPERTY</value>
</data>
</root>
49 changes: 48 additions & 1 deletion src/Cli/dotnet/Commands/Pack/PackCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
using System.CommandLine;
using Microsoft.DotNet.Cli.Commands.Restore;
using Microsoft.DotNet.Cli.Extensions;
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
using Microsoft.DotNet.Cli.Utils;
using NuGet.Commands;
using NuGet.Packaging.Core;

namespace Microsoft.DotNet.Cli.Commands.Pack;

Expand All @@ -23,7 +26,6 @@ public static PackCommand FromArgs(string[] args, string? msbuildPath = null)

public static PackCommand FromParseResult(ParseResult parseResult, string? msbuildPath = null)
{
parseResult.ShowHelpOrErrorIfAppropriate();

var msbuildArgs = parseResult.OptionValuesToBeForwarded(PackCommandParser.GetCommand()).Concat(parseResult.GetValue(PackCommandParser.SlnOrProjectArgument) ?? []);

Expand All @@ -40,16 +42,61 @@ public static PackCommand FromParseResult(ParseResult parseResult, string? msbui
CommonOptions.PropertiesOption,
CommonOptions.RestorePropertiesOption,
PackCommandParser.TargetOption);

return new PackCommand(
parsedMSBuildArgs.CloneWithAdditionalProperties(projectLocator.GetCustomDefaultConfigurationValueIfSpecified()),
noRestore,
msbuildPath);
}

public static int RunPackCommand(ParseResult parseResult)
{
var args = parseResult.GetValue(PackCommandParser.SlnOrProjectArgument)?.ToList() ?? new List<string>();
var nuspecPath = args[0];
Copy link
Member

Choose a reason for hiding this comment

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

What if I do dotnet pack path\to\some.nuspec another\path\my.csproj? Or just dotnet pack with 2 nuspec files?


var packArgs = new PackArgs()
{
Path = nuspecPath,
BasePath = Path.GetDirectoryName(nuspecPath),
Logger = new NuGetConsoleLogger(),
Exclude = new List<string>()
};

var properties = parseResult.GetValue(PackCommandParser.PropertiesOption);
if (properties != null)
{
foreach (var prop in properties)
{
var split = prop.Split('=', 2);
if (split.Length == 2)
packArgs.Properties[split[0]] = split[1];
}
}

var version = parseResult.GetValue(PackCommandParser.VersionOption);
if (!string.IsNullOrEmpty(version))
packArgs.Version = version;

var packCommandRunner = new PackCommandRunner(packArgs, null);
if (!packCommandRunner.RunPackageBuild())
return 1;
return 0;
}


public static int Run(ParseResult parseResult)
{
parseResult.HandleDebugSwitch();
parseResult.ShowHelpOrErrorIfAppropriate();

var args = parseResult.GetValue(PackCommandParser.SlnOrProjectArgument)?.ToList() ?? new List<string>();

if (args.Count > 0 && Path.GetExtension(args[0]).Equals(".nuspec", StringComparison.OrdinalIgnoreCase))
{
return RunPackCommand(parseResult);
}

// Fallback to MSBuild-based packing
return FromParseResult(parseResult).Execute();
}
}
21 changes: 20 additions & 1 deletion src/Cli/dotnet/Commands/Pack/PackCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using Microsoft.DotNet.Cli.Commands.Build;
using Microsoft.DotNet.Cli.Commands.Restore;
using Microsoft.DotNet.Cli.Extensions;
using Microsoft.DotNet.Cli.NuGetPackageDownloader;
using NuGet.Commands;
using NuGet.Common;
using static Microsoft.DotNet.Cli.Commands.Run.CSharpDirective;

namespace Microsoft.DotNet.Cli.Commands.Pack;

Expand Down Expand Up @@ -60,6 +64,20 @@ internal static class PackCommandParser

public static readonly Option<string[]> TargetOption = CommonOptions.RequiredMSBuildTargetOption("Pack", [("_IsPacking", "true")]);

public static readonly Option<string> VersionOption = new ForwardedOption<string>("--version")
{
Description = CliCommandStrings.PackCmdVersionDescription,
HelpName = CliCommandStrings.PackCmdVersion,
Hidden = false
}.ForwardAsSingle(o => $"-Version={o}");

public static readonly Option<string[]> PropertiesOption = new ForwardedOption<string[]>("--property")
{
Description = CliCommandStrings.PackCmdPropertiesDescription,
HelpName = CliCommandStrings.PackCmdProperties,
Hidden = false
};

private static readonly Command Command = ConstructCommand();

public static Command GetCommand()
Expand All @@ -82,10 +100,11 @@ private static Command ConstructCommand()
command.Options.Add(CommonOptions.InteractiveMsBuildForwardOption);
command.Options.Add(NoRestoreOption);
command.Options.Add(BuildCommandParser.VerbosityOption);
command.Options.Add(VersionOption);
command.Options.Add(CommonOptions.VersionSuffixOption);
command.Options.Add(ConfigurationOption);
command.Options.Add(CommonOptions.DisableBuildServersOption);
command.Options.Add(TargetOption);
command.Options.Add(TargetOption);

// Don't include runtime option because we want to include it specifically and allow the short version ("-r") to be used
RestoreCommandParser.AddImplicitRestoreOptions(command, includeRuntimeOption: false, includeNoDependenciesOption: true);
Expand Down
20 changes: 20 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions src/Cli/dotnet/Commands/xlf/CliCommandStrings.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading