11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4- #nullable disable
5-
64using System . CommandLine ;
75using Microsoft . DotNet . Cli . Commands . Restore ;
6+ using Microsoft . DotNet . Cli . Commands . Run ;
87using Microsoft . DotNet . Cli . Extensions ;
98
109namespace Microsoft . DotNet . Cli . Commands . Publish ;
@@ -14,49 +13,75 @@ public class PublishCommand : RestoringCommand
1413 private PublishCommand (
1514 IEnumerable < string > msbuildArgs ,
1615 bool noRestore ,
17- string msbuildPath = null )
16+ string ? msbuildPath = null )
1817 : base ( msbuildArgs , noRestore , msbuildPath )
1918 {
2019 }
2120
22- public static PublishCommand FromArgs ( string [ ] args , string msbuildPath = null )
21+ public static CommandBase FromArgs ( string [ ] args , string ? msbuildPath = null )
2322 {
2423 var parser = Parser . Instance ;
2524 var parseResult = parser . ParseFrom ( "dotnet publish" , args ) ;
2625 return FromParseResult ( parseResult ) ;
2726 }
2827
29- public static PublishCommand FromParseResult ( ParseResult parseResult , string msbuildPath = null )
28+ public static CommandBase FromParseResult ( ParseResult parseResult , string ? msbuildPath = null )
3029 {
3130 parseResult . HandleDebugSwitch ( ) ;
3231 parseResult . ShowHelpOrErrorIfAppropriate ( ) ;
3332
3433 var msbuildArgs = new List < string > ( )
3534 {
36- "-target:Publish" ,
3735 "--property:_IsPublishing=true" // This property will not hold true for MSBuild /t:Publish or in VS.
3836 } ;
3937
40- IEnumerable < string > slnOrProjectArgs = parseResult . GetValue ( PublishCommandParser . SlnOrProjectArgument ) ;
38+ string [ ] args = parseResult . GetValue ( PublishCommandParser . SlnOrProjectOrFileArgument ) ?? [ ] ;
39+
40+ LoggerUtility . SeparateBinLogArguments ( args , out var binLogArgs , out var nonBinLogArgs ) ;
4141
4242 CommonOptions . ValidateSelfContainedOptions ( parseResult . HasOption ( PublishCommandParser . SelfContainedOption ) ,
4343 parseResult . HasOption ( PublishCommandParser . NoSelfContainedOption ) ) ;
4444
4545 msbuildArgs . AddRange ( parseResult . OptionValuesToBeForwarded ( PublishCommandParser . GetCommand ( ) ) ) ;
4646
47+ bool noBuild = parseResult . HasOption ( PublishCommandParser . NoBuildOption ) ;
48+
49+ bool noRestore = noBuild || parseResult . HasOption ( PublishCommandParser . NoRestoreOption ) ;
50+
51+ if ( nonBinLogArgs is [ { } arg ] && VirtualProjectBuildingCommand . IsValidEntryPointPath ( arg ) )
52+ {
53+ if ( ! parseResult . HasOption ( PublishCommandParser . ConfigurationOption ) )
54+ {
55+ msbuildArgs . Add ( "-p:Configuration=Release" ) ;
56+ }
57+
58+ msbuildArgs . AddRange ( binLogArgs ) ;
59+
60+ return new VirtualProjectBuildingCommand (
61+ entryPointFileFullPath : Path . GetFullPath ( arg ) ,
62+ msbuildArgs : msbuildArgs ,
63+ verbosity : parseResult . GetValue ( CommonOptions . VerbosityOption ) ,
64+ interactive : parseResult . GetValue ( CommonOptions . InteractiveMsBuildForwardOption ) )
65+ {
66+ NoBuild = noBuild ,
67+ NoRestore = noRestore ,
68+ NoCache = true ,
69+ BuildTarget = "Publish" ,
70+ } ;
71+ }
72+
4773 ReleasePropertyProjectLocator projectLocator = new ( parseResult , MSBuildPropertyNames . PUBLISH_RELEASE ,
4874 new ReleasePropertyProjectLocator . DependentCommandOptions (
49- parseResult . GetValue ( PublishCommandParser . SlnOrProjectArgument ) ,
75+ nonBinLogArgs ,
5076 parseResult . HasOption ( PublishCommandParser . ConfigurationOption ) ? parseResult . GetValue ( PublishCommandParser . ConfigurationOption ) : null ,
5177 parseResult . HasOption ( PublishCommandParser . FrameworkOption ) ? parseResult . GetValue ( PublishCommandParser . FrameworkOption ) : null
5278 )
5379 ) ;
5480 msbuildArgs . AddRange ( projectLocator . GetCustomDefaultConfigurationValueIfSpecified ( ) ) ;
5581
56- msbuildArgs . AddRange ( slnOrProjectArgs ?? [ ] ) ;
82+ msbuildArgs . AddRange ( args ?? [ ] ) ;
5783
58- bool noRestore = parseResult . HasOption ( PublishCommandParser . NoRestoreOption )
59- || parseResult . HasOption ( PublishCommandParser . NoBuildOption ) ;
84+ msbuildArgs . Insert ( 0 , "-target:Publish" ) ;
6085
6186 return new PublishCommand (
6287 msbuildArgs ,
0 commit comments