Debian Codegraph @

vendor/opam/src/format/opamVariable.ml

raw
1 (**************************************************************************)
2 (* *)
3 (* Copyright 2012-2019 OCamlPro *)
4 (* Copyright 2012 INRIA *)
5 (* *)
6 (* All rights reserved. This file is distributed under the terms of the *)
7 (* GNU Lesser General Public License version 2.1, with the special *)
8 (* exception on linking described in the file LICENSE. *)
9 (* *)
10 (**************************************************************************)
11
12 include OpamStd.AbstractString
13
14 type variable = t
15
16 type variable_contents =
17 | B of bool
18 | S of string
19 | L of string list
20
21 let string_of_variable_contents = function
22 | B b -> string_of_bool b
23 | S s -> s
24 | L l -> String.concat " " l
25
26 let string str = S str
27
28 let bool b = B b
29
30 let int i = string (string_of_int i)
31
32 let dirname dir = string (OpamFilename.Dir.to_string dir)
33
34 module Full = struct
35
36 type scope =
37 | Global
38 | Self
39 | Package of OpamPackage.Name.t
40
41 type t = {
42 scope: scope;
43 variable: variable;
44 }
45
46 let variable t = t.variable
47 let scope t = t.scope
48 let package ?self t = match t.scope with
49 | Package p -> Some p
50 | Self -> self
51 | Global -> None
52
53 let create package variable =
54 let scope =
55 if OpamPackage.Name.to_string package = "_" then Self
56 else Package package
57 in
58 { scope; variable }
59
60 (* Read the variables overridden through the environment *)
61 let read_from_env v =
62 let var_str = to_string (variable v) in
63 let undash = OpamStd.String.map (function '-' -> '_' | c -> c) in
64 let var_hook =
65 match package v with
66 | Some n ->
67 Printf.sprintf "%s_%s" (undash (OpamPackage.Name.to_string n))
68 (undash var_str)
69 | None -> undash var_str
70 in
71 try match OpamStd.Env.get ("OPAMVAR_" ^ var_hook) with
72 | "true" | "1" -> Some (bool true)
73 | "false" | "0" -> Some (bool false)
74 | s -> Some (string s)
75 with Not_found -> None
76
77 let global variable =
78 { scope = Global; variable }
79
80 let self variable =
81 { scope = Self; variable }
82
83 let is_global variable = match variable.scope with
84 | Global -> true
85 | Self | Package _ -> false
86
87 let of_string s =
88 match OpamStd.String.rcut_at s ':' with
89 | None -> global (of_string s)
90 | Some ("_",v) ->
91 { scope = Self; variable = of_string v }
92 | Some (p,v) ->
93 create (OpamPackage.Name.of_string p) (of_string v)
94
95 let to_string t =
96 let prefix =
97 match t.scope with
98 | Global -> ""
99 | Self -> "_:"
100 | Package p -> OpamPackage.Name.to_string p ^ ":"
101 in
102 prefix ^ to_string t.variable
103
104 let to_json x =
105 `String (to_string x)
106
107 let of_json = function
108 | `String s -> (try Some (of_string s) with _ -> None)
109 | _ -> None
110
111 let compare {scope; variable} fv =
112 match scope, fv.scope with
113 | Global, Global | Self, Self ->
114 String.compare variable fv.variable
115 | Package n, Package m ->
116 let package = OpamPackage.Name.compare n m in
117 if package <> 0 then package else
118 String.compare variable fv.variable
119 | Global, _ | _, Self -> 1
120 | Self, _ | _, Global -> -1
121
122 let equal f g = compare f g = 0
123
124 module O = struct
125 type tmp = t
126 type t = tmp
127 let compare = compare
128 let to_string = to_string
129 let to_json = to_json
130 let of_json = of_json
131 end
132
133 module Set = OpamStd.Set.Make(O)
134
135 module Map = OpamStd.Map.Make(O)
136
137 end