From 9221b1ed6cc0a8431e2bbdac6698ede72d5b70e3 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Wed, 6 Jun 2018 02:02:56 +0700 Subject: [PATCH] Change echo to be variadic Signed-off-by: Rudi Grinberg --- src/action.ml | 22 ++++++++++++---------- src/action_intf.ml | 4 ++-- src/stdune/string.ml | 5 +++++ src/value.ml | 4 ++++ src/value.mli | 2 ++ test/blackbox-tests/test-cases/misc/run.t | 5 +---- 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/action.ml b/src/action.ml index c2ef1c7f..8c4c2d66 100644 --- a/src/action.ml +++ b/src/action.ml @@ -34,7 +34,7 @@ struct ; cstr "ignore-stderr" (t @> nil) (fun t -> Ignore (Stderr, t)) ; cstr "ignore-outputs" (t @> nil) (fun t -> Ignore (Outputs, t)) ; 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 "copy" (path @> path @> nil) (fun src dst -> Copy (src, dst)) (* @@ -78,7 +78,8 @@ 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] + | 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] | Copy (x, y) -> List [Sexp.unsafe_atom_of_string "copy"; path x; path y] @@ -150,7 +151,7 @@ module Make_mapper | Ignore (outputs, t) -> 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) + | Echo xs -> Echo (List.map xs ~f:(f_string ~dir)) | Cat x -> Cat (f_path ~dir x) | Copy (x, y) -> Copy (f_path ~dir x, f_path ~dir y) | Symlink (x, y) -> @@ -365,7 +366,7 @@ module Unexpanded = struct | Ignore (outputs, t) -> 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) + | Echo xs -> Echo (List.concat_map xs ~f:(E.strings ~dir ~f)) | Cat x -> Cat (E.path ~dir ~f x) | Copy (x, 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) | Unexpanded x -> Right x - let string = expand ~mode:Single ~map:(Value.to_string) - let strings = expand ~mode:Many ~map:(Value.to_strings) + let string = expand ~mode:Single ~map:Value.to_string + let strings = expand ~mode:Many ~map:Value.to_strings + let cat_strings = expand ~mode:Many ~map:Value.concat let path x = let error_loc = String_with_vars.loc x in 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 let rec partial_expand t ~dir ~map_exe ~f : Partial.t = @@ -457,7 +459,7 @@ module Unexpanded = struct | Ignore (outputs, t) -> 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) + | Echo xs -> Echo (List.map xs ~f:(E.cat_strings ~dir ~f)) | Cat x -> Cat (E.path ~dir ~f x) | Copy (x, 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 ~env:(Env.add env ~var ~value) | Redirect (Stdout, fn, Echo s) -> - Io.write_file fn s; + Io.write_file fn (String.concat s ~sep:" "); Fiber.return () | Redirect (outputs, fn, Run (Ok prog, args)) -> 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 | Progn l -> 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 -> Io.with_file_in fn ~f:(fun ic -> let oc = diff --git a/src/action_intf.ml b/src/action_intf.ml index c5c76f72..9c7c663f 100644 --- a/src/action_intf.ml +++ b/src/action_intf.ml @@ -29,7 +29,7 @@ module type Ast = sig | Redirect of Outputs.t * path * t | Ignore of Outputs.t * t | Progn of t list - | Echo of string + | Echo of string list | Cat of path | Copy of path * path | Symlink of path * path @@ -61,7 +61,7 @@ module type Helpers = sig val ignore_stderr : t -> t val ignore_outputs : t -> t val progn : t list -> t - val echo : string -> t + val echo : string list -> t val cat : path -> t val copy : path -> path -> t val symlink : path -> path -> t diff --git a/src/stdune/string.ml b/src/stdune/string.ml index 16a3c9f1..bc7e87f9 100644 --- a/src/stdune/string.ml +++ b/src/stdune/string.ml @@ -214,3 +214,8 @@ let enumerate_gen s = let enumerate_and = enumerate_gen "and" let enumerate_or = enumerate_gen "or" + +let concat ~sep = function + | [] -> "" + | [x] -> x + | xs -> concat ~sep xs diff --git a/src/value.ml b/src/value.ml index f8cf5828..0eb9b3e3 100644 --- a/src/value.ml +++ b/src/value.ml @@ -21,6 +21,10 @@ let to_path ?error_loc t ~dir = let strings = List.map ~f:(fun x -> String 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 = List.filter_map ~f:(function | String _ -> None diff --git a/src/value.mli b/src/value.mli index 72226e21..de89de48 100644 --- a/src/value.mli +++ b/src/value.mli @@ -15,3 +15,5 @@ val strings : string list -> t list val paths : Path.t list -> t list val paths_only : t list -> Path.t list + +val concat : t list -> dir:Path.t -> string diff --git a/test/blackbox-tests/test-cases/misc/run.t b/test/blackbox-tests/test-cases/misc/run.t index fffc64b1..514d25e7 100644 --- a/test/blackbox-tests/test-cases/misc/run.t +++ b/test/blackbox-tests/test-cases/misc/run.t @@ -2,9 +2,6 @@ File "dune", line 65, characters 21-44: Warning: Directory dir-that-doesnt-exist doesn't exist. No rule found for jbuild - File "dune", line 9, characters 43-47: - 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]