Skip to content

Commit e686b42

Browse files
authored
Merge pull request #426 from github/revert-425-fix-semver-comparison
Revert "Use semver package for semantic version comparison and precedence checking"
2 parents d924e9f + 9191873 commit e686b42

File tree

7 files changed

+266
-115
lines changed

7 files changed

+266
-115
lines changed

common/config/rush/pnpm-lock.yaml

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

extensions/ql-vscode/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,9 +590,7 @@
590590
"vscode-languageclient": "^6.1.3",
591591
"vscode-test-adapter-api": "~1.7.0",
592592
"vscode-test-adapter-util": "~0.7.0",
593-
"minimist": "~1.2.5",
594-
"semver": "~7.3.2",
595-
"@types/semver": "~7.2.0"
593+
"minimist": "~1.2.5"
596594
},
597595
"devDependencies": {
598596
"@types/chai": "^4.1.7",
Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,96 @@
1-
import * as semver from "semver";
21
import { runCodeQlCliCommand } from "./cli";
32
import { Logger } from "./logging";
43

54
/**
65
* Get the version of a CodeQL CLI.
76
*/
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> {
98
const output: string = await runCodeQlCliCommand(
109
codeQlPath,
1110
["version"],
1211
["--format=terse"],
1312
"Checking CodeQL version",
1413
logger
1514
);
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;
1796
}

0 commit comments

Comments
 (0)