generated from actions/javascript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
152 lines (113 loc) · 3.83 KB
/
index.js
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
const core = require('@actions/core');
const exec = require('@actions/exec');
const toolCache = require('@actions/tool-cache')
const io = require("@actions/io")
const schema = require("./schema.json")
const os = require('os')
const fs = require('fs')
const path = require('path')
const axios = require('axios')
function getDownloadArch(arch) {
if (arch === 'arm64') {
return '-arm64'
}
return ''
}
async function getDownloadUrl() {
let tag = "latest"
let response = null
try {
response = await axios({
url: "https://github.com/alexellis/arkade/releases/latest",
maxRedirects: 0,
method: "head",
timeout: 2500,
validateStatus: function (status) {
return status == 302
}
})
tag = response.headers.location;
} catch (error) {
throw error
}
// https://github.com/alexellis/arkade/releases/tag/0.9.17
//replace the first tag instance with "download"
tag = tag.replace("tag", "download")
let arch = getDownloadArch(os.arch())
core.info(`Arch: ${os.arch()}`)
return `${tag}/arkade${arch}`
}
// most @actions toolkit packages have async methods
async function run() {
try {
if(core.getInput("install-arkade") == "true") {
core.info("Installing arkade into tool cache")
let arkadeBinaryUrl = await getDownloadUrl()
core.info(`Download URL: ${arkadeBinaryUrl}`)
// Download arkade
const pathToDownload = await toolCache.downloadTool(arkadeBinaryUrl)
core.info("Downloaded arkade to: " + pathToDownload)
const cachePath = path.dirname(pathToDownload)
const arkadeFinalPath = path.join(cachePath, "arkade")
core.info(`Moving arkade from ${pathToDownload} to ${arkadeFinalPath}`)
await io.mv(pathToDownload, arkadeFinalPath)
fs.chmodSync(arkadeFinalPath, 0o755)
core.addPath(cachePath)
core.info(`Final path: ${arkadeFinalPath}`)
// Use arkade to download the various tools
const homedir = os.homedir()
const arkadePath = path.join(homedir, "/.arkade/bin")
core.info("Setting arkade's folder to: " + arkadePath)
// Add arkade's path to the PATH environment variable
core.addPath(arkadePath)
}
let added = 0
let tools = {}
let toolTimes = {}
for(let i = 0; i < schema.length; i++){
let tool = schema[i]
let inputName = tool;
let toolValue = core.getInput(inputName);
if(toolValue && toolValue.length) {
core.info(`Installing: ${tool} with ${toolValue}`)
tools[tool] = toolValue
let cmd = `arkade get --progress=false --quiet=true ${tool}`
if(toolValue != "latest") {
cmd += ` --version ${toolValue}`
}
core.debug(`Running: ${cmd}`)
let startTime = new Date().getTime()
await exec.exec(cmd)
toolTimes[tool] = new Date().getTime() - startTime
added++
}
}
if(core.getInput("print-summary") == "true") {
let rows = [ ]
rows.push([{data: 'Tool', header: true}, {data: 'Version', header: true}, {data: "Download time", header: true}])
let keys = Object.keys(tools)
keys = keys.sort()
for(let i in keys) {
rows.push([
keys[i],
tools[keys[i]],
(toolTimes[keys[i]]/1000).toFixed(2) + "s"
])
}
let addedStr = "tool"
if(added > 1) {
addedStr += "s"
}
await core.summary
.addHeading(`Arkade installed ${added} ${addedStr}`)
.addTable(rows)
.addLink('If you 💙 arkade, sponsor alexellis on GitHub!', 'https://github.com/sponsors/alexellis')
.write()
}
core.info("If you 💙 arkade, sponsor alexellis on GitHub https://github.com/sponsors/alexellis")
core.setOutput('tools', added+ " tools were installed");
} catch (error) {
core.setFailed(error.message);
}
}
run();