|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +#requires -version 4 |
| 3 | + |
| 4 | +Set-StrictMode -Version 2 |
| 5 | +$ErrorActionPreference = 'Stop' |
| 6 | + |
| 7 | +function Test-Template { |
| 8 | + [CmdletBinding()] |
| 9 | + param ( |
| 10 | + [string] $TemplateName, |
| 11 | + [string[]] $TemplateArguments, |
| 12 | + [string] $TemplatePackagePath = "Microsoft.DotNet.Web.ProjectTemplates.*-dev.nupkg", |
| 13 | + [string] $PackagePattern = "(?<PackageId>([A-Za-z]+(\.[A-Za-z]+)*))\.(?<Version>\d+\.\d)\.(?<Suffix>.*)", |
| 14 | + [string] $MainProjectRelativePath = $null, |
| 15 | + [ValidateSet("Debug", "Release")] |
| 16 | + [string] $Configuration = "Release", |
| 17 | + [ValidatePattern("net\d+\.\d+")] |
| 18 | + [string] $TargetFramework = "net9.0" |
| 19 | + ) |
| 20 | + |
| 21 | + if(-not (Test-Path "$PSScriptRoot/.dotnet")){ |
| 22 | + $dotnetFolder = Get-Command dotnet | Select-Object -ExpandProperty Source | Split-Path -Parent; |
| 23 | + Write-Verbose "Copying dotnet folder from $dotnetFolder to $PSScriptRoot/.dotnet"; |
| 24 | + Copy-Item -Path $dotnetFolder -Destination "$PSScriptRoot/.dotnet" -Recurse; |
| 25 | + } |
| 26 | + |
| 27 | + Write-Verbose "Patching Microsoft.AspNetCore.App"; |
| 28 | + $builtRuntime = Resolve-Path "$PSScriptRoot/../../../artifacts/installers/$Configuration/aspnetcore-runtime-*-dev-win-x64.zip"; |
| 29 | + Write-Verbose "Patching Microsoft.AspNetCore.App from $builtRuntime"; |
| 30 | + Remove-Item "$PSScriptRoot/.runtime" -Recurse -ErrorAction Ignore; |
| 31 | + Expand-Archive -Path $builtRuntime -DestinationPath "$PSScriptRoot/.runtime" -Force; |
| 32 | + Remove-Item "$PSScriptRoot/.dotnet/shared/Microsoft.AspNetCore.App/*-dev" -Recurse -ErrorAction Ignore; |
| 33 | + Write-Verbose "Copying $PSScriptRoot/.runtime/shared/Microsoft.AspNetCore.App to $PSScriptRoot/.dotnet/shared"; |
| 34 | + Copy-Item -Path "$PSScriptRoot/.runtime/shared/Microsoft.AspNetCore.App" -Destination "$PSScriptRoot/.dotnet/shared" -Recurse -Force; |
| 35 | + |
| 36 | + $env:DOTNET_ROOT = "$PSScriptRoot/.dotnet"; |
| 37 | + $env:DOTNET_ROOT_X86 = "$PSScriptRoot/.dotnet"; |
| 38 | + $env:Path = "$PSScriptRoot/.dotnet;$env:Path"; |
| 39 | + $tmpDir = "$PSScriptRoot/$templateName"; |
| 40 | + Remove-Item -Path $tmpDir -Recurse -ErrorAction Ignore; |
| 41 | + Push-Location ..; |
| 42 | + try { |
| 43 | + dotnet pack |
| 44 | + } |
| 45 | + finally { |
| 46 | + Pop-Location; |
| 47 | + } |
| 48 | + |
| 49 | + $PackagePath = Resolve-Path "$PSScriptRoot/../../../artifacts/packages/$Configuration/Shipping/$TemplatePackagePath"; |
| 50 | + |
| 51 | + $PackageName = (Get-Item $PackagePath).Name; |
| 52 | + |
| 53 | + if (-not (Test-Path "$($env:USERPROFILE)/.templateengine/packages/$PackageName")) { |
| 54 | + Write-Verbose "Installing package from $PackagePath"; |
| 55 | + dotnet new install $PackagePath; |
| 56 | + } |
| 57 | + else { |
| 58 | + Write-Verbose "Uninstalling package from $PackagePath"; |
| 59 | + if (-not ($PackageName -match $PackagePattern)) { |
| 60 | + Write-Error "$PackageName did not match $PackagePattern"; |
| 61 | + } |
| 62 | + $PackageId = $Matches["PackageId"]; |
| 63 | + $PackageVersion = $Matches["Version"]; |
| 64 | + Write-Verbose "Uninstalling existing package $PackageId.$PackageVersion"; |
| 65 | + dotnet new uninstall "$PackageId.$PackageVersion"; |
| 66 | + |
| 67 | + Write-Verbose "Installing package from $PackagePath"; |
| 68 | + dotnet new install $PackagePath; |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + Write-Verbose "Creating directory $tmpDir" |
| 73 | + New-Item -ErrorAction Ignore -Path $tmpDir -ItemType Directory | Out-Null; |
| 74 | + Push-Location $tmpDir -StackName TemplateFolder; |
| 75 | + try { |
| 76 | + $TemplateArguments = , "new" + $TemplateArguments + , "--no-restore"; |
| 77 | + Write-Verbose "Running dotnet command with arguments: $TemplateArguments"; |
| 78 | + dotnet @TemplateArguments; |
| 79 | + |
| 80 | + $proj = Get-ChildItem $tmpDir -Recurse -File -Filter '*.csproj' -Depth 3; |
| 81 | + if ($proj.Length -eq 0) { |
| 82 | + $proj = Get-ChildItem $tmpDir -Recurse -File -Filter '*.fsproj' -Depth 3; |
| 83 | + } |
| 84 | + |
| 85 | + $importPath = "$PSScriptRoot/../test/Templates.Tests/bin/$Configuration/$TargetFramework/TestTemplates"; |
| 86 | + # Define the XML string literals |
| 87 | + [xml]$importPropsXml = "<Import Project='$importPath/Directory.Build.props' />"; |
| 88 | + [xml]$importTargetsXml = "<Import Project='$importPath/Directory.Build.targets' />"; |
| 89 | + [xml]$propertyGroupXml = @" |
| 90 | +<PropertyGroup> |
| 91 | + <DisablePackageReferenceRestrictions>true</DisablePackageReferenceRestrictions> |
| 92 | + <TreatWarningsAsErrors>False</TreatWarningsAsErrors> |
| 93 | + <TrimmerSingleWarn>false</TrimmerSingleWarn> |
| 94 | +</PropertyGroup> |
| 95 | +"@; |
| 96 | + |
| 97 | + foreach ($projPath in $proj) { |
| 98 | + Write-Verbose "Updating project file '$projPath'"; |
| 99 | + # Read the XML content from the file |
| 100 | + [xml]$xmlContent = Get-Content -Path $projPath; |
| 101 | + |
| 102 | + # Find the Project element and add the new elements |
| 103 | + $projectElement = $xmlContent.Project; |
| 104 | + $projectElement.PrependChild($xmlContent.ImportNode($propertyGroupXml.PropertyGroup, $true)) | Out-Null; |
| 105 | + $projectElement.PrependChild($xmlContent.ImportNode($importTargetsXml.Import, $true)) | Out-Null; |
| 106 | + $projectElement.PrependChild($xmlContent.ImportNode($importPropsXml.Import, $true)) | Out-Null; |
| 107 | + |
| 108 | + # Save the modified XML content back to the file |
| 109 | + $xmlContent.Save($projPath); |
| 110 | + } |
| 111 | + |
| 112 | + if ($null -ne $MainProjectRelativePath) { |
| 113 | + Push-Location $MainProjectRelativePath; |
| 114 | + } |
| 115 | + |
| 116 | + if ('--auth' -in $TemplateArguments -and 'Individual' -in $TemplateArguments) { |
| 117 | + Write-Verbose "Running dotnet ef migrations" |
| 118 | + dotnet.exe ef migrations add Initial; |
| 119 | + } |
| 120 | + |
| 121 | + $publishOutputDir = "./.publish"; |
| 122 | + Write-Verbose "Running dotnet publish --configuration $Configuration --output $publishOutputDir"; |
| 123 | + dotnet.exe publish --configuration $Configuration --output $publishOutputDir; |
| 124 | + } |
| 125 | + finally { |
| 126 | + Pop-Location -StackName TemplateFolder; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +Export-ModuleMember Test-Template; |
0 commit comments