Skip to content

Commit 0b2b69c

Browse files
committed
Use MultiTarget values defined in Source project when not defined in Sample project
1 parent ee4c47f commit 0b2b69c

File tree

4 files changed

+61
-26
lines changed

4 files changed

+61
-26
lines changed
Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,46 @@
11
Param (
2-
[Parameter(HelpMessage = "The directory where props files for discovered projects should be saved.")]
3-
[string]$projectPropsOutputDir = "$PSScriptRoot/Generated",
4-
5-
[Parameter(HelpMessage = "Filters projects that have paths which match the provided string.")]
6-
[string]$Exclude = "*template*",
7-
8-
[Parameter(HelpMessage = "The directories to scan for projects. Supports wildcards.")]
9-
[string[]]$projectDirectories = @("$PSScriptRoot/../../*/*/samples/*.Samples/*.Samples.csproj", "$PSScriptRoot/../../*/*/src/*.csproj")
2+
[Parameter(HelpMessage = "The directory where props files for discovered projects should be saved.")]
3+
[string]$projectPropsOutputDir = "$PSScriptRoot/Generated"
104
)
115

12-
# Create output folder if not exists
6+
$preWorkingDir = $pwd;
7+
Set-Location $PSScriptRoot;
8+
9+
# Delete and recreate output folder.
10+
Remove-Item -Path $projectPropsOutputDir -Recurse -Force -ErrorAction SilentlyContinue | Out-Null;
1311
New-Item -ItemType Directory -Force -Path $projectPropsOutputDir -ErrorAction SilentlyContinue | Out-Null;
1412

