Skip to content

Commit 506ff24

Browse files
committed
cram - add (shell ..) option to the (cram) stanza
Signed-off-by: Haochen Kotoi-Xie <hx@kxc.inc>
1 parent 80a5248 commit 506ff24

File tree

6 files changed

+294
-16
lines changed

6 files changed

+294
-16
lines changed

src/dune_rules/cram/cram_rules.ml

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ type effective =
44
{ loc : Loc.t
55
; alias : Alias.Name.Set.t
66
; deps : unit Action_builder.t list
7+
; shell : Path.t Cram_exec.Shell_spec.t Action_builder.t
78
; sandbox : Sandbox_config.t
89
; enabled_if : Blang.t list
910
; locks : Path.Set.t
@@ -16,6 +17,7 @@ let empty_effective =
1617
; enabled_if = [ Blang.true_ ]
1718
; locks = Path.Set.empty
1819
; deps = []
20+
; shell = Action_builder.return Cram_exec.Shell_spec.default
1921
; sandbox = Sandbox_config.needs_sandboxing
2022
; packages = Package.Name.Set.empty
2123
}
@@ -59,12 +61,9 @@ let test_rule ~sctx ~expander ~dir (spec : effective)
5961
let script =
6062
Path.Build.append_source prefix_with (Cram_test.script test)
6163
in
62-
let action =
64+
let action ~shell_spec =
6365
Action.progn
64-
[ Cram_exec.action
65-
{ script = Path.build script
66-
; shell_spec = Cram_exec.Shell_spec.default
67-
}
66+
[ Cram_exec.action { script = Path.build script; shell_spec }
6867
; Diff
6968
{ Diff.optional = true
7069
; mode = Text
@@ -87,8 +86,8 @@ let test_rule ~sctx ~expander ~dir (spec : effective)
8786
|> Path.build |> Source_deps.files
8887
in
8988
Action_builder.dyn_memo_deps deps
90-
in
91-
Action.Full.make action ~locks ~sandbox:spec.sandbox
89+
and+ shell_spec = spec.shell in
90+
Action.Full.make (action ~shell_spec) ~locks ~sandbox:spec.sandbox
9291
in
9392
Memo.parallel_iter aliases ~f:(fun alias ->
9493
Alias_rules.add sctx ~alias ~loc cram))
@@ -149,15 +148,44 @@ let rules ~sctx ~expander ~dir tests =
149148
| false -> acc
150149
| true ->
151150
let* acc = acc in
152-
let* deps, sandbox =
151+
let* expander = Super_context.expander sctx ~dir in
152+
let shell, deps, sandbox =
153+
let return ?shell_deps ?sandbox shell =
154+
let sandbox =
155+
match sandbox with
156+
| None -> acc.sandbox
157+
| Some s -> Sandbox_config.inter acc.sandbox s
158+
and deps =
159+
match shell_deps with
160+
| None -> acc.deps
161+
| Some dep -> dep :: acc.deps
162+
in
163+
(shell, deps, sandbox)
164+
in
165+
let open Cram_exec.Shell_spec in
166+
match spec.shell with
167+
| System_shell -> return (Action_builder.return System_shell)
168+
| Bash_shell -> return (Action_builder.return Bash_shell)
169+
| Exec_file_shell p ->
170+
let shell_deps, sandbox =
171+
Dep_conf_eval.unnamed ~expander [ File p ]
172+
in
173+
let shell =
174+
Expander.(
175+
With_deps_if_necessary.expand_single_path expander p
176+
|> Deps.action_builder)
177+
|> Action_builder.map ~f:(fun p -> Exec_file_shell p)
178+
in
179+
return ~shell_deps ~sandbox shell
180+
in
181+
let deps, sandbox =
153182
match spec.deps with
154-
| None -> Memo.return (acc.deps, acc.sandbox)
155-
| Some deps ->
156-
let+ (deps : unit Action_builder.t), _, sandbox =
157-
let+ expander = Super_context.expander sctx ~dir in
158-
Dep_conf_eval.named ~expander deps
183+
| None -> (deps, sandbox)
184+
| Some deps' ->
185+
let (deps' : unit Action_builder.t), _, sandbox' =
186+
Dep_conf_eval.named ~expander deps'
159187
in
160-
(deps :: acc.deps, Sandbox_config.inter acc.sandbox sandbox)
188+
(deps' :: deps, Sandbox_config.inter sandbox sandbox')
161189
in
162190
let enabled_if = spec.enabled_if :: acc.enabled_if in
163191
let alias =
@@ -178,7 +206,15 @@ let rules ~sctx ~expander ~dir tests =
178206
Expander.expand_locks ~base expander spec.locks
179207
>>| Path.Set.of_list >>| Path.Set.union acc.locks
180208
in
181-
{ acc with enabled_if; locks; deps; alias; packages; sandbox })
209+
{ acc with
210+
enabled_if
211+
; locks
212+
; deps
213+
; shell
214+
; alias
215+
; packages
216+
; sandbox
217+
})
182218
in
183219
let test_rule () = test_rule ~sctx ~expander ~dir effective test in
184220
Only_packages.get_mask () >>= function

src/dune_rules/cram/cram_stanza.ml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,29 @@ let decode_applies_to =
1919
in
2020
subtree <|> predicate
2121

22+
type shell_spec =
23+
| System_shell
24+
| Bash_shell
25+
| Exec_file_shell of String_with_vars.t
26+
27+
let default_shell_spec = System_shell
28+
29+
let decode_shell_spec =
30+
let open Dune_lang.Decoder in
31+
let kw lit v =
32+
let+ _ = keyword lit in
33+
v
34+
in
35+
kw ":system" System_shell <|> kw ":bash" Bash_shell
36+
<|> let+ p = String_with_vars.decode in
37+
Exec_file_shell p
38+
2239
type t =
2340
{ loc : Loc.t
2441
; applies_to : applies_to
2542
; alias : Alias.Name.t option
2643
; deps : Dep_conf.t Bindings.t option
44+
; shell : shell_spec
2745
; enabled_if : Blang.t
2846
; locks : Locks.t
2947
; package : Package.t option
@@ -38,6 +56,7 @@ let decode =
3856
field "applies_to" decode_applies_to ~default:default_applies_to
3957
and+ alias = field_o "alias" Dune_lang.Alias.decode
4058
and+ deps = field_o "deps" (Bindings.decode Dep_conf.decode)
59+
and+ shell = field "shell" decode_shell_spec ~default:default_shell_spec
4160
and+ enabled_if = Enabled_if.decode ~allowed_vars:Any ~since:None ()
4261
and+ locks =
4362
Locks.field ~check:(Dune_lang.Syntax.since Stanza.syntax (2, 9)) ()
@@ -46,4 +65,4 @@ let decode =
4665
~check:(Dune_lang.Syntax.since Stanza.syntax (2, 8))
4766
()
4867
in
49-
{ loc; alias; deps; enabled_if; locks; applies_to; package })
68+
{ loc; alias; deps; shell; enabled_if; locks; applies_to; package })

