Skip to content

Commit

Permalink
cat can act on several files
Browse files Browse the repository at this point in the history
  • Loading branch information
emillon committed Apr 4, 2018
1 parent f6f66a6 commit 6d1c1d8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion doc/jbuild.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ The following constructions are available:
- ``(progn <DSL>...)`` to execute several commands in sequence
- ``(echo <string>)`` to output a string on stdout
- ``(write-file <file> <string>)`` writes ``<string>`` to ``<file>``
- ``(cat <file>)`` to print the contents of a file to stdout
- ``(cat <file> ...)`` to print the contents of files to stdout
- ``(copy <src> <dst>)`` to copy a file
- ``(copy# <src> <dst>)`` to copy a file and add a line directive at
the beginning
Expand Down
30 changes: 16 additions & 14 deletions src/action.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct
; cstr "ignore-outputs" (t @> nil) (fun t -> Ignore (Outputs, t))
; cstr_rest "progn" nil t (fun l -> Progn l)
; cstr "echo" (string @> nil) (fun x -> Echo x)
; cstr "cat" (path @> nil) (fun x -> Cat x)
; cstr_rest "cat" nil path (fun l -> Cat l)
; cstr "copy" (path @> path @> nil) (fun src dst -> Copy (src, dst))
(*
(* We don't expose symlink to the user yet since this might complicate things *)
Expand Down Expand Up @@ -85,7 +85,7 @@ struct
| Progn l -> List (Sexp.unsafe_atom_of_string "progn"
:: List.map l ~f:sexp_of_t)
| Echo x -> List [Sexp.unsafe_atom_of_string "echo"; string x]
| Cat x -> List [Sexp.unsafe_atom_of_string "cat"; path x]
| Cat xs -> List ([Sexp.unsafe_atom_of_string "cat"] @ List.map ~f:path xs)
| Copy (x, y) ->
List [Sexp.unsafe_atom_of_string "copy"; path x; path y]
| Symlink (x, y) ->
Expand Down Expand Up @@ -150,7 +150,7 @@ module Make_mapper
Ignore (outputs, map t ~dir ~f_program ~f_string ~f_path)
| Progn l -> Progn (List.map l ~f:(fun t -> map t ~dir ~f_program ~f_string ~f_path))
| Echo x -> Echo (f_string ~dir x)
| Cat x -> Cat (f_path ~dir x)
| Cat l -> Cat (List.map ~f:(f_path ~dir) l)
| Copy (x, y) -> Copy (f_path ~dir x, f_path ~dir y)
| Symlink (x, y) ->
Symlink (f_path ~dir x, f_path ~dir y)
Expand Down Expand Up @@ -420,7 +420,7 @@ module Unexpanded = struct
Ignore (outputs, expand t ~dir ~map_exe ~f)
| Progn l -> Progn (List.map l ~f:(fun t -> expand t ~dir ~map_exe ~f))
| Echo x -> Echo (E.string ~dir ~f x)
| Cat x -> Cat (E.path ~dir ~f x)
| Cat l -> Cat (List.map ~f:(E.path ~dir ~f) l)
| Copy (x, y) ->
Copy (E.path ~dir ~f x, E.path ~dir ~f y)
| Symlink (x, y) ->
Expand Down Expand Up @@ -521,7 +521,7 @@ module Unexpanded = struct
Ignore (outputs, partial_expand t ~dir ~map_exe ~f)
| Progn l -> Progn (List.map l ~f:(fun t -> partial_expand t ~dir ~map_exe ~f))
| Echo x -> Echo (E.string ~dir ~f x)
| Cat x -> Cat (E.path ~dir ~f x)
| Cat l -> Cat (List.map l ~f:(E.path ~dir ~f))
| Copy (x, y) ->
Copy (E.path ~dir ~f x, E.path ~dir ~f y)
| Symlink (x, y) ->
Expand Down Expand Up @@ -760,14 +760,16 @@ let rec exec t ~ectx ~dir ~env ~stdout_to ~stderr_to =
| Progn l ->
exec_list l ~ectx ~dir ~env ~stdout_to ~stderr_to
| Echo str -> exec_echo stdout_to str
| Cat fn ->
Io.with_file_in (Path.to_string fn) ~f:(fun ic ->
let oc =
match stdout_to with
| None -> stdout
| Some (_, oc) -> oc
in
Io.copy_channels ic oc);
| Cat fns ->
let oc =
match stdout_to with
| None -> stdout
| Some (_, oc) -> oc
in
List.iter fns ~f:(fun fn ->
Io.with_file_in (Path.to_string fn) ~f:(fun ic ->
Io.copy_channels ic oc);
);
Fiber.return ()
| Copy (src, dst) ->
Io.copy_file ~src:(Path.to_string src) ~dst:(Path.to_string dst);
Expand Down Expand Up @@ -967,7 +969,7 @@ module Infer = struct
match t with
| Run (prog, _) -> acc +<! prog
| Redirect (_, fn, t) -> infer (acc +@ fn) t
| Cat fn -> acc +< fn
| Cat fns -> List.fold_left fns ~init:acc ~f:(fun acc fn -> acc +< fn)
| Write_file (fn, _) -> acc +@ fn
| Rename (src, dst) -> acc +< src +@ dst
| Copy (src, dst)
Expand Down
4 changes: 2 additions & 2 deletions src/action_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module type Ast = sig
| Ignore of Outputs.t * t
| Progn of t list
| Echo of string
| Cat of path
| Cat of path list
| Copy of path * path
| Symlink of path * path
| Copy_and_add_line_directive of path * path
Expand Down Expand Up @@ -59,7 +59,7 @@ module type Helpers = sig
val ignore_outputs : t -> t
val progn : t list -> t
val echo : string -> t
val cat : path -> t
val cat : path list -> t
val copy : path -> path -> t
val symlink : path -> path -> t
val copy_and_add_line_directive : path -> path -> t
Expand Down

0 comments on commit 6d1c1d8

Please sign in to comment.