/ .pipelines / verifyCommonProps.ps1
verifyCommonProps.ps1
1 [CmdletBinding()] 2 Param( 3 [Parameter(Mandatory = $True, Position = 1)] 4 [string]$sourceDir 5 ) 6 7 # scan all csharp project in the source directory 8 function Get-CSharpProjects { 9 param ( 10 [string]$path 11 ) 12 13 # Get all .csproj files under the specified path 14 return Get-ChildItem -Path $path -Recurse -Filter *.csproj | Select-Object -ExpandProperty FullName 15 } 16 17 # Check if the project file imports 'Common.Dotnet.CsWinRT.props' 18 function Test-ImportSharedCsWinRTProps { 19 param ( 20 [string]$filePath 21 ) 22 23 # Load the XML content of the .csproj file 24 [xml]$csprojContent = Get-Content -Path $filePath 25 26 27 # Check if the Import element with Project attribute containing 'Common.Dotnet.CsWinRT.props' exists 28 return $csprojContent.Project.Import | Where-Object { $null -ne $_.Project -and $_.Project.EndsWith('Common.Dotnet.CsWinRT.props') } 29 } 30 31 # Call the function with the provided source directory 32 $csprojFilesArray = Get-CSharpProjects -path $sourceDir 33 34 $hasInvalidCsProj = $false 35 36 # Enumerate the array of file paths and call Validate-ImportSharedCsWinRTProps for each file 37 foreach ($csprojFile in $csprojFilesArray) { 38 # Skip if the file ends with 'TemplateCmdPalExtension.csproj' 39 if ($csprojFile -like '*TemplateCmdPalExtension.csproj') { 40 continue 41 } 42 43 # The CmdPal.Core projects use a common shared props file, so skip them 44 if ($csprojFile -like '*Microsoft.CmdPal.Core.*.csproj') { 45 continue 46 } 47 if ($csprojFile -like '*Microsoft.CmdPal.Ext.Shell.csproj') { 48 continue 49 } 50 51 $importExists = Test-ImportSharedCsWinRTProps -filePath $csprojFile 52 if (!$importExists) { 53 Write-Output "$csprojFile need to import 'Common.Dotnet.CsWinRT.props'." 54 $hasInvalidCsProj = $true 55 } 56 } 57 58 if ($hasInvalidCsProj) { 59 exit 1 60 } 61 62 exit 0