1513
# Discover projects in provided paths
16-
foreach ($path in $projectDirectories) {
17-
foreach ($projectPath in Get-ChildItem -Recurse -Path $path -Exclude $Exclude) {
18-
$relativePath = Resolve-Path -Relative -Path $projectPath;
19-
$relativePath = $relativePath.TrimStart('.\');
20-
$projectName = [System.IO.Path]::GetFileNameWithoutExtension($relativePath);
21-
22-
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 $projectPath "$projectPropsOutputDir/$projectName.props";
14+
foreach ($projectPath in Get-ChildItem -Directory -Depth 0 -Path "$PSScriptRoot/../../labs/") {
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.
21+
$srcPath = Resolve-Path "$projectPath\src";
22+
$srcProjectPath = Get-ChildItem -File "$srcPath\*.csproj";
23+
24+
$samplePath = Resolve-Path "$projectPath\samples\$projectName.Samples";
25+
$sampleProjectPath = Get-ChildItem -File "$samplePath\*.csproj";
26+
27+
if ($srcProjectPath.Length -eq 0) {
28+
Write-Error "Could not locate source csproj for $projectName";
29+
exit(-1);
2330
}
31+
32+
if ($sampleProjectPath.Length -eq 0) {
33+
Write-Error "Could not locate sample csproj for $projectName";
34+
exit(-1);
35+
}
36+
37+
# Generate <ProjectReference>s for sample project
38+
# 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");
40+
41+
# Generate <ProjectReference>s for src project
42+
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $srcProjectPath -outputPath "$projectPropsOutputDir/$($srcProjectPath.BaseName).props" -multiTargetFallbackPropsPath @("$srcPath/MultiTarget.props", "$PSScriptRoot/Defaults.props");
2443
}
2544

45+
46+
Set-Location $preWorkingDir;

MultiTarget/GenerateMultiTargetAwareProjectReferenceProps.ps1

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Param (
99
[string]$templatePath = "$PSScriptRoot/MultiTargetAwareProjectReference.props.template",
1010

1111
[Parameter(HelpMessage = "The path to the props file that contains the default MultiTarget values.")]
12-
[string]$multiTargetDefaultPropsPath = "$PSScriptRoot/Defaults.props",
12+
[string[]]$multiTargetFallbackPropsPath = @("$PSScriptRoot/Defaults.props"),
1313

1414
[Parameter(HelpMessage = "The placeholder text to replace when inserting the project file name into the template.")]
1515
[string]$projectFileNamePlaceholder = "[ProjectFileName]",
@@ -39,8 +39,13 @@ function LoadMultiTargetsFrom([string] $path) {
3939

4040
# If file does not exist
4141
if ($false -eq (Test-Path -Path $path -PathType Leaf)) {
42-
# Load defaults
43-
$fileContents = Get-Content $multiTargetDefaultPropsPath -ErrorAction Stop;
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+
}
4449
}
4550
else {
4651
# Load requested file

MultiTarget/MultiTarget.props

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
<Project>
2-
<Import Project="$(MSBuildThisFileDirectory)\Defaults.props" Condition="'$(MultiTarget)' == ''"/>
3-
<Import Project="$(MSBuildProjectDirectory)\MultiTarget.props" Condition="Exists('$(MSBuildProjectDirectory)\MultiTarget.props') AND '$(MultiTarget)' == ''"/>
2+
<PropertyGroup>
3+
<MultiTargetIsSampleProject Condition="$(MSBuildProjectName.EndsWith('.Samples')) == 'true'">true</MultiTargetIsSampleProject>
4+
</PropertyGroup>
5+
6+
<Import Project="$(MSBuildThisFileDirectory)\Defaults.props" Condition="'$(MultiTarget)' == ''" />
7+
<Import Project="$(MSBuildProjectDirectory)\MultiTarget.props" Condition="Exists('$(MSBuildProjectDirectory)\MultiTarget.props') AND '$(MultiTarget)' == ''" />
8+
9+
<!-- If in a sample project, pull the MultiTarget settings from the source project. -->
10+
<!-- This behavior is also implemented in GeneratedAllProjectReferences.ps1 for <ProjectReference> generation. -->
11+
<Import Project="../../src/MultiTarget.props" Condition="$(MultiTargetIsSampleProject) == 'true' AND !Exists('$(MSBuildProjectDirectory)\MultiTarget.props') AND Exists('../../src/MultiTarget.props')" />
412

513
<PropertyGroup>
614
<!--
@@ -70,4 +78,4 @@
7078
<HintPath Condition="Exists('c:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\Extensions\Xamarin.VisualStudio')">c:\Program Files\Microsoft Visual Studio\2022\Preview\Common7\IDE\Extensions\Xamarin.VisualStudio\Xamarin.Mac.dll</HintPath>
7179
</Reference>
7280
</ItemGroup>
73-
</Project>
81+
</Project>

MultiTarget/ReadMe.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# MultiTarget
22

3-
`<MultiTarget>` is a custom property that indicates which target a project is designed to be built for / run on.
3+
`<MultiTarget>` is a custom property that indicates which target a component is designed to be built for / run on.
44

55
The supplied targets are used to create project references, generate solution files, enable/disable TargetFrameworks, and build nuget packages.
66

77
## Basic usage
88

9-
Create a `MultiTarget.props` file in the same folder as your `.csproj` to change its MultiTarget.
9+
Create a `MultiTarget.props` file in the root of your source project to change the platform targets for your component. This will be picked up automatically by your sample project, unless it has a `MultiTarget.props` of its own defined.
1010

1111
By default, all available targets are enabled:
1212
```xml
@@ -17,7 +17,7 @@ By default, all available targets are enabled:
1717
</Project>
1818
```
1919

20-
A project with this `MultiTarget.props` would only target UWP, WASM and Android:
20+
For example, to only target UWP, WASM and Android:
2121

2222
```xml
2323
<Project>
@@ -27,9 +27,10 @@ A project with this `MultiTarget.props` would only target UWP, WASM and Android:
2727
</Project>
2828
```
2929

30+
3031
## ProjectReference Generation
3132

32-
The script `GenerateAllProjectReferences.ps1` will recursively scan the provided folders for `.csproj` files and generate a `.props` file with a MultiTarget-aware `<ProjectReference>` for each one.
33+
The script `GenerateAllProjectReferences.ps1` will scan for experiments and generate `.props` files for each project.
3334

3435
## NuGet Packages
3536

0 commit comments

Comments
 (0)