Skip to content

Commit a315d25

Browse files
committed
Fix template scripts, make them more flexible and robust
1 parent f2ea31d commit a315d25

File tree

4 files changed

+308
-13
lines changed

4 files changed

+308
-13
lines changed

src/ProjectTemplates/scripts/Run-BlazorWeb-Locally.ps1

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,84 @@
22
#requires -version 4
33

44
# This script packages, installs and creates a template to help with rapid iteration in the templating area.
5-
[CmdletBinding(PositionalBinding = $false)]
6-
param()
5+
[CmdletBinding(PositionalBinding = $false)]
6+
param(
7+
[Parameter(Mandatory = $false, Position = 0)]
8+
[ValidateSet("net9.0", "net10.0")]
9+
[string] $Framework = "net9.0",
10+
[Parameter(Mandatory = $false)]
11+
[switch] $NoRestore,
12+
[Parameter(Mandatory = $false)]
13+
[switch] $ExcludeLaunchSettings,
14+
[Parameter(Mandatory = $false)]
15+
[ValidateSet("None", "Server", "WebAssembly", "Auto")]
16+
[string] $Interactivity = "Server",
17+
[Parameter(Mandatory = $false)]
18+
[switch] $Empty,
19+
[Parameter(Mandatory = $false)]
20+
[ValidateSet("None", "Individual")]
21+
[string] $Auth = "None",
22+
[Parameter(Mandatory = $false)]
23+
[switch] $UseLocalDb,
24+
[Parameter(Mandatory = $false)]
25+
[switch] $AllInteractive,
26+
[Parameter(Mandatory = $false)]
27+
[switch] $NoHttps,
28+
[Parameter(Mandatory = $false)]
29+
[switch] $UseProgramMain,
30+
[Parameter(ValueFromRemainingArguments = $true)]
31+
[string[]] $Args
32+
)
733

8-
Set-StrictMode -Version 2
9-
$ErrorActionPreference = 'Stop'
34+
Set-StrictMode -Version 2
35+
$ErrorActionPreference = 'Stop'
1036

11-
. $PSScriptRoot\Test-Template.ps1
37+
$templateArguments = @("blazor");
1238

