Debian Codegraph @

vendor/opam/src/core/opamCoreConfig.ml

raw
1 (**************************************************************************)
2 (* *)
3 (* Copyright 2015-2018 OCamlPro *)
4 (* *)
5 (* All rights reserved. This file is distributed under the terms of the *)
6 (* GNU Lesser General Public License version 2.1, with the special *)
7 (* exception on linking described in the file LICENSE. *)
8 (* *)
9 (**************************************************************************)
10
11 module E = struct
12
13 type OpamStd.Config.E.t +=
14 | COLOR of OpamStd.Config.when_ option
15 | CONFIRMLEVEL of OpamStd.Config.answer option
16 | DEBUG of int option
17 | DEBUGSECTIONS of OpamStd.Config.sections option
18 | ERRLOGLEN of int option
19 | KEEPLOGS of bool option
20 | LOGS of string option
21 | MERGEOUT of bool option
22 | NO of bool option
23 | PRECISETRACKING of bool option
24 | SAFE of bool option
25 | STATUSLINE of OpamStd.Config.when_ option
26 | UTF8 of OpamStd.Config.when_ext option
27 | UTF8MSGS of bool option
28 | VERBOSE of OpamStd.Config.level option
29 | YES of bool option
30
31 open OpamStd.Config.E
32 let color = value (function COLOR c -> c | _ -> None)
33 let confirmlevel = value (function CONFIRMLEVEL c -> c | _ -> None)
34 let debug = value (function DEBUG i -> i | _ -> None)
35 let debugsections = value (function DEBUGSECTIONS s -> s | _ -> None)
36 let errloglen = value (function ERRLOGLEN i -> i | _ -> None)
37 let keeplogs = value (function KEEPLOGS b -> b | _ -> None)
38 let logs = value (function LOGS s -> s | _ -> None)
39 let mergeout = value (function MERGEOUT b -> b | _ -> None)
40 let no = value (function NO b -> b | _ -> None)
41 let precisetracking = value (function PRECISETRACKING b -> b | _ -> None)
42 let safe = value (function SAFE b -> b | _ -> None)
43 let statusline = value (function STATUSLINE c -> c | _ -> None)
44 let utf8 = value (function UTF8 c -> c | _ -> None)
45 let utf8msgs = value (function UTF8MSGS b -> b | _ -> None)
46 let verbose = value (function VERBOSE l -> l | _ -> None)
47 let yes = value (function YES b -> b | _ -> None)
48
49 end
50
51 type t = {
52 debug_level: int;
53 debug_sections: OpamStd.Config.sections;
54 verbose_level: OpamStd.Config.level;
55 color: OpamStd.Config.when_;
56 utf8: OpamStd.Config.when_ext;
57 disp_status_line: OpamStd.Config.when_;
58 confirm_level: [ OpamStd.Config.answer | `undefined ];
59 yes: bool option;
60 safe_mode: bool;
61 log_dir: string;
62 keep_log_dir: bool;
63 errlog_length: int;
64 merged_output: bool;
65 precise_tracking: bool;
66 cygbin: string option;
67 set: bool;
68 }
69
70 type 'a options_fun =
71 ?debug_level:int ->
72 ?debug_sections:OpamStd.Config.sections ->
73 ?verbose_level:OpamStd.Config.level ->
74 ?color:OpamStd.Config.when_ ->
75 ?utf8:OpamStd.Config.when_ext ->
76 ?disp_status_line:OpamStd.Config.when_ ->
77 ?confirm_level:OpamStd.Config.answer ->
78 ?yes:bool option ->
79 ?safe_mode:bool ->
80 ?log_dir:string ->
81 ?keep_log_dir:bool ->
82 ?errlog_length:int ->
83 ?merged_output:bool ->
84 ?precise_tracking:bool ->
85 ?cygbin:string ->
86 'a
87
88 let default = {
89 debug_level = 0;
90 debug_sections = OpamStd.String.Map.empty;
91 verbose_level = 0;
92 color = `Auto;
93 utf8 = `Auto;
94 disp_status_line = `Auto;
95 confirm_level = `undefined;
96 yes = None;
97 safe_mode = false;
98 log_dir =
99 (let user = try Unix.getlogin() with Unix.Unix_error _ -> "xxx" in
100 let base = Printf.sprintf "opam-%s-%d" user (OpamStubs.getpid()) in
101 Filename.(concat (get_temp_dir_name ()) base));
102 keep_log_dir = false;
103 errlog_length = 12;
104 merged_output = true;
105 precise_tracking = false;
106 cygbin = None;
107 set = false;
108 }
109
110 let setk k t
111 ?debug_level
112 ?debug_sections
113 ?verbose_level
114 ?color
115 ?utf8
116 ?disp_status_line
117 ?confirm_level
118 ?yes
119 ?safe_mode
120 ?log_dir
121 ?keep_log_dir
122 ?errlog_length
123 ?merged_output
124 ?precise_tracking
125 ?cygbin
126 =
127 let (+) x opt = match opt with Some x -> x | None -> x in
128 k {
129 debug_level = t.debug_level + debug_level;
130 debug_sections = t.debug_sections + debug_sections;
131 verbose_level = t.verbose_level + verbose_level;
132 color = t.color + color;
133 utf8 = t.utf8 + utf8;
134 disp_status_line = t.disp_status_line + disp_status_line;
135 confirm_level =
136 (match confirm_level with
137 | Some (`all_yes|`all_no|`ask|`unsafe_yes as c) -> c
138 | None -> t.confirm_level);
139 yes = t.yes + yes;
140 safe_mode = t.safe_mode + safe_mode;
141 log_dir = t.log_dir + log_dir;
142 keep_log_dir = t.keep_log_dir + keep_log_dir;
143 errlog_length = t.errlog_length + errlog_length;
144 merged_output = t.merged_output + merged_output;
145 precise_tracking = t.precise_tracking + precise_tracking;
146 cygbin = (match cygbin with Some _ -> cygbin | None -> t.cygbin);
147 set = true;
148 }
149
150 let set t = setk (fun x () -> x) t
151
152 (* Global configuration reference *)
153
154 let r = ref default
155
156 let update ?noop:_ = setk (fun cfg () -> r := cfg) !r
157
158 let initk k =
159 let open OpamStd in
160 let utf8 = Option.Op.(
161 E.utf8 () ++
162 (E.utf8msgs () >>= function
163 | true -> Some `Extended
164 | false -> None)
165 ) in
166 let yes =
167 match E.yes (), E.no () with
168 | Some true, _ -> Some (Some true)
169 | _, Some true -> Some (Some false)
170 | _, _ -> None
171 in
172 (setk (setk (fun c -> r := c; k)) !r)
173 ?debug_level:(E.debug ())
174 ?debug_sections:(E.debugsections ())
175 ?verbose_level:(E.verbose ())
176 ?color:(E.color ())
177 ?utf8
178 ?disp_status_line:(E.statusline ())
179 ?confirm_level:(E.confirmlevel ())
180 ?yes
181 ?safe_mode:(E.safe ())
182 ?log_dir:(E.logs ())
183 ?keep_log_dir:(E.keeplogs ())
184 ?errlog_length:(E.errloglen ())
185 ?merged_output:(E.mergeout ())
186 ?precise_tracking:(E.precisetracking ())
187 ?cygbin:None
188
189 let init ?noop:_ = initk (fun () -> ())
190
191 let answer () =
192 match !r.confirm_level, !r.yes with
193 | #OpamStd.Config.answer as c, _ -> c
194 | _, Some true -> `all_yes
195 | _, Some false -> `all_no
196 | _ -> `ask
197
198 let answer_is =
199 let answer = lazy (answer ()) in
200 fun a -> Lazy.force answer = a
201
202 let answer_is_yes () =
203 match answer () with
204 | #OpamStd.Config.yes_answer -> true
205 | _ -> false
206
207 let developer = not (String.equal OpamCoreConfigDeveloper.value "")