package opam_bin_lib

  1. Overview
  2. Docs

Source file commandUninstall.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(**************************************************************************)
(*                                                                        *)
(*    Copyright 2020 OCamlPro & Origin Labs                               *)
(*                                                                        *)
(*  All rights reserved. This file is distributed under the terms of the  *)
(*  GNU Lesser General Public License version 2.1, with the special       *)
(*  exception on linking described in the file LICENSE.                   *)
(*                                                                        *)
(**************************************************************************)

open Ezcmd.TYPES

(*
wrap-build-commands:
  ["%{hooks}%/sandbox.sh" "build"] {os = "linux" | os = "macos"}
wrap-install-commands:
  ["%{hooks}%/sandbox.sh" "install"] {os = "linux" | os = "macos"}
*)

let remove_opam_hooks file_contents =
  let rec iter items found rev =
    match items with
    | [] ->
      if found then begin
        Printf.eprintf "Found hooks to remove\n%!";
        Some ( List.rev rev )
      end
      else begin
        Printf.eprintf "No hooks to remove\n%!";
        None
      end
    | item :: items ->
      match item with
      | OpamParserTypes.Variable (_, name, _) ->
        begin
          match name with
          | "pre-build-commands"
          | "wrap-build-commands"
          | "pre-install-commands"
          | "wrap-install-commands"
          | "post-install-commands"
          | "pre-remove-commands"
            -> iter items true rev
          | _ -> iter items found ( item :: rev )
        end
      | _ ->
        iter items found ( item :: rev )
  in
  iter file_contents false []

let action () =
  Misc.change_opam_config (fun file_contents ->
      let file_contents = match remove_opam_hooks file_contents with
          None -> file_contents
        | Some file_contents -> file_contents
      in
      Printf.eprintf "Restoring sandboxing hooks\n%!";
      Some (
        List.rev @@
        Misc.opam_variable "wrap-build-commands"
          "%s"
          {|
  ["%{hooks}%/sandbox.sh" "build"] {os = "linux" | os = "macos"}
|} ::
        Misc.opam_variable "wrap-install-commands"
          "%s"
          {|
  ["%{hooks}%/sandbox.sh" "build"] {os = "linux" | os = "macos"}
|} ::
        List.rev file_contents
      )
    );
  ()

let cmd = Arg.{
  cmd_name = "uninstall" ;
  cmd_action = action ;
  cmd_args = [
    [ "restore" ], Unit (fun () ->
        Misc.restore_opam_config ();
        exit 0;
      ), Ezcmd.info "Restore the previous opam config file before install/uninstall";
  ];
  cmd_man = [];
  cmd_doc = "un-install from opam config";
}