1+ import { getCondaName , getRDetailsFromMetaHistory , isCondaInstallation } from '../conda' ;
12import { getRDetailsFromPath } from './locator' ;
23import { RExecutableRegistry } from './registry' ;
4+ import { IExecutableDetails , ExecutableType } from './types' ;
35
4- export type ExecutableType = RExecutable ;
5- export type VirtualExecutableType = VirtualRExecutable ;
6-
7- export function isVirtual ( executable : RExecutable ) : executable is VirtualRExecutable {
6+ export function isVirtual ( executable : AbstractExecutable ) : executable is VirtualRExecutable {
87 return executable instanceof VirtualRExecutable ;
98}
109
@@ -16,12 +15,12 @@ export class RExecutableFactory {
1615 }
1716
1817 public create ( executablePath : string ) : ExecutableType {
19- const oldExec = [ ...this . registry . executables . values ( ) ] . find ( ( v ) => v . rBin === executablePath ) ;
20- if ( oldExec ) {
21- return oldExec ;
18+ const cachedExec = [ ...this . registry . executables . values ( ) ] . find ( ( v ) => v . rBin === executablePath ) ;
19+ if ( cachedExec ) {
20+ return cachedExec ;
2221 } else {
23- let executable : RExecutable ;
24- if ( new RegExp ( '\\.conda' ) ?. exec ( executablePath ) ) {
22+ let executable : AbstractExecutable ;
23+ if ( isCondaInstallation ( executablePath ) ) {
2524 executable = new VirtualRExecutable ( executablePath ) ;
2625 } else {
2726 executable = new RExecutable ( executablePath ) ;
@@ -32,18 +31,10 @@ export class RExecutableFactory {
3231 }
3332}
3433
35- class RExecutable {
36- private _rBin : string ;
37- private _rVersion : string ;
38- private _arch : string ;
39-
40- constructor ( bin_path : string ) {
41- const details = getRDetailsFromPath ( bin_path ) ;
42- this . _rBin = bin_path ;
43- this . _rVersion = details . version ;
44- this . _arch = details . arch ;
45- }
46-
34+ export abstract class AbstractExecutable {
35+ protected _rBin : string ;
36+ protected _rVersion : string ;
37+ protected _rArch : string ;
4738 public get rBin ( ) : string {
4839 return this . _rBin ;
4940 }
@@ -53,39 +44,53 @@ class RExecutable {
5344 }
5445
5546 public get rArch ( ) : string {
56- return this . _arch ;
47+ return this . _rArch ;
48+ }
49+ public abstract tooltip : string ;
50+ }
51+
52+
53+ export class RExecutable extends AbstractExecutable {
54+ constructor ( executablePath : string ) {
55+ super ( ) ;
56+ const details = getRDetailsFromPath ( executablePath ) ;
57+ this . _rBin = executablePath ;
58+ this . _rVersion = details . version ;
59+ this . _rArch = details . arch ;
5760 }
5861
5962 public get tooltip ( ) : string {
60- const versionString = this . rVersion ? ` ${ this . rVersion } ` : '' ;
61- const archString = this . rArch ? ` ${ this . rArch } ` : '' ;
62- return `R${ versionString } ${ archString } ` ;
63+ if ( this . rVersion && this . rArch ) {
64+ return `R ${ this . rVersion } ${ this . rArch } ` ;
65+ }
66+ return `$(error) R` ;
67+ }
68+
69+ protected getDetailsFromPath ( execPath : string ) : IExecutableDetails {
70+ return getRDetailsFromPath ( execPath ) ;
6371 }
6472}
6573
66- class VirtualRExecutable extends RExecutable {
74+ export class VirtualRExecutable extends AbstractExecutable {
6775 private _name : string ;
6876
69- constructor ( bin_path : string ) {
70- super ( bin_path ) ;
71- const reg = new RegExp ( '(?<=\\/envs\\/)(.*?)(?=\\/)' ) ;
72- this . _name = reg ?. exec ( this . rBin ) ?. [ 0 ] ?? '' ;
77+ constructor ( executablePath : string ) {
78+ super ( ) ;
79+ this . _name = getCondaName ( executablePath ) ;
80+ const details = getRDetailsFromMetaHistory ( executablePath ) ;
81+ this . _rBin = executablePath ;
82+ this . _rVersion = details ?. version ?? '' ;
83+ this . _rArch = details ?. arch ?? '' ;
7384 }
7485
7586 public get name ( ) : string {
7687 return this . _name ;
7788 }
7889
7990 public get tooltip ( ) : string {
80- return `${ this . name } (${ super . tooltip } )` ;
81- }
82-
83- // todo, hardcoded
84- public get activationCommand ( ) : string [ ] {
85- if ( this . name ) {
86- return [ 'activate' , this . name ] ;
87- } else {
88- return [ 'activate' ] ;
91+ if ( this . rVersion && this . rArch ) {
92+ return `${ this . name } (R ${ this . rVersion } ${ this . rArch } )` ;
8993 }
94+ return `$(error) ${ this . name } ` ;
9095 }
9196}
0 commit comments