File: dist.ml

package info (click to toggle)
oasis 0.4.4-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,432 kB
  • ctags: 2,908
  • sloc: ml: 31,357; makefile: 166; sh: 75; ansic: 48; awk: 26
file content (199 lines) | stat: -rw-r--r-- 5,937 bytes parent folder | download | duplicates (2)
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
(******************************************************************************)
(* OASIS: architecture for building OCaml libraries and applications          *)
(*                                                                            *)
(* Copyright (C) 2011-2013, Sylvain Le Gall                                   *)
(* Copyright (C) 2008-2011, OCamlCore SARL                                    *)
(*                                                                            *)
(* This library is free software; you can redistribute it and/or modify it    *)
(* under the terms of the GNU Lesser General Public License as published by   *)
(* the Free Software Foundation; either version 2.1 of the License, or (at    *)
(* your option) any later version, with the OCaml static compilation          *)
(* exception.                                                                 *)
(*                                                                            *)
(* This library is distributed in the hope that it will be useful, but        *)
(* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY *)
(* or FITNESS FOR A PARTICULAR PURPOSE. See the file COPYING for more         *)
(* details.                                                                   *)
(*                                                                            *)
(* You should have received a copy of the GNU Lesser General Public License   *)
(* along with this library; if not, write to the Free Software Foundation,    *)
(* Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA              *)
(******************************************************************************)

let () =
  try
    Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
  with Not_found ->
    ()
;;

#use "topfind";;
#require "odn";;
#require "unix";;
#require "fileutils";;
#load "_build/src/oasis/oasis.cma";;
#directory "_build/src/oasis/";;

open OASISUtils
open OASISTypes


let debug_self = true


let exec nm =
  try
    FileUtil.which nm
  with Not_found ->
    failwithf "Executable '%s' not found." nm


let distdir = Filename.concat (Sys.getcwd ()) "dist"
let oasis_exec =
  FilePath.make_filename
    [(Sys.getcwd ()); "_build"; "src"; "cli"; "Main.byte"]
let git_exec = exec "git"
let tar_exec = exec "tar"

(* Argument parsing. *)
let dev = ref false


let () =
  Arg.parse
    [
      "-dev",
      Arg.Set dev,
      " Generate a dev tarball.";
    ]
    (failwithf "Don't know what to do with %S")
    "dist.ml: build tarball for oasis."


let ctxt = {!OASISContext.default with OASISContext.ignore_plugins = true}


let run = OASISExec.run ~ctxt


let with_tmpdir f =
  let res = Filename.temp_file "oasis-dist-" ".dir" in
  let pwd = Sys.getcwd () in
  let clean () =
    Sys.chdir pwd;
    FileUtil.rm ~recurse:true [res]
  in
    Sys.remove res;
    OASISFileUtil.mkdir ~ctxt res;
    try
      f res;
      clean ()
    with e ->
      clean ();
      raise e


let pkg = OASISParse.from_file ~ctxt OASISParse.default_oasis_fn


let uncommited_changes =
  match OASISExec.run_read_output ~ctxt git_exec ["status"; "--porcelain"] with
    | [] -> false
    | _ -> true


let () =
  if not debug_self && uncommited_changes then
    failwith "Uncommited changes."


let ver_str = OASISVersion.string_of_version pkg.version


let () =
  (* Verify that the built oasis match the version in _oasis. *)
  let exec_ver_str = OASISExec.run_read_one_line ~ctxt oasis_exec ["version"] in
  if exec_ver_str <> ver_str then
    failwithf
      "Version reported by %s (%S) is different from version in _oasis (%S)"
      oasis_exec exec_ver_str ver_str


let tag = if !dev then "dev" else ver_str
let () =
  if not !dev then begin
    let existing_tags = OASISExec.run_read_output ~ctxt git_exec ["tag"] in
    let most_recent_tag =
      List.fold_left
        (fun r e ->
           if OASISVersion.version_compare_string r e < 0 then
             e
           else
             r)
        tag existing_tags
    in
    print_endline ("Most recent tag: "^most_recent_tag);
    if List.mem tag existing_tags then
      failwithf "Tag %s already exists." tag;

    if most_recent_tag <> tag then
      failwithf
        "Tag %S is more recent than the tag %S to apply."
        most_recent_tag tag
  end


let topdir = pkg.name^"-"^tag
let tarball = Filename.concat distdir (topdir^".tar.gz")


let () =
  (* Create the tarball. *)
  run git_exec
    ["archive"; "--prefix";  (Filename.concat topdir "");
     "--format"; "tar.gz"; "HEAD"; "-o"; tarball];
  with_tmpdir
    (fun dn ->
       (* Uncompress tarball in tmpdir *)
       run tar_exec ["xz"; "-C"; dn; "-f"; tarball];

       (* Run OASIS setup inside the tarball and rebuild it. *)
       run oasis_exec ["-C"; Filename.concat dn topdir; "setup"];
       run tar_exec ["-C"; dn; "-czf"; tarball; topdir];

       Sys.chdir (Filename.concat dn topdir);

       if Sys.file_exists "setup.data" then
         failwith
           "Remaining 'setup.data' file.";
       if Sys.file_exists "configure" &&
          not (FileUtil.test FileUtil.Is_exec "configure") then
         failwith "'configure' is not executable.";

       if not !dev then
         (* Check that build, test, doc run smoothly *)
         run "ocaml" ["setup.ml"; "-all"];

       let bak_files =
         (* Check for remaining .bak files *)
         FileUtil.find (FileUtil.Has_extension "bak")
           Filename.current_dir_name
           (fun acc fn -> fn :: acc)
           []
       in
         if bak_files <> [] then
           failwithf
             "Remaining .bak files: %s."
             (String.concat ", " bak_files))


let () =
  if not !dev then begin
    run git_exec ["tag"; tag];
    run
      ~f_exit_code:
      (fun i ->
         if i <> 0 then
           OASISMessage.warning ~ctxt "Cannot sign '%s' with gpg" tarball)
      "gpg" ["-s"; "-a"; "-b"; tarball]
  end