Skip to content

Commit 0ee678e

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

File tree

3 files changed

+241
-10
lines changed

3 files changed

+241
-10
lines changed

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

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,83 @@
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+
if($Interactivity -in @("Auto", "WebAssembly")){
58+
$mainProjectRelativePath = "MyBlazorApp";
59+
}
60+
61+
if ($UseLocalDb) {
62+
$templateArguments += "-uld"
63+
}
64+
65+
if ($AllInteractive) {
66+
$templateArguments += "-ai"
67+
}
68+
69+
if ($NoHttps) {
70+
$templateArguments += "--no-https"
71+
}
72+
73+
if ($UseProgramMain) {
74+
$templateArguments += "--use-program-main"
75+
}
76+
77+
Import-Module -Name .\Test-Template.psm1;
78+
79+
Test-Template `
80+
-TemplateName "MyBlazorApp" `
81+
-TemplateArguments $templateArguments `
82+
-MainProjectRelativePath $mainProjectRelativePath `
83+
-TargetFramework $Framework `
84+
-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: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
$tmpDir = "$PSScriptRoot/$templateName";
22+
Remove-Item -Path $tmpDir -Recurse -ErrorAction Ignore;
23+
Push-Location ..;
24+
try {
25+
dotnet pack
26+
}
27+
finally {
28+
Pop-Location;
29+
}
30+
31+
$PackagePath = Resolve-Path "$PSScriptRoot/../../../artifacts/packages/$Configuration/Shipping/$TemplatePackagePath";
32+
33+
$PackageName = (Get-Item $PackagePath).Name;
34+
35+
if (-not (Test-Path "$($env:USERPROFILE)/.templateengine/packages/$PackageName")) {
36+
Write-Verbose "Installing package from $PackagePath";
37+
dotnet new install $PackagePath;
38+
}
39+
else {
40+
Write-Verbose "Uninstalling package from $PackagePath";
41+
if (-not ($PackageName -match $PackagePattern)) {
42+
Write-Error "$PackageName did not match $PackagePattern";
43+
}
44+
$PackageId = $Matches["PackageId"];
45+
$PackageVersion = $Matches["Version"];
46+
Write-Verbose "Uninstalling existing package $PackageId.$PackageVersion";
47+
dotnet new uninstall "$PackageId.$PackageVersion";
48+
49+
Write-Verbose "Installing package from $PackagePath";
50+
dotnet new install $PackagePath;
51+
}
52+
53+
54+
Write-Verbose "Creating directory $tmpDir"
55+
New-Item -ErrorAction Ignore -Path $tmpDir -ItemType Directory | Out-Null;
56+
Push-Location $tmpDir -StackName TemplateFolder;
57+
try {
58+
$TemplateArguments = , "new" + $TemplateArguments + , "--no-restore";
59+
Write-Verbose "Running dotnet command with arguments: $TemplateArguments";
60+
dotnet @TemplateArguments;
61+
62+
$proj = Get-ChildItem $tmpDir -Recurse -File -Filter '*.csproj' -Depth 3;
63+
if ($proj.Length -eq 0) {
64+
$proj = Get-ChildItem $tmpDir -Recurse -File -Filter '*.fsproj' -Depth 3;
65+
}
66+
67+
$importPath = "$PSScriptRoot/../test/Templates.Tests/bin/$Configuration/$TargetFramework/TestTemplates";
68+
# Define the XML string literals
69+
[xml]$importPropsXml = "<Import Project='$importPath/Directory.Build.props' />";
70+
[xml]$importTargetsXml = "<Import Project='$importPath/Directory.Build.targets' />";
71+
[xml]$propertyGroupXml = @"
72+
<PropertyGroup>
73+
<DisablePackageReferenceRestrictions>true</DisablePackageReferenceRestrictions>
74+
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
75+
<TrimmerSingleWarn>false</TrimmerSingleWarn>
76+
</PropertyGroup>
77+
"@;
78+
79+
foreach ($projPath in $proj) {
80+
Write-Verbose "Updating project file '$projPath'";
81+
# Read the XML content from the file
82+
[xml]$xmlContent = Get-Content -Path $projPath;
83+
84+
# Find the Project element and add the new elements
85+
$projectElement = $xmlContent.Project;
86+
$projectElement.PrependChild($xmlContent.ImportNode($propertyGroupXml.PropertyGroup, $true)) | Out-Null;
87+
$projectElement.PrependChild($xmlContent.ImportNode($importTargetsXml.Import, $true)) | Out-Null;
88+
$projectElement.PrependChild($xmlContent.ImportNode($importPropsXml.Import, $true)) | Out-Null;
89+
90+
# Save the modified XML content back to the file
91+
$xmlContent.Save($projPath);
92+
}
93+
94+
if ($null -ne $MainProjectRelativePath) {
95+
Push-Location $MainProjectRelativePath;
96+
}
97+
98+
if ($TemplateArguments -match '-au') {
99+
Write-Verbose "Running dotnet ef migrations"
100+
dotnet.exe ef migrations add Initial;
101+
}
102+
103+
$publishOutputDir = ".\.publish";
104+
Write-Verbose "Running dotnet publish --configuration $Configuration --output $publishOutputDir";
105+
dotnet.exe publish --configuration $Configuration --output $publishOutputDir;
106+
}
107+
finally {
108+
Pop-Location -StackName TemplateFolder;
109+
}
110+
}
111+
112+
Export-ModuleMember Test-Template;

0 commit comments

Comments
 (0)