13-
Test-Template "MyBlazorApp" "blazor" "Microsoft.DotNet.Web.ProjectTemplates.9.0.9.0.0-dev.nupkg" $true
39+
if ($ExcludeLaunchSettings) {
40+
$templateArguments += "--exclude-launch-settings"
41+
}
42+
43+
if ($Interactivity) {
44+
$templateArguments += "--interactivity"
45+
$templateArguments += $Interactivity;
46+
}
47+
48+
if ($Empty) {
49+
$templateArguments += "-e"
50+
}
51+
52+
if ($Auth) {
53+
$templateArguments += "--auth";
54+
$templateArguments += $Auth;
55+
}
56+
57+
$mainProjectRelativePath = $null;
58+
if($Interactivity -in @("Auto", "WebAssembly")){
59+
$mainProjectRelativePath = "MyBlazorApp";
60+
}
61+
62+
if ($UseLocalDb) {
63+
$templateArguments += "-uld"
64+
}
65+
66+
if ($AllInteractive) {
67+
$templateArguments += "-ai"
68+
}
69+
70+
if ($NoHttps) {
71+
$templateArguments += "--no-https"
72+
}
73+
74+
if ($UseProgramMain) {
75+
$templateArguments += "--use-program-main"
76+
}
77+
78+
Import-Module -Name .\Test-Template.psm1;
79+
80+
Test-Template `
81+
-TemplateName "MyBlazorApp" `
82+
-TemplateArguments $templateArguments `
83+
-MainProjectRelativePath $mainProjectRelativePath `
84+
-TargetFramework $Framework `
85+
-Verbose:$VerbosePreference;
Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,57 @@
1-
#!/usr/bin/env powershell
1+
#!/usr/bin/env pwsh
22
#requires -version 4
33

4+
# This script packages, installs and creates a template to help with rapid iteration in the templating area.
45
[CmdletBinding(PositionalBinding = $false)]
5-
param()
6+
param(
7+
[Parameter(Mandatory = $false, Position = 0)]
8+
[ValidateSet("net9.0", "net10.0")]
9+
[string] $Framework = "net9.0",
10+
[Parameter(Mandatory = $false)]
11+
[switch] $ExcludeLaunchSettings,
12+
[Parameter(Mandatory = $false)]
13+
[ValidateSet("None", "Individual")]
14+
[string] $Auth = "None",
15+
[Parameter(Mandatory = $false)]
16+
[switch] $UseLocalDb,
17+
[Parameter(Mandatory = $false)]
18+
[switch] $NoHttps,
19+
[Parameter(Mandatory = $false)]
20+
[switch] $UseProgramMain,
21+
[Parameter(ValueFromRemainingArguments = $true)]
22+
[string[]] $Args
23+
)
624

7-
. $PSScriptRoot\Test-Template.ps1
25+
Set-StrictMode -Version 2
26+
$ErrorActionPreference = 'Stop'
827

9-
Test-Template "webapp" "webapp -au Individual" "Microsoft.DotNet.Web.ProjectTemplates.9.0.9.0.0-dev.nupkg" $false
28+
$templateArguments = @("webapp");
29+
30+
if ($ExcludeLaunchSettings) {
31+
$templateArguments += "--exclude-launch-settings"
32+
}
33+
34+
if ($Auth) {
35+
$templateArguments += "--auth";
36+
$templateArguments += $Auth;
37+
}
38+
39+
if ($UseLocalDb) {
40+
$templateArguments += "-uld"
41+
}
42+
43+
if ($NoHttps) {
44+
$templateArguments += "--no-https"
45+
}
46+
47+
if ($UseProgramMain) {
48+
$templateArguments += "--use-program-main"
49+
}
50+
51+
Import-Module -Name .\Test-Template.psm1;
52+
53+
Test-Template `
54+
-TemplateName "MyWebApp" `
55+
-TemplateArguments $templateArguments `
56+
-TargetFramework $Framework `
57+
-Verbose:$VerbosePreference;
Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,57 @@
11
#!/usr/bin/env pwsh
22
#requires -version 4
33

4+
# This script packages, installs and creates a template to help with rapid iteration in the templating area.
45
[CmdletBinding(PositionalBinding = $false)]
5-
param()
6+
param(
7+
[Parameter(Mandatory = $false, Position = 0)]
8+
[ValidateSet("net9.0", "net10.0")]
9+
[string] $Framework = "net9.0",
10+
[Parameter(Mandatory = $false)]
11+
[switch] $ExcludeLaunchSettings,
12+
[Parameter(Mandatory = $false)]
13+
[ValidateSet("None", "Individual")]
14+
[string] $Auth = "None",
15+
[Parameter(Mandatory = $false)]
16+
[switch] $UseLocalDb,
17+
[Parameter(Mandatory = $false)]
18+
[switch] $NoHttps,
19+
[Parameter(Mandatory = $false)]
20+
[switch] $UseProgramMain,
21+
[Parameter(ValueFromRemainingArguments = $true)]
22+
[string[]] $Args
23+
)
624

725
Set-StrictMode -Version 2
826
$ErrorActionPreference = 'Stop'
927

10-
. $PSScriptRoot\Test-Template.ps1
28+
$templateArguments = @("mvc");
1129

12-
Test-Template "mvc" "mvc -au Individual" "Microsoft.DotNet.Web.ProjectTemplates.9.0.9.0.0-dev.nupkg" $false
30+
if ($ExcludeLaunchSettings) {
31+
$templateArguments += "--exclude-launch-settings"
32+
}
33+
34+
if ($Auth) {
35+
$templateArguments += "--auth";
36+
$templateArguments += $Auth;
37+
}
38+
39+
if ($UseLocalDb) {
40+
$templateArguments += "-uld"
41+
}
42+
43+
if ($NoHttps) {
44+
$templateArguments += "--no-https"
45+
}
46+
47+
if ($UseProgramMain) {
48+
$templateArguments += "--use-program-main"
49+
}
50+
51+
Import-Module -Name .\Test-Template.psm1;
52+
53+
Test-Template `
54+
-TemplateName "MyMvcApp" `
55+
-TemplateArguments $templateArguments `
56+
-TargetFramework $Framework `
57+
-Verbose:$VerbosePreference;
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)