Skip to content

Commit fd40f04

Browse files
authored
Revamp new project template build/test script (#25744)
* Add template manual test script * Update * Make compatible with macOS and VS Code Insiders
1 parent 46ad78f commit fd40f04

File tree

4 files changed

+121
-31
lines changed

4 files changed

+121
-31
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,4 @@ snapshots-diff/
382382
.dotnet
383383
temp
384384
.packages
385+
/src/Templates/.tempTemplateOutput

src/Templates/README.md

+32-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,39 @@
11

22
# MAUI Templates
33

4-
## Building / Testing
4+
For easy building and testing you can use the `build.ps1` script. This script is only for manual use and not part of any pipeline.
55

6-
Add the local artifacts to the NuGet.config:
6+
> [!NOTE]
7+
> On macOS you find encounter and error like: `error NU5119: Warning As Error: File '/file/path/.DS_Store' was not added to the package. Files and folders starting with '.' or ending with '.nupkg' are excluded by default. To include this file, use -NoDefaultExcludes from the commandline` when this happens, run a `git clean -xfd` on the repository to remove all `.DS_Store` files from the filesystem.
78
8-
```xml
9-
<add key="LocalMauiTemplates" value="./artifacts" />
10-
```
9+
## Functionality
1110

12-
```dotnetcli
13-
# uninstall, build, and install the templates
14-
dotnet new uninstall Microsoft.Maui.Templates.net8
15-
dotnet pack Microsoft.Maui.sln
16-
dotnet new install artifacts\packages\Release\Shipping\Microsoft.Maui.Templates.*.nupkg
11+
The script:
12+
* Deletes the `.tempTemplateOutput` folder which is used for the temporary files used by this script
13+
* Builds the `src\Templates\src\Microsoft.Maui.Templates.csproj` project
14+
* Packs the `src\Templates\src\Microsoft.Maui.Templates.csproj` project into a .nupkg file and outputs it to the `.tempTemplateOutput` directory, this directly is excluded from git
15+
* Uninstalls any previous manual installations of .NET MAUI templates
16+
* Empties the `~\templateengine` folder
17+
* Finds and installs the resulting .nupkg artifact in the `.tempTemplateOutput` directory
18+
* Creates a new .NET MAUI project based on the latest changes in the template
19+
* Opens the new .NET MAUI project in Visual Studio (or on Mac in Visual Studio Code)
1720

18-
# then just in the maui folder, so you get a NuGet.config
19-
mkdir myproject
20-
cd myproject
21-
dotnet new maui
22-
```
21+
## Parameters
22+
23+
The script defines a coupe of parameters you can use. All have default values, so you only have to set them whenever you want to deviate from the default behavior.
24+
25+
The parameters are as follows:
26+
27+
* `projectType`: Specifies the type of .NET project to create (default is `maui`).
28+
* `templateVersion`: Specifies the version number to use for the template pack build (default is `13.3.7`, needs to be a valid major, minor, patch version number, for example 1.2.3).
29+
* `templatesProjectPath`: Specifies the path to the template project to build (default is `src\Microsoft.Maui.Templates.csproj`).
30+
* `startVsAfterBuild`: Specifies whether to start Visual Studio (Code) after creating the new project with the latest template changes (default is `true`).
31+
32+
### Example usage with parameters
33+
34+
Find sample usages of the different parameters below, of course these can be mixed and matched as needed.
35+
36+
* Instead of a default .NET MAUI app, use the Blazor Hybrid template: `.\build.ps1 -projectType maui-blazor`
37+
* Set a custom version number for the template: `.\build.ps1 -templateVersion 1.2.3`
38+
* Build another template project: `.\build.ps1 -templatesProjectPath src\Microsoft.Maui.Templates-new.csproj`
39+
* Don't start VS after creating the new project using the latest template changes: `.\build.ps1 -startVsAfterBuild $false`

src/Templates/build.ps1

+59-16
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,68 @@
1-
$PACKAGEVERSION = "6.0.101-preview.10.9991"
1+
param (
2+
[Parameter(Mandatory=$false, HelpMessage="Specify the type of .NET project to create (e.g., maui, maui-blazor, mauilib, etc.).")]
3+
[string]$projectType = "maui", # Default to maui if no project type is specified
4+
[Parameter(Mandatory=$false, HelpMessage="Specify the version number to use for the template pack build (should have x.y.z format)")]
5+
[string]$templateVersion = "13.3.7",
6+
[Parameter(Mandatory=$false, HelpMessage="Specify the path to the template project to build")]
7+
[string]$templatesProjectPath = "src\Microsoft.Maui.Templates.csproj",
8+
[Parameter(Mandatory=$false, HelpMessage="Specify whether to start Visual Studio (Code) after creating the new project with the latest template changes")]
9+
[bool]$startVsAfterBuild = $true
10+
)
211

3-
& dotnet new -u "Microsoft.Maui.Templates"
4-
# & dotnet new -u "../../artifacts/Microsoft.Maui.Templates.$PACKAGEVERSION.nupkg"
12+
# Source the utils script for some common functionalities
13+
. .\eng\utils.ps1
514

6-
if (Test-Path "../../artifacts/Microsoft.Maui.Templates.*.nupkg") {
7-
Remove-Item -Force "../../artifacts/Microsoft.Maui.Templates.*.nupkg"
8-
}
15+
# Clean up previous artifacts
16+
Remove-Item -Path .\.tempTemplateOutput -Recurse -Force -ErrorAction SilentlyContinue
17+
18+
# Build the Microsoft.Maui.Templates.csproj project
19+
dotnet build -t:Rebuild $templatesProjectPath -p:PackageVersion=$templateVersion
920

10-
& dotnet build -t:Rebuild src/Microsoft.Maui.Templates.csproj -p:PackageVersion="$PACKAGEVERSION"
11-
& dotnet pack src/Microsoft.Maui.Templates.csproj -p:PackageVersion="$PACKAGEVERSION"
12-
& dotnet new -u Microsoft.Maui.Templates
21+
# Pack the Microsoft.Maui.Templates.csproj project
22+
dotnet pack $templatesProjectPath -p:PackageVersion=$templateVersion -o .tempTemplateOutput
1323

14-
if (Test-Path ~/.templateengine/) {
15-
Remove-Item -Path ~/.templateengine/ -Recurse -Force
24+
# Find the resulting nupkg artifact
25+
$nupkgPath = Get-ChildItem -Path .tempTemplateOutput -Filter *.nupkg -Recurse | Select-Object -First 1
26+
27+
if ($nupkgPath -eq $null) {
28+
Write-Error "No templates nupkg file found. Ensure the build was successful."
29+
exit 1
1630
}
1731

18-
& dotnet new -i "../../artifacts/Microsoft.Maui.Templates.$PACKAGEVERSION.nupkg"
32+
# Uninstall previous (manual) install of .NET MAUI templates
33+
Uninstall-MauiTemplates
34+
35+
# Clean users templates folder
36+
Empty-UserHomeTemplateEngineFolder
1937

20-
if (Test-Path ./TestMaui/) {
21-
Remove-Item -Force -Recurse ./TestMaui/
38+
# Install the template pack
39+
dotnet new install $nupkgPath.FullName
40+
41+
# Create a new dotnet project using the specified project type
42+
dotnet new $projectType -o ./.tempTemplateOutput/NewProject --force
43+
44+
if ($startVsAfterBuild -eq $false) {
45+
exit 0
2246
}
2347

24-
& dotnet new maui -n TestMaui
25-
# & code ./TestMaui/
48+
# Start Visual Studio with the newly created project
49+
$projectFilePath = Resolve-Path "./.tempTemplateOutput/NewProject/NewProject.csproj"
50+
$projectFolderPath = Split-Path -Path $projectFilePath
51+
52+
if ([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)) {
53+
Start-Process "devenv.exe" -ArgumentList $projectFilePath
54+
} elseif ([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::OSX)) {
55+
# Check if VS Code Insiders is installed
56+
$vscodeInsidersPath = "/Applications/Visual Studio Code - Insiders.app"
57+
$vscodeStablePath = "/Applications/Visual Studio Code.app"
58+
59+
if (Test-Path $vscodeInsidersPath) {
60+
Start-Process "code-insiders" -ArgumentList $projectFolderPath
61+
} elseif (Test-Path $vscodeStablePath) {
62+
Start-Process "code" -ArgumentList $projectFolderPath
63+
} else {
64+
Write-Error "Neither Visual Studio Code Insiders nor Visual Studio Code stable is installed. Cannot open VS Code, however a new project is created at $projectFolderPath."
65+
}
66+
} else {
67+
Write-Error "Unsupported operating system."
68+
}

src/Templates/eng/utils.ps1

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
function Get-MauiDotNetVersion {
2+
param (
3+
[string]$propsFilePath = "..\..\Directory.Build.props"
4+
)
5+
6+
# Load the XML content
7+
[xml]$xmlContent = Get-Content -Path $propsFilePath
8+
9+
# Read the values of _MauiDotNetVersionMajor and _MauiDotNetVersionMinor nodes
10+
$versionMajor = $xmlContent.Project.PropertyGroup._MauiDotNetVersionMajor.InnerText
11+
# $versionMinor = $xmlContent.Project.PropertyGroup._MauiDotNetVersionMinor.InnerText
12+
13+
# Concatenate the values with "net" prefix
14+
$version = "net$versionMajor"
15+
16+
# Return the concatenated version
17+
return $version
18+
}
19+
20+
function Uninstall-MauiTemplates {
21+
$currentMauiVersion = Get-MauiDotNetVersion
22+
dotnet new uninstall Microsoft.Maui.Templates.$currentMauiVersion
23+
}
24+
25+
function Empty-UserHomeTemplateEngineFolder {
26+
if (Test-Path ~/.templateengine/) {
27+
Remove-Item -Path ~/.templateengine/ -Recurse -Force
28+
}
29+
}

0 commit comments

Comments
 (0)