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 Feb 22, 2024
1 parent a883ec0 commit 7073911
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions src/state/opamEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -874,35 +874,32 @@ let env_hook_script shell =
(env_hook_script_base shell)

let source root shell f =
let fname = OpamFilename.prettify (OpamPath.init root // f) in
let unix_transform ?using_backslashes () =
let cygpath = Lazy.force OpamSystem.get_cygpath_path_transform in
cygpath ~pathlist:false fname
|> OpamStd.Env.escape_single_quotes ?using_backslashes
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 sanitized_fname =
let fname = OpamFilename.prettify (OpamPath.init root // f) in
let unix_transform ?using_backslashes () =
let cygpath = Lazy.force OpamSystem.get_cygpath_path_transform in
cygpath ~pathlist:false fname
|> OpamStd.Env.escape_single_quotes ?using_backslashes
in
match shell with
| SH_csh | SH_sh | SH_bash | SH_zsh -> unix_transform ()
| SH_fish -> unix_transform ~using_backslashes:true ()
| SH_cmd | SH_pwsh _ -> fname
in
Printf.sprintf if_exists_source sanitized_fname sanitized_fname

let if_not_login_script shell t =
match shell with
| SH_csh ->
let fname = unix_transform () in
Printf.sprintf "if ( -f '%s' ) source '%s' >& /dev/null\n"
fname fname
| SH_fish ->
let fname = unix_transform ~using_backslashes:true () in
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 ->
let fname = unix_transform () in
Printf.sprintf "test -r '%s' && . '%s' > /dev/null 2> /dev/null || true\n"
fname fname
| SH_zsh ->
let fname = unix_transform () in
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 @@ -1127,7 +1124,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 @@ -1179,17 +1176,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 7073911

Please sign in to comment.