generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstaller.ts
444 lines (396 loc) · 12.7 KB
/
installer.ts
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import {cwd} from 'process'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import * as semver from 'semver'
import * as util from './util'
import json from './versions.json'
export type VersionType = {
version: string
aptname?: string
type: string
arch?: string
address: string
}
// Store information about the environment
const osPlat = os.platform() // possible values: win32 (Windows), linux (Linux), darwin (macOS)
core.debug(`platform: ${osPlat}`)
/**
* @returns An array of all OpenModelica versions available for download / install
*/
export function getOMVersions(): string[] {
// Get versions
const versions: string[] = []
let osVersionLst: VersionType[] = []
switch (osPlat) {
case 'linux':
osVersionLst = json.linux
break
case 'win32':
osVersionLst = json.windows
break
case 'darwin':
osVersionLst = json.mac
break
default:
// Array stays empty
}
for (const ver of osVersionLst) {
versions.push(ver.version)
}
core.debug(`Available versions: ${versions.toString()}`)
return versions
}
/**
* @param versionInput Version to find
* @returns Highest available version matching versionInput.
*/
export function getOMVersion(versionInput: string): VersionType {
if (osPlat !== 'linux' && osPlat !== 'win32' && osPlat !== 'darwin') {
throw new Error(`getOMVersion: OS ${osPlat} not supported.`)
}
let maxVersion: string | null
if (
versionInput === 'nightly' ||
versionInput === 'stable' ||
versionInput === 'release'
) {
maxVersion = versionInput
} else if (versionInput.includes('dev')) {
maxVersion = versionInput
} else {
// Use the highest available version that matches versionInput
const availableReleases = getOMVersions()
maxVersion = semver.maxSatisfying(availableReleases, versionInput)
if (maxVersion == null) {
// Check pre-releases
core.debug(`Checking pre releases`)
// Workaround so that 20 is smaller than 100. Add leading zeroes
for (let i=0; i<availableReleases.length; i++) {
if (availableReleases[i].includes('-dev-')) {
const splittedArray = availableReleases[i].split('-dev-')
core.debug(`Splitted array of ${availableReleases[i].toString()}: ${splittedArray.toString()}`)
if (Number(splittedArray[1]) < 100) {
core.debug(`Smaller 100`)
availableReleases[i] = `${splittedArray[0]}-dev-00${splittedArray[1]}`
} else if (Number(splittedArray[1]) < 1000) {
core.debug(`Smaller 1000`)
availableReleases[i] = `${splittedArray[0]}-dev-0${splittedArray[1]}`
}
}
}
core.debug(`Available versions: ${availableReleases.toString()}`)
maxVersion = semver.maxSatisfying(availableReleases, `>${versionInput}-dev`, { includePrerelease: true })
if (maxVersion == null) {
throw new Error(
`Could not find a OpenModelica version that matches ${versionInput}`
)
} else {
// Remove leading zeroes
const splittedArray = maxVersion.split('-dev-')
maxVersion = `${splittedArray[0]}-dev-${Number(splittedArray[1])}`
}
}
}
core.debug(`Searching for ${versionInput}, found max version: ${maxVersion}`)
// Return highest version from versions.json
let osVersionLst: VersionType[] = []
switch (osPlat) {
case 'linux':
osVersionLst = json.linux
break
case 'win32':
osVersionLst = json.windows
break
case 'darwin':
osVersionLst = json.mac
break
default:
// Array stays empty
}
for (const ver of osVersionLst) {
if (ver.version === maxVersion) {
return ver
}
}
throw new Error(`Could not find version ${maxVersion} in database.`)
}
/**
* Install OpenModelica packages with apt.
*
* @param packages APT packages to install.
* @param version Version object to install.
* @param bit String specifying 32 or 64 bit version.
* @param useSudo true if root rights are required.
*/
async function aptInstallOM(
packages: string[],
version: VersionType,
bit: string,
useSudo: boolean
): Promise<void> {
let sudo: string
if (useSudo) {
sudo = 'sudo'
} else {
sudo = ''
}
// Get architecture
let out = await exec.getExecOutput(`/bin/bash -c "dpkg --print-architecture"`)
let arch = out.stdout.trim()
switch (arch) {
case 'amd64':
if (bit === '32') arch = 'i386'
break
case 'arm64':
if (bit === '32') arch = 'armhf'
break
case 'armhf':
if (bit === '64')
throw new Error(`Architecture is "armhf", 64bit not supported.`)
break
case 'i386':
if (bit === '64')
throw new Error(`Architecture is "i386", 64bit not supported.`)
break
default:
throw new Error(`Unknown architecture ${arch}.`)
}
// Check if distribution is available
out = await exec.getExecOutput(`/bin/bash -c "lsb_release -cs"`)
const distro = out.stdout.trim()
if (
version.version !== 'nightly' &&
version.version !== 'stable' &&
version.version !== 'release'
) {
const response = await fetch(`${version.address}dists/${distro}`)
if (response.status === 404) {
throw new Error(
`Distribution ${distro} not available for OpenModelica version ${version.version}.`
)
}
}
// Remove old previous openmodelica.list
await exec.exec(
`/bin/bash -c "${sudo} rm -f /etc/apt/sources.list.d/openmodelica.list /usr/share/keyrings/openmodelica-keyring.gpg"`
)
// Add OpenModelica PGP public key
await exec.exec(
`/bin/bash -c "curl -fsSL http://build.openmodelica.org/apt/openmodelica.asc ${'|'} ${sudo} gpg --dearmor -o /usr/share/keyrings/openmodelica-keyring.gpg"`
)
await exec.exec(
`/bin/bash -c "echo deb [arch=${arch} signed-by=/usr/share/keyrings/openmodelica-keyring.gpg] \
${version.address} ${distro} ${version.type} \
${'|'} ${sudo} tee /etc/apt/sources.list.d/openmodelica.list"`
)
// Install OpenModelica packages
core.info(`Running apt-get inexec--versionstall`)
await exec.exec(`${sudo} apt-get clean`)
await exec.exec(`${sudo} apt-get update`)
for (const pkg of packages) {
if (version.type === 'nightly' || !version.aptname) {
core.debug(`Running: /bin/bash -c "${sudo} apt-get install ${pkg} -qy"`)
await exec.exec(`/bin/bash -c "${sudo} apt-get install ${pkg} -qy"`)
} else {
core.debug(
`/bin/bash -c "${sudo} apt-get install ${pkg}=${version.aptname} -V -qy`
)
await exec.exec(
`/bin/bash -c "${sudo} apt-get install ${pkg}=${version.aptname} -V -qy"`
)
}
}
}
/**
* Install omc using the Windows installer executable.
*
* @param version Version object to install.
* @param bit String specifying 32 or 64 bit version.
*/
async function winInstallOM(version: VersionType, bit: string): Promise<void> {
// Download OpenModelica installer to tmp/
const installer = await util.downloadCachedSync(
version.address,
'tmp',
version.version === 'nightly'
)
if (bit !== version.arch) {
throw new Error(`Architecture doesn't match architecture of version.`)
}
// Run installer
core.info(`Running installer ${installer}`)
await exec.exec(`${installer} /S /v /qn`)
// Add OpenModelica to PATH and set OPENMODELICAHOME
const openmodelicahome = fs
.readdirSync('C:\\Program Files\\')
.filter(function (file) {
return (
fs.lstatSync(path.join('C:\\Program Files\\', file)).isDirectory() &&
file.startsWith('OpenModelica')
)
})
const pathToOmc = path.join('C:\\Program Files\\', openmodelicahome[0], 'bin')
core.info(`Adding ${pathToOmc} to PATH`)
core.addPath(pathToOmc)
core.exportVariable(
'OPENMODELICAHOME',
path.join('C:\\Program Files\\', openmodelicahome[0])
)
// Clean up
fs.rmSync('tmp', {recursive: true})
}
/**
* Install omc using the Windows installer executable.
*
* @param version Version object to install.
*/
async function macInstallOM(version: VersionType): Promise<void> {
// Download OpenModelica pkg file tmp/
const pkg = await util.downloadCachedSync(
version.address,
'tmp',
version.version === 'nightly'
)
// Check for homebrew
///bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
//(echo; echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"') >> /home/arch/.bashrc
//eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
// Run installer
core.info(`Running installer with package ${pkg}`)
await exec.exec(
`installer -verbose -pkg ${pkg} -target CurrentUserHomeDirectory`
)
const out = await exec.getExecOutput('find', ['/Users/runner', '-name', 'omc'])
if (out.exitCode !== 0) {
core.debug(`Error message: ${out.stderr}`)
core.setFailed(Error(`Couldn't find omc. Exit code: ${out.exitCode}`))
}
// Update PATH
const pathToOmc = '/Users/runner/opt/omc/bin'
core.info(`Adding ${pathToOmc} to PATH`)
core.addPath(pathToOmc)
// Clean up
fs.rmSync('tmp', {recursive: true})
}
/**
* Install OpenModelica packages (omc, OMSimulator)
*
* @param packages (APT) packages to install.
* @param version Version of OpenModelica to be installed.
* @param architectureInput 64 or 32 bit.
*/
export async function installOM(
packages: string[],
version: VersionType,
architectureInput: string
): Promise<void> {
switch (osPlat) {
case 'linux':
await aptInstallOM(packages, version, architectureInput, true)
break
case 'win32':
await winInstallOM(version, architectureInput)
break
case 'darwin':
await macInstallOM(version)
break
default:
throw new Error(`Platform ${osPlat} is not supported`)
}
}
/**
* Test if program has been installed and print the version.
*/
export async function showVersion(program: string): Promise<string> {
const out = await exec.getExecOutput(program, ['--version'])
if (out.exitCode !== 0) {
core.debug(`Error message: ${out.stderr}`)
core.setFailed(
Error(
`${program} could not be installed properly. Exit code: ${out.exitCode}`
)
)
}
const version = out.stdout.trim().split(' ')[1]
return version
}
/**
* Install Modelica libraries with the OpenModelica package manager
*
* @param librariesInput List of Modelica libraries with versions
*/
export async function installLibs(librariesInput: string[]): Promise<void> {
const filename = genInstallScript(librariesInput)
// Run install script
core.info(`Running install script ${filename}`)
await exec.exec(`omc ${filename}`)
// Clean up
fs.rmSync(filename)
}
/**
* Write install script for Modelica libraries
* @param librariesInput
*/
function genInstallScript(librariesInput: string[]): string {
const filename = path.join(cwd(), 'installLibs.mos')
const installPackages: string[] = []
for (const library of librariesInput) {
const matches = library.match(/\s*\b(\w+)\b\s*(.*)\b/)
if (!matches) {
throw new Error(`Invalid library name ${library}`)
}
const name = matches[1]
const version = matches[2]
installPackages.push(`if not installPackage(${name}, "${version}", exactMatch=true) then
print("Failed to install ${library}");
print(getErrorString());
exit(1);
else
print("Installed: ${library}\\n");
end if;\n`)
}
const content = `updatePackageIndex(); getErrorString();
${installPackages.join('\n')}`
// Write file
core.debug(`Writing ${filename}`)
fs.writeFile(filename, content, function (err) {
if (err) {
core.setFailed(Error(`Failed to write install script ${filename}.`))
}
})
return filename
}
/**
* Install OpenModelica omc-diff program.
*
* @param useSudo true if root rights are required.
*/
export async function installOmcDiff(useSudo: boolean): Promise<void> {
switch (osPlat) {
case 'linux':
break
case 'win32':
core.info(`Windows version of OpenModelica already installs omc-diff.`)
return
default:
throw new Error(
`omc-diff not available for platform ${osPlat}. Open a feature request on https://github.com/AnHeuermann/omc-diff.`
)
}
const sudo: string = useSudo ? 'sudo' : ''
// Download executable from https://github.com/AnHeuermann/omc-diff/
const url =
'https://github.com/AnHeuermann/omc-diff/releases/download/v0.1/linux-64.tar.gz'
const file = url.split('/').pop()
if (file === undefined) {
throw new Error(`Something wrong with the url`)
}
await exec.exec(`wget ${url}`)
// Extract .tar.gz
await exec.exec(`${sudo} tar -xvf ${file} -C /usr/bin/`)
// Clean up
fs.rmSync(file)
}