Skip to content

Commit

Permalink
Add Powershell script to validate whether CSharp project correctly im…
Browse files Browse the repository at this point in the history
…port shared props, update pipeline to enforce such validation, and fixed all projects that didn't import this shared props correctly
  • Loading branch information
jamrobot committed Mar 4, 2025
1 parent cca3f44 commit 2878dc3
Showing 4 changed files with 71 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .pipelines/v2/templates/job-build-project.yml
Original file line number Diff line number Diff line change
@@ -314,6 +314,11 @@ jobs:
flattenFolders: True
OverWrite: True

# Check if all projects import common props.
- pwsh: |-
& '.pipelines/verifyCommonProps.ps1' -sourceDir '$(build.sourcesdirectory)'
displayName: Audit projects for shared common props
# Check if deps.json files don't reference different dll versions.
- pwsh: |-
& '.pipelines/verifyDepsJsonLibraryVersions.ps1' -targetDir '$(build.sourcesdirectory)\$(BuildPlatform)\$(BuildConfiguration)'
60 changes: 60 additions & 0 deletions .pipelines/verifyCommonProps.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True, Position = 1)]
[string]$sourceDir
)

# scan all csharp project in the source directory
function Get-CSharpProjects {
param (
[string]$path
)

# Get all .csproj files under the specified path
$csprojFiles = Get-ChildItem -Path $path -Recurse -Filter *.csproj

# Initialize an array to hold the file paths
$csprojArray = @()

foreach ($file in $csprojFiles) {
# Add the full path of each .csproj file to the array
$csprojArray += $file.FullName
}

# Return the array
return $csprojArray
}

# Check if the project file imports 'Common.Dotnet.CsWinRT.props'
function Test-ImportSharedCsWinRTProps {
param (
[string]$filePath
)

# Load the XML content of the .csproj file
[xml]$csprojContent = Get-Content -Path $filePath


# Check if the Import element with Project attribute containing 'Common.Dotnet.CsWinRT.props' exists
return $csprojContent.Project.Import | Where-Object { $null -ne $_.Project -and $_.Project.EndsWith('Common.Dotnet.CsWinRT.props') }
}

# Call the function with the provided source directory
$csprojFilesArray = Get-CSharpProjects -path $sourceDir

$hasInvalidCsProj = $false

# Enumerate the array of file paths and call Validate-ImportSharedCsWinRTProps for each file
foreach ($csprojFile in $csprojFilesArray) {
$importExists = Test-ImportSharedCsWinRTProps -filePath $csprojFile
if (!$importExists) {
Write-Output "$csprojFile need to import 'Common.Dotnet.CsWinRT.props'."
$hasInvalidCsProj = $true
}
}

if ($hasInvalidCsProj) {
exit 1
}

exit 0
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- Look at Directory.Build.props in root for common stuff as well -->
<Import Project="..\..\Common.Dotnet.CsWinRT.props" />

<PropertyGroup>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
4 changes: 3 additions & 1 deletion src/modules/Hosts/Hosts.FuzzTests/Hosts.FuzzTests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<!-- Look at Directory.Build.props in root for common stuff as well -->
<Import Project="..\..\Common.Dotnet.CsWinRT.props" />

<PropertyGroup>
<TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
<LangVersion>latest</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

0 comments on commit 2878dc3

Please sign in to comment.