Skip to content

Commit a34b971

Browse files
committed
Refactor project reference generation to include test projects.
- Introduces new MultiTarget-aware props template for shared project imports alongside the existing csproj template - Adjusts project reference generation scripts to include test projects when present - Refactors the file names of the generated project reference props to cleanly distinguish between samples, tests and source for easier wildcard importing without needing to know the project filename. - Updates the wildcard imports in our three "AllComponent" heads to only include generated imports for source and samples, rather than all files.
1 parent 1a7612c commit a34b971

9 files changed

+55
-20
lines changed

GenerateAllSolution.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ foreach ($componentName in $Components) {
148148

149149
[void]$projects.Add(".\components\$($componentPath.BaseName)\src\*.csproj")
150150
[void]$projects.Add(".\components\$($componentPath.BaseName)\samples\*.Samples.csproj")
151-
[void]$projects.Add(".\components\$($componentPath.BaseName)\tests\*.Tests\*.shproj")
151+
[void]$projects.Add(".\components\$($componentPath.BaseName)\tests\*.shproj")
152152
} else {
153153
Write-Warning "Component $($componentPath.BaseName) doesn't MultiTarget any of $MultiTargets and won't be added to the solution.";
154154
}

MultiTarget/EnabledMultiTargets.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
<!--Indicates which MultiTargets are enabled via UseTargetFrameworks script. -->
33
<!-- Do not commit changes made to default values by tooling. -->
44
<PropertyGroup>
5-
<EnabledMultiTargets>wasm;uwp;netstandard;</EnabledMultiTargets>
5+
<EnabledMultiTargets>wasm;wasdk;</EnabledMultiTargets>
66
</PropertyGroup>
77
</Project>

MultiTarget/GenerateAllProjectReferences.ps1

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,29 @@ foreach ($componentName in $Components) {
3535
continue;
3636
}
3737

