Skip to content

Commit 4541db0

Browse files
authored
Fixed building in codespaces (#380)
* Fixed an issue where labs wouldn't build in codespaces * Fixed MultiTarget system in codespaces. Moved retrieval of multitarget and fallback logic to new file. * Fixed building locally * Added lost changes caused by git suppression * Removed vscode launch generation from sln gen * Removed unused $MultiTarget param
1 parent a5e22c6 commit 4541db0

6 files changed

+93
-69
lines changed

GenerateAllSolution.ps1

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Param (
3030

3131
# Generate required props for "All" solution.
3232
& ./common/MultiTarget/GenerateAllProjectReferences.ps1
33-
& ./common/GenerateVSCodeLaunchConfig.ps1
3433

3534
# Set up constant values
3635
$generatedSolutionFilePath = 'Toolkit.Labs.All.sln'

GenerateVSCodeLaunchConfig.ps1

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ function CreateVsCodeLaunchConfigJson {
2020
`"/p:UnoSourceGeneratorUseGenerationHost=true`",
2121
`"/p:UnoSourceGeneratorUseGenerationController=false`",
2222
`"/p:UnoRemoteControlPort=443`",
23-
`"--project=`$`{workspaceFolder`}/components/$projectName/samples/$projectName.Wasm/$projectName.Wasm.csproj`"
23+
`"--project=`$`{workspaceFolder`}/components/$projectName/heads/Wasm/$projectName.Wasm.csproj`"
2424
],
2525
`"presentation`": {
2626
`"group`": `"2`"
2727
},
28-
`"cwd`": `"`$`{workspaceFolder`}/components/$projectName/samples/$projectName.Wasm`"
28+
`"cwd`": `"`$`{workspaceFolder`}/components/$projectName/heads/Wasm`"
2929
}";
3030
}
3131

@@ -38,8 +38,10 @@ $launchConfig.configurations = @();
3838
$launchConfig.configurations += $originalConfigurations[0];
3939
$launchConfig.configurations += $originalConfigurations[1];
4040

41-
foreach ($wasmProjectPath in Get-ChildItem -Recurse -Path "$PSScriptRoot/../*/*/samples/*.Wasm/*.Wasm.csproj") {
42-
$projectName = [System.IO.Path]::GetFileNameWithoutExtension($wasmProjectPath) -Replace ".Wasm", "";
41+
& $PSScriptRoot/MultiTarget/GenerateAllProjectReferences.ps1 -MultiTarget 'wasm'
42+
43+
foreach ($projectPath in Get-ChildItem -Directory -Depth 0 -Path "$PSScriptRoot/../components/*") {
44+
$projectName = [System.IO.Path]::GetFileNameWithoutExtension($projectPath);
4345
Write-Host "Generating VSCode launch config for $projectName";
4446

4547
$configJson = CreateVsCodeLaunchConfigJson $projectName;
+6-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Param (
22
[Parameter(HelpMessage = "The directory where props files for discovered projects should be saved.")]
3-
[string]$projectPropsOutputDir = "$PSScriptRoot/Generated"
3+
[string]$projectPropsOutputDir = "$PSScriptRoot/Generated",
4+
5+
[Parameter(HelpMessage = "Only projects that support these targets will have references generated for use by deployable heads.")]
6+
[string[]] $MultiTarget = @("uwp", "wasdk", "wpf", "wasm", "linuxgtk", "macos", "ios", "android")
47
)
58

69
$preWorkingDir = $pwd;
@@ -12,35 +15,18 @@ New-Item -ItemType Directory -Force -Path $projectPropsOutputDir -ErrorAction Si
1215

1316
# Discover projects in provided paths
1417
foreach ($projectPath in Get-ChildItem -Directory -Depth 0 -Path "$PSScriptRoot/../../components/*") {
15-
# Normalize project path
16-
$projectName = $projectPath.Name;
17-
18-
# Folder layout is expected to match the Community Toolkit.
19-
# Uses the <MultiTarget> values from the source library project as the fallback for the sample project.
20-
# This behavior also implemented in MultiTarget.props for TargetFramework evaluation.
2118
$srcPath = Resolve-Path "$($projectPath.FullName)\src";
2219
$srcProjectPath = Get-ChildItem -File "$srcPath\*.csproj";
2320

2421
$samplePath = Resolve-Path "$($projectPath.FullName)\samples";
2522
$sampleProjectPath = Get-ChildItem -File "$samplePath\*.csproj";
2623

27-
if ($srcProjectPath.Length -eq 0) {
28-
Write-Error "Could not locate source csproj for $projectName";
29-
exit(-1);
30-
}
31-
32-
if ($sampleProjectPath.Length -eq 0) {
33-
Write-Error "Could not locate sample csproj for $projectName";
34-
exit(-1);
35-
}
36-
3724
# Generate <ProjectReference>s for sample project
3825
# Use source project MultiTarget as first fallback.
39-
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $sampleProjectPath -outputPath "$projectPropsOutputDir/$($sampleProjectPath.BaseName).props" -multiTargetFallbackPropsPath @("$srcPath/MultiTarget.props", "$samplePath/MultiTarget.props", "$PSScriptRoot/Defaults.props");
26+
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $sampleProjectPath -outputPath "$projectPropsOutputDir/$($sampleProjectPath.BaseName).props" -MultiTarget $MultiTarget
4027

4128
# Generate <ProjectReference>s for src project
42-
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $srcProjectPath -outputPath "$projectPropsOutputDir/$($srcProjectPath.BaseName).props" -multiTargetFallbackPropsPath @("$srcPath/MultiTarget.props", "$PSScriptRoot/Defaults.props");
29+
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $srcProjectPath -outputPath "$projectPropsOutputDir/$($srcProjectPath.BaseName).props" -MultiTarget $MultiTarget
4330
}
4431

45-
4632
Set-Location $preWorkingDir;

MultiTarget/GenerateMultiTargetAwareProjectReferenceProps.ps1

+22-43
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Param (
55
[Parameter(HelpMessage = "A path to a .props file where generated content should be saved to.", Mandatory = $true)]
66
[string]$outputPath,
77

8-
[Parameter(HelpMessage = "The path to the template used to generate the props file.")]
8+
[Parameter(HelpMessage = "The path to the template used to generate the props file.")]
99
[string]$templatePath = "$PSScriptRoot/MultiTargetAwareProjectReference.props.template",
1010

1111
[Parameter(HelpMessage = "The path to the props file that contains the default MultiTarget values.")]
@@ -15,7 +15,11 @@ Param (
1515
[string]$projectFileNamePlaceholder = "[ProjectFileName]",
1616

1717
[Parameter(HelpMessage = "The placeholder text to replace when inserting the project path into the template.")]
18-
[string]$projectRootPlaceholder = "[ProjectRoot]"
18+
[string]$projectRootPlaceholder = "[ProjectRoot]",
19+
20+
[Parameter(HelpMessage = "Only projects that support these targets will have references generated for use by deployable heads.")]
21+
[ValidateSet("uwp", "wasdk", "wpf", "wasm", "linuxgtk", "macos", "ios", "android")]
22+
[string[]] $MultiTarget = @("uwp", "wasdk", "wpf", "wasm", "linuxgtk", "macos", "ios", "android")
1923
)
2024

2125
$preWorkingDir = $pwd;
@@ -29,56 +33,31 @@ Set-Location $preWorkingDir;
2933
# Insert csproj file name.
3034
$csprojFileName = [System.IO.Path]::GetFileName($relativeProjectPath);
3135
$templateContents = $templateContents -replace [regex]::escape($projectFileNamePlaceholder), $csprojFileName;
36+
$projectName = (Get-Item (Split-Path -Parent $projectPath)).Name;
3237

3338
# Insert project directory
3439
$projectDirectoryRelativeToRoot = [System.IO.Path]::GetDirectoryName($relativeProjectPath).TrimStart('.').TrimStart('\');
3540
$templateContents = $templateContents -replace [regex]::escape($projectRootPlaceholder), "$projectDirectoryRelativeToRoot";
3641

37-
function LoadMultiTargetsFrom([string] $path) {
38-
$fileContents = "";
39-
40-
# If file does not exist
41-
if ($false -eq (Test-Path -Path $path -PathType Leaf)) {
42-
# Load first available default
43-
foreach ($fallbackPath in $multiTargetFallbackPropsPath) {
44-
if (Test-Path $fallbackPath) {
45-
$fileContents = Get-Content $fallbackPath -ErrorAction Stop;
46-
break;
47-
}
48-
}
49-
}
50-
else {
51-
# Load requested file
52-
$fileContents = Get-Content $path -ErrorAction Stop;
53-
}
54-
55-
# Parse file contents
56-
$regex = Select-String -Pattern '<MultiTarget>(.+?)<\/MultiTarget>' -InputObject $fileContents;
57-
58-
if ($null -eq $regex -or $null -eq $regex.Matches -or $null -eq $regex.Matches.Groups -or $regex.Matches.Groups.Length -lt 2) {
59-
Write-Error "Couldn't get MultiTarget property from $path";
60-
exit(-1);
61-
}
62-
63-
return $regex.Matches.Groups[1].Value;
64-
}
65-
6642
# Load multitarget preferences for project
67-
$multiTargets = LoadMultiTargetsFrom("$([System.IO.Path]::GetDirectoryName($projectPath))\MultiTarget.props");
68-
43+
$multiTargets = & $PSScriptRoot\GetMultiTargets.ps1 -ComponentName $projectName -ErrorAction Stop;
44+
6945
$templateContents = $templateContents -replace [regex]::escape("[IntendedTargets]"), $multiTargets;
70-
7146
$multiTargets = $multiTargets.Split(';');
72-
Write-Host "Generating project references for $([System.IO.Path]::GetFileNameWithoutExtension($csprojFileName)): $($multiTargets -Join ', ')"
7347

74-
$templateContents = $templateContents -replace [regex]::escape("[CanTargetWasm]"), "'$($multiTargets.Contains("wasm").ToString().ToLower())'";
75-
$templateContents = $templateContents -replace [regex]::escape("[CanTargetUwp]"), "'$($multiTargets.Contains("uwp").ToString().ToLower())'";
76-
$templateContents = $templateContents -replace [regex]::escape("[CanTargetWasdk]"), "'$($multiTargets.Contains("wasdk").ToString().ToLower())'";
77-
$templateContents = $templateContents -replace [regex]::escape("[CanTargetWpf]"), "'$($multiTargets.Contains("wpf").ToString().ToLower())'";
78-
$templateContents = $templateContents -replace [regex]::escape("[CanTargetLinuxGtk]"), "'$($multiTargets.Contains("linuxgtk").ToString().ToLower())'";
79-
$templateContents = $templateContents -replace [regex]::escape("[CanTargetMacOS]"), "'$($multiTargets.Contains("macos").ToString().ToLower())'";
80-
$templateContents = $templateContents -replace [regex]::escape("[CanTargetiOS]"), "'$($multiTargets.Contains("ios").ToString().ToLower())'";
81-
$templateContents = $templateContents -replace [regex]::escape("[CanTargetDroid]"), "'$($multiTargets.Contains("android").ToString().ToLower())'";
48+
function ShouldMultiTarget([string] $target) {
49+
return ($multiTargets.Contains($target) -and $MultiTarget.Contains($target)).ToString().ToLower()
50+
}
51+
52+
Write-Host "Generating project references for $([System.IO.Path]::GetFileNameWithoutExtension($csprojFileName)): $($multiTargets -Join ', ')"
53+
$templateContents = $templateContents -replace [regex]::escape("[CanTargetWasm]"), "'$(ShouldMultiTarget "wasm")'";
54+
$templateContents = $templateContents -replace [regex]::escape("[CanTargetUwp]"), "'$(ShouldMultiTarget "uwp")'";
55+
$templateContents = $templateContents -replace [regex]::escape("[CanTargetWasdk]"), "'$(ShouldMultiTarget "wasdk")'";
56+
$templateContents = $templateContents -replace [regex]::escape("[CanTargetWpf]"), "'$(ShouldMultiTarget "wpf")'";
57+
$templateContents = $templateContents -replace [regex]::escape("[CanTargetLinuxGtk]"), "'$(ShouldMultiTarget "linuxgtk")'";
58+
$templateContents = $templateContents -replace [regex]::escape("[CanTargetMacOS]"), "'$(ShouldMultiTarget "macos")'";
59+
$templateContents = $templateContents -replace [regex]::escape("[CanTargetiOS]"), "'$(ShouldMultiTarget "ios")'";
60+
$templateContents = $templateContents -replace [regex]::escape("[CanTargetDroid]"), "'$(ShouldMultiTarget "droid")'";
8261

8362
# Save to disk
8463
Set-Content -Path $outputPath -Value $templateContents;

MultiTarget/GetMultiTargets.ps1

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<#
2+
.SYNOPSIS
3+
Returns the defined MultiTarget value for the provided project name.
4+
.DESCRIPTION
5+
Locates a component by the provided name from the root ./components folder, and pulls the <MultiTarget> value that should be used.
6+
The MultiTarget value can be defined by a component in multiple places, but in a MultiTarget.props next to a *.csproj.
7+
8+
The load order is as follows:
9+
- ./components/SomeComponent/src/MultiTarget.props
10+
- ./components/SomeComponent/samples/MultiTarget.props
11+
- $PSScriptRoot/Defaults.props
12+
.PARAMETER ComponentName
13+
The name of the component.
14+
.EXAMPLE
15+
C:\PS> .\GetMultiTargets "CanvasView"
16+
Return the multitargets for the component named "CanvasView".
17+
.NOTES
18+
Author: Windows Community Toolkit Labs Team
19+
Date: February 27, 2023
20+
#>
21+
Param (
22+
[Parameter(HelpMessage = "The name of the component.", Mandatory = $true)]
23+
[string]$ComponentName
24+
)
25+
26+
$componentPath = "$PSScriptRoot/../../components/$ComponentName";
27+
28+
Test-Path $componentPath -ErrorAction Stop | Out-Null
29+
30+
# Folder layout is expected to match the Community Toolkit.
31+
# Uses the <MultiTarget> values from the source library project as the fallback for the sample project.
32+
# This behavior also implemented in MultiTarget.props for TargetFramework evaluation.
33+
$parent = Split-Path -Parent $projectPath
34+
$srcPath = Resolve-Path "$parent\..\src";
35+
$samplePath = Resolve-Path "$parent\..\samples";
36+
37+
$multiTargetFallbackPropsPaths = @("$srcPath/MultiTarget.props", "$samplePath/MultiTarget.props", "$PSScriptRoot/Defaults.props")
38+
39+
$fileContents = "";
40+
# Load first available default
41+
foreach ($fallbackPath in $multiTargetFallbackPropsPaths) {
42+
if (Test-Path $fallbackPath) {
43+
$fileContents = Get-Content $fallbackPath -ErrorAction Stop;
44+
break;
45+
}
46+
}
47+
48+
# Parse file contents
49+
$regex = Select-String -Pattern '<MultiTarget>(.+?)<\/MultiTarget>' -InputObject $fileContents;
50+
51+
if ($null -eq $regex -or $null -eq $regex.Matches -or $null -eq $regex.Matches.Groups -or $regex.Matches.Groups.Length -lt 2) {
52+
Write-Error "Couldn't get MultiTarget property from $path";
53+
exit(-1);
54+
}
55+
56+
$value = $regex.Matches.Groups[1].Value;
57+
58+
return $value;

ProjectHeads/GenerateSingleSampleHeads.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Param (
2929
[switch]$UseDiagnostics = $false
3030
)
3131

32-
if ($Env:Path.ToLower().Contains("msbuild") -eq $false) {
32+
if ($null -ne $Env:Path -and $Env:Path.ToLower().Contains("msbuild") -eq $false) {
3333
Write-Host
3434
Write-Host -ForegroundColor Red "Please run from a command window that has MSBuild.exe on the PATH"
3535
Write-Host

0 commit comments

Comments
 (0)