Skip to content

Commit

Permalink
Always test if file exists before sourcing, in all shells
Browse files Browse the repository at this point in the history
  • Loading branch information
ElectreAAS committed Jan 24, 2024
1 parent dde6e34 commit 4d1387c
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/state/opamEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -873,25 +873,21 @@ let env_hook_script shell =
(env_hook_script_base shell)

let source root shell f =
let if_exists_source: _ format = match shell with
| SH_csh -> "if ( -f %s ) source %s\n"
| SH_fish -> "test -r %s && source %s\n"
| SH_sh | SH_bash -> "test -r %s && . %s\n"
| SH_zsh -> "[[ -r %s ]] && source %s\n"
| SH_cmd -> "if exist \"%s\" call \"%s\"\n"
| SH_pwsh _ -> "if Test-Path \"%s\" { . \"%s\" }\n"
in
let fname = OpamFilename.prettify (OpamPath.init root // f) in
Printf.sprintf if_exists_source fname fname

let if_not_login_script shell t =
match shell with
| SH_csh ->
Printf.sprintf "if ( -f %s ) source %s >& /dev/null\n" fname fname
| SH_fish ->
if f = init_file shell then
Printf.sprintf "if not status is-login; and test -r %s\n source %s\nend\n" fname fname
else
Printf.sprintf "source %s\n" fname
| SH_sh | SH_bash ->
Printf.sprintf "test -r %s && . %s > /dev/null 2> /dev/null || true\n"
fname fname
| SH_zsh ->
Printf.sprintf "[[ ! -r %s ]] || source %s > /dev/null 2> /dev/null\n"
fname fname
| SH_cmd ->
Printf.sprintf "if exist \"%s\" call \"%s\" >NUL 2>NUL\n" fname fname
| SH_pwsh _ ->
Printf.sprintf ". \"%s\" *> $null\n" fname
| SH_fish -> Printf.sprintf "if not status is-login\n %send\n" t
| _ -> t (* Seems this option is only needed for fish *)

let if_interactive_script shell t e =
let ielse else_opt = match else_opt with
Expand Down Expand Up @@ -1116,7 +1112,7 @@ let update_dot_profile root dot_profile shell =
# This section can be safely removed at any time if needed.\n\
%s\
# END opam configuration\n"
(source root shell init_file) in
(if_not_login_script shell (source root shell init_file)) in
OpamFilename.write dot_profile (old_body ^ opam_section);
OpamConsole.msg " Added %d lines after line %d in %s.\n"
(count_lines opam_section - 1) (count_lines old_body) pretty_dot_profile
Expand Down Expand Up @@ -1168,17 +1164,21 @@ let setup
opam_root_msg;
begin match dot_profile with
| Some dot_profile ->
let re = Re.compile (Re.char '\n') in
let to_add = Re.replace_string re ~by:"\n " @@
if_not_login_script shell @@
source root shell @@ init_file shell
in
OpamConsole.msg
"If you allow it to, this initialisation step will update\n\
\ your %s configuration by adding the following line to %s:\n\
\ your %s configuration by adding the following line(s) to %s:\n\
\n\
\ %s\
\n\
\ Otherwise, every time"
(OpamConsole.colorise `bold (string_of_shell shell))
(OpamConsole.colorise `cyan @@ OpamFilename.prettify dot_profile)
(OpamConsole.colorise `bold @@ String.concat "\n " @@
String.split_on_char '\n' (source root shell (init_file shell)));
(OpamConsole.colorise `bold to_add);
| None ->
OpamConsole.msg "When"
end;
Expand Down

0 comments on commit 4d1387c

Please sign in to comment.