Skip to content

Commit a9bb29b

Browse files
committed
fix: refactor main file into separate files
1 parent 1f515ad commit a9bb29b

14 files changed

+357
-302
lines changed

dist/node12/actions_python.e8fa8bb0.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.

dist/node12/setup-cpp.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/node12/setup-cpp.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.

dist/node16/actions_python.79f8ffa3.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.

dist/node16/setup-cpp.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/node16/setup-cpp.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.
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
1-
module.exports = {
1+
/** @typedef {import("jest")} jestConfig */
2+
const jestConfig = {
23
preset: "ts-jest/presets/js-with-ts-esm",
34
extensionsToTreatAsEsm: [".ts"],
45
transformIgnorePatterns: [], // transform everything
56
testEnvironment: "node",
67
testMatch: ["**/*.test.ts"],
78
testPathIgnorePatterns: ["<rootDir>/src/python/setup-python/"],
9+
// tsconfig
10+
transform: {
11+
"^.+\\.tsx?$": [
12+
"ts-jest",
13+
/** @type {import("ts-jest")} */
14+
{
15+
importHelpers: true,
16+
useESM: true,
17+
},
18+
],
19+
},
820
// coverage
921
collectCoverageFrom: ["src/**/*.{ts,tsx}"],
1022
coveragePathIgnorePatterns: ["assets", ".css.d.ts"],
1123
verbose: true,
1224
}
25+
26+
export default jestConfig

src/__tests__/main.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { syncVersions, getVersion } from "../versions/versions"
2-
import { getCompilerInfo, Inputs, parseArgs } from "../main"
2+
import { parseArgs } from "../cli-options"
3+
import { Inputs } from "../tool"
4+
import { getCompilerInfo } from "../compilers"
35

46
jest.setTimeout(300000)
57
describe("getCompilerInfo", () => {

src/cli-options.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { getInput } from "@actions/core"
2+
import { info } from "ci-log"
3+
import mri from "mri"
4+
import { InstallationInfo } from "./utils/setup/setupBin"
5+
import { Inputs, inputs } from "./tool"
6+
7+
export function parseArgs(args: string[]): Opts {
8+
return mri<Record<Inputs, string | undefined> & { help: boolean }>(args, {
9+
string: inputs,
10+
default: Object.fromEntries(inputs.map((inp) => [inp, maybeGetInput(inp)])),
11+
alias: { h: "help" },
12+
boolean: "help",
13+
})
14+
}
15+
16+
export function printHelp() {
17+
info(`
18+
setup-cpp [options]
19+
setup-cpp --compiler llvm --cmake true --ninja true --ccache true --vcpkg true
20+
21+
Install all the tools required for building and testing C++/C projects.
22+
23+
--architecture\t the cpu architecture to install the tools for. By default it uses the current CPU architecture.
24+
--compiler\t the <compiler> to install.
25+
\t You can specify the version instead of specifying just the name e.g: --compiler 'llvm-13.0.0'
26+
27+
--tool_name\t pass "true" or pass the <version> you would like to install for this tool. e.g. --conan true or --conan "1.42.1"
28+
29+
All the available tools:
30+
--llvm
31+
--gcc
32+
--vcvarsall
33+
--cmake
34+
--ninja
35+
--vcpkg
36+
--bazel
37+
--meson
38+
--conan
39+
--make
40+
--task
41+
--ccache
42+
--sccache
43+
--cppcheck
44+
--clangformat
45+
--clangtidy
46+
--doxygen
47+
--gcovr
48+
--opencppcoverage
49+
--kcov
50+
--python
51+
--choco
52+
--brew
53+
--nala
54+
--sevenzip
55+
--graphviz
56+
--powershell
57+
`)
58+
}
59+
/** Get an object from github actions */
60+
61+
export function maybeGetInput(key: string) {
62+
const value = getInput(key.toLowerCase())
63+
if (value !== "false" && value !== "") {
64+
return value
65+
}
66+
return undefined // skip installation
67+
}
68+
export type Opts = mri.Argv<
69+
Record<Inputs, string | undefined> & {
70+
help: boolean
71+
}
72+
>
73+
74+
export function getSuccessMessage(tool: string, installationInfo: InstallationInfo | undefined | void) {
75+
let msg = `✅ ${tool} was installed successfully:`
76+
if (installationInfo === undefined) {
77+
return msg
78+
}
79+
if ("installDir" in installationInfo) {
80+
msg += `\n- The installation directory is ${installationInfo.installDir}`
81+
}
82+
if (installationInfo.binDir !== "") {
83+
msg += `\n- The binary directory is ${installationInfo.binDir}`
84+
}
85+
return msg
86+
}

src/compilers.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { endGroup, notice, startGroup } from "@actions/core"
2+
import { error, info } from "ci-log"
3+
import { join } from "path"
4+
import semverValid from "semver/functions/valid"
5+
import { getSuccessMessage } from "./cli-options"
6+
import { setupGcc } from "./gcc/gcc"
7+
import { activateGcovGCC, activateGcovLLVM } from "./gcovr/gcovr"
8+
import { setupLLVM } from "./llvm/llvm"
9+
import { setupMSVC } from "./msvc/msvc"
10+
import { addEnv } from "./utils/env/addEnv"
11+
import { getVersion } from "./versions/versions"
12+
13+
/** Detecting the compiler version. Divide the given string by `-` and use the second element as the version */
14+
export function getCompilerInfo(compilerAndVersion: string) {
15+
const compilerAndMaybeVersion = compilerAndVersion.split("-")
16+
const compiler = compilerAndMaybeVersion[0]
17+
if (1 in compilerAndMaybeVersion) {
18+
const maybeVersion = compilerAndMaybeVersion[1]
19+
if (semverValid(maybeVersion) !== null) {
20+
return { compiler, version: maybeVersion }
21+
} else {
22+
info(`Invalid semver version ${maybeVersion} used for the compiler.`)
23+
return { compiler, version: maybeVersion }
24+
}
25+
}
26+
return { compiler, version: undefined }
27+
}
28+
29+
/** Installing the specified compiler */
30+
export async function installCompiler(
31+
compilerAndVersion: string,
32+
osVersion: number[] | null,
33+
setupCppDir: string,
34+
arch: string,
35+
successMessages: string[],
36+
hasLLVM: boolean,
37+
errorMessages: string[]
38+
) {
39+
try {
40+
const { compiler, version } = getCompilerInfo(compilerAndVersion)
41+
42+
// install the compiler. We allow some aliases for the compiler name
43+
startGroup(`Installing ${compiler} ${version ?? ""}`)
44+
switch (compiler) {
45+
case "llvm":
46+
case "clang":
47+
case "clang++": {
48+
const installationInfo = await setupLLVM(
49+
getVersion("llvm", version, osVersion),
50+
join(setupCppDir, "llvm"),
51+
arch
52+
)
53+
54+
await activateGcovLLVM()
55+
56+
successMessages.push(getSuccessMessage("llvm", installationInfo))
57+
break
58+
}
59+
case "gcc":
60+
case "mingw":
61+
case "cygwin":
62+
case "msys": {
63+
const gccVersion = getVersion("gcc", version, osVersion)
64+
const installationInfo = await setupGcc(gccVersion, join(setupCppDir, "gcc"), arch)
65+
66+
if (hasLLVM) {
67+
// remove back the added CPPFLAGS of LLVM that include the LLVM headers
68+
await addEnv("CPPFLAGS", "")
69+
}
70+
71+
await activateGcovGCC(gccVersion)
72+
73+
successMessages.push(getSuccessMessage("gcc", installationInfo))
74+
break
75+
}
76+
case "cl":
77+
case "msvc":
78+
case "msbuild":
79+
case "vs":
80+
case "visualstudio":
81+
case "visualcpp":
82+
case "visualc++": {
83+
const installationInfo = await setupMSVC(
84+
getVersion("msvc", version, osVersion),
85+
join(setupCppDir, "msvc"),
86+
arch
87+
)
88+
89+
if (hasLLVM) {
90+
// remove the CPPFLAGS of LLVM that include the LLVM headers
91+
await addEnv("CPPFLAGS", "")
92+
}
93+
94+
successMessages.push(getSuccessMessage("msvc", installationInfo))
95+
break
96+
}
97+
case "appleclang":
98+
case "applellvm": {
99+
notice("Assuming apple-clang is already installed")
100+
await Promise.all([addEnv("CC", "clang"), addEnv("CXX", "clang++")])
101+
successMessages.push(getSuccessMessage("apple-clang", undefined))
102+
break
103+
}
104+
default: {
105+
errorMessages.push(`Unsupported compiler ${compiler}`)
106+
}
107+
}
108+
} catch (err) {
109+
error(err as string | Error)
110+
errorMessages.push(`Failed to install the ${compilerAndVersion}`)
111+
}
112+
113+
endGroup()
114+
}

0 commit comments

Comments
 (0)