Skip to content

Commit 0439a83

Browse files
authored
Publish/pack file-based apps in Release config by default (#50681)
1 parent 6b2817e commit 0439a83

File tree

5 files changed

+54
-27
lines changed

5 files changed

+54
-27
lines changed

src/Cli/dotnet/Commands/CommandFactory.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ internal static CommandBase CreateVirtualOrPhysicalCommand(
1717
Func<MSBuildArgs, string?, CommandBase> createPhysicalCommand,
1818
IEnumerable<Option> optionsToUseWhenParsingMSBuildFlags,
1919
ParseResult parseResult,
20-
string? msbuildPath = null)
20+
string? msbuildPath = null,
21+
Func<MSBuildArgs, MSBuildArgs>? transformer = null)
2122
{
2223
var args = parseResult.GetValue(catchAllUserInputArgument) ?? [];
2324
LoggerUtility.SeparateBinLogArguments(args, out var binLogArgs, out var nonBinLogArgs);
@@ -32,11 +33,13 @@ internal static CommandBase CreateVirtualOrPhysicalCommand(
3233
CommonOptions.GetTargetResultOption,
3334
CommonOptions.GetResultOutputFileOption,
3435
]);
36+
msbuildArgs = transformer?.Invoke(msbuildArgs) ?? msbuildArgs;
3537
return configureVirtualCommand(msbuildArgs, Path.GetFullPath(arg));
3638
}
3739
else
3840
{
3941
var msbuildArgs = MSBuildArgs.AnalyzeMSBuildArguments([.. forwardedArgs, .. args], [.. optionsToUseWhenParsingMSBuildFlags]);
42+
msbuildArgs = transformer?.Invoke(msbuildArgs) ?? msbuildArgs;
4043
return createPhysicalCommand(msbuildArgs, msbuildPath);
4144
}
4245
}

src/Cli/dotnet/Commands/Pack/PackCommand.cs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,10 @@ public static CommandBase FromParseResult(ParseResult parseResult, string? msbui
4949
NoRestore = noRestore,
5050
NoCache = true,
5151
},
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-
},
52+
(msbuildArgs, msbuildPath) => new PackCommand(
53+
msbuildArgs,
54+
noRestore,
55+
msbuildPath),
6556
optionsToUseWhenParsingMSBuildFlags:
6657
[
6758
CommonOptions.PropertiesOption,
@@ -70,7 +61,17 @@ public static CommandBase FromParseResult(ParseResult parseResult, string? msbui
7061
PackCommandParser.VerbosityOption,
7162
],
7263
parseResult,
73-
msbuildPath);
64+
msbuildPath,
65+
(msbuildArgs) =>
66+
{
67+
ReleasePropertyProjectLocator projectLocator = new(parseResult, MSBuildPropertyNames.PACK_RELEASE,
68+
new ReleasePropertyProjectLocator.DependentCommandOptions(
69+
nonBinLogArgs,
70+
parseResult.HasOption(PackCommandParser.ConfigurationOption) ? parseResult.GetValue(PackCommandParser.ConfigurationOption) : null
71+
)
72+
);
73+
return msbuildArgs.CloneWithAdditionalProperties(projectLocator.GetCustomDefaultConfigurationValueIfSpecified());
74+
});
7475
}
7576

7677
private static LogLevel MappingVerbosityToNugetLogLevel(VerbosityOptions? verbosity)

