Source file opam.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
(**************************************************************************)
(*                                                                        *)
(*    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 EzCompat
open Ez_opam_file.V1
open Types
open Ez_file.V1
open EzFile.OP

module OpamParser = struct
  module FullPos = struct
    let value_from_string s f =
      try OpamParser.FullPos.value_from_string s f
      with exn ->
        Printf.eprintf "Error with [%s]:\n" s;
        raise exn
  end
end

let dev_repo p =
  match Misc.dev_repo p with
  | Some s -> Some (Printf.sprintf "git+%s.git" s)
  | None -> None

let opam_of_project kind package =
  let p = package.project in
  let open OpamParserTypes.FullPos in
  let filename = "opam" in
  let pos = { filename ; start = 0,0 ; stop = 0, 0 } in
  let elem pelem = { pelem ; pos } in
  let string s = elem ( String s ) in
  let list v = elem ( List ( elem v ))  in
  let var s v = elem ( Variable ( elem s, v)) in
  let var_string s v = var s ( string v ) in
  let var_list s v = var s ( list v ) in
  let optionals = ref [] in
  let add_optional_string s = function
    | None -> ()
    | Some v -> optionals := var_string s v :: !optionals
  in
  add_optional_string "homepage" (Misc.homepage p);
  add_optional_string "doc" (Misc.doc_gen p);
  add_optional_string "bug-reports" (Misc.bug_reports p);
  add_optional_string "dev-repo" (dev_repo p);
  add_optional_string "tags"
    ( match p.github_organization with
      | None -> None
      | Some github_organization ->
          Some (Printf.sprintf "org:%s" github_organization) );

  let build_commands =
    match kind, package.kind with
    | Deps, _
    | _, Virtual -> []
    | _ ->
        [
          var "build"
            (
              OpamParser.FullPos.value_from_string
                (Printf.sprintf "%s%s%s%s"
                   {|
[
  ["dune" "subst"] {dev}
  ["sh" "-c" "./scripts/before.sh build '%{name}%'" ]
  ["dune" "build" "-p" name "-j" jobs "@install"
|}
                   (if
                     match StringMap.find "no-opam-test" package.p_fields with
                     | exception Not_found -> false
                     | "false" | "no" -> false
                     | _ -> true
                    then
                      ""
                    else
                      {|"@runtest" {with-test}|}
                   )
                   (if
                     match StringMap.find "no-opam-doc" package.p_fields with
                     | exception Not_found -> false
                     | "false" | "no" -> false
                     | _ -> true
                    then
                      ""
                    else
                      {|"@doc" {with-doc}|}
                   )
                   {|
  ]
  ["sh" "-c" "./scripts/after.sh build '%{name}%'" ]
]
|})
                filename
            );
          var "install"
            (
              OpamParser.FullPos.value_from_string
                {|
[
  ["sh" "-c" "./scripts/before.sh install '%{name}%'" ]
]
|} filename
            );
        ]
  in
  let depend_of_dep (name, d) =
    let b = Buffer.create 100 in
    Printf.bprintf b {| "%s" { |} name;
    List.iteri
      (fun i version ->
         if i > 0 then Printf.bprintf b "& ";
         match version with
         | Version -> Printf.bprintf b "= version"
         | NoVersion -> ()
         | Semantic (major, minor, fix) ->
             Printf.bprintf b {|>= "%d.%d.%d" & < "%d.0.0" |} major minor fix
               (major + 1)
         | Lt version -> Printf.bprintf b {| < "%s" |} version
         | Le version -> Printf.bprintf b {| <= "%s" |} version
         | Eq version -> Printf.bprintf b {| = "%s" |} version
         | Ge version -> Printf.bprintf b {| >= "%s" |} version
         | Gt version -> Printf.bprintf b {| > "%s" |} version)
      d.depversions;
    if d.deptest then Printf.bprintf b " with-test ";
    if d.depdoc then Printf.bprintf b " with-doc ";
    Printf.bprintf b "}\n";
    (*  Printf.eprintf "parse: %s\n%!" s; *)
    OpamParser.FullPos.value_from_string (Buffer.contents b) filename
  in
  let depends =
    match kind with
    | ProgramPart ->
        [ var "depends" (
              list
                [ OpamParser.FullPos.value_from_string
                    (Printf.sprintf
                       {|
                                "%s" { = version }
|}
                       (Misc.package_lib package))
                    filename
                ] )]
    | Single
    | LibraryPart
    | Deps ->
        let initial_deps =
          match package.kind with
          | Virtual ->
              begin
                match StringMap.find "gen-opam" package.p_fields with
                | exception _ -> []
                | s -> match String.lowercase s with
                  | "all" ->
                      List.map (fun pp ->
                          OpamParser.FullPos.value_from_string
                            ( if package.p_version = pp.p_version then
                                Printf.sprintf
                                  {| "%s" { = version } |} pp.name
                              else
                                Printf.sprintf
                                  {| "%s" { = %S } |} pp.name
                                  (Misc.p_version pp )
                            )
                            filename ;
                        )
                        (List.filter (fun pp -> package != pp
                                     )
                           p.packages)
                  | "some" -> []
                  | _ -> []
              end
          | _ -> [
              OpamParser.FullPos.value_from_string
                (Printf.sprintf {| "ocaml" { >= "%s" } |} p.min_edition)
                filename ;
              OpamParser.FullPos.value_from_string
                (Printf.sprintf {| "dune" { >= "%s" } |}
                  (* We insert here the infimum version computed internally instead of copying
                     the user given specification, if any. This is not a problem since the infimum
                     meets the given criterias by definition. It also helps opam by giving him
                     less constraints so it can be seen as optimization. *)
                   package.project.dune_version)
                filename
            ]
        in
        let alldeps =
          (Misc.p_dependencies package) @ (Misc.p_tools package)
        in
        let depends, depopts =
          List.partition (fun  ( _, d ) -> not d.depopt) alldeps
        in
        let depends = List.map  depend_of_dep depends in
        let depopts = List.map  depend_of_dep depopts in
        [ var "depends" (list ( initial_deps @ depends) ) ]
        @ ( match depopts with
            | [] -> []
            | _ -> [ var "depopts" ( list depopts )])
  in
  let file_contents =
    [ var_string "opam-version" "2.0";
      var_string "name"
        ( match kind with
          | LibraryPart -> Misc.package_lib package
          | Single
          | ProgramPart ->
              package.name
          | Deps -> package.name );
      var_string "version" (Misc.p_version package);
      var_string "license" (License.name p);
      var_string "synopsis"
        ( match kind with
          | LibraryPart -> Misc.p_synopsis package ^ " (library)"
          | Deps -> Misc.p_synopsis package
          | Single
          | ProgramPart ->
              Misc.p_synopsis package );
      var_string "description" (Misc.p_description package);
      var_list "authors" (List.map string (Misc.p_authors package));
      var_list "maintainer" (List.map string p.authors)
    ]
    @ List.rev !optionals @ build_commands @ depends
  in
  let f = { file_contents; file_name = filename } in
  let s = OpamPrinter.FullPos.opamfile f in
  String.concat "\n"
    ( [ "# This file was generated by `drom` from `drom.toml`.";
        "# Do not modify, or add to the `skip` field of `drom.toml`.";
        s
      ]
      @
      let s = Subst.package_paren ("", package) "opam-trailer" in
      if s = "" then
        []
      else
        [ s ] )

let () = Unix.putenv "OPAMCLI" "2.0"

let exec ?(y = false) cmd args =
  Misc.call
    (Array.of_list
       ( [ "opam" ] @ cmd
       @ ( if y then
           [ "-y" ]
         else
           [] )
       @ args ))

let init ?y ?switch ?edition () =
  let opam_root = Globals.opam_root () in

  if not (Sys.file_exists opam_root) then
    let args =
      match switch with
      | None -> [ "--bare" ]
      | Some switch -> [ "--comp"; switch ]
    in
    exec ?y [ "init" ] args
  else
    match switch with
    | None -> ()
    | Some switch ->
      if Filename.is_relative switch then
        if not (Sys.file_exists (opam_root // switch)) then
          exec ?y [ "switch"; "create" ]
            ( match edition with
            | None -> [ switch ]
            | Some edition -> [ switch; edition ] )

let run ?y ?error ?switch ?edition cmd args =
  init ?y ?switch ?edition ();
  match error with
  | None -> exec ?y cmd args
  | Some error -> ( try exec ?y cmd args with exn -> error := Some exn )