@@ -5,14 +5,19 @@ import { ShellScriptEditState, ShellSetupState, ShellStartupScriptProvider } fro
55import { traceError , traceInfo , traceVerbose } from '../../../../common/logging' ;
66import which from 'which' ;
77import { BASH_ENV_KEY , ZSH_ENV_KEY } from './bashConstants' ;
8+ import { ShellConstants } from '../../../common/shellConstants' ;
9+ import { hasStartupCode , insertStartupCode , removeStartupCode } from '../common/editUtils' ;
810
911async function isBashLikeInstalled ( ) : Promise < boolean > {
10- const result = await Promise . all ( [ which ( 'bash' , { nothrow : true } ) , which ( 'sh' , { nothrow : true } ) ] ) ;
12+ const result = await Promise . all ( [
13+ which ( ShellConstants . BASH , { nothrow : true } ) ,
14+ which ( ShellConstants . SH , { nothrow : true } ) ,
15+ ] ) ;
1116 return result . some ( ( r ) => r !== null ) ;
1217}
1318
1419async function isZshInstalled ( ) : Promise < boolean > {
15- const result = await which ( 'zsh' , { nothrow : true } ) ;
20+ const result = await which ( ShellConstants . ZSH , { nothrow : true } ) ;
1621 return result !== null ;
1722}
1823
@@ -44,23 +49,15 @@ const regionEnd = '# <<< vscode python';
4449
4550function getActivationContent ( key : string ) : string {
4651 const lineSep = '\n' ;
47-
48- return [
49- '' ,
50- '' ,
51- regionStart ,
52- `if [ -n "$${ key } " ] && [ "$TERM_PROGRAM" = "vscode" ]; then` ,
53- ` . "$${ key } "` ,
54- 'fi' ,
55- regionEnd ,
56- '' ,
57- ] . join ( lineSep ) ;
52+ return [ `if [ -n "$${ key } " ] && [ "$TERM_PROGRAM" = "vscode" ]; then` , ` . "$${ key } "` , 'fi' ] . join ( lineSep ) ;
5853}
5954
6055async function isStartupSetup ( profile : string , key : string ) : Promise < ShellSetupState > {
6156 if ( await fs . pathExists ( profile ) ) {
6257 const content = await fs . readFile ( profile , 'utf8' ) ;
63- return content . includes ( key ) ? ShellSetupState . Setup : ShellSetupState . NotSetup ;
58+ return hasStartupCode ( content , regionStart , regionEnd , [ key ] )
59+ ? ShellSetupState . Setup
60+ : ShellSetupState . NotSetup ;
6461 } else {
6562 return ShellSetupState . NotSetup ;
6663 }
@@ -70,23 +67,18 @@ async function setupStartup(profile: string, key: string, name: string): Promise
7067 const activationContent = getActivationContent ( key ) ;
7168
7269 try {
73- // Create profile directory if it doesn't exist
7470 await fs . mkdirp ( path . dirname ( profile ) ) ;
7571
76- // Create or update profile
7772 if ( ! ( await fs . pathExists ( profile ) ) ) {
78- // Create new profile with our content
7973 await fs . writeFile ( profile , activationContent ) ;
8074 traceInfo ( `SHELL: Created new ${ name } profile at: ${ profile } \n${ activationContent } ` ) ;
8175 } else {
82- // Update existing profile
8376 const content = await fs . readFile ( profile , 'utf8' ) ;
84- if ( ! content . includes ( key ) ) {
85- await fs . writeFile ( profile , `${ content } ${ activationContent } ` ) ;
86- traceInfo ( `SHELL: Updated existing ${ name } profile at: ${ profile } \n${ activationContent } ` ) ;
87- } else {
88- // Already contains our activation code
77+ if ( hasStartupCode ( content , regionStart , regionEnd , [ key ] ) ) {
8978 traceInfo ( `SHELL: ${ name } profile already contains activation code at: ${ profile } ` ) ;
79+ } else {
80+ await fs . appendFile ( profile , insertStartupCode ( content , regionStart , regionEnd , activationContent ) ) ;
81+ traceInfo ( `SHELL: Updated existing ${ name } profile at: ${ profile } \n${ activationContent } ` ) ;
9082 }
9183 }
9284 return true ;
@@ -99,14 +91,12 @@ async function setupStartup(profile: string, key: string, name: string): Promise
9991async function removeStartup ( profile : string , key : string ) : Promise < boolean > {
10092 if ( ! ( await fs . pathExists ( profile ) ) ) {
10193 return true ;
102- } // If the file doesn't exist, we're done. No need to remove anything. Return true to indicate success.
94+ }
95+
10396 try {
10497 const content = await fs . readFile ( profile , 'utf8' ) ;
105- if ( content . includes ( key ) ) {
106- // Use regex to remove the entire region including newlines
107- const pattern = new RegExp ( `${ regionStart } [\\s\\S]*?${ regionEnd } \\n?` , 'g' ) ;
108- const newContent = content . replace ( pattern , '' ) ;
109- await fs . writeFile ( profile , newContent ) ;
98+ if ( hasStartupCode ( content , regionStart , regionEnd , [ key ] ) ) {
99+ await fs . writeFile ( profile , removeStartupCode ( content , regionStart , regionEnd ) ) ;
110100 traceInfo ( `SHELL: Removed activation from profile at: ${ profile } ` ) ;
111101 } else {
112102 traceVerbose ( `Profile at ${ profile } does not contain activation code` ) ;
0 commit comments