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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
(*---------------------------------------------------------------------------
   Copyright (c) 2016 Daniel C. Bünzli. All rights reserved.
   Distributed under the ISC license, see terms at the end of the file.
   dune-release 1.6.0
  ---------------------------------------------------------------------------*)

open Bos_setup

module D = struct
  let fetch_head = "${fetch_head}"
end

let os_tool_env name os =
  let pre = match os with `Build_os -> "BUILD_OS_" | `Host_os -> "HOST_OS_" in
  pre ^ String.Ascii.uppercase name

let os_bin_dir_env = function
  | `Build_os -> "BUILD_OS_BIN"
  | `Host_os -> "HOST_OS_XBIN"

let os_suff_env = function
  | `Build_os -> "BUILD_OS_SUFF"
  | `Host_os -> "HOST_OS_SUFF"

let ocamlfindable name =
  match name with
  | ( "ocamlc" | "ocamlcp" | "ocamlmktop" | "ocamlopt" | "ocamldoc" | "ocamldep"
    | "ocamlmklib" | "ocamlbrowser" ) as tool ->
      let toolchain = Cmd.empty in
      Some Cmd.(v "ocamlfind" %% toolchain % tool)
  | _ -> None

let tool name os =
  match OS.Env.var (os_tool_env name os) with
  | Some cmd -> Cmd.v cmd
  | None -> (
      match OS.Env.var (os_bin_dir_env os) with
      | Some path -> Cmd.v Fpath.(to_string @@ (v path / name))
      | None -> (
          match OS.Env.var (os_suff_env os) with
          | Some suff -> Cmd.v (name ^ suff)
          | None -> (
              match ocamlfindable name with
              | Some cmd -> cmd
              | None -> Cmd.v name)))

