@@ -873,25 +873,21 @@ let env_hook_script shell =
873
873
(env_hook_script_base shell)
874
874
875
875
let source root shell f =
876
+ let if_exists_source: _ format = match shell with
877
+ | SH_csh -> " if ( -f %s ) source %s\n "
878
+ | SH_fish -> " test -r %s && source %s\n "
879
+ | SH_sh | SH_bash -> " test -r %s && . %s\n "
880
+ | SH_zsh -> " [[ -r %s ]] && source %s\n "
881
+ | SH_cmd -> " if exist \" %s\" call \" %s\"\n "
882
+ | SH_pwsh _ -> " if Test-Path \" %s\" { . \" %s\" }\n "
883
+ in
876
884
let fname = OpamFilename. prettify (OpamPath. init root // f) in
885
+ Printf. sprintf if_exists_source fname fname
886
+
887
+ let if_not_login_script shell t =
877
888
match shell with
878
- | SH_csh ->
879
- Printf. sprintf " if ( -f %s ) source %s >& /dev/null\n " fname fname
880
- | SH_fish ->
881
- if f = init_file shell then
882
- Printf. sprintf " if not status is-login; and test -r %s\n source %s\n end\n " fname fname
883
- else
884
- Printf. sprintf " source %s\n " fname
885
- | SH_sh | SH_bash ->
886
- Printf. sprintf " test -r %s && . %s > /dev/null 2> /dev/null || true\n "
887
- fname fname
888
- | SH_zsh ->
889
- Printf. sprintf " [[ ! -r %s ]] || source %s > /dev/null 2> /dev/null\n "
890
- fname fname
891
- | SH_cmd ->
892
- Printf. sprintf " if exist \" %s\" call \" %s\" >NUL 2>NUL\n " fname fname
893
- | SH_pwsh _ ->
894
- Printf. sprintf " . \" %s\" *> $null\n " fname
889
+ | SH_fish -> Printf. sprintf " if not status is-login\n %send\n " t
890
+ | _ -> t (* Seems this option is only needed for fish *)
895
891
896
892
let if_interactive_script shell t e =
897
893
let ielse else_opt = match else_opt with
@@ -1116,7 +1112,7 @@ let update_dot_profile root dot_profile shell =
1116
1112
# This section can be safely removed at any time if needed.\n \
1117
1113
%s\
1118
1114
# END opam configuration\n "
1119
- (source root shell init_file) in
1115
+ (if_not_login_script shell ( source root shell init_file) ) in
1120
1116
OpamFilename. write dot_profile (old_body ^ opam_section);
1121
1117
OpamConsole. msg " Added %d lines after line %d in %s.\n "
1122
1118
(count_lines opam_section - 1 ) (count_lines old_body) pretty_dot_profile
@@ -1168,17 +1164,21 @@ let setup
1168
1164
opam_root_msg;
1169
1165
begin match dot_profile with
1170
1166
| Some dot_profile ->
1167
+ let re = Re. compile (Re. char '\n' ) in
1168
+ let to_add = Re. replace_string re ~by: " \n " @@
1169
+ if_not_login_script shell @@
1170
+ source root shell @@ init_file shell
1171
+ in
1171
1172
OpamConsole. msg
1172
1173
" If you allow it to, this initialisation step will update\n \
1173
- \ your %s configuration by adding the following line to %s:\n \
1174
+ \ your %s configuration by adding the following line(s) to %s:\n \
1174
1175
\n \
1175
1176
\ %s\
1176
1177
\n \
1177
1178
\ Otherwise, every time"
1178
1179
(OpamConsole. colorise `bold (string_of_shell shell))
1179
1180
(OpamConsole. colorise `cyan @@ OpamFilename. prettify dot_profile)
1180
- (OpamConsole. colorise `bold @@ String. concat " \n " @@
1181
- String. split_on_char '\n' (source root shell (init_file shell)));
1181
+ (OpamConsole. colorise `bold to_add);
1182
1182
| None ->
1183
1183
OpamConsole. msg " When"
1184
1184
end ;
0 commit comments