|
| 1 | +Param ( |
| 2 | + [Parameter(HelpMessage = "The full path of the csproj to generated references to.", Mandatory = $true)] |
| 3 | + [string]$projectPath, |
| 4 | + |
| 5 | + [Parameter(HelpMessage = "A path to a .props file where generated content should be saved to.", Mandatory = $true)] |
| 6 | + [string]$outputPath, |
| 7 | + |
| 8 | + [Parameter(HelpMessage = "The path to the template used to generate the props file.")] |
| 9 | + [string]$templatePath = "$PSScriptRoot/MultiTargetAwareProjectReference.props.template", |
| 10 | + |
| 11 | + [Parameter(HelpMessage = "The path to the props file that contains the default MultiTarget values.")] |
| 12 | + [string[]]$multiTargetFallbackPropsPath = @("$PSScriptRoot/Defaults.props"), |
| 13 | + |
| 14 | + [Parameter(HelpMessage = "The placeholder text to replace when inserting the project file name into the template.")] |
| 15 | + [string]$projectFileNamePlaceholder = "[ProjectFileName]", |
| 16 | + |
| 17 | + [Parameter(HelpMessage = "The placeholder text to replace when inserting the project path into the template.")] |
| 18 | + [string]$projectRootPlaceholder = "[ProjectRoot]" |
| 19 | +) |
| 20 | + |
| 21 | +$preWorkingDir = $pwd; |
| 22 | +Set-Location $PSScriptRoot; |
| 23 | + |
| 24 | +$relativeProjectPath = Invoke-Expression -C "(Resolve-Path -Relative -Path $projectPath)"; |
| 25 | +$templateContents = Get-Content -Path $templatePath; |
| 26 | + |
| 27 | +Set-Location $preWorkingDir; |
| 28 | + |
| 29 | +# Insert csproj file name. |
| 30 | +$csprojFileName = [System.IO.Path]::GetFileName($relativeProjectPath); |
| 31 | +$templateContents = $templateContents -replace [regex]::escape($projectFileNamePlaceholder), $csprojFileName; |
| 32 | + |
| 33 | +# Insert project directory |
| 34 | +$relativeProjectDirectory = [System.IO.Path]::GetDirectoryName($relativeProjectPath); |
| 35 | +$templateContents = $templateContents -replace [regex]::escape($projectRootPlaceholder), "$relativeProjectDirectory"; |
| 36 | + |
| 37 | +function LoadMultiTargetsFrom([string] $path) { |
| 38 | + $fileContents = ""; |
| 39 | + |
| 40 | + # If file does not exist |
| 41 | + if ($false -eq (Test-Path -Path $path -PathType Leaf)) { |
| 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 | + } |
| 49 | + } |
| 50 | + else { |
| 51 | + # Load requested file |
| 52 | + $fileContents = Get-Content $path -ErrorAction Stop; |
| 53 | + } |
| 54 | + |
| 55 | + # Parse file contents |
| 56 | + $regex = Select-String -Pattern '<MultiTarget>(.+?)<\/MultiTarget>' -InputObject $fileContents; |
| 57 | + |
| 58 | + if ($null -eq $regex -or $null -eq $regex.Matches -or $null -eq $regex.Matches.Groups -or $regex.Matches.Groups.Length -lt 2) { |
| 59 | + Write-Error "Couldn't get MultiTarget property from $path"; |
| 60 | + exit(-1); |
| 61 | + } |
| 62 | + |
| 63 | + return $regex.Matches.Groups[1].Value; |
| 64 | +} |
| 65 | + |
| 66 | +# Load multitarget preferences for project |
| 67 | +$multiTargets = LoadMultiTargetsFrom("$([System.IO.Path]::GetDirectoryName($projectPath))\MultiTarget.props"); |
| 68 | + |
| 69 | +$templateContents = $templateContents -replace [regex]::escape("[IntendedTargets]"), $multiTargets; |
| 70 | + |
| 71 | +$multiTargets = $multiTargets.Split(';'); |
| 72 | +Write-Host "Generating project references for $([System.IO.Path]::GetFileNameWithoutExtension($csprojFileName)): $($multiTargets -Join ', ')" |
| 73 | + |
| 74 | +$templateContents = $templateContents -replace [regex]::escape("[CanTargetWasm]"), "'$($multiTargets.Contains("wasm").ToString().ToLower())'"; |
| 75 | +$templateContents = $templateContents -replace [regex]::escape("[CanTargetUwp]"), "'$($multiTargets.Contains("uwp").ToString().ToLower())'"; |
| 76 | +$templateContents = $templateContents -replace [regex]::escape("[CanTargetWasdk]"), "'$($multiTargets.Contains("wasdk").ToString().ToLower())'"; |
| 77 | +$templateContents = $templateContents -replace [regex]::escape("[CanTargetWpf]"), "'$($multiTargets.Contains("wpf").ToString().ToLower())'"; |
| 78 | +$templateContents = $templateContents -replace [regex]::escape("[CanTargetLinuxGtk]"), "'$($multiTargets.Contains("linuxgtk").ToString().ToLower())'"; |
| 79 | +$templateContents = $templateContents -replace [regex]::escape("[CanTargetMacOS]"), "'$($multiTargets.Contains("macos").ToString().ToLower())'"; |
| 80 | +$templateContents = $templateContents -replace [regex]::escape("[CanTargetiOS]"), "'$($multiTargets.Contains("ios").ToString().ToLower())'"; |
| 81 | +$templateContents = $templateContents -replace [regex]::escape("[CanTargetDroid]"), "'$($multiTargets.Contains("android").ToString().ToLower())'"; |
| 82 | + |
| 83 | +# Save to disk |
| 84 | +Set-Content -Path $outputPath -Value $templateContents; |
0 commit comments