Skip to content

Commit 3d88a4c

Browse files
committed
add tool to help developers
Currently there is just one command, which prints out lines that can be copy/pasted into .install files.
1 parent 0e64dbf commit 3d88a4c

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.DS_Store
22
*~
33
/tmp
4+
/bin/biopam-dev.ml.exe

bin/biopam-dev.ml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#! /usr/bin/env ocamlscript
2+
Ocaml.ocamlflags := ["-thread"];
3+
Ocaml.packs := ["phat.async_unix"]
4+
--
5+
open Core.Std
6+
open Async.Std
7+
module Phat = Phat_async_unix.Std
8+
9+
let make_install_file ?src_prefix preserve_dirs root_dir src_dir
10+
: (Phat.rel_file * Phat.rel_file option) list Or_error.t Deferred.t
11+
=
12+
let base : Phat.rel_dir = Phat.Item (Phat.last_of_rel src_dir) in
13+
let src_prefix : Phat.rel_dir = match src_prefix with
14+
| None -> base
15+
| Some src_prefix -> Phat.concat src_prefix base
16+
in
17+
let dst_prefix = base in
18+
Phat.fold (Phat.concat root_dir src_dir) ~init:[] ~f:(fun accum x ->
19+
let x = match x with
20+
| `File x -> Some (
21+
Phat.concat src_prefix x,
22+
if preserve_dirs then Some (Phat.concat dst_prefix x) else None
23+
)
24+
| `Dir _ -> None
25+
| `Broken_link x -> (
26+
Log.Global.info "Skipping broken link %s" (Phat.to_string x);
27+
None
28+
)
29+
in
30+
return (x::accum)
31+
) >>|?
32+
List.filter_map ~f:Fn.id >>|?
33+
List.rev
34+
35+
36+
(******************************************************************************)
37+
(* CLI *)
38+
(******************************************************************************)
39+
(** Async_extra.Command is missing this function. See pull request:
40+
- https://github.com/janestreet/async_extra/pull/6
41+
*)
42+
let async_or_error' ~summary ?readme params main =
43+
Command.async_or_error ~summary ?readme (Command.Spec.of_params params) main
44+
45+
46+
module Param = struct
47+
include Command.Param
48+
49+
let log_level =
50+
flag "-log" (optional_with_default `Info Log.Level.arg)
51+
~doc:(sprintf "level Log level can be debug, info, or error. \
52+
Default is info.")
53+
54+
let src_prefix =
55+
flag "-src-prefix" (optional Phat.Cli.rel_dir)
56+
~doc:"DIR Additional prefix added to source file paths."
57+
58+
let preserve_dirs =
59+
flag "-preserve-dirs" (optional_with_default false bool)
60+
~doc:"BOOL Preserve directory structure. Default: false."
61+
62+
let common_args remaining_args =
63+
log_level @> remaining_args
64+
65+
end
66+
67+
(** Handle [common_args] in a common way. Returns path to config
68+
file. *)
69+
let common_handler log_level : unit Or_error.t Deferred.t =
70+
Log.Global.set_level log_level;
71+
return (Ok ())
72+
73+
let main : Command.t = async_or_error'
74+
~summary:"print .install file lines"
75+
Param.(
76+
common_args @@
77+
src_prefix @> preserve_dirs @>
78+
anon ("ROOT_DIR" %: Phat.Cli.dir_of_any_kind) @>
79+
anon ("INSTALL_DIR" %: Phat.Cli.rel_dir) @>
80+
nil
81+
)
82+
(fun a src_prefix preserve_dirs root_dir src_dir () ->
83+
common_handler a >>=? fun () ->
84+
(match root_dir with
85+
| `Abs x -> return (Ok x)
86+
| `Rel x ->
87+
Unix.getcwd() >>|
88+
Phat.abs_dir >>|? fun cwd ->
89+
Phat.concat cwd x
90+
) >>=? fun root_dir ->
91+
make_install_file ?src_prefix preserve_dirs root_dir src_dir >>=? fun l ->
92+
let stdout = force Writer.stdout in
93+
Deferred.Or_error.List.iter l ~f:(fun (src,dst) ->
94+
Writer.writef stdout " \"%s\"" (Phat.to_string src);
95+
Option.value_map dst ~default:() ~f:(fun dst ->
96+
Writer.writef stdout " {\"%s\"}" (Phat.to_string dst)
97+
);
98+
Writer.newline stdout;
99+
return (Ok ())
100+
)
101+
)
102+
103+
let () =
104+
try Command.run ~version:"dev" main
105+
with e -> eprintf "%s\n" (Exn.to_string e)

0 commit comments

Comments
 (0)