Skip to content

Commit

Permalink
Allow pre/post-session-commands hook to print to the console
Browse files Browse the repository at this point in the history
Closes ocaml#4359
  • Loading branch information
AltGr authored and rjbou committed Dec 17, 2020
1 parent 523f638 commit 2c9f202
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
4 changes: 4 additions & 0 deletions doc/pages/Manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,10 @@ for <span class="opam">opam</span>.
- `hooks`: the directory where scripts created using `opamrc`'s
[`init-scripts:`](#opamrcfield-init-scripts) field are created.

In addition, the output of these hooks is printed to the user, so
`post-session-commands` may be used to output extra information upon session
completion.

- <a id="configfield-repository-validation-command">`repository-validation-command: [ <term> { <filter> } ... ]`</a>:
defines a command to run on the upstream repositories to validate their
authenticity. When this is specified, and for repositories that define
Expand Down
21 changes: 15 additions & 6 deletions src/client/opamSolution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ let confirmation ?ask requested solution =
OpamPackage.Name.Set.equal requested solution_packages
|| OpamConsole.confirm "Do you want to continue?"

let run_hook_job t name ?(local=[]) w =
let run_hook_job t name ?(local=[]) ?(allow_stdout=false) w =
let shell_env = OpamEnv.get_full ~force_path:true t in
let mk_cmd = function
| cmd :: args ->
Expand All @@ -772,9 +772,18 @@ let run_hook_job t name ?(local=[]) w =
try Some (List.assoc v local)
with Not_found -> OpamPackageVar.resolve_switch t v
in
OpamProcess.Job.of_fun_list
(OpamStd.List.filter_map (fun cmd -> mk_cmd cmd)
(OpamFilter.commands env w))
let rec iter_commands = function
| [] -> Done None
| None :: commands -> iter_commands commands
| Some cmdf :: commands ->
let cmd = cmdf () in
cmd @@> fun result ->
if allow_stdout then
List.iter (OpamConsole.msg "%s\n") result.r_stdout;
if OpamProcess.is_success result then iter_commands commands
else Done (Some (cmd, result))
in
iter_commands (List.map mk_cmd (OpamFilter.commands env w))
@@+ function
| Some (cmd, _err) ->
OpamConsole.error "The %s hook failed at %S"
Expand Down Expand Up @@ -864,7 +873,7 @@ let apply ?ask t action ~requested ?add_roots ?(assume_built=false) solution =
var_def "depexts" (OpamStd.String.Set.elements depexts);
] in
run_job @@
run_hook_job t "pre-session" ~local
run_hook_job t "pre-session" ~local ~allow_stdout:true
(OpamFile.Wrappers.pre_session
(OpamFile.Config.wrappers t.switch_global.config))
in
Expand All @@ -885,7 +894,7 @@ let apply ?ask t action ~requested ?add_roots ?(assume_built=false) solution =
OpamVariable.Full.of_string "failure", B (not success);
] in
run_job @@
run_hook_job t "post-session" ~local
run_hook_job t "post-session" ~local ~allow_stdout:true
(OpamFile.Wrappers.post_session
(OpamFile.Config.wrappers t.switch_global.config))
in
Expand Down

0 comments on commit 2c9f202

Please sign in to comment.