@@ -2,14 +2,15 @@ import * as fetch from "node-fetch";
22import * as fs from "fs-extra" ;
33import * as os from "os" ;
44import * as path from "path" ;
5+ import * as semver from "semver" ;
56import * as unzipper from "unzipper" ;
67import * as url from "url" ;
78import { ExtensionContext , Event } from "vscode" ;
89import { DistributionConfig } from "./config" ;
910import { InvocationRateLimiter , InvocationRateLimiterResultKind , showAndLogErrorMessage } from "./helpers" ;
1011import { logger } from "./logging" ;
1112import * as helpers from "./helpers" ;
12- import { getCodeQlCliVersion , tryParseVersionString , Version } from "./cli-version" ;
13+ import { getCodeQlCliVersion } from "./cli-version" ;
1314
1415/**
1516 * distribution.ts
@@ -41,9 +42,7 @@ const DEFAULT_DISTRIBUTION_REPOSITORY_NAME = "codeql-cli-binaries";
4142 */
4243export const DEFAULT_DISTRIBUTION_VERSION_CONSTRAINT : VersionConstraint = {
4344 description : "2.*.*" ,
44- isVersionCompatible : ( v : Version ) => {
45- return v . majorVersion === 2 && v . minorVersion >= 0 ;
46- }
45+ isVersionCompatible : ( v : semver . SemVer ) => semver . satisfies ( v , "2.x" )
4746} ;
4847
4948export interface DistributionProvider {
@@ -403,20 +402,16 @@ export class ReleasesApiConsumer {
403402 return false ;
404403 }
405404
406- const version = tryParseVersionString ( release . tag_name ) ;
407- if ( version === undefined || ! versionConstraint . isVersionCompatible ( version ) ) {
408- return false ;
409- }
410-
411- return true ;
405+ const version = semver . parse ( release . tag_name ) ;
406+ return version !== null && versionConstraint . isVersionCompatible ( version ) ;
412407 } ) ;
413- // tryParseVersionString must succeed due to the previous filtering step
408+ // Tag names must all be parsable to semvers due to the previous filtering step.
414409 const latestRelease = compatibleReleases . sort ( ( a , b ) => {
415- const versionComparison = versionCompare ( tryParseVersionString ( b . tag_name ) ! , tryParseVersionString ( a . tag_name ) ! ) ;
416- if ( versionComparison = == 0 ) {
417- return b . created_at . localeCompare ( a . created_at ) ;
410+ const versionComparison = semver . compare ( semver . parse ( b . tag_name ) ! , semver . parse ( a . tag_name ) ! ) ;
411+ if ( versionComparison ! == 0 ) {
412+ return versionComparison ;
418413 }
419- return versionComparison ;
414+ return b . created_at . localeCompare ( a . created_at , "en-US" ) ;
420415 } ) [ 0 ] ;
421416 if ( latestRelease === undefined ) {
422417 throw new Error ( "No compatible CodeQL CLI releases were found. " +
@@ -516,29 +511,6 @@ export async function extractZipArchive(archivePath: string, outPath: string): P
516511 } ) ) ;
517512}
518513
519- /**
520- * Comparison of semantic versions.
521- *
522- * Returns a positive number if a is greater than b.
523- * Returns 0 if a equals b.
524- * Returns a negative number if a is less than b.
525- */
526- export function versionCompare ( a : Version , b : Version ) : number {
527- if ( a . majorVersion !== b . majorVersion ) {
528- return a . majorVersion - b . majorVersion ;
529- }
530- if ( a . minorVersion !== b . minorVersion ) {
531- return a . minorVersion - b . minorVersion ;
532- }
533- if ( a . patchVersion !== b . patchVersion ) {
534- return a . patchVersion - b . patchVersion ;
535- }
536- if ( a . prereleaseVersion !== undefined && b . prereleaseVersion !== undefined ) {
537- return a . prereleaseVersion . localeCompare ( b . prereleaseVersion ) ;
538- }
539- return 0 ;
540- }
541-
542514function codeQlLauncherName ( ) : string {
543515 return ( os . platform ( ) === "win32" ) ? "codeql.exe" : "codeql" ;
544516}
@@ -571,7 +543,7 @@ export type FindDistributionResult =
571543interface CompatibleDistributionResult {
572544 codeQlPath : string ;
573545 kind : FindDistributionResultKind . CompatibleDistribution ;
574- version : Version ;
546+ version : semver . SemVer ;
575547}
576548
577549interface UnknownCompatibilityDistributionResult {
@@ -582,7 +554,7 @@ interface UnknownCompatibilityDistributionResult {
582554interface IncompatibleDistributionResult {
583555 codeQlPath : string ;
584556 kind : FindDistributionResultKind . IncompatibleDistribution ;
585- version : Version ;
557+ version : semver . SemVer ;
586558}
587559
588560interface NoDistributionResult {
@@ -722,7 +694,7 @@ export interface GithubRelease {
722694 assets : GithubReleaseAsset [ ] ;
723695
724696 /**
725- * The creation date of the release on GitHub.
697+ * The creation date of the release on GitHub, in ISO 8601 format .
726698 */
727699 created_at : string ;
728700
@@ -769,7 +741,7 @@ export interface GithubReleaseAsset {
769741
770742interface VersionConstraint {
771743 description : string ;
772- isVersionCompatible ( version : Version ) : boolean ;
744+ isVersionCompatible ( version : semver . SemVer ) : boolean ;
773745}
774746
775747export class GithubApiError extends Error {
0 commit comments