Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Concat_or_split #849

Merged
merged 23 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
69c0ab4
Add test for concat_or_split
rgrinberg Jun 4, 2018
c1d6fae
Remove Concat_or_split
rgrinberg Jun 4, 2018
bb58cf8
s/false/true/
rgrinberg Jun 4, 2018
8351fcb
Move the multivalue error to a function
rgrinberg Jun 4, 2018
731b61b
Improve the error message with invalid strings
rgrinberg Jun 4, 2018
9545d9a
Add length function to exapnsions
rgrinberg Jun 4, 2018
eab7c46
Add flag to allow/disallow multivalue expansions
rgrinberg Jun 5, 2018
cadee0e
Write explicit interface for Expand_to
rgrinberg Jun 5, 2018
588129d
Move Var_expansion to own module
rgrinberg Jun 5, 2018
bab65e9
Allow for proper expansoin of vars in super contexts
rgrinberg Jun 5, 2018
589943d
Simplify String_with_vars
rgrinberg Jun 5, 2018
ff173b9
Share quote handling in partial and normal expansion
rgrinberg Jun 5, 2018
9221b1e
Change echo to be variadic
rgrinberg Jun 5, 2018
774306c
Remove old usage for Var_expansion in ppx driver
rgrinberg Jun 5, 2018
124d942
s/jbuild/dune/ in misc test
Jun 6, 2018
243f343
implement expand in terms of partial_expand
rgrinberg Jun 6, 2018
9cc8ff9
Special case t.items = [Text _] and t.items = []
rgrinberg Jun 6, 2018
c96df4d
Inline expand_var
rgrinberg Jun 6, 2018
6ebff9d
Move Value.t list functions to Value.L
rgrinberg Jun 6, 2018
7d8a7e9
Fix incorrect concatenation for multivalues in quoted context
rgrinberg Jun 6, 2018
bdeef73
Add test for proper concatenation
rgrinberg Jun 6, 2018
abfa90b
Update tests
rgrinberg Jun 6, 2018
bc53047
Merge branch 'master' into remove-concat-or-split
rgrinberg Jun 6, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 51 additions & 123 deletions src/action.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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))
(*
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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) ->
Expand Down Expand Up @@ -270,69 +271,13 @@ module Unresolved = struct
| Search s -> Ok (f s))
end

module Var_expansion = struct
module Concat_or_split = struct
type t =
| Concat (* default *)
| Split (* the variable is a "split" list of items *)
end
let prog_and_args_of_values p ~dir =
match p with
| [] -> (Unresolved.Program.Search "", [])
| Value.Path p :: xs -> (This p, Value.L.to_strings ~dir xs)
| String s :: xs ->
(Unresolved.Program.of_string ~dir s, Value.L.to_strings ~dir xs)

open Concat_or_split

type t =
| Paths of Path.t list * Concat_or_split.t
| Strings of string list * Concat_or_split.t

let is_multivalued = function
| Paths (_, Split) | Strings (_, Split) -> true
| Paths (_, Concat) | Strings (_, Concat) -> false

type context = Path.t (* For String_with_vars.Expand_to *)

let concat = function
| [s] -> s
| l -> String.concat ~sep:" " l

let string_of_path ~dir p = Path.reach ~from:dir p
let path_of_string dir s = Path.relative dir s

let to_strings dir = function
| Strings (l, Split ) -> l
| Strings (l, Concat) -> [concat l]
| Paths (l, Split ) -> List.map l ~f:(string_of_path ~dir)
| Paths (l, Concat) -> [concat (List.map l ~f:(string_of_path ~dir))]

let to_string (dir: context) = function
| Strings (l, _) -> concat l
| Paths (l, _) -> concat (List.map l ~f:(string_of_path ~dir))

let to_path dir = function
| Strings (l, _) -> path_of_string dir (concat l)
| Paths ([p], _) -> p
| Paths (l, _) ->
path_of_string dir (concat (List.map l ~f:(string_of_path ~dir)))

