forked from coursier/setup-action
/
main.ts
140 lines (131 loc) · 4.4 KB
/
main.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
import * as cli from '@actions/exec'
import * as core from '@actions/core'
import * as os from 'os'
import * as path from 'path'
import * as tc from '@actions/tool-cache'
let csVersion = core.getInput('version')
if (!csVersion) csVersion = '2.1.0-M7-39-gb8f3d7532'
const scalaCLIVersion = '0.2.0'
const coursierVersionSpec = csVersion
async function execOutput(cmd: string, ...args: string[]): Promise<string> {
let output = ''
const options = {
listeners: {
stdout: (data: Buffer) => {
output += data.toString()
},
},
}
await cli.exec(cmd, args.filter(Boolean), options)
return output.trim()
}
async function downloadCoursier(): Promise<string> {
const baseUrl = `https://github.com/coursier/coursier/releases/download/v${csVersion}/cs-x86_64`
let csBinary = ''
switch (process.platform) {
case 'linux': {
const guid = await tc.downloadTool(`${baseUrl}-pc-linux.gz`)
const arc = `${guid}.gz`
await cli.exec('mv', [guid, arc])
csBinary = arc
break
}
case 'darwin': {
const guid = await tc.downloadTool(`${baseUrl}-apple-darwin.gz`)
const arc = `${guid}.gz`
await cli.exec('mv', [guid, arc])
csBinary = arc
break
}
case 'win32': {
const guid = await tc.downloadTool(`${baseUrl}-pc-win32.zip`)
const arc = `${guid}.zip`
await cli.exec('mv', [guid, arc])
csBinary = arc
break
}
default:
core.setFailed(`Unknown process.platform: ${process.platform}`)
}
if (!csBinary) core.setFailed(`Couldn't download Coursier`)
if (csBinary.endsWith('.gz')) {
await cli.exec('gzip', ['-d', csBinary])
csBinary = csBinary.slice(0, csBinary.length - '.gz'.length)
}
if (csBinary.endsWith('.zip')) {
const destDir = csBinary.slice(0, csBinary.length - '.zip'.length)
await cli.exec('unzip', ['-j', csBinary, 'cs-x86_64-pc-win32.exe', '-d', destDir])
csBinary = `${destDir}\\cs-x86_64-pc-win32.exe`
}
await cli.exec('chmod', ['+x', csBinary])
return csBinary
}
async function cs(...args: string[]): Promise<string> {
const previous = tc.find('cs', coursierVersionSpec)
if (previous) {
core.addPath(previous)
} else {
const csBinary = await downloadCoursier()
const binaryName = process.platform === 'win32' ? 'cs.exe' : 'cs'
const csCached = await tc.cacheFile(csBinary, binaryName, 'cs', csVersion)
core.addPath(csCached)
}
return execOutput('cs', ...args)
}
async function run(): Promise<void> {
try {
await core.group('Install Coursier', async () => {
await cs('--help')
core.setOutput('cs-version', csVersion)
})
await core.group('Install JVM', async () => {
const jvmInput = core.getInput('jvm')
const jvmArg = jvmInput ? ['--jvm', jvmInput] : []
if (!jvmInput && process.env.JAVA_HOME) {
core.info(`skipping, JVM is already installed in ${process.env.JAVA_HOME}`)
} else {
await cs('java', ...jvmArg, '-version')
const csJavaHome = await cs('java-home', ...jvmArg)
core.exportVariable('JAVA_HOME', csJavaHome)
core.addPath(path.join(csJavaHome, 'bin'))
}
})
await core.group('Install Apps', async () => {
const value = core.getInput('apps').trim()
const apps: string[] = value.split(' ')
const scalaCLIVersionInput = core.getInput('scala-cli-version')
let version
if (scalaCLIVersionInput) {
if (scalaCLIVersionInput === 'latest') {
version = ''
} else {
version = scalaCLIVersionInput
}
} else {
version = scalaCLIVersion
}
apps.push(`scala-cli${version ? `:${version}` : ''}`)
if (value && apps.length) {
const coursierBinDir = path.join(os.homedir(), 'cs', 'bin')
core.exportVariable('COURSIER_BIN_DIR', coursierBinDir)
core.addPath(coursierBinDir)
await cs('install', '--contrib', ...apps)
core.setOutput(
'scala-cli-version',
await execOutput('scala-cli', 'version', '--cli-version'),
)
}
})
await core.group('Config --power', async () => {
const powerInput = core.getInput('power').trim()
const isPower = powerInput === 'true'
if (isPower) {
await execOutput('scala-cli', 'config', 'power', 'true')
}
})
} catch (error: unknown) {
const msg = error instanceof Error ? error.message : String(error)
core.setFailed(msg)
}
}
run()