Skip to content

Commit d10b354

Browse files
Add -aot parameter to the console template (#31739)
This is similar to the `api` template in ASP.NET that also allows `-aot` (or `--publish-native-aot`) option (see dotnet/aspnetcore#46064). We set similar properties: * `<PublishAot>true</PublishAot>` (this is the obvious one) * `<InvariantGlobalization>true</InvariantGlobalization>` to get rid of ICU dependency on Linux and make the hello world executable ~20% smaller in general, even outside Linux. * a commented out `<StripSymbols>true</StripSymbols>`. This is for discoverability. We default this to `false` due to platform conventions, but many users would be fine setting this to `true`. `true` is the .NET convention. I took the string from ASP.NET. I could have also copied the localized strings but I guess it's better to not step on the toes of the localization team.
1 parent 3eafa1f commit d10b354

File tree

46 files changed

+229
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+229
-0
lines changed

src/Tests/Microsoft.TemplateEngine.Cli.UnitTests/ParserTests/Approvals/TabCompletionTests.TemplateCommand_GetAllSuggestions.verified.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ Default: false
8181
InsertText: --output,
8282
Detail: Location to place the generated output.
8383
},
84+
{
85+
Label: --publish-native-aot,
86+
Kind: Keyword,
87+
SortText: --publish-native-aot,
88+
InsertText: --publish-native-aot,
89+
Detail:
90+
Whether to enable the project for publishing as native AOT.
91+
Type: bool
92+
Default: false
93+
94+
},
8495
{
8596
Label: --type,
8697
Kind: Keyword,
@@ -106,6 +117,17 @@ Default: false
106117
InsertText: -?,
107118
Detail: Show help and usage information
108119
},
120+
{
121+
Label: -aot,
122+
Kind: Keyword,
123+
SortText: -aot,
124+
InsertText: -aot,
125+
Detail:
126+
Whether to enable the project for publishing as native AOT.
127+
Type: bool
128+
Default: false
129+
130+
},
109131
{
110132
Label: -f,
111133
Kind: Keyword,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// See https://aka.ms/new-console-template for more information
2+
Console.WriteLine("Hello, World!");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>%FRAMEWORK%</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<PublishAot>true</PublishAot>
9+
<InvariantGlobalization>true</InvariantGlobalization>
10+
11+
<!-- Uncomment below line to make native publish outputs a lot smaller on Linux and macOS -->
12+
<!-- <StripSymbols>true</StripSymbols> -->
13+
</PropertyGroup>
14+
15+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The template "Console App" was created successfully.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// See https://aka.ms/new-console-template for more information
2+
Console.WriteLine("Hello, World!");
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>%FRAMEWORK%</TargetFramework>
6+
<RootNamespace>vb_console</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
<PublishAot>true</PublishAot>
10+
<InvariantGlobalization>true</InvariantGlobalization>
11+
12+
<!-- Uncomment below line to make native publish outputs a lot smaller on Linux and macOS -->
13+
<!-- <StripSymbols>true</StripSymbols> -->
14+
</PropertyGroup>
15+
16+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The template "Console App" was created successfully.

src/Tests/dotnet-new.Tests/Approvals/DotnetNewHelpTests.CanShowHelpForTemplate_MatchOnNonChoiceParam.verified.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Template options:
3030
--use-program-main Whether to generate an explicit Program class and Main method instead of top-level statements.
3131
Type: bool
3232
Default: false
33+
-aot, --publish-native-aot Whether to enable the project for publishing as native AOT.
34+
Type: bool
35+
Default: false
3336

3437
To see help for other template languages (F#, VB), use --language option:
3538
dotnet new console -h --language F#

src/Tests/dotnet-new.Tests/Approvals/DotnetNewHelpTests.CanShowHelpForTemplate_console.verified.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Template options:
3030
--use-program-main Whether to generate an explicit Program class and Main method instead of top-level statements.
3131
Type: bool
3232
Default: false
33+
-aot, --publish-native-aot Whether to enable the project for publishing as native AOT.
34+
Type: bool
35+
Default: false
3336

3437
To see help for other template languages (F#, VB), use --language option:
3538
dotnet new console -h --language F#

src/Tests/dotnet-new.Tests/CommonTemplatesTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,66 @@ public void NuGetConfigPermissions()
164164
Directory.Delete(workingDir, true);
165165
}
166166

167+
[Theory]
168+
[InlineData(new object[] { "console", "C#" })]
169+
[InlineData(new object[] { "console", "VB" })]
170+
public async void AotVariants(string name, string language)
171+
{
172+
// "net8.0";
173+
string currentDefaultFramework = $"net{Environment.Version.Major}.{Environment.Version.Minor}";
174+
175+
string workingDir = CreateTemporaryFolder(folderName: $"{name}-{language}");
176+
string outputDir = "MyProject";
177+
string projName = name;
178+
179+
List<string> args = new() { "-o", outputDir };
180+
// VB build would fail for name 'console' (root namespace would conflict with BCL namespace)
181+
if (language.Equals("VB") == true && name.Equals("console"))
182+
{
183+
projName = "vb-console";
184+
args.Add("-n");
185+
args.Add(projName);
186+
}
187+
args.Add("-aot");
188+
189+
// Do not bother restoring. This would need to restore the AOT compiler.
190+
// We would need a nuget.config for that and it's a waste of time anyway.
191+
args.Add("--no-restore");
192+
193+
string extension = language == "VB" ? "vbproj" : "csproj";
194+
195+
string projectDir = Path.Combine(workingDir, outputDir);
196+
string finalProjectName = Path.Combine(projectDir, $"{projName}.{extension}");
197+
198+
Dictionary<string, string> environmentUnderTest = new() { ["DOTNET_NOLOGO"] = false.ToString() };
199+
TestContext.Current.AddTestEnvironmentVariables(environmentUnderTest);
200+
201+
TemplateVerifierOptions options = new TemplateVerifierOptions(templateName: name)
202+
{
203+
TemplateSpecificArgs = args,
204+
SnapshotsDirectory = "Approvals",
205+
OutputDirectory = workingDir,
206+
SettingsDirectory = _fixture.HomeDirectory,
207+
VerifyCommandOutput = true,
208+
DoNotPrependTemplateNameToScenarioName = false,
209+
DoNotAppendTemplateArgsToScenarioName = true,
210+
ScenarioName = language.Replace('#', 's').ToLower(),
211+
VerificationExcludePatterns = new[] { "*/stderr.txt", "*\\stderr.txt" },
212+
DotnetExecutablePath = TestContext.Current.ToolsetUnderTest.DotNetHostPath,
213+
}
214+
.WithCustomEnvironment(environmentUnderTest)
215+
.WithCustomScrubbers(
216+
ScrubbersDefinition.Empty
217+
.AddScrubber(sb => sb.Replace($"<TargetFramework>{currentDefaultFramework}</TargetFramework>", "<TargetFramework>%FRAMEWORK%</TargetFramework>"))
218+
.AddScrubber(sb => sb.Replace(finalProjectName, "%PROJECT_PATH%").UnixifyDirSeparators().ScrubByRegex("(^ Restored .* \\()(.*)(\\)\\.)", "$1%DURATION%$3", RegexOptions.Multiline), "txt")
219+
);
220+
221+
VerificationEngine engine = new VerificationEngine(_logger);
222+
await engine.Execute(options).ConfigureAwait(false);
223+
224+
Directory.Delete(workingDir, true);
225+
}
226+
167227
#region Project templates language features tests
168228

169229
/// <summary>

0 commit comments

Comments
 (0)