Skip to content

Commit bd2585f

Browse files
committed
fix: fix version extraction from the compiler input
1 parent 2d4c04a commit bd2585f

File tree

6 files changed

+56
-17
lines changed

6 files changed

+56
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ jobs:
115115
compiler:
116116
- llvm
117117
- gcc
118-
# you can specify the version after `-` like `llvm-11`.
118+
# you can specify the version after `-` like `llvm-13.0.0`.
119119
steps:
120120
- name: Setup Cpp
121121
uses: aminya/setup-cpp@v1

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: "Amin Yahyaabadi"
44

55
inputs:
66
compiler:
7-
description: "The compiler to use and its optinal version separated by - e.g. llvm-11"
7+
description: "The compiler to use and its optional version separated by - e.g. llvm-13.0.0"
88
required: false
99
architecture:
1010
description: "The CPU architecture"

dist/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/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.

src/__tests__/main.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { getCompilerInfo } from "../main"
2+
3+
jest.setTimeout(300000)
4+
describe("getCompilerInfo", () => {
5+
it("version will be undefined if not provided", () => {
6+
const { compiler, version } = getCompilerInfo("llvm")
7+
expect(compiler).toBe("llvm")
8+
expect(version).toBeUndefined()
9+
})
10+
11+
it("extracts version", () => {
12+
const { compiler, version } = getCompilerInfo("llvm-12.0.0")
13+
expect(compiler).toBe("llvm")
14+
expect(version).toBe("12.0.0")
15+
})
16+
17+
it("finds a version even if not semver", () => {
18+
const { compiler, version } = getCompilerInfo("llvm-12")
19+
expect(compiler).toBe("llvm")
20+
expect(version).toBe("12")
21+
})
22+
})

src/main.ts

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import untildify from "untildify"
1818
import { isCI } from "./utils/env/isci"
1919

2020
import semverValid from "semver/functions/valid"
21+
// import semverCoerce from "semver/functions/coerce"
2122
import { getVersion } from "./default_versions"
2223
import { setupGcc } from "./gcc/gcc"
2324
import { InstallationInfo } from "./utils/setup/setupBin"
2425
import { error, success } from "./utils/io/io"
2526
import { setupVcpkg } from "./vcpkg/vcpkg"
2627
import { join } from "path"
28+
import { warning } from "@actions/core"
2729

2830
/** The setup functions */
2931
const setups = {
@@ -135,18 +137,7 @@ export async function main(args: string[]): Promise<number> {
135137
const maybeCompiler = opts.compiler
136138
try {
137139
if (maybeCompiler !== undefined) {
138-
// detecting the compiler version. Divide the given string by `-` and use the second element as the version
139-
const compilerAndMaybeVersion = maybeCompiler.split("-")
140-
const compiler = compilerAndMaybeVersion[0]
141-
let version: string | undefined
142-
if (1 in compilerAndMaybeVersion) {
143-
const maybeVersion = compilerAndMaybeVersion[1]
144-
if (semverValid(maybeVersion) !== null) {
145-
version = maybeVersion
146-
} else {
147-
error(`Invalid version ${maybeVersion} used for the compiler. Using the default version...`)
148-
}
149-
}
140+
const { compiler, version } = getCompilerInfo(maybeCompiler)
150141

151142
// install the compiler. We allow some aliases for the compiler name
152143
switch (compiler) {
@@ -219,6 +210,32 @@ main(process.argv)
219210
process.exitCode = 1
220211
})
221212

213+
/** Detecting the compiler version. Divide the given string by `-` and use the second element as the version */
214+
export function getCompilerInfo(maybeCompiler: string) {
215+
const compilerAndMaybeVersion = maybeCompiler.split("-")
216+
const compiler = compilerAndMaybeVersion[0]
217+
if (1 in compilerAndMaybeVersion) {
218+
const maybeVersion = compilerAndMaybeVersion[1]
219+
if (semverValid(maybeVersion) !== null) {
220+
return { compiler, version: maybeVersion }
221+
} else {
222+
// version coercion
223+
// try {
224+
// // find the semver version of an integer
225+
// const coercedVersion = semverCoerce(maybeVersion)
226+
// if (coercedVersion !== null) {
227+
// return { compiler, version: coercedVersion.version }
228+
// }
229+
// } catch (err) {
230+
// // handled in the end
231+
// }
232+
warning(`Invalid semver version ${maybeVersion} used for the compiler.`)
233+
return { compiler, version: maybeVersion }
234+
}
235+
}
236+
return { compiler, version: undefined }
237+
}
238+
222239
function printHelp() {
223240
core.info(`
224241
setup_cpp [options]
@@ -228,7 +245,7 @@ Install all the tools required for building and testing C++/C projects.
228245
229246
--architecture\t the cpu architecture to install the tools for. By default it uses the current CPU architecture.
230247
--compiler\t the <compiler> to install.
231-
\t You can specify the version instead of specifying just the name e.g: --compiler 'llvm-11'
248+
\t You can specify the version instead of specifying just the name e.g: --compiler 'llvm-13.0.0'
232249
233250
--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"
234251

0 commit comments

Comments
 (0)