Skip to content

Commit 80a5248

Browse files
committed
cram_exec - add shell_spec to action spec
Signed-off-by: Haochen Kotoi-Xie <hx@kxc.inc>
1 parent 1dad660 commit 80a5248

File tree

4 files changed

+66
-13
lines changed

4 files changed

+66
-13
lines changed

src/dune_rules/action_unexpanded.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ let rec expand (t : Dune_lang.Action.t) ~context : Action.t Action_expander.t =
480480
O.Pipe (outputs, l)
481481
| Cram script ->
482482
let+ script = E.dep script in
483-
Cram_exec.action script
483+
Cram_exec.(action { script; shell_spec = Shell_spec.default })
484484
| Withenv _ | Substitute _ | Patch _ ->
485485
(* these can only be provided by the package language which isn't expanded here *)
486486
assert false

src/dune_rules/cram/cram_exec.ml

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
open Import
22

3+
module Shell_spec = struct
4+
type 'path t =
5+
| System_shell
6+
| Bash_shell
7+
| Exec_file_shell of 'path
8+
9+
let default = System_shell
10+
11+
let conv f = function
12+
| System_shell -> System_shell
13+
| Bash_shell -> Bash_shell
14+
| Exec_file_shell p -> Exec_file_shell (f p)
15+
end
16+
317
module Sanitizer : sig
418
[@@@ocaml.warning "-32"]
519

@@ -376,7 +390,7 @@ let create_sh_script cram_stanzas ~temp_dir : sh_script Fiber.t =
376390
let _display_with_bars s =
377391
List.iter (String.split_lines s) ~f:(Printf.eprintf "| %s\n")
378392

379-
let run ~env ~script lexbuf : string Fiber.t =
393+
let run ~env ~script ~shell_spec lexbuf : string Fiber.t =
380394
let temp_dir =
381395
let suffix =
382396
let basename = Path.basename script in
@@ -414,8 +428,14 @@ let run ~env ~script lexbuf : string Fiber.t =
414428
let open Fiber.O in
415429
let+ () =
416430
let sh =
417-
let path = Env_path.path Env.initial in
418-
Option.value_exn (Bin.which ~path "sh")
431+
let which shell_name =
432+
let path = Env_path.path Env.initial in
433+
Option.value_exn (Bin.which ~path shell_name)
434+
in
435+
match shell_spec with
436+
| Shell_spec.System_shell -> which "sh"
437+
| Bash_shell -> which "bash"
438+
| Exec_file_shell sh -> sh
419439
in
420440
let metadata =
421441
let name =
@@ -436,24 +456,38 @@ let run ~env ~script lexbuf : string Fiber.t =
436456
let sanitized = sanitize ~parent_script:sh_script.script raw in
437457
compose_cram_output sanitized
438458

439-
let run ~env ~script =
440-
run_expect_test script ~f:(fun lexbuf -> run ~env ~script lexbuf)
459+
let run ~env ~script ~shell_spec =
460+
run_expect_test script ~f:(fun lexbuf -> run ~env ~script ~shell_spec lexbuf)
461+
462+
type ('path, _) spec =
463+
{ script : 'path
464+
; shell_spec : 'path Shell_spec.t
465+
}
441466

442467
module Spec = struct
443-
type ('path, _) t = 'path
468+
type ('path, 'target) t = ('path, 'target) spec
444469

445470
let name = "cram"
446471

447472
let version = 1
448473

449-
let bimap path f _ = f path
474+
let bimap spec f _ =
475+
{ script = f spec.script; shell_spec = Shell_spec.conv f spec.shell_spec }
450476

451477
let is_useful_to ~distribute:_ ~memoize:_ = true
452478

453-
let encode script path _ : Dune_lang.t =
454-
List [ Dune_lang.atom_or_quoted_string "cram"; path script ]
479+
let encode { script; shell_spec } path _ : Dune_sexp.t =
480+
let atom = Dune_lang.atom_or_quoted_string in
481+
let sh : Dune_lang.t =
482+
match shell_spec with
483+
| System_shell -> List [ atom "system" ]
484+
| Bash_shell -> List [ atom "bash" ]
485+
| Exec_file_shell p -> List [ atom "executable"; path p ]
486+
in
487+
List [ atom "cram"; path script; List [ atom "shell"; sh ] ]
455488

456-
let action script ~ectx:_ ~(eenv : Action.Ext.env) = run ~env:eenv.env ~script
489+
let action { script; shell_spec } ~ectx:_ ~(eenv : Action.Ext.env) =
490+
run ~env:eenv.env ~script ~shell_spec
457491
end
458492

459493
let action script =

src/dune_rules/cram/cram_exec.mli

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
11
open Import
22

3-
val action : Path.t -> Action.t
3+
module Shell_spec : sig
4+
type 'path t =
5+
| System_shell
6+
| Bash_shell
7+
| Exec_file_shell of 'path
8+
9+
val conv : ('p1 -> 'p2) -> 'p1 t -> 'p2 t
10+
11+
val default : _ t
12+
end
13+
14+
type ('path, _) spec =
15+
{ script : 'path
16+
; shell_spec : 'path Shell_spec.t
17+
}
18+
19+
val action : (Path.t, Import.Path.Build.t) spec -> Action.t

src/dune_rules/cram/cram_rules.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ let test_rule ~sctx ~expander ~dir (spec : effective)
6161
in
6262
let action =
6363
Action.progn
64-
[ Cram_exec.action (Path.build script)
64+
[ Cram_exec.action
65+
{ script = Path.build script
66+
; shell_spec = Cram_exec.Shell_spec.default
67+
}
6568
; Diff
6669
{ Diff.optional = true
6770
; mode = Text

0 commit comments

Comments
 (0)