package drom_lib

  1. Overview
  2. Docs

Source file genVersion.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
(**************************************************************************)
(*                                                                        *)
(*    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.                   *)
(*                                                                        *)
(**************************************************************************)


let file package _file =
  Printf.sprintf
{|#!/usr/bin/env ocaml
;;
#load "unix.cma"

let query cmd =
  let chan = Unix.open_process_in cmd in
  try
    let out = input_line chan in
     if Unix.close_process_in chan = Unix.WEXITED 0 then
       Some out
     else None
   with End_of_file -> None

let commit_hash = query "git show -s --pretty=format:%%H"
let commit_date = query "git show -s --pretty=format:%%ci"
let version = %S

let string_option = function
  | None -> "None"
  | Some s -> Printf.sprintf "Some %%S" s

let () =
  Format.printf "@[<v>";
  Format.printf "let version = %%S@," version;
  Format.printf
    "let commit_hash = %%s@," (string_option commit_hash);
  Format.printf
    "let commit_date = %%s@," (string_option commit_date);
  Format.printf "@]@.";
  ()
|}
  (Misc.p_version package)

let dune _package file =
  Printf.sprintf
  {|
(rule
    (targets %s)
    (deps (:script %st) package.toml)
    (action (with-stdout-to %%{targets} (run %%{ocaml} unix.cma %%{script}))))
|}
  file
  file