This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-bindings.ps1
173 lines (145 loc) · 5.76 KB
/
generate-bindings.ps1
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
function Get-SwiftWinRTVersion {
$Projections = Get-Content -Path $PSScriptRoot\projections.json | ConvertFrom-Json
return $Projections."swift-winrt"
}
function Get-PackageString {
param(
$Package
)
if ($Package) {
return " <package id=""$($Package.Id)"" version=""$($Package.Version)"" />`n"
}
}
function Restore-Nuget {
param(
[string]$PackagesDir
)
$NugetDownloadPath = Join-Path $env:TEMP "nuget.exe"
if (-not (Test-Path $NugetDownloadPath)) {
Invoke-WebRequest -Uri "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe" -OutFile $NugetDownloadPath
}
$Projections = Get-Content -Path $PSScriptRoot\projections.json | ConvertFrom-Json
$SwiftWinRTVersion = Get-SwiftWinRTVersion
$PackagesConfigContent = "<?xml version=""1.0"" encoding=""utf-8""?>`n"
$PackagesConfigContent += "<packages>`n"
$PackagesConfigContent += " <package id=""TheBrowserCompany.SwiftWinRT"" version=""$SwiftWinRTVersion"" />`n"
if ($Projections.Package) {
$PackagesConfigContent += Get-PackageString -Package $Projections.Package
}
$Projections.Packages | ForEach-Object {
$PackagesConfigContent += Get-PackageString -Package $_
}
$Projections.Dependencies | ForEach-Object {
$PackagesConfigContent += Get-PackageString -Package $_
}
$PackagesConfigContent += "</packages>"
if (-not (Test-Path "$PSScriptRoot\.packages")) {
New-Item -ItemType Directory -Path "$PSScriptRoot\.packages" | Out-Null
}
$PackagesConfigPath = Join-Path $PSScriptRoot ".packages\packages.config"
$PackagesConfigContent | Out-File -FilePath $PackagesConfigPath -Encoding ascii
& $NugetDownloadPath restore $PackagesConfigPath -PackagesDirectory $PackagesDir | Out-Null
if ($LASTEXITCODE -ne 0) {
Write-Host "Nuget restore failed!" -ForegroundColor Red
exit 1
}
}
function Get-WinMDInputs() {
param(
$Package
)
$Id = $Package.Id
$Version = $Package.Version
return Get-ChildItem -Path $PackagesDir\$Id.$Version\ -Filter *.winmd -Recurse
}
function Copy-Project {
param(
[string]$OutputLocation,
[string]$ProjectName
)
if ($ProjectName) {
$ProjectGeneratedDir = if ($ProjectName -eq "CWinRT") { "$ProjectName" } else { "$ProjectName\Generated" }
$ProjectDir = Join-Path $PSScriptRoot "Sources\$ProjectGeneratedDir"
if (Test-Path $ProjectDir) {
Remove-Item -Path $ProjectDir -Recurse -Force
}
Copy-Item -Path $OutputLocation\Sources\$ProjectName -Destination $ProjectDir -Recurse -Force
}
}
function Invoke-SwiftWinRT() {
param(
[string]$PackagesDir
)
$Projections = Get-Content -Path $PSScriptRoot\projections.json | ConvertFrom-Json
$SwiftWinRTVersion = Get-SwiftWinRTVersion
# write generated bindings to a temp directory since swiftwinrt will generate all dependencies and the CWinRT
$OutputLocation = Join-Path $PSScriptRoot ".generated"
if (Test-Path $OutputLocation) {
Remove-Item -Path $OutputLocation -Recurse -Force
}
$RspParams = "-output $OutputLocation`n"
# read projections.json and for each "include" write to -include param. for each "exclude" write to -exclude param
$Projections.Include | ForEach-Object {
$RspParams += "-include $_`n"
}
$Projections.Exclude | ForEach-Object {
$RspParams += "-exclude $_`n"
}
if ($Projections.Package) {
Get-WinMDInputs -Package $Package | ForEach-Object {
$RspParams += "-input $($_.FullName)`n"
}
}
$Projections.Packages | ForEach-Object {
Get-WinMDInputs -Package $Package | ForEach-Object {
$RspParams += "-input $($_.FullName)`n"
}
}
$Projections.Dependencies | ForEach-Object {
Get-WinMDInputs -Package $Package | ForEach-Object {
$RspParams += "-input $($_.FullName)`n"
}
}
# write rsp params to file
$RspFile = Join-Path $PSScriptRoot "swift-winrt.rsp"
$RspParams | Out-File -FilePath $RspFile -Encoding ascii
& $PackagesDir\TheBrowserCompany.SwiftWinRT.$SwiftWinRTVersion\bin\swiftwinrt.exe "@$RspFile"
if ($LASTEXITCODE -ne 0) {
Write-Host "swiftwinrt failed with error code $LASTEXITCODE" -ForegroundColor Red
exit 1
}
$Projections.Projects | ForEach-Object {
Copy-Project -OutputLocation $OutputLocation -ProjectName $_
}
if ($Projections.Project) {
Copy-Project -OutputLocation $OutputLocation -ProjectName $Projections.Project
}
}
function Copy-NativeBinaries {
param(
[string]$PackagesDir
)
$Projections = Get-Content -Path $PSScriptRoot\projections.json | ConvertFrom-Json
$Package = $Projections.Package.Id
$PackageVersion = $Projectinos.Package.Version
$PackageDir = Join-Path $PackagesDir "$Package.$PackageVersion"
$PackagesRuntimeDir = Join-Path $PackageDir "runtimes\win-x64\native"
if (-not (Test-Path $PackagesRuntimeDir)) {
return
}
$PackagesBinaries = Get-ChildItem -Path $PackagesRuntimeDir -Filter *.dll -Recurse
$ProjectName = $Projections.Project
$ProjectDir = Join-Path $PSScriptRoot "Sources\${ProjectName}"
$ProjectBinaryDir = Join-Path $ProjectDir "NativeBinaries"
if (-not (Test-Path $ProjectBinaryDir)) {
New-Item -Path $ProjectBinaryDir -ItemType Directory -Force | Out-Null
}
$PackagesBinaries | ForEach-Object {
Copy-Item -Path $_.FullName -Destination $ProjectBinaryDir -Force
}
}
$PackagesDir = Join-Path $PSScriptRoot ".packages"
Restore-Nuget -PackagesDir $PackagesDir
Invoke-SwiftWinRT -PackagesDir $PackagesDir
Copy-NativeBinaries -PackagesDir $PackagesDir
Write-Host "SwiftWinRT bindings generated successfully!" -ForegroundColor Green