38-
# Find all components source csproj (when wildcard), or find specific component csproj by name.
38+
# Find all components source/sample/test projects (when wildcard), or find specific component projects by component name.
3939
foreach ($componentPath in Get-Item "$PSScriptRoot/../../components/$componentName/") {
4040
$componentName = $componentPath.BaseName;
4141
Write-Output "Generating project references for component $componentName at $componentPath";
4242

43-
# Find source and sample csproj files
43+
# Find source, sample and test project files
4444
$componentSourceCsproj = Get-ChildItem $componentPath/src/*.csproj -ErrorAction SilentlyContinue;
4545
$componentSampleCsproj = Get-ChildItem $componentPath/samples/*.csproj -ErrorAction SilentlyContinue;
46+
$componentTestProj = Get-ChildItem $componentPath/tests/*.projitems -ErrorAction SilentlyContinue;
4647

4748
# Generate <ProjectReference>s for sample project
4849
# Use source project MultiTarget as first fallback.
4950
if ($null -ne $componentSampleCsproj -and (Test-Path $componentSampleCsproj)) {
50-
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $componentSampleCsproj -outputPath "$projectPropsOutputDir/$($componentSampleCsproj.BaseName).props" -MultiTargets $MultiTargets
51+
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $componentSampleCsproj -outputPath "$projectPropsOutputDir/$($componentName).Samples.props" -MultiTargets $MultiTargets
5152
}
5253

53-
# Generate <ProjectReference>s for src project
54-
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $componentSourceCsproj -outputPath "$projectPropsOutputDir/$($componentSourceCsproj.BaseName).props" -MultiTargets $MultiTargets
54+
if ($null -ne $componentTestProj -and (Test-Path $componentTestProj)) {
55+
# Generate references for test project
56+
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $componentTestProj -outputPath "$projectPropsOutputDir/$($componentName).Tests.props" -MultiTargets $MultiTargets
57+
}
58+
59+
# Generate references for src project
60+
& $PSScriptRoot\GenerateMultiTargetAwareProjectReferenceProps.ps1 -projectPath $componentSourceCsproj -outputPath "$projectPropsOutputDir/$($componentName).Source.props" -MultiTargets $MultiTargets
5561
}
5662
}
5763

MultiTarget/GenerateMultiTargetAwareProjectReferenceProps.ps1

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Param (
2-
[Parameter(HelpMessage = "The full path of the csproj to generated references to.", Mandatory = $true)]
2+
[Parameter(HelpMessage = "The full path of the csproj or projitems to generate a reference to.", Mandatory = $true)]
33
[string]$projectPath,
44

5-
[Parameter(HelpMessage = "A path to a .props file where generated content should be saved to.", Mandatory = $true)]
5+
[Parameter(HelpMessage = "A path to where the generated .props file containing the reference should be saved to.", Mandatory = $true)]
66
[string]$outputPath,
77

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

1111
[Parameter(HelpMessage = "The placeholder text to replace when inserting the project file name into the template.")]
1212
[string]$projectFileNamePlaceholder = "[ProjectFileName]",
@@ -20,6 +20,16 @@ Param (
2020
[string[]] $MultiTargets = @("uwp", "wasdk", "wpf", "wasm", "linuxgtk", "macos", "ios", "android", "netstandard")
2121
)
2222

23+
if ($projectPath.EndsWith(".projitems")) {
24+
$templatePath = "$PSScriptRoot/MultiTargetAwareSharedProjectImport.props.template"
25+
} elseif ($projectPath.EndsWith(".csproj")) {
26+
$templatePath = "$PSScriptRoot/MultiTargetAwareProjectReference.props.template";
27+
28+
} else {
29+
Write-Error "The specified project path is not a valid csproj or projitems file: $projectPath";
30+
exit(-1);
31+
}
32+
2333
$templateContents = Get-Content -Path $templatePath;
2434

2535
$preWorkingDir = $pwd;
@@ -29,9 +39,9 @@ $relativeProjectPath = Resolve-Path -Relative -Path $projectPath
2939

3040
Set-Location $preWorkingDir;
3141

32-
# Insert csproj file name.
33-
$csprojFileName = [System.IO.Path]::GetFileName($projectPath);
34-
$templateContents = $templateContents -replace [regex]::escape($projectFileNamePlaceholder), $csprojFileName;
42+
# Insert project file name.
43+
$projectFileName = [System.IO.Path]::GetFileName($projectPath);
44+
$templateContents = $templateContents -replace [regex]::escape($projectFileNamePlaceholder), $projectFileName;
3545

3646
# Insert component directory
3747
$componentDirectoryRelativeToRoot = [System.IO.Path]::GetDirectoryName($relativeProjectPath).TrimStart('.').TrimStart('\');
@@ -67,7 +77,7 @@ function ShouldMultiTargetMsBuildValue([string] $target) {
6777
$targeted = @("uwp", "wasdk", "wpf", "wasm", "linuxgtk", "macos", "ios", "android", "netstandard").Where({ ShouldMultiTarget $_ })
6878

6979
if ($targeted.Count -gt 0) {
70-
Write-Host "Generating project references for $([System.IO.Path]::GetFileNameWithoutExtension($csprojFileName)): $($targeted -Join ', ')"
80+
Write-Host "Generating project references for $([System.IO.Path]::GetFileNameWithoutExtension($projectFileName)): $($targeted -Join ', ')"
7181
}
7282

7383
$templateContents = $templateContents -replace [regex]::escape("[CanTargetWasm]"), "'$(ShouldMultiTargetMsBuildValue "wasm")'";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!--
2+
This file was generated by a tool.
3+
Adds a ProjectReference only if the referenced project indicates it can target the referencing project.
4+
-->
5+
<Project>
6+
<Import Condition="($(IsWasm) == 'true' AND 'false' == 'true') OR
7+
($(IsUwp) == 'true' AND 'false' == 'true') OR
8+
($(IsWinAppSdk) == 'true' AND 'true' == 'true') OR
9+
($(IsWpf) == 'true' AND 'false' == 'true') OR
10+
($(IsGtk) == 'true' AND 'false' == 'true') OR
11+
($(IsMacOS) == 'true' AND 'false' == 'true') OR
12+
($(IsiOS) == 'true' AND 'false' == 'true') OR
13+
($(IsDroid) == 'true' AND 'false' == 'true') OR
14+
($(IsNetstandard) == 'true' AND 'false' == 'true')"
15+
Project="$(RepositoryDirectory)[ProjectRoot]\[ProjectFileName]"
16+
Label="Shared" />
17+
</Project>
18+
19+

ProjectHeads/AllComponents/Tests.Head.AllComponents.props

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
<!-- Needed Unit Testing helper base classes -->
33
<Import Project="$(ToolingDirectory)\CommunityToolkit.Tests.Shared\CommunityToolkit.Tests.Shared.projitems" Label="Unit Testing Helpers" />
44

5-
<!-- Visual Studio likes to delete the following line - but it's needed to find the tests -->
6-
<Import Project="$(RepositoryDirectory)components\**\*.Tests.projitems" Label="Shared" />
7-
85
<!-- Include all base code to be tested -->
96
<Import Project="$(ToolingDirectory)\MultiTarget\Generated\*.props" />
107
</Project>

ProjectHeads/AllComponents/Uwp/CommunityToolkit.App.Uwp.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
<Import Project="$(ToolingDirectory)\MultiTarget\EnabledTargetFrameworks.props" />
1616
<Import Project="$(ToolingDirectory)\ProjectHeads\App.Head.Uwp.props" />
1717
<Import Project="$(ToolingDirectory)\MultiTarget\DefinedConstants.props" />
18-
<Import Project="$(ToolingDirectory)\MultiTarget\Generated\*.props" />
18+
<Import Project="$(ToolingDirectory)\MultiTarget\Generated\*.Samples.props" />
19+
<Import Project="$(ToolingDirectory)\MultiTarget\Generated\*.Source.props" />
1920

2021
<PropertyGroup>
2122
<RootNamespace>CommunityToolkit.App.Uwp</RootNamespace>

ProjectHeads/AllComponents/Wasdk/CommunityToolkit.App.Wasdk.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
<Import Project="$(ToolingDirectory)\MultiTarget\EnabledTargetFrameworks.props" />
1515

1616
<Import Project="$(ToolingDirectory)\MultiTarget\DefinedConstants.props" />
17-
<Import Project="$(ToolingDirectory)\MultiTarget\Generated\*.props" />
17+
<Import Project="$(ToolingDirectory)\MultiTarget\Generated\*.Samples.props" />
18+
<Import Project="$(ToolingDirectory)\MultiTarget\Generated\*.Source.props" />
1819
<Import Project="$(ToolingDirectory)\ProjectHeads\App.Head.WinAppSdk.props" />
1920

2021
<PropertyGroup>

ProjectHeads/AllComponents/Wasm/CommunityToolkit.App.Wasm.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717

1818
<Import Project="$(ToolingDirectory)\MultiTarget\WinUI.TargetVersion.props" />
1919
<Import Project="$(ToolingDirectory)\MultiTarget\DefinedConstants.props" />
20-
<Import Project="$(ToolingDirectory)\MultiTarget\Generated\*.props" />
20+
<Import Project="$(ToolingDirectory)\MultiTarget\Generated\*.Samples.props" />
21+
<Import Project="$(ToolingDirectory)\MultiTarget\Generated\*.Source.props" />
2122
<Import Project="$(ToolingDirectory)\ProjectHeads\App.Head.Wasm.props" />
2223

2324
<ItemGroup>

0 commit comments

Comments
 (0)