@@ -7,11 +7,46 @@ import { isWindows } from '../../../../common/utils/platformUtils';
77import { ShellScriptEditState , ShellSetupState , ShellStartupScriptProvider } from '../startupProvider' ;
88import { runCommand } from '../utils' ;
99
10+ import { getGlobalPersistentState } from '../../../../common/persistentState' ;
1011import { ShellConstants } from '../../../common/shellConstants' ;
1112import { hasStartupCode , insertStartupCode , removeStartupCode } from '../common/editUtils' ;
1213import { extractProfilePath , PROFILE_TAG_END , PROFILE_TAG_START } from '../common/shellUtils' ;
1314import { POWERSHELL_ENV_KEY , PWSH_SCRIPT_VERSION } from './pwshConstants' ;
1415
16+ const PWSH_PROFILE_PATH_CACHE_KEY = 'PWSH_PROFILE_PATH_CACHE' ;
17+ const PS5_PROFILE_PATH_CACHE_KEY = 'PS5_PROFILE_PATH_CACHE' ;
18+ let pwshProfilePath : string | undefined ;
19+ let ps5ProfilePath : string | undefined ;
20+ async function clearPwshCache ( shell : 'powershell' | 'pwsh' ) : Promise < void > {
21+ const global = await getGlobalPersistentState ( ) ;
22+ if ( shell === 'powershell' ) {
23+ ps5ProfilePath = undefined ;
24+ await global . clear ( [ PS5_PROFILE_PATH_CACHE_KEY ] ) ;
25+ } else {
26+ pwshProfilePath = undefined ;
27+ await global . clear ( [ PWSH_PROFILE_PATH_CACHE_KEY ] ) ;
28+ }
29+ }
30+
31+ async function setProfilePathCache ( shell : 'powershell' | 'pwsh' , profilePath : string ) : Promise < void > {
32+ const global = await getGlobalPersistentState ( ) ;
33+ if ( shell === 'powershell' ) {
34+ ps5ProfilePath = profilePath ;
35+ await global . set ( PS5_PROFILE_PATH_CACHE_KEY , profilePath ) ;
36+ } else {
37+ pwshProfilePath = profilePath ;
38+ await global . set ( PWSH_PROFILE_PATH_CACHE_KEY , profilePath ) ;
39+ }
40+ }
41+
42+ function getProfilePathCache ( shell : 'powershell' | 'pwsh' ) : string | undefined {
43+ if ( shell === 'powershell' ) {
44+ return ps5ProfilePath ;
45+ } else {
46+ return pwshProfilePath ;
47+ }
48+ }
49+
1550async function isPowerShellInstalled ( shell : string ) : Promise < boolean > {
1651 try {
1752 await which ( shell ) ;
@@ -23,6 +58,12 @@ async function isPowerShellInstalled(shell: string): Promise<boolean> {
2358}
2459
2560async function getProfileForShell ( shell : 'powershell' | 'pwsh' ) : Promise < string > {
61+ const cachedPath = getProfilePathCache ( shell ) ;
62+ if ( cachedPath ) {
63+ traceInfo ( `SHELL: ${ shell } profile path from cache: ${ cachedPath } ` ) ;
64+ return cachedPath ;
65+ }
66+
2667 try {
2768 const content = await runCommand (
2869 isWindows ( )
@@ -33,6 +74,7 @@ async function getProfileForShell(shell: 'powershell' | 'pwsh'): Promise<string>
3374 if ( content ) {
3475 const profilePath = extractProfilePath ( content ) ;
3576 if ( profilePath ) {
77+ setProfilePathCache ( shell , profilePath ) ;
3678 traceInfo ( `SHELL: ${ shell } profile found at: ${ profilePath } ` ) ;
3779 return profilePath ;
3880 }
@@ -146,9 +188,16 @@ async function removePowerShellStartup(shell: string, profile: string): Promise<
146188export class PowerShellClassicStartupProvider implements ShellStartupScriptProvider {
147189 public readonly name : string = 'PowerShell5' ;
148190 public readonly shellType : string = 'powershell' ;
191+ private _isInstalled : boolean | undefined ;
149192
193+ private async checkInstallation ( ) : Promise < boolean > {
194+ if ( this . _isInstalled === undefined ) {
195+ this . _isInstalled = await isPowerShellInstalled ( 'powershell' ) ;
196+ }
197+ return this . _isInstalled ;
198+ }
150199 async isSetup ( ) : Promise < ShellSetupState > {
151- const isInstalled = await isPowerShellInstalled ( 'powershell' ) ;
200+ const isInstalled = await this . checkInstallation ( ) ;
152201 if ( ! isInstalled ) {
153202 traceVerbose ( 'PowerShell is not installed' ) ;
154203 return ShellSetupState . NotInstalled ;
@@ -165,7 +214,7 @@ export class PowerShellClassicStartupProvider implements ShellStartupScriptProvi
165214 }
166215
167216 async setupScripts ( ) : Promise < ShellScriptEditState > {
168- const isInstalled = await isPowerShellInstalled ( 'powershell' ) ;
217+ const isInstalled = await this . checkInstallation ( ) ;
169218 if ( ! isInstalled ) {
170219 traceVerbose ( 'PowerShell is not installed' ) ;
171220 return ShellScriptEditState . NotInstalled ;
@@ -182,7 +231,7 @@ export class PowerShellClassicStartupProvider implements ShellStartupScriptProvi
182231 }
183232
184233 async teardownScripts ( ) : Promise < ShellScriptEditState > {
185- const isInstalled = await isPowerShellInstalled ( 'powershell' ) ;
234+ const isInstalled = await this . checkInstallation ( ) ;
186235 if ( ! isInstalled ) {
187236 traceVerbose ( 'PowerShell is not installed' ) ;
188237 return ShellScriptEditState . NotInstalled ;
@@ -197,14 +246,26 @@ export class PowerShellClassicStartupProvider implements ShellStartupScriptProvi
197246 }
198247 return ShellScriptEditState . NotEdited ;
199248 }
249+ async clearCache ( ) : Promise < void > {
250+ await clearPwshCache ( 'powershell' ) ;
251+ }
200252}
201253
202254export class PwshStartupProvider implements ShellStartupScriptProvider {
203255 public readonly name : string = 'PowerShell' ;
204256 public readonly shellType : string = ShellConstants . PWSH ;
205257
258+ private _isInstalled : boolean | undefined ;
259+
260+ private async checkInstallation ( ) : Promise < boolean > {
261+ if ( this . _isInstalled === undefined ) {
262+ this . _isInstalled = await isPowerShellInstalled ( 'pwsh' ) ;
263+ }
264+ return this . _isInstalled ;
265+ }
266+
206267 async isSetup ( ) : Promise < ShellSetupState > {
207- const isInstalled = await isPowerShellInstalled ( 'pwsh' ) ;
268+ const isInstalled = await this . checkInstallation ( ) ;
208269 if ( ! isInstalled ) {
209270 traceVerbose ( 'PowerShell is not installed' ) ;
210271 return ShellSetupState . NotInstalled ;
@@ -221,7 +282,7 @@ export class PwshStartupProvider implements ShellStartupScriptProvider {
221282 }
222283
223284 async setupScripts ( ) : Promise < ShellScriptEditState > {
224- const isInstalled = await isPowerShellInstalled ( 'pwsh' ) ;
285+ const isInstalled = await this . checkInstallation ( ) ;
225286 if ( ! isInstalled ) {
226287 traceVerbose ( 'PowerShell is not installed' ) ;
227288 return ShellScriptEditState . NotInstalled ;
@@ -238,7 +299,7 @@ export class PwshStartupProvider implements ShellStartupScriptProvider {
238299 }
239300
240301 async teardownScripts ( ) : Promise < ShellScriptEditState > {
241- const isInstalled = await isPowerShellInstalled ( 'pwsh' ) ;
302+ const isInstalled = await this . checkInstallation ( ) ;
242303 if ( ! isInstalled ) {
243304 traceVerbose ( 'PowerShell is not installed' ) ;
244305 return ShellScriptEditState . NotInstalled ;
@@ -253,4 +314,7 @@ export class PwshStartupProvider implements ShellStartupScriptProvider {
253314 }
254315 return ShellScriptEditState . NotEdited ;
255316 }
317+ async clearCache ( ) : Promise < void > {
318+ await clearPwshCache ( 'pwsh' ) ;
319+ }
256320}
0 commit comments