-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbuild.ps1
396 lines (323 loc) · 10.8 KB
/
build.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# CAcert Root Certificates Installer
# Copyright (C) 2012 CAcert Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Requirements:
# Saxon-HE (for the wxl2xliff conversions,
# any other XSLT 2.0 processor would do, but you probably would need
# to adjust the wxl2xliff and xliff2wxl functions)
param (
[string] $command = "-help",
[array] $cultures = @{
"en-US"=""
"bg-BG"="bg"
"cs-CZ"="cs"
"da-DK"="da"
"de-DE"="de"
"el-GR"="el"
"es-ES"="es"
"fi-FI"="fi"
"fr-FR"="fr"
"hu-HU"="hu"
"it-IT"="it"
"ja-JP"="ja"
"lv-LV"="lv"
"nl-NL"="nl"
"pl-PL"="pl"
"pt-BR"="pt_BR"
"pt-PT"="pt_PT"
"ru-RU"="ru"
"sv-SE"="sv"
"tr-TR"="tr"
"zh-CN"="zh_CN"
"zh-TW"="zh_TW"
}.Keys
)
$base_name = "CAcert_Root_Certificates"
$culture_map = @{
"en-US"=""
"bg-BG"="bg"
"cs-CZ"="cs"
"da-DK"="da"
"de-DE"="de"
"el-GR"="el"
"es-ES"="es"
"fi-FI"="fi"
"fr-FR"="fr"
"hu-HU"="hu"
"it-IT"="it"
"ja-JP"="ja"
"lv-LV"="lv"
"nl-NL"="nl"
"pl-PL"="pl"
"pt-BR"="pt_BR"
"pt-PT"="pt"
"ru-RU"="ru"
"sv-SE"="sv"
"tr-TR"="tr"
"zh-CN"="zh_CN"
"zh-TW"="zh_TW"
}
$langid_map = @{
"en-US"=1033
"bg-BG"=1026
"cs-CZ"=1029
"da-DK"=1030
"de-DE"=1031
"el-GR"=1032
"es-ES"=1034
"fi-FI"=1035
"fr-FR"=1036
"hu-HU"=1038
"it-IT"=1040
"ja-JP"=1041
"lv-LV"=1062
"nl-NL"=1043
"pl-PL"=1045
"pt-BR"=1046
"pt-PT"=2070
"ru-RU"=1049
"sv-SE"=1053
"tr-TR"=1055
"zh-CN"=2052
"zh-TW"=1028
}
$wxs_file = ".\${base_name}.wxs"
$obj_dir = ".\build\objects\"
$obj_file = "${obj_dir}\${base_name}.wixobj"
$out_filename = "${base_name}.msi"
function wxl2xliff([String] $wxl, [String] $xliff = "${wxl}.xlf") {
Transform.exe -xsl:wxl2xliff.xsl "-s:$wxl" "-o:$xliff"
if ($LASTEXITCODE -ne 0) {
throw "wxl2xliff: Conversion from `"$wxl`" to `"$xliff`" failed (error code: ${LASTEXITCODE})."
}
}
function xliff2wxl([String] $xliff, [String] $wxl = "${xliff}.wxl") {
Transform.exe -xsl:xliff2wxl.xsl "-s:$xliff" "-o:$wxl"
if ($LASTEXITCODE -ne 0) {
throw "xliff2wxl: Conversion from `"$xliff`" to `"$wxl`" failed (error code: ${LASTEXITCODE})."
}
}
function l10n_template {
Write-Output "Converting localisation template ..."
wxl2xliff "${base_name}_template.wxl" "${base_name}_template.xlf"
Write-Output "Localisation template converted.`n"
}
function update_l10n {
Write-Output "Updating localisation files ..."
$webclient = New-Object System.Net.WebClient
foreach ($culture in $cultures) {
if ($culture -eq "en-US") {
continue
}
if ($culture_map.Keys -notcontains $culture) {
Write-Warning "Skipping language $culture as there is no mapping to a simple language name defined"
continue
}
Write-Output "Updating ${culture}:"
$url = "http://translations.cacert.org/export/installer/"+$culture_map[$culture]+"/CAcert_Root_Certificates.xlf"
$file_name = "${base_name}_${culture}.xlf"
New-Item "$file_name" -ItemType file -Force|Out-Null
Write-Output "Retrieving $url ..."
$webclient.DownloadFile("$url", (Resolve-Path "$file_name").Path)
if (!$?) {
Write-Error "Could not retrieve $url => Skipping language ${culture}!"
continue
}
Write-Output "Stored as ${file_name}."
Write-Output "Converting ..."
xliff2wxl "$file_name" "${base_name}_${culture}.wxl"
Write-Output "Update of ${culture} complete."
}
Write-Output "Update of localisation files complete.`n"
}
$script:wix = $null
function get_wix_path {
if ($script:wix) {
# Fast path
return $script:wix
}
$script:wix = $Env:WIX
if ($script:wix) {
return $script:wix
}
$reg_key = Get-ItemProperty "HKCU:\Software\Wow6432Node\Microsoft\Windows Installer XML\3.?" -ErrorAction SilentlyContinue
$script:wix = $reg_key.InstallFolder
if ($script:wix) {
return $script:wix
}
$reg_key = Get-ItemProperty "HKLM:\Software\Wow6432Node\Microsoft\Windows Installer XML\3.?" -ErrorAction SilentlyContinue
$script:wix = $reg_key.InstallFolder
if ($script:wix) {
return $script:wix
}
Write-Error "Could not find the WiX Toolset 3.X. Please make sure the WIX environment variable is set."
}
function build_objects {
Write-Output "Compiling object file ..."
$wix_bin = (get_wix_path)+"\bin\"
New-Item "$obj_dir" -ItemType directory -Force | Out-Null
&($wix_bin+"\candle.exe") -ext WixIIsExtension -out "$obj_file" "$wxs_file"
if ($LASTEXITCODE -ne 0) {
throw "Compiling the object file failed!"
}
Write-Output "Object file was compiled successfully.`n"
}
function build_installer([string] $culture="en-US") {
Write-Output "Building installer for ${culture} ..."
$wix_bin = (get_wix_path)+"\bin\"
$culture_alias = $culture
if ($culture -eq "en-US") {
$culture_alias = "template"
}
$build_dir = ".\build\${culture_alias}\"
New-Item "$build_dir" -ItemType directory -Force | Out-Null
&($wix_bin+"\light.exe") -ext WixIIsExtension -ext WixUIExtension -cultures:${culture} -loc "${base_name}_${culture_alias}.wxl" -sice:ICE105 -out "${build_dir}\${out_filename}" "$obj_file"
if ($LASTEXITCODE -ne 0) {
throw "Building the installer for ${culture} failed!"
}
Write-Output "Installer for ${culture} successfully built."
}
function build_installers {
Write-Output "Building installers for each specified culture ..."
foreach ($culture in $cultures) {
build_installer $culture
}
Write-Output "Succesfully built all specified installers.`n"
}
function build_transforms {
Write-Output "Generating transform files for each specified culture ..."
foreach ($culture in $cultures) {
if ($culture -eq "en-US") {
continue
}
Write-Output "Generating transform for langauge ${culture}"
$wix_bin = (get_wix_path)+"\bin\"
$build_dir = ".\build\${culture}\"
New-Item "$build_dir" -ItemType directory -Force | Out-Null
&($wix_bin+"\torch.exe") -p -t language -out "${build_dir}\${base_name}.mst" ".\build\template\${out_filename}" "${build_dir}\${out_filename}"
if ($LASTEXITCODE -ne 0) {
throw "Building the transform for ${culture} failed!"
}
Write-Output "Transform for ${culture} successfully built."
}
Write-Output "Successfully built all specified transforms.`n"
}
function msi_add_language_substorage([String] $msi, [String] $transform, [int] $langid) {
Write-Output "Integrating transform for language ${langid} ..."
# Import assembly
$wix_path = get_wix_path
Add-Type -Path "${wix_path}\SDK\Microsoft.Deployment.WindowsInstaller.dll"
# Open MSI
$db = new-object Microsoft.Deployment.WindowsInstaller.Database((Resolve-Path $msi).Path, [Microsoft.Deployment.WindowsInstaller.DatabaseOpenMode]::Transact)
if (!$?) {
throw "Could not open the MSI database"
}
try {
# Set up the new record
$record = $db.CreateRecord(2); if (!$?) { throw "Could not create record to be inserted as substorage!" }
$record.SetString(1, "$langid"); if (!$?) { throw "Could not set the name of the substorage to be inserted!" }
$record.SetStream(2, (Resolve-Path $transform).Path); if (!$?) { throw "Could not read data from ${transform}!" }
# Insert the new record
$view = $db.OpenView("SELECT `Name`,`Data` FROM _Storages"); if (!$?) { throw "Could not open the substorages view!" }
$view.Execute(); if (!$?) { throw "Could not execute the substorages view!" }
$view.Assign($record); if (!$?) { throw "Could not write the data to the substorage!" }
$view.Close()
$record.Close()
# Update summary information stream
$si = $db.SummaryInfo; if (!$?) { throw "Could not get summary information!" }
$template = $si.Template; if (!$?) { throw "Could not get the template property from the summary information!" }
$template += ",${langid}"
$si.Template = $template; if (!$?) { throw "Could not set the template property of the summary information!" }
$si.Persist(); if (!$?) { throw "Could not save summary information!" }
$si.Close()
$db.Commit(); if (!$?) { throw "Could not commit outstanding database transactions!" }
} finally {
$db.Close()
}
Write-Output "Successfully integrated transform for language ${langid}."
}
function msi_add_language_transforms {
Write-Output "Embedding the language transforms into the main installer ..."
$result_path = ".\build\${out_filename}"
Copy-Item ".\build\template\${out_filename}" $result_path
foreach ($culture in $cultures) {
if ($culture -eq "en-US") {
continue
}
if ($langid_map.Keys -notcontains $culture) {
Write-Warning "Skipping language $culture as there is no mapping to a language id defined"
continue
}
$langid = $langid_map[$culture]
$transform = ".\build\${culture}\${base_name}.mst"
try {
msi_add_language_substorage $result_path $transform $langid
} catch {
Write-Warning "Error when embedding the language transform for ${culture}:`n ${_}`nSkipping ${culture}."
continue
}
}
Write-Output "Finished embedding the language transforms.`n"
}
function sign_installer {
Write-Output "Signing the installer ..."
signtool.exe sign /d "CAcert Root Certificates" /du "http://www.cacert.org" /n "CAcert Release Signing" /t "http://timestamp.verisign.com/scripts/timstamp.dll" ".\build\${out_filename}"
if ($LASTEXITCODE -ne 0) {
throw "Signing the installer failed (error code: ${LASTEXITCODE}).`nMaybe the signtool.exe is not on the path?"
}
}
##### Script Part #####
$help = "Build the CAcert Installer
build.ps1 [-command] <string> [[-cultures] <array>]
Available Commands:
l10n_template: generate localisation template (aka *_template.xlf)
update_l10n: update the localisation files from the translation server
quick_build: only build the english installer
installer: build the complete installer
signature: sign the installer
Usually you want to use 'update_l10n' first to download the localisation files
and then use 'installer' to build the final installer"
switch -regex ($command) {
"(/\?|-*help)" {
Write-Output $help
}
"l10n_template" {
l10n_template
}
"update_l10n" {
update_l10n
}
"quick_build" {
build_objects
build_installer
Write-Output "Finished building the installer."
Write-Output "You can find it in .\build\template\"
}
"installer" {
build_objects
build_installers
build_transforms
msi_add_language_transforms
Write-Output "Finished building the installer."
}
"signature" {
sign_installer
Write-Output "Finished signing the installer."
}
default {
Write-Output "Command $command not recognised"
Write-Output $help
}
}