let cmd = Cmd.of_list @@ Cmd.to_list @@ tool "opam" `Host_os

(* Publish *)

let shortest x =
  List.hd (List.sort (fun x y -> compare (String.length x) (String.length y)) x)

let prepare_package ~build_dir ~dry_run ~version vcs name =
  (* copy opam, descr and url files *)
  let dir = Fmt.str "%s.%a" name Version.pp version in
  let src = Fpath.(build_dir / dir) in
  let dst = Fpath.(v "packages" / name / dir) in
  let cp f =
    OS.File.exists Fpath.(src / f) >>= function
    | true ->
        Sos.cp ~dry_run ~rec_:false ~force:true
          ~src:Fpath.(src / f)
          ~dst:Fpath.(dst / f)
    | _ -> Ok ()
  in
  OS.Dir.exists src >>= fun exists ->
  (if exists then Ok ()
  else
    R.error_msgf
      "%a does not exist, did you run:\n  dune-release opam pkg -p %s\n"
      Fpath.pp src name)
  >>= fun () ->
  OS.Dir.create ~path:true dst >>= fun _ ->
  cp "opam" >>= fun () ->
  cp "url" >>= fun () ->
  cp "descr" >>= fun () ->
  (* git add *)
  Vcs.run_git_quiet vcs ~dry_run ~force:true Cmd.(v "add" % p dst)

let prepare ~dry_run ?msg ~local_repo ~remote_repo ~opam_repo ~version ~tag
    names =
  let msg =
    match msg with
    | None -> Ok Cmd.empty
    | Some msg ->
        OS.Dir.current () >>= fun cwd ->
        let file = Fpath.(cwd / "_build" / "submit-msg") in
        Sos.write_file ~dry_run ~force:true file msg >>| fun () ->
        Cmd.(v "--file" % p file)
  in
  msg >>= fun msg ->
  Sos.dir_exists ~dry_run Fpath.(local_repo / ".git") >>= fun exists ->
  (if exists then Ok ()
  else R.error_msgf "%a is not a valid Git repository." Fpath.pp local_repo)
  >>= fun () ->
  let git_for_repo r = Cmd.of_list (Cmd.to_list @@ Vcs.cmd r) in
  Vcs.get () >>= fun repo ->
  let git = git_for_repo repo in
  let upstream =
    let user, repo = opam_repo in
    Printf.sprintf "https://github.com/%s/%s.git" user repo
  in
  let remote_branch = "master" in
  let pkg = shortest names in
  let branch = Fmt.str "release-%s-%a" pkg Vcs.Tag.pp tag in
  let prepare_repo () =
    App_log.status (fun l ->
        l "Fetching %a" Text.Pp.url (upstream ^ "#" ^ remote_branch));
    Vcs.run_git_quiet repo ~dry_run ~force:true
      Cmd.(v "fetch" % upstream % remote_branch)
    >>= fun () ->
    Vcs.run_git_string repo ~dry_run ~force:true ~default:(Sos.out D.fetch_head)
      Cmd.(v "rev-parse" % "FETCH_HEAD")
    >>= fun id ->
    (* make a branch *)
    let delete_branch () =
      if not (Vcs.branch_exists ~dry_run:false repo branch) then Ok ()
      else
        match
          Vcs.run_git_quiet repo ~dry_run ~force:true
            Cmd.(v "checkout" % "master")
        with
        | Ok () ->
            Vcs.run_git_quiet repo ~dry_run ~force:true
              Cmd.(v "branch" % "-D" % branch)
        | Error _ ->
            let out = OS.Cmd.run_out Cmd.(git % "status") in
            OS.Cmd.out_lines out >>= fun (out, _) ->
            R.error_msgf "git checkout in %a failed:\n %s" Fpath.pp local_repo
              (String.concat ~sep:"\n" out)
    in
    delete_branch () >>= fun () ->
    App_log.status (fun l ->
        l "Checking out a local %a branch" Text.Pp.commit branch);
    Vcs.checkout repo ~dry_run:false ~branch ~commit_ish:(Commit_ish id)
  in
  let prepare_packages ~build_dir =
    Stdext.Result.List.iter
      ~f:(prepare_package ~build_dir ~dry_run ~version repo)
  in
  let commit_and_push () =
    Vcs.run_git_quiet repo ~dry_run Cmd.(v "commit" %% msg) >>= fun () ->
    App_log.status (fun l ->
        l "Pushing %a to %a" Text.Pp.commit branch Text.Pp.url remote_repo);
    Vcs.run_git_quiet repo ~dry_run
      Cmd.(v "push" % "--force" % remote_repo % branch % "--set-upstream")
  in
  OS.Dir.current () >>= fun cwd ->
  let build_dir = Fpath.(cwd / "_build") in
  Sos.with_dir ~dry_run local_repo
    (fun () ->
      prepare_repo () >>= fun () ->
      prepare_packages ~build_dir names >>= fun () ->
      commit_and_push () >>= fun () -> Ok branch)
    ()
  |> R.join

(* Files *)

module File = struct
  (* Try to compose with the OpamFile.OPAM API *)

  let id x = x
  let list f v = [ f v ]

  let field name field conv =
    (name, fun acc o -> String.Map.add name (conv (field o)) acc)

  let opt_field name field conv =
    ( name,
      fun acc o ->
        match field o with
        | None -> acc
        | Some v -> String.Map.add name (conv v) acc )

  let deps_conv d =
    let add_pkg acc (n, _) = OpamPackage.Name.to_string n :: acc in
    OpamFormula.fold_left add_pkg [] d

  let fields =
    [
      opt_field "name" OpamFile.OPAM.name_opt (list OpamPackage.Name.to_string);
      opt_field "version" OpamFile.OPAM.version_opt
        (list OpamPackage.Version.to_string);
      field "opam-version" OpamFile.OPAM.opam_version
        (list OpamVersion.to_string);
      field "available" OpamFile.OPAM.available (list OpamFilter.to_string);
      field "maintainer" OpamFile.OPAM.maintainer id;
      field "homepage" OpamFile.OPAM.homepage id;
      field "authors" OpamFile.OPAM.author id;
      field "license" OpamFile.OPAM.license id;
      field "doc" OpamFile.OPAM.doc id;
      field "tags" OpamFile.OPAM.tags id;
      field "bug-reports" OpamFile.OPAM.bug_reports id;
      opt_field "dev-repo" OpamFile.OPAM.dev_repo (list OpamUrl.to_string);
      field "depends" OpamFile.OPAM.depends deps_conv;
      field "depopts" OpamFile.OPAM.depopts deps_conv;
      opt_field "description" OpamFile.OPAM.descr_body (list id);
      opt_field "synopsis" OpamFile.OPAM.synopsis (list id);
    ]

  let fields ~dry_run file =
    if not (Sys.file_exists (Fpath.to_string file)) then
      R.error_msgf "Internal error: file %a not found" Fpath.pp file
    else
      let parse file =
        let file = OpamFilename.of_string (Fpath.to_string file) in
        let opam = OpamFile.OPAM.read (OpamFile.make file) in
        let known_fields =
          let add_field acc (_, field) = field acc opam in
          List.fold_left add_field String.Map.empty fields
        in
        (* FIXME add OpamFile.OPAM.extensions when supported *)
        known_fields
      in
      Logs.info (fun m -> m "Parsing opam file %a" Fpath.pp file);
      try Ok (parse file)
      with _ ->
        if dry_run then Ok String.Map.empty
        else
          (* Apparently in at least opam-lib 1.2.2, the error will be
             logged on stdout. *)
          R.error_msgf "%a: could not parse opam file" Fpath.pp file
end

module Descr = struct
  type t = string * string option

  let of_string s =
    match String.cuts ~sep:"\n" s with
    | [] -> assert false (* String.cuts never returns the empty list *)
    | [ synopsis ] | [ synopsis; "" ] -> Ok (synopsis, None)
    | synopsis :: descr -> Ok (synopsis, Some (String.concat ~sep:"\n" descr))

  let to_string = function
    | synopsis, None -> synopsis
    | synopsis, Some descr -> strf "%s\n%s" synopsis descr

  let of_readme ?flavour r =
    let parse_synopsis l =
      let error l = R.error_msgf "%S: can't extract opam synopsis" l in
      let ok s = Ok String.(Ascii.capitalize @@ String.Sub.to_string s) in
      let not_white c = not (Char.Ascii.is_white c) in
      let skip_non_white l = String.Sub.drop ~sat:not_white l in
      let skip_white l = String.Sub.drop ~sat:Char.Ascii.is_white l in
      let start = String.sub l |> skip_white |> skip_non_white |> skip_white in
      match String.Sub.head start with
      | None -> error l
      | Some c when Char.Ascii.is_letter c -> ok start
      | Some _ -> (
          (* Try to skip a separator. *)
          let start = start |> skip_non_white |> skip_white in
          match String.Sub.head start with
          | None -> error l
          | Some _ -> ok start)
    in
    let drop_line l =
      String.is_prefix ~affix:"Home page:" l
      || String.is_prefix ~affix:"Homepage:" l
      || String.is_prefix ~affix:"Contact:" l
      || String.is_prefix ~affix:"%%VERSION" l
    in
    let keep_line l = not (drop_line l) in
    match Text.head ?flavour r with
    | None -> R.error_msgf "Could not extract opam description."
    | Some (title, text) -> (
        let sep = "\n" in
        let title = Text.header_title ?flavour title in
        parse_synopsis title >>= fun synopsis ->
        Ok (String.cuts ~sep text) >>= fun text ->
        Ok (List.filter keep_line text) >>= function
        | [] | [ "" ] -> Ok (synopsis, None)
        | text -> Ok (synopsis, Some (String.concat ~sep text)))

  let of_readme_file file =
    let flavour = Text.flavour_of_fpath file in
    OS.File.read file
    >>= (fun text -> of_readme ?flavour text)
    |> R.reword_error_msg ~replace:true (fun m ->
           R.msgf "%a: %s" Fpath.pp file m)
end

module Url = struct
  let v ~uri ~file =
    let hash algo = OpamHash.compute ~kind:algo file in
    let checksum = List.map hash [ `SHA256; `SHA512 ] in
    let url = OpamUrl.parse uri in
    OpamFile.URL.create ~checksum url

  let with_distrib_file ~dry_run ~uri distrib_file =
    match OS.File.exists distrib_file with
    | Ok true ->
        let file = Fpath.to_string distrib_file in
        Ok (v ~uri ~file)
    | _ ->
        if dry_run then Ok OpamFile.URL.empty
        else OS.File.must_exist distrib_file >>= fun _ -> assert false
end

module Version = struct
  type t = V1_2_2 | V2

  let pp fs = function
    | V1_2_2 -> Format.fprintf fs "v1.2.2"
    | V2 -> Format.fprintf fs "v2"

  let equal v1 v2 =
    match (v1, v2) with V1_2_2, V1_2_2 | V2, V2 -> true | _ -> false

  let of_string v =
    if Bos_setup.String.is_prefix v ~affix:"1." then
      if String.equal v "1.2.2" then Ok V1_2_2
      else R.error_msgf "unsupported opam version: %S" v
    else
      match String.cut ~sep:"2." v with
      | Some ("", _) -> Ok V2
      | _ -> R.error_msgf "unsupported opam version: %S" v

  let cli () =
    match
      OS.Cmd.run_out Cmd.(cmd % "--version") |> OS.Cmd.out_string ~trim:true
    with
    | Ok (s, (_, `Exited 0)) ->
        of_string s >>= fun v ->
        if equal v V1_2_2 then
          App_log.unhappy (fun l -> l "%s" Deprecate.Opam_1_x.client_warning);
        Ok v
    | Ok (_, (_, s)) -> R.error_msgf "opam: %a" OS.Cmd.pp_status s
    | Error (`Msg e) -> R.error_msgf "opam: %s" e

  let cli = lazy (cli ())
end

(*---------------------------------------------------------------------------
   Copyright (c) 2016 Daniel C. Bünzli

   Permission to use, copy, modify, and/or distribute this software for any
   purpose with or without fee is hereby granted, provided that the above
   copyright notice and this permission notice appear in all copies.

   THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
   WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
   MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
   ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  ---------------------------------------------------------------------------*)