11using System ;
2+ using System . Collections . Generic ;
23using System . ComponentModel ;
34using System . IO ;
45using System . Linq ;
@@ -18,18 +19,21 @@ internal sealed class SwitchVersionCommand : AsyncCommand<SwitchVersionCommand.S
1819 private readonly IJavaInstallationsAdapter _javaInstallationsAdapter ;
1920 private readonly IPathAdapter _pathAdapter ;
2021 private readonly ILogger _logger ;
22+ private readonly IShellAdapter _shellAdapter ;
2123
2224 public SwitchVersionCommand (
2325 IJavaHomeAdapter javaHomeAdapter ,
2426 IJavaInstallationsAdapter javaInstallationsAdapter ,
2527 IPathAdapter pathAdapter ,
26- ILogger logger
28+ ILogger logger ,
29+ IShellAdapter shellAdapter
2730 )
2831 {
2932 _javaHomeAdapter = javaHomeAdapter ;
3033 _javaInstallationsAdapter = javaInstallationsAdapter ;
3134 _pathAdapter = pathAdapter ;
3235 _logger = logger ;
36+ _shellAdapter = shellAdapter ;
3337 }
3438
3539 [ UsedImplicitly ]
@@ -56,6 +60,8 @@ public override async Task<int> ExecuteAsync(CommandContext context, Settings se
5660 . AddChoices ( installations . Select ( x => x . Location ) . ToArray ( ) )
5761 ) ;
5862
63+ string javaHome = null ;
64+ string javaBin = null ;
5965 await AnsiConsole . Status ( )
6066 . StartAsync ( "Applying..." , async ctx =>
6167 {
@@ -66,20 +72,42 @@ await AnsiConsole.Status()
6672 ? EnvironmentScope . Machine
6773 : EnvironmentScope . User ;
6874
69- var javaHome = await _javaHomeAdapter . GetValue ( EnvironmentScope . Process ) ;
75+ javaHome = await _javaHomeAdapter . GetValue ( EnvironmentScope . Process ) ;
7076 var paths = ( await _pathAdapter . GetValue ( scope ) ) . ToList ( ) ;
7177 if ( ! string . IsNullOrEmpty ( javaHome ) )
7278 {
7379 paths = paths . Where ( x => ! x . StartsWith ( javaHome , StringComparison . OrdinalIgnoreCase ) ) . ToList ( ) ;
7480 }
7581
76- paths . Add ( Path . Combine ( selected , "bin" ) ) ;
82+ javaBin = Path . Combine ( selected , "bin" ) ;
83+ paths . Add ( javaBin ) ;
7784
7885 await _javaHomeAdapter . SetValue ( selected , scope ) ;
7986 await _pathAdapter . SetValue ( paths , scope ) ;
8087 } ) . ConfigureAwait ( false ) ;
8188
82- AnsiConsole . MarkupLine ( "[yellow]The environment has been modified. You need to refresh it.[/]" ) ;
89+ var shellType = _shellAdapter . GetShellType ( ) ;
90+ var refreshCommands = new List < string > ( ) ;
91+ switch ( shellType )
92+ {
93+ case ShellType . PowerShell :
94+ refreshCommands . Add ( $ "$env:JAVA_HOME=\" { javaHome } \" ") ;
95+ refreshCommands . Add ( $ "$env:PATH=\" { javaBin } { Path . PathSeparator } $($env:PATH)\" ") ;
96+ break ;
97+ case ShellType . CommandPrompt :
98+ refreshCommands . Add ( $ "set \" JAVA_HOME={ javaHome } \" ") ;
99+ refreshCommands . Add ( $ "set \" PATH={ javaBin } { Path . PathSeparator } %PATH%\" ") ;
100+ break ;
101+ }
102+
103+ AnsiConsole . MarkupLine ( refreshCommands . Count > 0
104+ ? "[yellow]The environment has been modified. Apply modifications:[/]"
105+ : "[yellow]The environment has been modified. You need to refresh it.[/]" ) ;
106+
107+ foreach ( var line in refreshCommands )
108+ {
109+ Console . WriteLine ( line ) ;
110+ }
83111 return 0 ;
84112 }
85113 }
0 commit comments