44using System . Collections . ObjectModel ;
55using System . CommandLine ;
66using System . CommandLine . Parsing ;
7- using System . Configuration ;
87using Microsoft . DotNet . Cli . Commands . Build ;
98using Microsoft . DotNet . Cli . Commands . Restore ;
9+ using Microsoft . DotNet . Cli . Commands . Run ;
1010using Microsoft . DotNet . Cli . Extensions ;
1111using Microsoft . DotNet . Cli . NuGetPackageDownloader ;
1212using Microsoft . DotNet . Cli . Utils ;
1313using NuGet . Commands ;
1414using NuGet . Common ;
1515using NuGet . Packaging ;
16- using NuGet . Packaging . Core ;
1716
1817namespace Microsoft . DotNet . Cli . Commands . Pack ;
1918
@@ -23,33 +22,54 @@ public class PackCommand(
2322 string ? msbuildPath = null
2423 ) : RestoringCommand ( msbuildArgs , noRestore , msbuildPath : msbuildPath )
2524{
26- public static PackCommand FromArgs ( string [ ] args , string ? msbuildPath = null )
25+ public static CommandBase FromArgs ( string [ ] args , string ? msbuildPath = null )
2726 {
2827 var parseResult = Parser . Parse ( [ "dotnet" , "pack" , ..args ] ) ;
2928 return FromParseResult ( parseResult , msbuildPath ) ;
3029 }
3130
32- public static PackCommand FromParseResult ( ParseResult parseResult , string ? msbuildPath = null )
31+ public static CommandBase FromParseResult ( ParseResult parseResult , string ? msbuildPath = null )
3332 {
34- var msbuildArgs = parseResult . OptionValuesToBeForwarded ( PackCommandParser . GetCommand ( ) ) . Concat ( parseResult . GetValue ( PackCommandParser . SlnOrProjectArgument ) ?? [ ] ) ;
35-
36- ReleasePropertyProjectLocator projectLocator = new ( parseResult , MSBuildPropertyNames . PACK_RELEASE ,
37- new ReleasePropertyProjectLocator . DependentCommandOptions (
38- parseResult . GetValue ( PackCommandParser . SlnOrProjectArgument ) ,
39- parseResult . HasOption ( PackCommandParser . ConfigurationOption ) ? parseResult . GetValue ( PackCommandParser . ConfigurationOption ) : null
40- )
41- ) ;
42-
43- bool noRestore = parseResult . HasOption ( PackCommandParser . NoRestoreOption ) || parseResult . HasOption ( PackCommandParser . NoBuildOption ) ;
44- var parsedMSBuildArgs = MSBuildArgs . AnalyzeMSBuildArguments (
45- msbuildArgs ,
46- CommonOptions . PropertiesOption ,
47- CommonOptions . RestorePropertiesOption ,
48- PackCommandParser . TargetOption ,
49- PackCommandParser . VerbosityOption ) ;
50- return new PackCommand (
51- parsedMSBuildArgs . CloneWithAdditionalProperties ( projectLocator . GetCustomDefaultConfigurationValueIfSpecified ( ) ) ,
52- noRestore ,
33+ var args = parseResult . GetValue ( PackCommandParser . SlnOrProjectOrFileArgument ) ?? [ ] ;
34+
35+ LoggerUtility . SeparateBinLogArguments ( args , out var binLogArgs , out var nonBinLogArgs ) ;
36+
37+ bool noBuild = parseResult . HasOption ( PackCommandParser . NoBuildOption ) ;
38+
39+ bool noRestore = noBuild || parseResult . HasOption ( PackCommandParser . NoRestoreOption ) ;
40+
41+ return CommandFactory . CreateVirtualOrPhysicalCommand (
42+ PackCommandParser . GetCommand ( ) ,
43+ PackCommandParser . SlnOrProjectOrFileArgument ,
44+ ( msbuildArgs , appFilePath ) => new VirtualProjectBuildingCommand (
45+ entryPointFileFullPath : Path . GetFullPath ( appFilePath ) ,
46+ msbuildArgs : msbuildArgs )
47+ {
48+ NoBuild = noBuild ,
49+ NoRestore = noRestore ,
50+ NoCache = true ,
51+ } ,
52+ ( msbuildArgs , msbuildPath ) =>
53+ {
54+ ReleasePropertyProjectLocator projectLocator = new ( parseResult , MSBuildPropertyNames . PACK_RELEASE ,
55+ new ReleasePropertyProjectLocator . DependentCommandOptions (
56+ nonBinLogArgs ,
57+ parseResult . HasOption ( PackCommandParser . ConfigurationOption ) ? parseResult . GetValue ( PackCommandParser . ConfigurationOption ) : null
58+ )
59+ ) ;
60+ return new PackCommand (
61+ msbuildArgs . CloneWithAdditionalProperties ( projectLocator . GetCustomDefaultConfigurationValueIfSpecified ( ) ) ,
62+ noRestore ,
63+ msbuildPath ) ;
64+ } ,
65+ optionsToUseWhenParsingMSBuildFlags :
66+ [
67+ CommonOptions . PropertiesOption ,
68+ CommonOptions . RestorePropertiesOption ,
69+ PackCommandParser . TargetOption ,
70+ PackCommandParser . VerbosityOption ,
71+ ] ,
72+ parseResult ,
5373 msbuildPath ) ;
5474 }
5575
@@ -67,7 +87,7 @@ private static LogLevel MappingVerbosityToNugetLogLevel(VerbosityOptions? verbos
6787
6888 public static int RunPackCommand ( ParseResult parseResult )
6989 {
70- var args = parseResult . GetValue ( PackCommandParser . SlnOrProjectArgument ) ? . ToList ( ) ?? new List < string > ( ) ;
90+ var args = parseResult . GetValue ( PackCommandParser . SlnOrProjectOrFileArgument ) ? . ToList ( ) ?? new List < string > ( ) ;
7191
7292 if ( args . Count != 1 )
7393 {
@@ -112,7 +132,7 @@ public static int Run(ParseResult parseResult)
112132 parseResult . HandleDebugSwitch ( ) ;
113133 parseResult . ShowHelpOrErrorIfAppropriate ( ) ;
114134
115- var args = parseResult . GetValue ( PackCommandParser . SlnOrProjectArgument ) ? . ToList ( ) ?? new List < string > ( ) ;
135+ var args = parseResult . GetValue ( PackCommandParser . SlnOrProjectOrFileArgument ) ? . ToList ( ) ?? new List < string > ( ) ;
116136
117137 if ( args . Count > 0 && Path . GetExtension ( args [ 0 ] ) . Equals ( ".nuspec" , StringComparison . OrdinalIgnoreCase ) )
118138 {
0 commit comments