Skip to content

Commit 57bce71

Browse files
authored
Merge pull request #97 from CommunityToolkit/fix/multitarget/sample-always-inheriting-source-targets
Fixed sample projects always inheriting source project MultiTarget.props
2 parents 5490aac + 7ba6aba commit 57bce71

File tree

3 files changed

+43
-69
lines changed

3 files changed

+43
-69
lines changed

MultiTarget/GenerateAllProjectReferences.ps1

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Param (
44

55
[Parameter(HelpMessage = "Only projects that support these targets will have references generated for use by deployable heads.")]
66
[string[]] $MultiTarget = @("uwp", "wasdk", "wpf", "wasm", "linuxgtk", "macos", "ios", "android", "netstandard")
7-
)
7+
)
88

99
$preWorkingDir = $pwd;
1010
Set-Location $PSScriptRoot;
@@ -17,13 +17,13 @@ New-Item -ItemType Directory -Force -Path $projectPropsOutputDir -ErrorAction Si
1717
foreach ($projectPath in Get-ChildItem -Directory -Path "$PSScriptRoot/../../components/*") {
1818
$srcPath = Resolve-Path "$($projectPath.FullName)\src";
1919
$srcProjectPath = Get-ChildItem -File "$srcPath\*.csproj";
20-
21-
$samplePath = Resolve-Path "$($projectPath.FullName)\samples";
22-
$sampleProjectPath = Get-ChildItem -File "$samplePath\*.csproj";
20+
$sampleProjectPath = Get-ChildItem -File "$($projectPath.FullName)\samples\*.csproj" -ErrorAction SilentlyContinue;
2321

2422
# Generate <ProjectReference>s for sample project
2523
# Use source project MultiTarget as first fallback.
26-
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $sampleProjectPath -outputPath "$projectPropsOutputDir/$($sampleProjectPath.BaseName).props" -MultiTarget $MultiTarget
24+
if ($null -ne $sampleProjectPath -and (Test-Path $sampleProjectPath)) {
25+
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $sampleProjectPath -outputPath "$projectPropsOutputDir/$($sampleProjectPath.BaseName).props" -MultiTarget $MultiTarget
26+
}
2727

2828
# Generate <ProjectReference>s for src project
2929
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $srcProjectPath -outputPath "$projectPropsOutputDir/$($srcProjectPath.BaseName).props" -MultiTarget $MultiTarget

MultiTarget/GenerateMultiTargetAwareProjectReferenceProps.ps1

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ Param (
88
[Parameter(HelpMessage = "The path to the template used to generate the props file.")]
99
[string]$templatePath = "$PSScriptRoot/MultiTargetAwareProjectReference.props.template",
1010

11-
[Parameter(HelpMessage = "The path to the props file that contains the default MultiTarget values.")]
12-
[string[]]$multiTargetFallbackPropsPath = @("$PSScriptRoot/Defaults.props"),
13-
1411
[Parameter(HelpMessage = "The placeholder text to replace when inserting the project file name into the template.")]
1512
[string]$projectFileNamePlaceholder = "[ProjectFileName]",
1613

@@ -33,15 +30,50 @@ Set-Location $preWorkingDir;
3330
# Insert csproj file name.
3431
$csprojFileName = [System.IO.Path]::GetFileName($relativeProjectPath);
3532
$templateContents = $templateContents -replace [regex]::escape($projectFileNamePlaceholder), $csprojFileName;
36-
$projectName = (Get-Item (Split-Path -Parent $projectPath)).Name;
3733

3834
# Insert project directory
3935
$projectDirectoryRelativeToRoot = [System.IO.Path]::GetDirectoryName($relativeProjectPath).TrimStart('.').TrimStart('\');
4036
$templateContents = $templateContents -replace [regex]::escape($projectRootPlaceholder), "$projectDirectoryRelativeToRoot";
4137

4238
# Load multitarget preferences for project
43-
$multiTargets = & $PSScriptRoot\GetMultiTargets.ps1 -ComponentName $projectName -ErrorAction Stop;
44-
39+
# Folder layout is expected to match the Community Toolkit.
40+
$projectName = (Get-Item (Split-Path -Parent (Split-Path -Parent $projectPath))).Name;
41+
$componentPath = "$PSScriptRoot/../../components/$projectName";
42+
43+
$srcPath = Resolve-Path "$componentPath\src";
44+
$samplePath = "$componentPath\samples";
45+
46+
# Uses the <MultiTarget> values from the source library project as the fallback for the sample project.
47+
# This behavior also implemented in TargetFramework evaluation.
48+
$multiTargetFallbackPropsPaths = @()
49+
50+
if($projectPath.ToLower().Contains('sample')) {
51+
$multiTargetFallbackPropsPaths += @("$samplePath/MultiTarget.props", "$srcPath/MultiTarget.props")
52+
} else {
53+
$multiTargetFallbackPropsPaths += @("$srcPath/MultiTarget.props")
54+
}
55+
56+
$multiTargetFallbackPropsPaths += @("$PSScriptRoot/Defaults.props")
57+
58+
# Load first available default
59+
$fileContents = "";
60+
foreach ($fallbackPath in $multiTargetFallbackPropsPaths) {
61+
if (Test-Path $fallbackPath) {
62+
$fileContents = Get-Content $fallbackPath -ErrorAction Stop;
63+
break;
64+
}
65+
}
66+
67+
# Parse file contents
68+
$regex = Select-String -Pattern '<MultiTarget>(.+?)<\/MultiTarget>' -InputObject $fileContents;
69+
70+
if ($null -eq $regex -or $null -eq $regex.Matches -or $null -eq $regex.Matches.Groups -or $regex.Matches.Groups.Length -lt 2) {
71+
Write-Error "Couldn't get MultiTarget property from $path";
72+
exit(-1);
73+
}
74+
75+
$multiTargets = $regex.Matches.Groups[1].Value;
76+
4577
$templateContents = $templateContents -replace [regex]::escape("[IntendedTargets]"), $multiTargets;
4678
$multiTargets = $multiTargets.Split(';');
4779

MultiTarget/GetMultiTargets.ps1

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)