@@ -3,6 +3,7 @@ import path from 'path';
33import { commands , ConfigurationTarget , l10n , window , workspace } from 'vscode' ;
44import { PythonCommandRunConfiguration , PythonEnvironment , PythonEnvironmentApi } from '../../api' ;
55import { traceLog , traceVerbose } from '../../common/logging' ;
6+ import { PEP440Version } from '../../common/utils/pep440Version' ;
67import { isWindows } from '../../common/utils/platformUtils' ;
78import { ShellConstants } from '../../features/common/shellConstants' ;
89import { getDefaultEnvManagerSetting , setDefaultEnvManagerBroken } from '../../features/settings/settingHelpers' ;
@@ -21,51 +22,6 @@ export function isNumber(obj: unknown): obj is number {
2122 return typeof obj === 'number' && ! isNaN ( obj ) ;
2223}
2324
24- export function shortVersion ( version : string ) : string {
25- const pattern = / ( \d ) \. ( \d + ) (?: \. ( \d + ) ? ) ? / gm;
26- const match = pattern . exec ( version ) ;
27- if ( match ) {
28- if ( match [ 3 ] ) {
29- return `${ match [ 1 ] } .${ match [ 2 ] } .${ match [ 3 ] } ` ;
30- }
31- return `${ match [ 1 ] } .${ match [ 2 ] } .x` ;
32- }
33- return version ;
34- }
35-
36- export function isGreater ( a : string | undefined , b : string | undefined ) : boolean {
37- if ( ! a && ! b ) {
38- return false ;
39- }
40- if ( ! a ) {
41- return false ;
42- }
43- if ( ! b ) {
44- return true ;
45- }
46-
47- try {
48- const aParts = a . split ( '.' ) ;
49- const bParts = b . split ( '.' ) ;
50- for ( let i = 0 ; i < aParts . length ; i ++ ) {
51- if ( i >= bParts . length ) {
52- return true ;
53- }
54- const aPart = parseInt ( aParts [ i ] , 10 ) ;
55- const bPart = parseInt ( bParts [ i ] , 10 ) ;
56- if ( aPart > bPart ) {
57- return true ;
58- }
59- if ( aPart < bPart ) {
60- return false ;
61- }
62- }
63- } catch {
64- return false ;
65- }
66- return false ;
67- }
68-
6925export function sortEnvironments ( collection : PythonEnvironment [ ] ) : PythonEnvironment [ ] {
7026 return collection . sort ( ( a , b ) => {
7127 // Environments with errors should be sorted to the end
@@ -76,7 +32,12 @@ export function sortEnvironments(collection: PythonEnvironment[]): PythonEnviron
7632 return - 1 ;
7733 }
7834 if ( a . version !== b . version ) {
79- return isGreater ( a . version , b . version ) ? - 1 : 1 ;
35+ const av = PEP440Version . parse ( a . version ) ;
36+ const bv = PEP440Version . parse ( b . version ) ;
37+ if ( av && bv ) {
38+ return PEP440Version . compare ( bv , av ) ; // descending
39+ }
40+ return a . version ? 1 : - 1 ;
8041 }
8142 const value = a . name . localeCompare ( b . name ) ;
8243 if ( value !== 0 ) {
@@ -96,7 +57,9 @@ export function getLatest(collection: PythonEnvironment[]): PythonEnvironment |
9657
9758 let latest = candidates [ 0 ] ;
9859 for ( const env of candidates ) {
99- if ( isGreater ( env . version , latest . version ) ) {
60+ const av = PEP440Version . parse ( env . version ) ;
61+ const bv = PEP440Version . parse ( latest . version ) ;
62+ if ( av && bv && PEP440Version . compare ( av , bv ) > 0 ) {
10063 latest = env ;
10164 }
10265 }
@@ -114,31 +77,6 @@ export function pathForGitBash(binPath: string): string {
11477 return isWindows ( ) ? binPath . replace ( / \\ / g, '/' ) . replace ( / ^ ( [ a - z A - Z ] ) : / , '/$1' ) : binPath ;
11578}
11679
117- /**
118- * Compares two semantic version strings. Support sonly simple 1.1.1 style versions.
119- * @param version1 First version
120- * @param version2 Second version
121- * @returns -1 if version1 < version2, 0 if equal, 1 if version1 > version2
122- */
123- export function compareVersions ( version1 : string , version2 : string ) : number {
124- const v1Parts = version1 . split ( '.' ) . map ( Number ) ;
125- const v2Parts = version2 . split ( '.' ) . map ( Number ) ;
126-
127- for ( let i = 0 ; i < Math . max ( v1Parts . length , v2Parts . length ) ; i ++ ) {
128- const v1Part = v1Parts [ i ] || 0 ;
129- const v2Part = v2Parts [ i ] || 0 ;
130-
131- if ( v1Part > v2Part ) {
132- return 1 ;
133- }
134- if ( v1Part < v2Part ) {
135- return - 1 ;
136- }
137- }
138-
139- return 0 ;
140- }
141-
14280function buildPwshActivationCommands ( ps1Path : string ) : PythonCommandRunConfiguration [ ] {
14381 const commands : PythonCommandRunConfiguration [ ] = [ ] ;
14482 if ( isWindows ( ) ) {
0 commit comments