Change echo to be variadic

Signed-off-by: Rudi Grinberg <rudi.grinberg@gmail.com>
This commit is contained in:
Rudi Grinberg 2018-06-06 02:02:56 +07:00
parent ff173b98d8
commit 9221b1ed6c
6 changed files with 26 additions and 16 deletions

View File

@ -34,7 +34,7 @@ struct
; cstr "ignore-stderr" (t @> nil) (fun t -> Ignore (Stderr, t)) ; cstr "ignore-stderr" (t @> nil) (fun t -> Ignore (Stderr, t))
; cstr "ignore-outputs" (t @> nil) (fun t -> Ignore (Outputs, t)) ; cstr "ignore-outputs" (t @> nil) (fun t -> Ignore (Outputs, t))
; cstr "progn" (rest t) (fun l -> Progn l) ; cstr "progn" (rest t) (fun l -> Progn l)
; cstr "echo" (string @> nil) (fun x -> Echo x) ; cstr "echo" (string @> rest string) (fun x xs -> Echo (x::xs))
; cstr "cat" (path @> nil) (fun x -> Cat x) ; cstr "cat" (path @> nil) (fun x -> Cat x)
; cstr "copy" (path @> path @> nil) (fun src dst -> Copy (src, dst)) ; cstr "copy" (path @> path @> nil) (fun src dst -> Copy (src, dst))
(* (*
@ -78,7 +78,8 @@ struct
] ]
| Progn l -> List (Sexp.unsafe_atom_of_string "progn" | Progn l -> List (Sexp.unsafe_atom_of_string "progn"
:: List.map l ~f:sexp_of_t) :: List.map l ~f:sexp_of_t)
| Echo x -> List [Sexp.unsafe_atom_of_string "echo"; string x] | Echo xs ->
List (Sexp.unsafe_atom_of_string "echo" :: List.map xs ~f:string)
| Cat x -> List [Sexp.unsafe_atom_of_string "cat"; path x] | Cat x -> List [Sexp.unsafe_atom_of_string "cat"; path x]
| Copy (x, y) -> | Copy (x, y) ->
List [Sexp.unsafe_atom_of_string "copy"; path x; path y] List [Sexp.unsafe_atom_of_string "copy"; path x; path y]
@ -150,7 +151,7 @@ module Make_mapper
| Ignore (outputs, t) -> | Ignore (outputs, t) ->
Ignore (outputs, map t ~dir ~f_program ~f_string ~f_path) 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)) | 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) | Echo xs -> Echo (List.map xs ~f:(f_string ~dir))
| Cat x -> Cat (f_path ~dir x) | Cat x -> Cat (f_path ~dir x)
| Copy (x, y) -> Copy (f_path ~dir x, f_path ~dir y) | Copy (x, y) -> Copy (f_path ~dir x, f_path ~dir y)
| Symlink (x, y) -> | Symlink (x, y) ->
@ -365,7 +366,7 @@ module Unexpanded = struct
| Ignore (outputs, t) -> | Ignore (outputs, t) ->
Ignore (outputs, expand t ~dir ~map_exe ~f) Ignore (outputs, expand t ~dir ~map_exe ~f)
| Progn l -> Progn (List.map l ~f:(fun t -> 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) | Echo xs -> Echo (List.concat_map xs ~f:(E.strings ~dir ~f))
| Cat x -> Cat (E.path ~dir ~f x) | Cat x -> Cat (E.path ~dir ~f x)
| Copy (x, y) -> | Copy (x, y) ->
Copy (E.path ~dir ~f x, E.path ~dir ~f y) Copy (E.path ~dir ~f x, E.path ~dir ~f y)
@ -408,12 +409,13 @@ module Unexpanded = struct
| Expanded e -> Left (map e ~dir) | Expanded e -> Left (map e ~dir)
| Unexpanded x -> Right x | Unexpanded x -> Right x
let string = expand ~mode:Single ~map:(Value.to_string) let string = expand ~mode:Single ~map:Value.to_string
let strings = expand ~mode:Many ~map:(Value.to_strings) let strings = expand ~mode:Many ~map:Value.to_strings
let cat_strings = expand ~mode:Many ~map:Value.concat
let path x = let path x =
let error_loc = String_with_vars.loc x in let error_loc = String_with_vars.loc x in
expand ~mode:Single ~map:(Value.to_path ~error_loc) x expand ~mode:Single ~map:(Value.to_path ~error_loc) x
let prog_and_args = expand ~mode:Many ~map:(prog_and_args_of_values) let prog_and_args = expand ~mode:Many ~map:prog_and_args_of_values
end end
let rec partial_expand t ~dir ~map_exe ~f : Partial.t = let rec partial_expand t ~dir ~map_exe ~f : Partial.t =
@ -457,7 +459,7 @@ module Unexpanded = struct
| Ignore (outputs, t) -> | Ignore (outputs, t) ->
Ignore (outputs, partial_expand t ~dir ~map_exe ~f) 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)) | Progn l -> Progn (List.map l ~f:(fun t -> partial_expand t ~dir ~map_exe ~f))
| Echo x -> Echo (E.string ~dir ~f x) | Echo xs -> Echo (List.map xs ~f:(E.cat_strings ~dir ~f))
| Cat x -> Cat (E.path ~dir ~f x) | Cat x -> Cat (E.path ~dir ~f x)
| Copy (x, y) -> | Copy (x, y) ->
Copy (E.path ~dir ~f x, E.path ~dir ~f y) Copy (E.path ~dir ~f x, E.path ~dir ~f y)
@ -686,7 +688,7 @@ let rec exec t ~ectx ~dir ~env ~stdout_to ~stderr_to =
exec t ~ectx ~dir ~stdout_to ~stderr_to exec t ~ectx ~dir ~stdout_to ~stderr_to
~env:(Env.add env ~var ~value) ~env:(Env.add env ~var ~value)
| Redirect (Stdout, fn, Echo s) -> | Redirect (Stdout, fn, Echo s) ->
Io.write_file fn s; Io.write_file fn (String.concat s ~sep:" ");
Fiber.return () Fiber.return ()
| Redirect (outputs, fn, Run (Ok prog, args)) -> | Redirect (outputs, fn, Run (Ok prog, args)) ->
let out = Process.File fn in let out = Process.File fn in
@ -703,7 +705,7 @@ let rec exec t ~ectx ~dir ~env ~stdout_to ~stderr_to =
redirect ~ectx ~dir outputs Config.dev_null t ~env ~stdout_to ~stderr_to redirect ~ectx ~dir outputs Config.dev_null t ~env ~stdout_to ~stderr_to
| Progn l -> | Progn l ->
exec_list l ~ectx ~dir ~env ~stdout_to ~stderr_to exec_list l ~ectx ~dir ~env ~stdout_to ~stderr_to
| Echo str -> exec_echo stdout_to str | Echo strs -> exec_echo stdout_to (String.concat strs ~sep:" ")
| Cat fn -> | Cat fn ->
Io.with_file_in fn ~f:(fun ic -> Io.with_file_in fn ~f:(fun ic ->
let oc = let oc =

View File

@ -29,7 +29,7 @@ module type Ast = sig
| Redirect of Outputs.t * path * t | Redirect of Outputs.t * path * t
| Ignore of Outputs.t * t | Ignore of Outputs.t * t
| Progn of t list | Progn of t list
| Echo of string | Echo of string list
| Cat of path | Cat of path
| Copy of path * path | Copy of path * path
| Symlink of path * path | Symlink of path * path
@ -61,7 +61,7 @@ module type Helpers = sig
val ignore_stderr : t -> t val ignore_stderr : t -> t
val ignore_outputs : t -> t val ignore_outputs : t -> t
val progn : t list -> t val progn : t list -> t
val echo : string -> t val echo : string list -> t
val cat : path -> t val cat : path -> t
val copy : path -> path -> t val copy : path -> path -> t
val symlink : path -> path -> t val symlink : path -> path -> t

View File

@ -214,3 +214,8 @@ let enumerate_gen s =
let enumerate_and = enumerate_gen "and" let enumerate_and = enumerate_gen "and"
let enumerate_or = enumerate_gen "or" let enumerate_or = enumerate_gen "or"
let concat ~sep = function
| [] -> ""
| [x] -> x
| xs -> concat ~sep xs

View File

@ -21,6 +21,10 @@ let to_path ?error_loc t ~dir =
let strings = List.map ~f:(fun x -> String x) let strings = List.map ~f:(fun x -> String x)
let paths = List.map ~f:(fun x -> Path x) let paths = List.map ~f:(fun x -> Path x)
let concat ts ~dir =
List.map ~f:(to_string ~dir) ts
|> String.concat ~sep:" "
let paths_only = let paths_only =
List.filter_map ~f:(function List.filter_map ~f:(function
| String _ -> None | String _ -> None

View File

@ -15,3 +15,5 @@ val strings : string list -> t list
val paths : Path.t list -> t list val paths : Path.t list -> t list
val paths_only : t list -> Path.t list val paths_only : t list -> Path.t list
val concat : t list -> dir:Path.t -> string

View File

@ -2,9 +2,6 @@
File "dune", line 65, characters 21-44: File "dune", line 65, characters 21-44:
Warning: Directory dir-that-doesnt-exist doesn't exist. Warning: Directory dir-that-doesnt-exist doesn't exist.
No rule found for jbuild No rule found for jbuild
File "dune", line 9, characters 43-47: diff alias runtest
Error: Variable ${^} expands to 4 values, however a single value is expected here. Please quote this atom.
File "dune", line 16, characters 44-48:
Error: Variable ${^} expands to 2 values, however a single value is expected here. Please quote this atom.
diff alias runtest diff alias runtest
[1] [1]