src/Cli/dotnet/Commands/Publish/PublishCommand.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,25 @@ public static CommandBase FromParseResult(ParseResult parseResult, string? msbui
5252
NoRestore = noRestore,
5353
NoCache = true,
5454
},
55-
(msbuildArgs, msbuildPath) => {
55+
(msbuildArgs, msbuildPath) => new PublishCommand(
56+
msbuildArgs: msbuildArgs,
57+
noRestore: noRestore,
58+
msbuildPath: msbuildPath
59+
),
60+
[CommonOptions.PropertiesOption, CommonOptions.RestorePropertiesOption, PublishCommandParser.TargetOption, PublishCommandParser.VerbosityOption],
61+
parseResult,
62+
msbuildPath,
63+
(msbuildArgs) =>
64+
{
5665
var options = new ReleasePropertyProjectLocator.DependentCommandOptions(
5766
nonBinLogArgs,
5867
parseResult.HasOption(PublishCommandParser.ConfigurationOption) ? parseResult.GetValue(PublishCommandParser.ConfigurationOption) : null,
5968
parseResult.HasOption(PublishCommandParser.FrameworkOption) ? parseResult.GetValue(PublishCommandParser.FrameworkOption) : null
6069
);
6170
var projectLocator = new ReleasePropertyProjectLocator(parseResult, MSBuildPropertyNames.PUBLISH_RELEASE, options);
6271
var releaseModeProperties = projectLocator.GetCustomDefaultConfigurationValueIfSpecified();
63-
return new PublishCommand(
64-
msbuildArgs: msbuildArgs.CloneWithAdditionalProperties(releaseModeProperties),
65-
noRestore: noRestore,
66-
msbuildPath: msbuildPath
67-
);
68-
},
69-
[CommonOptions.PropertiesOption, CommonOptions.RestorePropertiesOption, PublishCommandParser.TargetOption, PublishCommandParser.VerbosityOption],
70-
parseResult,
71-
msbuildPath
72+
return msbuildArgs.CloneWithAdditionalProperties(releaseModeProperties);
73+
}
7274
);
7375
}
7476

src/Cli/dotnet/ReleasePropertyProjectLocator.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using System.Collections.ObjectModel;
55
using System.CommandLine;
66
using System.Diagnostics;
7+
using Microsoft.Build.Evaluation;
78
using Microsoft.Build.Execution;
9+
using Microsoft.DotNet.Cli.Commands.Run;
810
using Microsoft.DotNet.Cli.Utils;
911
using Microsoft.NET.Build.Tasks;
1012
using Microsoft.VisualStudio.SolutionPersistence.Model;
@@ -108,7 +110,12 @@ DependentCommandOptions commandOptions
108110
{
109111
foreach (string arg in _slnOrProjectArgs.Append(Directory.GetCurrentDirectory()))
110112
{
111-
if (IsValidProjectFilePath(arg))
113+
if (VirtualProjectBuildingCommand.IsValidEntryPointPath(arg))
114+
{
115+
return new VirtualProjectBuildingCommand(Path.GetFullPath(arg), MSBuildArgs.FromProperties(globalProps))
116+
.CreateProjectInstance(ProjectCollection.GlobalProjectCollection);
117+
}
118+
else if (IsValidProjectFilePath(arg))
112119
{
113120
return TryGetProjectInstance(arg, globalProps);
114121
}

test/dotnet.Tests/CommandTests/Run/RunFileTests.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,14 @@ public void Publish()
15041504
new DirectoryInfo(publishDir).Sub("Program")
15051505
.Should().Exist()
15061506
.And.NotHaveFilesMatching("*.deps.json", SearchOption.TopDirectoryOnly); // no deps.json file for AOT-published app
1507+
1508+
new RunExeCommand(Log, Path.Join(publishDir, "Program", $"Program{Constants.ExeSuffix}"))
1509+
.Execute()
1510+
.Should().Pass()
1511+
.And.HaveStdOut("""
1512+
Hello from Program
1513+
Release config
1514+
""");
15071515
}
15081516

15091517
[Fact]
@@ -1689,6 +1697,9 @@ public void Pack()
16891697
File.WriteAllText(programFile, """
16901698
#:property PackAsTool=true
16911699
Console.WriteLine($"Hello; EntryPointFilePath set? {AppContext.GetData("EntryPointFilePath") is string}");
1700+
#if !DEBUG
1701+
Console.WriteLine("Release config");
1702+
#endif
16921703
""");
16931704

16941705
// Run unpacked.
@@ -1719,7 +1730,10 @@ public void Pack()
17191730
.WithWorkingDirectory(testInstance.Path)
17201731
.Execute()
17211732
.Should().Pass()
1722-
.And.HaveStdOutContaining("Hello; EntryPointFilePath set? False");
1733+
.And.HaveStdOutContaining("""
1734+
Hello; EntryPointFilePath set? False
1735+
Release config
1736+
""");
17231737
}
17241738

17251739
[Fact]

0 commit comments

Comments
 (0)