let to_prog_and_args dir exp : Unresolved.Program.t * string list =
let module P = Unresolved.Program in
match exp with
| Paths ([p], _) -> (This p, [])
| Strings ([s], _) -> (P.of_string ~dir s, [])
| Paths ([], _) | Strings ([], _) -> (Search "", [])
| Paths (l, Concat) ->
(This
(path_of_string dir
(concat (List.map l ~f:(string_of_path ~dir)))),
[])
| Strings (l, Concat) ->
(P.of_string ~dir (concat l), l)
| Paths (p :: l, Split) ->
(This p, List.map l ~f:(string_of_path ~dir))
| Strings (s :: l, Split) ->
(P.of_string ~dir s, l)
end

module VE = Var_expansion
module To_VE = String_with_vars.Expand_to(VE)
module SW = String_with_vars

module Unexpanded = struct
Expand Down Expand Up @@ -370,37 +315,33 @@ module Unexpanded = struct
include Past

module E = struct
let expand ~generic ~special ~map ~dir ~f = function
| Left x -> map x
| Right template ->
match To_VE.expand dir template ~f with
| Expansion e -> special dir e
| String s -> generic dir s
[@@inlined always]

let string ~dir ~f x =
expand ~dir ~f x
~generic:(fun _dir x -> x)
~special:VE.to_string
~map:(fun x -> x)

let strings ~dir ~f x =
expand ~dir ~f x
~generic:(fun _dir x -> [x])
~special:VE.to_strings
~map:(fun x -> [x])

let path ~dir ~f x =
expand ~dir ~f x
~generic:VE.path_of_string
~special:VE.to_path
~map:(fun x -> x)

let prog_and_args ~dir ~f x =
expand ~dir ~f x
~generic:(fun _dir s -> (Program.of_string ~dir s, []))
~special:VE.to_prog_and_args
~map:(fun x -> (x, []))
let expand ~dir ~mode ~f ~l ~r =
Either.map ~l
~r:(fun s -> r (String_with_vars.expand s ~dir ~f ~mode) ~dir)

let string =
expand ~mode:Single
~l:(fun x -> x)
~r:Value.to_string

let strings =
expand ~mode:Many
~l:(fun x -> [x])
~r:Value.L.to_strings

let path e =
let error_loc =
match e with
| Left _ -> None
| Right r -> Some (String_with_vars.loc r) in
expand ~mode:Single
~l:(fun x -> x)
~r:Value.(to_path ?error_loc) e

let prog_and_args =
expand ~mode:Many
~l:(fun x -> (x, []))
~r:prog_and_args_of_values
end

let rec expand t ~dir ~map_exe ~f : Unresolved.t =
Expand All @@ -425,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)
Expand Down Expand Up @@ -463,31 +404,18 @@ module Unexpanded = struct
end

module E = struct
let expand ~generic ~special ~dir ~f template =
match To_VE.partial_expand dir template ~f with
| Expansion e -> Left (special dir e)
| String s -> Left (generic dir s)
let expand ~dir ~mode ~f ~map x =
match String_with_vars.partial_expand ~mode ~dir ~f x with
| Expanded e -> Left (map e ~dir)
| Unexpanded x -> Right x

let string ~dir ~f x =
expand ~dir ~f x
~generic:(fun _dir x -> x)
~special:VE.to_string

let strings ~dir ~f x =
expand ~dir ~f x
~generic:(fun _dir x -> [x])
~special:VE.to_strings

let path ~dir ~f x =
expand ~dir ~f x
~generic:VE.path_of_string
~special:VE.to_path

let prog_and_args ~dir ~f x =
expand ~dir ~f x
~generic:(fun dir s -> (Unresolved.Program.of_string ~dir s, []))
~special:VE.to_prog_and_args
let string = expand ~mode:Single ~map:Value.to_string
let strings = expand ~mode:Many ~map:Value.L.to_strings
let cat_strings = expand ~mode:Many ~map:Value.L.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
end

let rec partial_expand t ~dir ~map_exe ~f : Partial.t =
Expand Down Expand Up @@ -531,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)
Expand Down Expand Up @@ -760,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
Expand All @@ -777,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 =
Expand Down
21 changes: 2 additions & 19 deletions src/action.mli
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
open! Import