src/dune_rules/cram/cram_stanza.mli

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@ type applies_to =
44
| Whole_subtree
55
| Files_matching_in_this_dir of Predicate_lang.Glob.t
66

7+
type shell_spec =
8+
| System_shell (** the default *)
9+
| Bash_shell
10+
| Exec_file_shell of String_with_vars.t
11+
12+
val default_shell_spec : shell_spec
13+
714
type t =
815
{ loc : Loc.t (* ; dir : Path.t *)
916
; applies_to : applies_to
1017
; alias : Alias.Name.t option
1118
; deps : Dep_conf.t Bindings.t option
19+
; shell : shell_spec
1220
; enabled_if : Blang.t
1321
; locks : Locks.t
1422
; package : Package.t option

src/dune_rules/expander.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,11 @@ module With_deps_if_necessary = struct
782782
Value.to_path_in_build_or_external v
783783
~error_loc:(String_with_vars.loc sw) ~dir:t.dir)
784784

785+
let expand_single_path t sw =
786+
let+ v = expand t ~mode:Single sw in
787+
Value.to_path_in_build_or_external v ~error_loc:(String_with_vars.loc sw)
788+
~dir:t.dir
789+
785790
let expand_str t sw =
786791
let+ v = expand t ~mode:Single sw in
787792
Value.to_string v ~dir:(Path.build t.dir)

src/dune_rules/expander.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ module With_deps_if_necessary : sig
105105

106106
val expand_path : t -> String_with_vars.t -> Path.t list Deps.t
107107

108+
val expand_single_path : t -> String_with_vars.t -> Path.t Deps.t
109+
108110
val expand_str : t -> String_with_vars.t -> string Deps.t
109111
end
110112

0 commit comments

Comments
 (0)