-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathGenerateMultiTargetAwareProjectReferenceProps.ps1
More file actions
94 lines (70 loc) · 4.59 KB
/
GenerateMultiTargetAwareProjectReferenceProps.ps1
File metadata and controls
94 lines (70 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Param (
[Parameter(HelpMessage = "The full path of the csproj or projitems to generate a reference to.", Mandatory = $true)]
[string]$projectPath,
[Parameter(HelpMessage = "A path to where the generated .props file containing the reference should be saved to.", Mandatory = $true)]
[string]$outputPath,
[Parameter(HelpMessage = "The path to the template used to generate the props file.")]
[string]$templatePath,
[Parameter(HelpMessage = "The placeholder text to replace when inserting the project file name into the template.")]
[string]$projectFileNamePlaceholder = "[ProjectFileName]",
[Parameter(HelpMessage = "The placeholder text to replace when inserting the project path into the template.")]
[string]$projectRootPlaceholder = "[ProjectRoot]",
[Parameter(HelpMessage = "Only projects that support these targets will have references generated for use by deployable heads.")]
[ValidateSet("uwp", "wasdk", "wpf", "wasm", "linuxgtk", "macos", "ios", "android", "netstandard")]
[Alias("mt")]
[string[]] $MultiTargets = @("uwp", "wasdk", "wpf", "wasm", "linuxgtk", "macos", "ios", "android", "netstandard")
)
if ($projectPath.EndsWith(".projitems")) {
$templatePath = "$PSScriptRoot/MultiTargetAwareSharedProjectImport.props.template"
} elseif ($projectPath.EndsWith(".csproj")) {
$templatePath = "$PSScriptRoot/MultiTargetAwareProjectReference.props.template";
} else {
Write-Error "The specified project path is not a valid csproj or projitems file: $projectPath";
exit(-1);
}
$templateContents = Get-Content -Path $templatePath;
$preWorkingDir = $pwd;
Set-Location "$PSScriptRoot/../../"
$relativeProjectPath = Resolve-Path -Relative -Path $projectPath
Set-Location $preWorkingDir;
# Insert project file name.
$projectFileName = [System.IO.Path]::GetFileName($projectPath);
$templateContents = $templateContents -replace [regex]::escape($projectFileNamePlaceholder), $projectFileName;
# Insert component directory
$componentDirectoryRelativeToRoot = [System.IO.Path]::GetDirectoryName($relativeProjectPath).TrimStart('.').TrimStart('\');
$templateContents = $templateContents -replace [regex]::escape($projectRootPlaceholder), "$componentDirectoryRelativeToRoot";
# Get component name from project path
$componentPath = Get-Item "$projectPath/../../"
# Load multitarget preferences for component
$multiTargetPrefs = & $PSScriptRoot\Get-MultiTargets.ps1 -component $($componentPath.BaseName)
if ($null -eq $multiTargetPrefs) {
Write-Error "Couldn't get MultiTarget property for $componentPath";
exit(-1);
}
# Ensure multiTargetPrefs is not empty
if ($multiTargetPrefs.Length -eq 0) {
Write-Error "MultiTarget property is empty for $projectPath";
exit(-1);
}
$templateContents = $templateContents -replace [regex]::escape("[IntendedTargets]"), $multiTargetPrefs;
function ShouldMultiTarget([string] $target) {
return ($multiTargetPrefs.Contains($target) -and $MultiTargets.Contains($target))
}
function ShouldMultiTargetMsBuildValue([string] $target) {
return $(ShouldMultiTarget $target).ToString().ToLower()
}
$targeted = @("uwp", "wasdk", "wpf", "wasm", "linuxgtk", "macos", "ios", "android", "netstandard").Where({ ShouldMultiTarget $_ })
if ($targeted.Count -gt 0) {
Write-Host "Generating project references for $([System.IO.Path]::GetFileNameWithoutExtension($projectFileName)): $($targeted -Join ', ')"
}
$templateContents = $templateContents -replace [regex]::escape("[CanTargetWasm]"), "'$(ShouldMultiTargetMsBuildValue "wasm")'";
$templateContents = $templateContents -replace [regex]::escape("[CanTargetUwp]"), "'$(ShouldMultiTargetMsBuildValue "uwp")'";
$templateContents = $templateContents -replace [regex]::escape("[CanTargetWasdk]"), "'$(ShouldMultiTargetMsBuildValue "wasdk")'";
$templateContents = $templateContents -replace [regex]::escape("[CanTargetWpf]"), "'$(ShouldMultiTargetMsBuildValue "wpf")'";
$templateContents = $templateContents -replace [regex]::escape("[CanTargetLinuxGtk]"), "'$(ShouldMultiTargetMsBuildValue "linuxgtk")'";
$templateContents = $templateContents -replace [regex]::escape("[CanTargetMacOS]"), "'$(ShouldMultiTargetMsBuildValue "macos")'";
$templateContents = $templateContents -replace [regex]::escape("[CanTargetiOS]"), "'$(ShouldMultiTargetMsBuildValue "ios")'";
$templateContents = $templateContents -replace [regex]::escape("[CanTargetDroid]"), "'$(ShouldMultiTargetMsBuildValue "android")'";
$templateContents = $templateContents -replace [regex]::escape("[CanTargetNetstandard]"), "'$(ShouldMultiTargetMsBuildValue "netstandard")'";
# Save to disk
Set-Content -Path $outputPath -Value $templateContents;