Skip to content

Commit

Permalink
Add workspace-paths variable expansion in paths field
Browse files Browse the repository at this point in the history
  • Loading branch information
bobot committed Jul 31, 2019
1 parent 1e337ae commit 2f7dca4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
52 changes: 34 additions & 18 deletions src/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -530,22 +530,40 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets
in
native :: List.filter_opt others

let extend_paths t ~env ~build_dir =
let module Eval =
Ordered_set_lang.Make(String)
(struct
type t = string
type key = string
let key x = x
end)
in
let t =
let f (var, t) =
let parse ~loc:_ s = s in
let standard = Env.path env |> List.map ~f:Path.to_string in
var, Eval.eval t ~parse ~standard
let extend_paths ~env ~context =
let t = Workspace.Context.paths context in
let expand (var,t) =
let _, _ =
Ordered_set_lang.Unexpanded.files t
~f:(fun incl ->
User_error.raise ~loc:(String_with_vars.loc incl)
[ Pp.text "The :include are not accepted in workspace file"]
)
in
let t =
Ordered_set_lang.Unexpanded.expand t
~dir:Path.root
~files_contents:Path.Map.empty
~f:(fun s ->
String_with_vars.expand s
~mode:String_with_vars.Mode.Many
~dir:Path.root
~f:(fun var _ ->
match String_with_vars.Var.name var with
| "workspace_root" ->
(* We use String otherwise it doesn't stay absolute *)
Some [Value.String (Path.to_absolute_filename Path.root)]
| _ -> None
)
)
in
List.map ~f t
let parse ~loc:_ s = s in
let standard = Env.path env |> List.map ~f:Path.to_string in
var, Ordered_set_lang.String.eval t ~parse ~standard
in
let t = List.map ~f:expand t in
let build_dir =
Path.Build.relative Path.Build.root (Workspace.Context.name context)
in
let vars =
let to_absolute_filename s =
Expand Down Expand Up @@ -656,9 +674,7 @@ let instantiate_context env (workspace : Workspace.t)
(Env_nodes.extra_env
~profile:(Workspace.Context.profile context) env_nodes)
in
let build_dir =
Path.Build.relative Path.Build.root (Workspace.Context.name context) in
let env = extend_paths ~env ~build_dir (Workspace.Context.paths context) in
let env = extend_paths ~env ~context in
match context with
| Default { targets; name; host_context = _; profile; env = _
; toolchain ; paths = _; loc = _ } ->
Expand Down
4 changes: 2 additions & 2 deletions src/workspace.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module Context = struct
; toolchain : string option
; name : string
; host_context : string option
; paths : (string * Ordered_set_lang.t) list
; paths : (string * Ordered_set_lang.Unexpanded.t) list
}

let t ~profile =
Expand All @@ -78,7 +78,7 @@ module Context = struct
in
field "paths" ~default:[]
(Syntax.since Stanza.syntax (1, 12) >>>
map ~f (repeat (pair (located string) Ordered_set_lang.decode)))
map ~f (repeat (pair (located string) Ordered_set_lang.Unexpanded.decode)))
and+ loc = loc
in
Option.iter
Expand Down
4 changes: 2 additions & 2 deletions src/workspace.mli
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module Context : sig
; toolchain : string option
; name : string
; host_context : string option
; paths : (string * Ordered_set_lang.t) list
; paths : (string * Ordered_set_lang.Unexpanded.t) list
}
end
module Opam : sig
Expand All @@ -43,7 +43,7 @@ module Context : sig
val env : t -> Dune_env.Stanza.t

val profile : t -> string
val paths : t -> (string * Ordered_set_lang.t) list
val paths : t -> (string * Ordered_set_lang.Unexpanded.t) list

val host_context : t -> string option
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

(context
(default
(paths (PATH bin :standard) (FOO a b /c \ b))))
(paths (PATH bin :standard) (FOO a b /c %{workspace_root}/d \ b))))
2 changes: 1 addition & 1 deletion test/blackbox-tests/test-cases/workspace-paths/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$ dune build @default
hello alias default
Hello: $TESTCASE_ROOT/_build/default/a:/c
Hello: $TESTCASE_ROOT/_build/default/a:/c:$TESTCASE_ROOT/d

$ mkdir sub
$ cat > sub/dune-workspace <<EOF
Expand Down

0 comments on commit 2f7dca4

Please sign in to comment.