Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
jfversluis committed Nov 8, 2024
1 parent 7f5baac commit 74aad31
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 96 deletions.
44 changes: 29 additions & 15 deletions src/Templates/README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@

# 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:
## Functionality

```xml
<add key="LocalMauiTemplates" value="./artifacts" />
```
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)

```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
## Parameters

# then just in the maui folder, so you get a NuGet.config
mkdir myproject
cd myproject
dotnet new maui
```
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`
65 changes: 49 additions & 16 deletions src/Templates/build.ps1
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
$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)) {
Start-Process "code" -ArgumentList $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
}
}
65 changes: 0 additions & 65 deletions src/Templates/test.ps1

This file was deleted.

0 comments on commit 74aad31

Please sign in to comment.