package opam-format

  1. Overview
  2. Docs

Source file opamVariable.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
(**************************************************************************)
(*                                                                        *)
(*    Copyright 2012-2019 OCamlPro                                        *)
(*    Copyright 2012 INRIA                                                *)
(*                                                                        *)
(*  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.                   *)
(*                                                                        *)
(**************************************************************************)

include OpamStd.AbstractString

type variable = t

type variable_contents =
  | B of bool
  | S of string
  | L of string list

let string_of_variable_contents = function
  | B b -> string_of_bool b
  | S s -> s
  | L l -> String.concat " " l

let string str = S str

let bool b = B b

let int i = string (string_of_int i)

let dirname dir = string (OpamFilename.Dir.to_string dir)

module Full = struct

  type scope =
    | Global
    | Self
    | Package of OpamPackage.Name.t

  type t = {
    scope: scope;
    variable: variable;
  }

  let variable t = t.variable
  let scope t = t.scope
  let package ?self t = match t.scope with
    | Package p -> Some p
    | Self -> self
    | Global -> None

  let create package variable =
    let scope =
      if OpamPackage.Name.to_string package = "_" then Self
      else Package package
    in
    { scope; variable }

  (* Read the variables overridden through the environment *)
  let read_from_env v =
    let var_str = to_string (variable v) in
    let undash = OpamStd.String.map (function '-' -> '_' | c -> c) in
    let var_hook =
      match package v with
      | Some n ->
        Printf.sprintf "%s_%s" (undash (OpamPackage.Name.to_string n))
          (undash var_str)
      | None -> undash var_str
    in
    try match OpamStd.Env.get ("OPAMVAR_" ^ var_hook) with
      | "true"  | "1" -> Some (bool true)
      | "false" | "0" -> Some (bool false)
      | s             -> Some (string s)
    with Not_found -> None

  let global variable =
    { scope = Global; variable }

  let self variable =
    { scope = Self; variable }

  let is_global variable = match variable.scope with
    | Global -> true
    | Self | Package _ -> false

  let of_string s =
    match OpamStd.String.rcut_at s ':' with
    | None -> global (of_string s)
    | Some ("_",v) ->
      { scope = Self; variable = of_string v }
    | Some (p,v) ->
      create (OpamPackage.Name.of_string p) (of_string v)

  let to_string t =
    let prefix =
      match t.scope with
      | Global -> ""
      | Self -> "_:"
      | Package p -> OpamPackage.Name.to_string p ^ ":"
    in
    prefix ^ to_string t.variable

  let to_json x =
    `String (to_string x)

  let of_json = function
    | `String s -> (try Some (of_string s) with _ -> None)
    | _ -> None

  let compare {scope; variable} fv =
    match scope, fv.scope with
    | Global, Global | Self, Self ->
      String.compare variable fv.variable
    | Package n, Package m ->
      let package = OpamPackage.Name.compare n m in
      if package <> 0 then package else
        String.compare variable fv.variable
    | Global, _ | _, Self -> 1
    | Self, _ | _, Global -> -1

  let equal f g = compare f g = 0

  module O = struct
    type tmp = t
    type t = tmp
    let compare = compare
    let to_string = to_string
    let to_json = to_json
    let of_json = of_json
  end

  module Set = OpamStd.Set.Make(O)

  module Map = OpamStd.Map.Make(O)

end
OCaml

Innovation. Community. Security.