module Var_expansion : sig
module Concat_or_split : sig
type t =
| Concat (** default *)
| Split (** the variable is a "split" list of items *)
end

type t =
| Paths of Path.t list * Concat_or_split.t
| Strings of string list * Concat_or_split.t

val to_string : Path.t -> t -> string
(** [to_string dir v] convert the variable expansion to a string.
If it is a path, the corresponding string will be relative to
[dir]. *)
end

module Outputs : module type of struct include Action_intf.Outputs end

(** result of the lookup of a program, the path to it or information about the
Expand Down Expand Up @@ -99,15 +82,15 @@ module Unexpanded : sig
: t
-> dir:Path.t
-> map_exe:(Path.t -> Path.t)
-> f:(Loc.t -> String.t -> Var_expansion.t option)
-> f:(Loc.t -> String.t -> Value.t list option)
-> Unresolved.t
end

val partial_expand
: t
-> dir:Path.t
-> map_exe:(Path.t -> Path.t)
-> f:(Loc.t -> string -> Var_expansion.t option)
-> f:(Loc.t -> string -> Value.t list option)
-> Partial.t
end

Expand Down
4 changes: 2 additions & 2 deletions src/action_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/gen_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ module Gen(P : Install_rules.Params) = struct
+-----------------------------------------------------------------+ *)

let interpret_locks ~dir ~scope locks =
List.map locks ~f:(fun s ->
Path.relative dir (SC.expand_vars sctx ~dir ~scope s))
List.map locks ~f:(SC.expand_vars_path sctx ~dir ~scope)

let user_rule (rule : Rule.t) ~dir ~scope =
let targets : SC.Action.targets =
Expand Down
8 changes: 3 additions & 5 deletions src/inline_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ include Sub_system.Register_end_point(
in

let extra_vars =
String.Map.singleton "library-name"
(Action.Var_expansion.Strings ([lib.name], Concat))
String.Map.singleton "library-name" ([Value.String lib.name])
in

let runner_libs =
Expand All @@ -210,10 +209,9 @@ include Sub_system.Register_end_point(
let target = Path.relative inline_test_dir main_module_filename in
let source_modules = Module.Name.Map.values source_modules in
let files ml_kind =
Action.Var_expansion.Paths (
Value.L.paths (
List.filter_map source_modules ~f:(fun m ->
Module.file m ~dir ml_kind),
Split)
Module.file m ~dir ml_kind))
in
let extra_vars =
List.fold_left
Expand Down
6 changes: 2 additions & 4 deletions src/preprocessing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,7 @@ let lint_module sctx ~dir ~dep_kind ~lint ~lib_name ~scope ~dir_kind =
>>| fun (exe, driver) ->
(exe,
let extra_vars =
String_map.singleton "corrected-suffix"
(Action.Var_expansion.Strings ([corrected_suffix], Split))
String_map.singleton "corrected-suffix" [Value.String corrected_suffix]
in
Build.memoize "ppx flags"
(SC.expand_and_eval_set sctx driver.info.lint_flags
Expand Down Expand Up @@ -558,8 +557,7 @@ let make sctx ~dir ~dep_kind ~lint ~preprocess
get_ppx_driver sctx ~loc ~scope ~dir_kind pps >>| fun (exe, driver) ->
(exe,
let extra_vars =
String_map.singleton "corrected-suffix"
(Action.Var_expansion.Strings ([corrected_suffix], Split))
String_map.singleton "corrected-suffix" [Value.String corrected_suffix]
in
Build.memoize "ppx flags"
(SC.expand_and_eval_set sctx driver.info.flags
Expand Down
5 changes: 5 additions & 0 deletions src/stdune/either.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
type ('a, 'b) t =
| Left of 'a
| Right of 'b

let map t ~l ~r =
match t with
| Left x -> l x
| Right x -> r x
2 changes: 2 additions & 0 deletions src/stdune/either.mli
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
type ('a, 'b) t =
| Left of 'a
| Right of 'b

val map : ('a, 'b) t -> l:('a -> 'c) -> r:('b -> 'c) -> 'c
5 changes: 5 additions & 0 deletions src/stdune/string.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading