Skip to content

Commit c654c98

Browse files
committed
feat: support using setup-cpp from CLI
1 parent 736836f commit c654c98

File tree

5 files changed

+104
-53
lines changed

5 files changed

+104
-53
lines changed

dist/main.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"@actions/io": "^1.1.1",
3939
"@actions/tool-cache": "^1.7.1",
4040
"hasha": "^5.2.2",
41+
"mri": "^1.2.0",
4142
"semver": "^7.3.5",
4243
"which": "^2.0.2"
4344
},
@@ -49,6 +50,7 @@
4950
"devDependencies": {
5051
"@types/cross-spawn": "^6.0.2",
5152
"@types/jest": "^27.0.1",
53+
"@types/mri": "^1.1.1",
5254
"@types/node": "^16.9.1",
5355
"@types/semver": "^7.3.8",
5456
"@types/which": "^2.0.1",

pnpm-lock.yaml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 87 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ import { setupMSVC } from "./msvc/msvc"
1313
import { setupNinja } from "./ninja/ninja"
1414
import { setupOpencppcoverage } from "./opencppcoverage/opencppcoverage"
1515
import { setupPython } from "./python/python"
16+
import mri from "mri"
1617

1718
import semverValid from "semver/functions/valid"
1819
import { getVersion } from "./default_versions"
1920
import { setupGcc } from "./gcc/gcc"
21+
import { InstallationInfo } from "./utils/setup/setupBin"
2022

23+
/** The setup functions */
2124
const setups = {
2225
cmake: setupCmake,
2326
ninja: setupNinja,
@@ -36,6 +39,7 @@ const setups = {
3639
msvc: setupMSVC,
3740
}
3841

42+
/** The tools that can be installed */
3943
const tools: Array<keyof typeof setups> = [
4044
"cmake",
4145
"ninja",
@@ -54,22 +58,35 @@ const tools: Array<keyof typeof setups> = [
5458
"msvc",
5559
]
5660

57-
type Inputs = "compiler" | keyof typeof setups
61+
/** The possible inputs to the program */
62+
type Inputs = keyof typeof setups | "compiler" | "architecture"
5863

59-
function maybeGetInput(key: Inputs) {
60-
const value = core.getInput(key.toLowerCase())
61-
if (value !== "false" && value !== "") {
62-
return value
63-
}
64-
return undefined // skip installation
65-
}
64+
// an array of possible inputs
65+
const inputs: Array<Inputs> = ["compiler", "architecture", ...tools]
66+
67+
/** The main entry function */
68+
export async function main(args: string[]): Promise<number> {
69+
// parse options using mri or github actions
70+
const opts = mri<Record<Inputs, string | undefined>>(args, {
71+
string: inputs,
72+
default: Object.fromEntries(inputs.map((inp) => [inp, maybeGetInput(inp)])),
73+
})
6674

67-
export async function main(): Promise<number> {
68-
const arch = core.getInput("architecture") || process.arch
75+
// cpu architecture
76+
const arch = opts.architecture ?? process.arch
77+
78+
// the installation dir for the tools that are downloaded directly
6979
const setupCppDir = process.env.SETUP_CPP_DIR ?? "~/setup_cpp"
80+
81+
// report messages
82+
const successMessages: string[] = []
83+
const errorMessages: string[] = []
84+
85+
// installing the specified compiler
86+
const maybeCompiler = opts.compiler
7087
try {
71-
const maybeCompiler = maybeGetInput("compiler")
7288
if (maybeCompiler !== undefined) {
89+
// detecting the compiler version. Devide the given string by `-` and use the second element as the version
7390
const compilerAndMaybeVersion = maybeCompiler.split("-")
7491
const compiler = compilerAndMaybeVersion[0]
7592
let version: string | undefined
@@ -82,6 +99,7 @@ export async function main(): Promise<number> {
8299
}
83100
}
84101

102+
// install the compiler. We allow some aliases for the compiler name
85103
switch (compiler) {
86104
case "llvm":
87105
case "clang":
@@ -107,61 +125,79 @@ export async function main(): Promise<number> {
107125
break
108126
}
109127
default: {
110-
core.error(`Unsupported compiler ${compiler}`)
128+
errorMessages.push(`Unsupported compiler ${compiler}`)
111129
}
112130
}
113131
}
132+
} catch (e) {
133+
errorMessages.push(`Failed to install the ${maybeCompiler}`)
134+
}
135+
136+
// installing the specified tools
137+
138+
// loop over the tools and run their setup function
139+
for (const tool of tools) {
140+
// get the version or "true" or undefined for this tool from the options
141+
const value = opts[tool]
142+
143+
// skip if undefined
144+
if (value !== undefined) {
145+
// get the setup function
146+
const setupFunction = setups[tool]
147+
148+
// runnig the setup function for this tool
149+
try {
150+
// eslint-disable-next-line no-await-in-loop
151+
const installationInfo = await setupFunction(getVersion(tool, value), setupCppDir, arch)
114152

115-
const toolsSucceeded: string[] = []
116-
const toolsErrored: string[] = []
117-
for (const tool of tools) {
118-
const version = maybeGetInput(tool)
119-
if (version !== undefined) {
120-
const setupFunction = setups[tool]
121-
122-
try {
123-
// eslint-disable-next-line no-await-in-loop
124-
const installationInfo = await setupFunction(getVersion(tool, version), setupCppDir, arch)
125-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
126-
if (installationInfo !== undefined) {
127-
let success = `${tool} was successfully installed`
128-
if ("installDir" in installationInfo) {
129-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
130-
// @ts-ignore typescript is confused about the existence of installDir
131-
success += `\nThe installation direcotry is ${installationInfo.installDir}`
132-
}
133-
if (installationInfo.binDir !== "") {
134-
success += `\nThe binary direcotry is ${installationInfo.binDir}`
135-
}
136-
toolsSucceeded.push(success)
137-
} else {
138-
toolsSucceeded.push(`${tool} was successfully installed`)
139-
}
140-
} catch (e) {
141-
toolsErrored.push(`${tool} failed to install`)
153+
// preparing a report string
154+
if (installationInfo !== undefined) {
155+
successMessages.push(getSuccessMessage(tool, installationInfo))
156+
} else {
157+
successMessages.push(`${tool} was successfully installed`)
142158
}
159+
} catch (e) {
160+
// push error message to the logger
161+
errorMessages.push(`${tool} failed to install`)
143162
}
144163
}
145-
146-
console.log("\n\n\n")
147-
toolsSucceeded.forEach((tool) => core.info(tool))
148-
toolsErrored.forEach((tool) => core.error(tool))
149-
} catch (err) {
150-
core.error(err as string | Error)
151-
core.setFailed("install-cpp failed")
152-
return 1
153164
}
154165

166+
// report the messages in the end
167+
console.log("\n\n\n")
168+
successMessages.forEach((tool) => core.info(tool))
169+
errorMessages.forEach((tool) => core.error(tool))
170+
155171
core.info("install-cpp finished")
156-
return 0
172+
return errorMessages.length === 0 ? 0 : 1 // exit with non-zero if any error message
157173
}
158-
159-
main()
174+
// Run main
175+
main(process.argv)
160176
.then((ret) => {
161177
process.exitCode = ret
162178
})
163179
.catch((error) => {
164-
core.error("main() failed!")
180+
core.error("main() panicked!")
165181
core.error(error as string | Error)
166182
process.exitCode = 1
167183
})
184+
185+
/** Get an object from github actions */
186+
function maybeGetInput(key: string) {
187+
const value = core.getInput(key.toLowerCase())
188+
if (value !== "false" && value !== "") {
189+
return value
190+
}
191+
return undefined // skip installation
192+
}
193+
194+
function getSuccessMessage(tool: string, installationInfo: InstallationInfo) {
195+
let success = `${tool} was successfully installed`
196+
if ("installDir" in installationInfo) {
197+
success += `\nThe installation direcotry is ${installationInfo.installDir}`
198+
}
199+
if (installationInfo.binDir !== "") {
200+
success += `\nThe binary direcotry is ${installationInfo.binDir}`
201+
}
202+
return success
203+
}

0 commit comments

Comments
 (0)