Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revamp new project template build/test script #25744

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,4 @@ snapshots-diff/
.dotnet
temp
.packages
/src/Templates/.tempTemplateOutput
47 changes: 32 additions & 15 deletions src/Templates/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@

# MAUI Templates

## Building / Testing
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.

Add the local artifacts to the NuGet.config:
> [!NOTE]
> 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.

```xml
<add key="LocalMauiTemplates" value="./artifacts" />
```
## Functionality

```dotnetcli
# uninstall, build, and install the templates
dotnet new uninstall Microsoft.Maui.Templates.net8
dotnet pack Microsoft.Maui.sln
dotnet new install artifacts\packages\Release\Shipping\Microsoft.Maui.Templates.*.nupkg
The script:
* Deletes the `.tempTemplateOutput` folder which is used for the temporary files used by this script
* Builds the `src\Templates\src\Microsoft.Maui.Templates.csproj` project
* 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
* Uninstalls any previous manual installations of .NET MAUI templates
* Empties the `~\templateengine` folder
* Finds and installs the resulting .nupkg artifact in the `.tempTemplateOutput` directory
* Creates a new .NET MAUI project based on the latest changes in the template
* Opens the new .NET MAUI project in Visual Studio (or on Mac in Visual Studio Code)

# then just in the maui folder, so you get a NuGet.config
mkdir myproject
cd myproject
dotnet new maui
```
## Parameters

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.

The parameters are as follows:

* `projectType`: Specifies the type of .NET project to create (default is `maui`).
* `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).
* `templatesProjectPath`: Specifies the path to the template project to build (default is `src\Microsoft.Maui.Templates.csproj`).
* `startVsAfterBuild`: Specifies whether to start Visual Studio (Code) after creating the new project with the latest template changes (default is `true`).

### Example usage with parameters

Find sample usages of the different parameters below, of course these can be mixed and matched as needed.

* Instead of a default .NET MAUI app, use the Blazor Hybrid template: `.\build.ps1 -projectType maui-blazor`
* Set a custom version number for the template: `.\build.ps1 -templateVersion 1.2.3`
* Build another template project: `.\build.ps1 -templatesProjectPath src\Microsoft.Maui.Templates-new.csproj`
* Don't start VS after creating the new project using the latest template changes: `.\build.ps1 -startVsAfterBuild $false`
75 changes: 59 additions & 16 deletions src/Templates/build.ps1
Original file line number Diff line number Diff line change
@@ -1,25 +1,68 @@
$PACKAGEVERSION = "6.0.101-preview.10.9991"
param (
[Parameter(Mandatory=$false, HelpMessage="Specify the type of .NET project to create (e.g., maui, maui-blazor, mauilib, etc.).")]
[string]$projectType = "maui", # Default to maui if no project type is specified
[Parameter(Mandatory=$false, HelpMessage="Specify the version number to use for the template pack build (should have x.y.z format)")]
[string]$templateVersion = "13.3.7",
[Parameter(Mandatory=$false, HelpMessage="Specify the path to the template project to build")]
[string]$templatesProjectPath = "src\Microsoft.Maui.Templates.csproj",
[Parameter(Mandatory=$false, HelpMessage="Specify whether to start Visual Studio (Code) after creating the new project with the latest template changes")]
[bool]$startVsAfterBuild = $true
)

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

if (Test-Path "../../artifacts/Microsoft.Maui.Templates.*.nupkg") {
Remove-Item -Force "../../artifacts/Microsoft.Maui.Templates.*.nupkg"
}
# Clean up previous artifacts
Remove-Item -Path .\.tempTemplateOutput -Recurse -Force -ErrorAction SilentlyContinue

# Build the Microsoft.Maui.Templates.csproj project
dotnet build -t:Rebuild $templatesProjectPath -p:PackageVersion=$templateVersion

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

if (Test-Path ~/.templateengine/) {
Remove-Item -Path ~/.templateengine/ -Recurse -Force
# Find the resulting nupkg artifact
$nupkgPath = Get-ChildItem -Path .tempTemplateOutput -Filter *.nupkg -Recurse | Select-Object -First 1

if ($nupkgPath -eq $null) {
Write-Error "No templates nupkg file found. Ensure the build was successful."
exit 1
}

& dotnet new -i "../../artifacts/Microsoft.Maui.Templates.$PACKAGEVERSION.nupkg"
# Uninstall previous (manual) install of .NET MAUI templates
Uninstall-MauiTemplates

# Clean users templates folder
Empty-UserHomeTemplateEngineFolder

if (Test-Path ./TestMaui/) {
Remove-Item -Force -Recurse ./TestMaui/
# Install the template pack
dotnet new install $nupkgPath.FullName

# Create a new dotnet project using the specified project type
dotnet new $projectType -o ./.tempTemplateOutput/NewProject --force

if ($startVsAfterBuild -eq $false) {
exit 0
}

& dotnet new maui -n TestMaui
# & code ./TestMaui/
# Start Visual Studio with the newly created project
$projectFilePath = Resolve-Path "./.tempTemplateOutput/NewProject/NewProject.csproj"
$projectFolderPath = Split-Path -Path $projectFilePath

if ([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::Windows)) {
Start-Process "devenv.exe" -ArgumentList $projectFilePath
} elseif ([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform([System.Runtime.InteropServices.OSPlatform]::OSX)) {
# Check if VS Code Insiders is installed
$vscodeInsidersPath = "/Applications/Visual Studio Code - Insiders.app"
$vscodeStablePath = "/Applications/Visual Studio Code.app"

if (Test-Path $vscodeInsidersPath) {
Start-Process "code-insiders" -ArgumentList $projectFolderPath
} elseif (Test-Path $vscodeStablePath) {
Start-Process "code" -ArgumentList $projectFolderPath
} else {
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."
}
} else {
Write-Error "Unsupported operating system."
}
29 changes: 29 additions & 0 deletions src/Templates/eng/utils.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function Get-MauiDotNetVersion {
param (
[string]$propsFilePath = "..\..\Directory.Build.props"
)

# Load the XML content
[xml]$xmlContent = Get-Content -Path $propsFilePath

# Read the values of _MauiDotNetVersionMajor and _MauiDotNetVersionMinor nodes
$versionMajor = $xmlContent.Project.PropertyGroup._MauiDotNetVersionMajor.InnerText
# $versionMinor = $xmlContent.Project.PropertyGroup._MauiDotNetVersionMinor.InnerText

# Concatenate the values with "net" prefix
$version = "net$versionMajor"

# Return the concatenated version
return $version
}

function Uninstall-MauiTemplates {
$currentMauiVersion = Get-MauiDotNetVersion
dotnet new uninstall Microsoft.Maui.Templates.$currentMauiVersion
}

function Empty-UserHomeTemplateEngineFolder {
if (Test-Path ~/.templateengine/) {
Remove-Item -Path ~/.templateengine/ -Recurse -Force
}
}
Loading