@@ -55,6 +55,16 @@ impl EnvShell {
5555 EnvShell :: Powershell => "env.ps1" ,
5656 }
5757 }
58+
59+ fn source_command ( self , env_dir : & vt_path:: AbsolutePath ) -> String {
60+ let path = env_dir. join ( self . env_file_name ( ) ) . to_string ( ) ;
61+ match self {
62+ Self :: Posix => format ! ( ". \" {}\" " , escape_posix_double_quoted_string( & path) ) ,
63+ Self :: Fish => format ! ( "source \" {}\" " , escape_fish_double_quoted_string( & path) ) ,
64+ Self :: Nu => format ! ( "source \" {}\" " , escape_nu_double_quoted_string( & path) ) ,
65+ Self :: Powershell => format ! ( ". '{}'" , escape_powershell_single_quoted_string( & path) ) ,
66+ }
67+ }
5868}
5969
6070/// Execute the setup command.
@@ -1116,7 +1126,7 @@ async fn create_env_files() -> Result<(), Error> {
11161126/// Inspect the profiles setup writes, without changing them or using a saved flag.
11171127fn has_configured_profile ( config : & vp_shared:: EnvConfig ) -> bool {
11181128 let shell = config. vp_shell . as_deref ( ) . map ( str:: to_ascii_lowercase) ;
1119- ALL_SHELL_PROFILES . iter ( ) . any ( | profile| {
1129+ for profile in ALL_SHELL_PROFILES {
11201130 let relevant = match shell. as_deref ( ) {
11211131 Some ( "zsh" ) => matches ! ( profile. root, ShellProfileRoot :: Zsh ) ,
11221132 Some ( "bash" ) => matches ! ( profile. root, ShellProfileRoot :: Home ) ,
@@ -1126,51 +1136,54 @@ fn has_configured_profile(config: &vp_shared::EnvConfig) -> bool {
11261136 _ => true ,
11271137 } ;
11281138 if !relevant {
1129- return false ;
1139+ continue ;
11301140 }
11311141 let path = resolve_profile_path ( profile, & config. user_home ) ;
1132- let Ok ( content) = std:: fs:: read_to_string ( path) else { return false } ;
1142+ let Ok ( content) = std:: fs:: read_to_string ( path) else { continue } ;
11331143 let env_file = config. dirs . config . join ( profile. env_file ) ;
11341144 let absolute = env_file. to_string ( ) ;
1135- let relative = render_home_relative_path ( env_file. as_path ( ) , config. user_home . as_path ( ) ) ;
1145+ let home_relative =
1146+ render_home_relative_path ( env_file. as_path ( ) , config. user_home . as_path ( ) ) ;
11361147 let escape = match profile. env_file {
11371148 "env.fish" => escape_fish_double_quoted_string,
11381149 "env.nu" => escape_nu_double_quoted_string,
11391150 _ => escape_posix_double_quoted_string,
11401151 } ;
1141- let relative = if profile. env_file == "env.nu" {
1142- escape ( & render_nu_path_ref ( & relative ) )
1152+ let escaped_relative = if profile. env_file == "env.nu" {
1153+ escape ( & render_nu_path_ref ( & home_relative ) )
11431154 } else {
1144- escape_home_relative_double_quoted_path ( & relative , escape)
1155+ escape_home_relative_double_quoted_path ( & home_relative , escape)
11451156 } ;
11461157 let mut arguments = vec ! [
11471158 format!( "\" {}\" " , escape( & absolute) ) ,
11481159 format!( "'{absolute}'" ) ,
11491160 absolute,
1150- format!( "\" {relative }\" " ) ,
1151- format!( "\" {}\" " , relative . replacen( "$HOME" , "${HOME}" , 1 ) ) ,
1152- render_nu_path_ref( & relative ) ,
1161+ format!( "\" {escaped_relative }\" " ) ,
1162+ format!( "\" {}\" " , escaped_relative . replacen( "$HOME" , "${HOME}" , 1 ) ) ,
1163+ render_nu_path_ref( & escaped_relative ) ,
11531164 ] ;
11541165 if profile. env_file == "env.nu" {
1155- let relative =
1156- render_home_relative_path ( env_file. as_path ( ) , config. user_home . as_path ( ) ) ;
1157- arguments. push ( format ! ( "'{}'" , render_nu_path_ref( & relative) ) ) ;
1166+ arguments. push ( format ! ( "'{}'" , render_nu_path_ref( & home_relative) ) ) ;
11581167 }
1159- content. lines ( ) . any ( |line| {
1168+ for line in content. lines ( ) {
11601169 let line = line. trim_start ( ) ;
11611170 let Some ( argument) = line. strip_prefix ( ". " ) . or_else ( || line. strip_prefix ( "source " ) )
11621171 else {
1163- return false ;
1172+ continue ;
11641173 } ;
1165- arguments. iter ( ) . any ( |expected| {
1166- argument. trim_start ( ) . strip_prefix ( expected) . is_some_and ( |rest| {
1174+ let argument = argument. trim_start ( ) ;
1175+ if arguments. iter ( ) . any ( |expected| {
1176+ argument. strip_prefix ( expected) . is_some_and ( |rest| {
11671177 rest. is_empty ( )
11681178 || rest. starts_with ( char:: is_whitespace)
11691179 || rest. starts_with ( ';' )
11701180 } )
1171- } )
1172- } )
1173- } )
1181+ } ) {
1182+ return true ;
1183+ }
1184+ }
1185+ }
1186+ false
11741187}
11751188
11761189/// Print instructions for sourcing the environment files and adding bin to `PATH`.
@@ -1181,36 +1194,21 @@ fn print_path_instructions(env_dir: &vt_path::AbsolutePath) {
11811194 // SHELL describes the login shell, which may differ from this terminal.
11821195 let shell = env. vp_shell . as_deref ( ) . and_then ( |s| s. parse ( ) . ok ( ) ) ;
11831196 let commands = [
1184- ( Shell :: Posix , "Bash/Zsh" , "env" ) ,
1185- ( Shell :: Fish , "Fish" , "env.fish" ) ,
1186- ( Shell :: NuShell , "Nushell" , "env.nu" ) ,
1187- ( Shell :: PowerShell , "PowerShell" , "env.ps1" ) ,
1197+ ( Shell :: Posix , "Bash/Zsh" , EnvShell :: Posix ) ,
1198+ ( Shell :: Fish , "Fish" , EnvShell :: Fish ) ,
1199+ ( Shell :: NuShell , "Nushell" , EnvShell :: Nu ) ,
1200+ ( Shell :: PowerShell , "PowerShell" , EnvShell :: Powershell ) ,
11881201 ] ;
11891202 if shell == Some ( Shell :: Cmd ) {
11901203 // cmd has no sourceable environment file. A new terminal inherits the
11911204 // persistent PATH written by setup.
11921205 output:: raw ( " In cmd.exe, open a new terminal to load the updated PATH." ) ;
11931206 } else {
1194- for ( kind, label, file ) in commands {
1207+ for ( kind, label, env_shell ) in commands {
11951208 if shell. is_some_and ( |s| s != kind) {
11961209 continue ;
11971210 }
1198- let path = env_dir. join ( file) . to_string ( ) ;
1199- let command = match kind {
1200- Shell :: Posix => {
1201- format ! ( ". \" {}\" " , escape_posix_double_quoted_string( & path) )
1202- }
1203- Shell :: Fish => {
1204- format ! ( "source \" {}\" " , escape_fish_double_quoted_string( & path) )
1205- }
1206- Shell :: NuShell => {
1207- format ! ( "source \" {}\" " , escape_nu_double_quoted_string( & path) )
1208- }
1209- Shell :: PowerShell => {
1210- format ! ( ". '{}'" , escape_powershell_single_quoted_string( & path) )
1211- }
1212- Shell :: Cmd => unreachable ! ( ) ,
1213- } ;
1211+ let command = env_shell. source_command ( env_dir) ;
12141212 if shell. is_some ( ) {
12151213 output:: raw ( & format ! ( " {command}" ) ) ;
12161214 } else {
@@ -1219,9 +1217,12 @@ fn print_path_instructions(env_dir: &vt_path::AbsolutePath) {
12191217 }
12201218 }
12211219 output:: raw ( "" ) ;
1222- if ( !cfg ! ( windows) || shell == Some ( Shell :: NuShell ) )
1223- && !matches ! ( shell, Some ( Shell :: Cmd | Shell :: PowerShell ) )
1224- {
1220+ let supports_profile_check = match shell {
1221+ Some ( Shell :: Cmd | Shell :: PowerShell ) => false ,
1222+ Some ( Shell :: NuShell ) => true ,
1223+ _ => !cfg ! ( windows) ,
1224+ } ;
1225+ if supports_profile_check {
12251226 if has_configured_profile ( & env) {
12261227 output:: raw ( " Or open a new terminal to load your configured shell profile." ) ;
12271228 } else {
0 commit comments