diff --git a/.gitignore b/.gitignore index bd1b26cc4f05..d82850c71066 100644 --- a/.gitignore +++ b/.gitignore @@ -382,3 +382,4 @@ snapshots-diff/ .dotnet temp .packages +/src/Templates/.tempTemplateOutput diff --git a/src/Templates/README.md b/src/Templates/README.md index 1685972e7261..c2b72b94a49e 100644 --- a/src/Templates/README.md +++ b/src/Templates/README.md @@ -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 - -``` +## 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` \ No newline at end of file diff --git a/src/Templates/build.ps1 b/src/Templates/build.ps1 index bffd564cf5d3..e56ef9186fd3 100644 --- a/src/Templates/build.ps1 +++ b/src/Templates/build.ps1 @@ -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/ \ No newline at end of file +# 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." +} \ No newline at end of file diff --git a/src/Templates/eng/utils.ps1 b/src/Templates/eng/utils.ps1 new file mode 100644 index 000000000000..40b65f42c38a --- /dev/null +++ b/src/Templates/eng/utils.ps1 @@ -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 + } +} \ No newline at end of file