-
Notifications
You must be signed in to change notification settings - Fork 28
/
cli.ts
211 lines (190 loc) · 5.8 KB
/
cli.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
import * as core from '@actions/core'
import * as io from '@actions/io'
import * as tc from '@actions/tool-cache'
import * as exec from '@actions/exec'
import * as http from '@actions/http-client'
import * as path from 'path'
import * as os from 'os'
import * as fs from './fs'
import * as utils from './utils'
export const identifier = 'ClojureToolsDeps'
const client = new http.HttpClient('actions/setup-clojure', undefined, {
allowRetries: true,
maxRetries: 1
})
async function toolVersion(
version: string,
githubAuth: string
): Promise<string> {
core.debug('=== Check tool version')
if (version === 'latest') {
const res = await client.getJson<{tag_name: string}>(
'https://api.github.com/repos/clojure/brew-install/releases/latest',
{Authorization: githubAuth}
)
const versionString = res.result?.tag_name
if (versionString) {
return versionString
}
throw new Error(`Can't obtain latest Clojure CLI version`)
} else {
return version
}
}
function isResourceProvided(
target: string,
assets: {browser_download_url: string}[]
): boolean {
for (const asset of assets) {
if (asset.browser_download_url === target) {
return true
}
}
return false
}
async function getUrls(
tag: string,
githubAuth: string
): Promise<{posix?: string; linux: string; windows: string}> {
core.debug('=== Get download URLs')
const res = await client.getJson<{
assets: {browser_download_url: string}[]
}>(`https://api.github.com/repos/clojure/brew-install/releases/tags/${tag}`, {
Authorization: githubAuth
})
const posix_install_url = `https://github.com/clojure/brew-install/releases/download/${tag}/posix-install.sh`
const assets = res.result?.assets
if (assets && isResourceProvided(posix_install_url, assets)) {
return {
posix: posix_install_url,
linux: `https://github.com/clojure/brew-install/releases/download/${tag}/linux-install.sh`,
windows: `github.com/clojure/brew-install/releases/download/${tag}/win-install.ps1`
}
} else {
return {
linux: `https://download.clojure.org/install/linux-install-${tag}.sh`,
windows: `download.clojure.org/install/win-install-${tag}.ps1`
}
}
}
export async function setup(
requestedVersion: string,
githubAuth: string
): Promise<void> {
core.debug('=== Run setup')
const version = await toolVersion(requestedVersion, githubAuth)
const installDir = utils.isWindows()
? 'C:\\Program Files\\WindowsPowerShell\\Modules'
: '/tmp/usr/local/opt'
const toolPath = tc.find(
identifier,
utils.getCacheVersionString(version),
os.arch()
)
if (toolPath) {
core.info(`Clojure CLI found in cache ${toolPath}`)
await fs.mkdir(installDir, {recursive: true})
await fs.cp(toolPath, path.join(installDir, 'ClojureTools'), {
recursive: true
})
} else {
const {linux, posix, windows} = await getUrls(version, githubAuth)
if (utils.isWindows()) {
await exec.exec(`powershell -c "iwr -useb ${windows} | iex"`, [], {
// Install to a modules location common to powershell/pwsh
env: {PSModulePath: installDir},
input: Buffer.from('1')
})
core.debug(
`clojure tools deps installed to ${path.join(
installDir,
'ClojureTools'
)}`
)
await tc.cacheDir(
path.join(installDir, 'ClojureTools'),
identifier,
utils.getCacheVersionString(version)
)
} else {
let clojureInstallScript
if (utils.isMacOS()) {
if (posix) {
clojureInstallScript = await tc.downloadTool(
posix,
undefined,
githubAuth
)
} else {
clojureInstallScript = await tc.downloadTool(
linux,
undefined,
githubAuth
)
await MacOSDeps(clojureInstallScript, githubAuth)
}
} else {
clojureInstallScript = await tc.downloadTool(
linux,
undefined,
githubAuth
)
}
const clojureToolsDir = await runLinuxInstall(
clojureInstallScript,
path.join(installDir, 'ClojureTools')
)
core.debug(`clojure tools deps installed to ${clojureToolsDir}`)
await tc.cacheDir(
clojureToolsDir,
identifier,
utils.getCacheVersionString(version)
)
}
}
core.exportVariable(
'CLOJURE_INSTALL_DIR',
path.join(installDir, 'ClojureTools')
)
if (!utils.isWindows()) {
core.addPath(path.join(installDir, 'ClojureTools', 'bin'))
}
}
async function runLinuxInstall(
installScript: string,
destinationFolder: string
): Promise<string> {
core.debug('=== Install on Linux')
await io.mkdirP(destinationFolder)
const file = path.normalize(installScript)
await exec.exec('bash', [file, '--prefix', destinationFolder])
return destinationFolder
}
async function MacOSDeps(file: string, githubAuth: string): Promise<void> {
core.debug('=== Install extra deps for MacOS')
const data = await fs.readFile(file, 'utf-8')
const newValue = data.replace(
/install -D/gim,
'$(brew --prefix coreutils)/bin/ginstall -D'
)
await fs.writeFile(file, newValue, 'utf-8')
await exec.exec('brew', ['install', 'coreutils'], {
env: {
HOMEBREW_GITHUB_API_TOKEN: githubAuth.substring(7),
HOMEBREW_NO_INSTALL_CLEANUP: 'true',
HOME: process.env['HOME'] || ''
}
})
}
export async function getLatestDepsClj(githubAuth: string): Promise<string> {
core.debug('=== Fetch latest version of deps clj')
const res = await client.getJson<{tag_name: string}>(
`https://api.github.com/repos/borkdude/deps.clj/releases/latest`,
{Authorization: githubAuth}
)
const result = res.result?.tag_name?.replace(/^v/, '')
if (result) {
return result
}
throw new Error(`Can't obtain latest deps.clj version`)
}