|
1 | | -import * as semver from "semver"; |
2 | 1 | import { runCodeQlCliCommand } from "./cli"; |
3 | 2 | import { Logger } from "./logging"; |
4 | 3 |
|
5 | 4 | /** |
6 | 5 | * Get the version of a CodeQL CLI. |
7 | 6 | */ |
8 | | -export async function getCodeQlCliVersion(codeQlPath: string, logger: Logger): Promise<semver.SemVer | undefined> { |
| 7 | +export async function getCodeQlCliVersion(codeQlPath: string, logger: Logger): Promise<Version | undefined> { |
9 | 8 | const output: string = await runCodeQlCliCommand( |
10 | 9 | codeQlPath, |
11 | 10 | ["version"], |
12 | 11 | ["--format=terse"], |
13 | 12 | "Checking CodeQL version", |
14 | 13 | logger |
15 | 14 | ); |
16 | | - return semver.parse(output.trim()) || undefined; |
| 15 | + return tryParseVersionString(output.trim()); |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Try to parse a version string, returning undefined if we can't parse it. |
| 20 | + * |
| 21 | + * Version strings must contain a major, minor, and patch version. They may optionally |
| 22 | + * start with "v" and may optionally contain some "tail" string after the major, minor, and |
| 23 | + * patch versions, for example as in `v2.1.0+baf5bff`. |
| 24 | + */ |
| 25 | +export function tryParseVersionString(versionString: string): Version | undefined { |
| 26 | + const match = versionString.match(versionRegex); |
| 27 | + if (match === null) { |
| 28 | + return undefined; |
| 29 | + } |
| 30 | + return { |
| 31 | + buildMetadata: match[5], |
| 32 | + majorVersion: Number.parseInt(match[1], 10), |
| 33 | + minorVersion: Number.parseInt(match[2], 10), |
| 34 | + patchVersion: Number.parseInt(match[3], 10), |
| 35 | + prereleaseVersion: match[4], |
| 36 | + rawString: versionString, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Regex for parsing semantic versions |
| 42 | + * |
| 43 | + * From the semver spec https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string |
| 44 | + */ |
| 45 | +const versionRegex = new RegExp(String.raw`^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)` + |
| 46 | + String.raw`(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?` + |
| 47 | + String.raw`(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`); |
| 48 | + |
| 49 | +/** |
| 50 | + * A version of the CodeQL CLI. |
| 51 | + */ |
| 52 | +export interface Version { |
| 53 | + /** |
| 54 | + * Build metadata |
| 55 | + * |
| 56 | + * For example, this will be `abcdef0` for version 2.1.0-alpha.1+abcdef0. |
| 57 | + * Build metadata must be ignored when comparing versions. |
| 58 | + */ |
| 59 | + buildMetadata: string | undefined; |
| 60 | + |
| 61 | + /** |
| 62 | + * Major version number |
| 63 | + * |
| 64 | + * For example, this will be `2` for version 2.1.0-alpha.1+abcdef0. |
| 65 | + */ |
| 66 | + majorVersion: number; |
| 67 | + |
| 68 | + /** |
| 69 | + * Minor version number |
| 70 | + * |
| 71 | + * For example, this will be `1` for version 2.1.0-alpha.1+abcdef0. |
| 72 | + */ |
| 73 | + minorVersion: number; |
| 74 | + |
| 75 | + /** |
| 76 | + * Patch version number |
| 77 | + * |
| 78 | + * For example, this will be `0` for version 2.1.0-alpha.1+abcdef0. |
| 79 | + */ |
| 80 | + patchVersion: number; |
| 81 | + |
| 82 | + /** |
| 83 | + * Prerelease version |
| 84 | + * |
| 85 | + * For example, this will be `alpha.1` for version 2.1.0-alpha.1+abcdef0. |
| 86 | + * The prerelease version must be considered when comparing versions. |
| 87 | + */ |
| 88 | + prereleaseVersion: string | undefined; |
| 89 | + |
| 90 | + /** |
| 91 | + * Raw version string |
| 92 | + * |
| 93 | + * For example, this will be `2.1.0-alpha.1+abcdef0` for version 2.1.0-alpha.1+abcdef0. |
| 94 | + */ |
| 95 | + rawString: string; |
17 | 96 | } |
0 commit comments