Skip to content

Commit accca46

Browse files
authored
refactor: add [Action_ext] to simplify custom action definitions (#10840)
Signed-off-by: Rudi Grinberg <[email protected]>
1 parent b962278 commit accca46

File tree

18 files changed

+109
-195
lines changed

18 files changed

+109
-195
lines changed

boot/libs.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ let local_libraries =
5050
; ("src/fswatch_win", Some "Fswatch_win", false, None)
5151
; ("src/dune_file_watcher", Some "Dune_file_watcher", false, None)
5252
; ("src/dune_engine", Some "Dune_engine", false, None)
53+
; ("src/action_ext", Some "Action_ext", false, None)
5354
; ("src/promote", Some "Promote", false, None)
5455
; ("src/ocaml-config", Some "Ocaml_config", false, None)
5556
; ("src/ocaml", Some "Ocaml", false, None)

src/action_ext/action_ext.ml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
open Stdune
2+
module Action = Dune_engine.Action
3+
4+
module Make (S : Action.Ext.Spec) = struct
5+
module Spec = struct
6+
include S
7+
8+
let encode t f g =
9+
let open Sexp in
10+
List [ Atom name; Atom (Int.to_string version); S.encode t f g ]
11+
;;
12+
end
13+
14+
let action p =
15+
let module M = struct
16+
type path = Path.t
17+
type target = Path.Build.t
18+
19+
module Spec = Spec
20+
21+
let v = p
22+
end
23+
in
24+
Action.Extension (module M)
25+
;;
26+
end

src/action_ext/action_ext.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
open Stdune
2+
3+
module Make (S : Dune_engine.Action.Ext.Spec) : sig
4+
val action : (Path.t, Path.Build.t) S.t -> Dune_engine.Action.t
5+
end

src/action_ext/dune

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(library
2+
(name action_ext)
3+
(libraries dune_engine fiber stdune))

src/dune_patch/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(library
22
(name dune_patch)
3-
(libraries stdune fiber dune_engine dune_lang dune_re)
3+
(libraries stdune fiber dune_engine dune_lang action_ext dune_re)
44
(instrumentation
55
(backend bisect_ppx)))

src/dune_patch/dune_patch.ml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,29 +104,20 @@ module Spec = struct
104104
type ('path, 'target) t = 'path
105105

106106
let name = "patch"
107-
let version = 1
107+
let version = 2
108108
let bimap patch f _ = f patch
109109
let is_useful_to ~memoize = memoize
110-
let encode patch input _ : Sexp.t = List [ Atom name; input patch ]
110+
let encode patch input _ : Sexp.t = input patch
111111

112112
let action patch ~ectx:_ ~(eenv : Action.env) =
113113
exec !Dune_engine.Clflags.display ~patch ~dir:eenv.working_dir ~stderr:eenv.stderr_to
114114
;;
115115
end
116116

117117
(* CR-someday alizter: This should be an action builder. *)
118-
let action ~patch =
119-
let module M = struct
120-
type path = Path.t
121-
type target = Path.Build.t
118+
module Action = Action_ext.Make (Spec)
122119

123-
module Spec = Spec
124-
125-
let v = patch
126-
end
127-
in
128-
Action.Extension (module M)
129-
;;
120+
let action ~patch = Action.action patch
130121

131122
module For_tests = struct
132123
let exec = exec

src/dune_rules/copy_line_directive.ml

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,12 @@ module Spec = struct
7070
type ('path, 'target) t = 'path * 'target * merlin
7171

7272
let name = "copy-line-directive"
73-
let version = 1
73+
let version = 2
7474
let bimap (src, dst, merlin) f g = f src, g dst, merlin
7575
let is_useful_to ~memoize = memoize
7676

7777
let encode (src, dst, merlin) path target : Sexp.t =
78-
List
79-
[ Atom "copy-line-directive"
80-
; path src
81-
; target dst
82-
; Atom (Bool.to_string (bool_of_merlin merlin))
83-
]
78+
List [ path src; target dst; Atom (Bool.to_string (bool_of_merlin merlin)) ]
8479
;;
8580

8681
let action (src, dst, merlin) ~ectx:_ ~eenv:_ =
@@ -97,17 +92,10 @@ module Spec = struct
9792
;;
9893
end
9994

100-
let action (context : Context.t) ~src ~dst =
101-
let module M = struct
102-
type path = Path.t
103-
type target = Path.Build.t
104-
105-
module Spec = Spec
95+
module A = Action_ext.Make (Spec)
10696

107-
let v = src, dst, if Context.merlin context then Spec.Yes else No
108-
end
109-
in
110-
Action.Extension (module M)
97+
let action (context : Context.t) ~src ~dst =
98+
A.action (src, dst, if Context.merlin context then Spec.Yes else No)
11199
;;
112100

113101
let builder context ~src ~dst =

src/dune_rules/cram/cram_exec.ml

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -455,22 +455,13 @@ module Spec = struct
455455
type ('path, _) t = 'path
456456

457457
let name = "cram"
458-
let version = 1
458+
let version = 2
459459
let bimap path f _ = f path
460460
let is_useful_to ~memoize:_ = true
461-
let encode script path _ : Sexp.t = List [ Atom name; path script ]
461+
let encode script path _ : Sexp.t = List [ path script ]
462462
let action script ~ectx:_ ~(eenv : Action.env) = run ~env:eenv.env ~script
463463
end
464464

465-
let action script =
466-
let module M = struct
467-
type path = Path.t
468-
type target = Path.Build.t
465+
module Action = Action_ext.Make (Spec)
469466

470-
module Spec = Spec
471-
472-
let v = script
473-
end
474-
in
475-
Action.Extension (module M)
476-
;;
467+
let action = Action.action

src/dune_rules/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
dune_targets
3030
opam_core
3131
promote
32+
action_ext
3233
build_path_prefix_map
3334
dune_engine
3435
dune_vcs

src/dune_rules/fetch_rules.ml

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,14 @@ module Spec = struct
7373
}
7474

7575
let name = "source-fetch"
76-
let version = 1
76+
let version = 2
7777
let bimap t _ g = { t with target = g t.target }
7878
let is_useful_to ~memoize = memoize
7979

8080
let encode { target; url = _, url; checksum; kind } _ encode_target : Sexp.t =
8181
List
82-
([ Sexp.Atom name
83-
; encode_target target
84-
; Atom (OpamUrl.to_string url)
82+
([ encode_target target
83+
; Sexp.Atom (OpamUrl.to_string url)
8584
; Atom
8685
(match kind with
8786
| `File -> "file"
@@ -128,18 +127,9 @@ module Spec = struct
128127
;;
129128
end
130129

131-
let action ~url ~checksum ~target ~kind =
132-
let module M = struct
133-
type path = Path.t
134-
type target = Path.Build.t
130+
module A = Action_ext.Make (Spec)
135131

136-
module Spec = Spec
137-
138-
let v = { Spec.target; checksum; url; kind }
139-
end
140-
in
141-
Action.Extension (module M)
142-
;;
132+
let action ~url ~checksum ~target ~kind = A.action { Spec.target; checksum; url; kind }
143133

144134
let extract_checksums_and_urls (lockdir : Dune_pkg.Lock_dir.t) =
145135
Package.Name.Map.fold
@@ -275,12 +265,12 @@ module Copy = struct
275265
}
276266

277267
let name = "copy-dir"
278-
let version = 1
268+
let version = 2
279269
let bimap t f g = { src_dir = f t.src_dir; dst_dir = g t.dst_dir }
280270
let is_useful_to ~memoize = memoize
281271

282272
let encode { src_dir; dst_dir } path target : Sexp.t =
283-
List [ Atom name; path src_dir; target dst_dir ]
273+
List [ path src_dir; target dst_dir ]
284274
;;
285275

286276
let action { src_dir; dst_dir } ~ectx:_ ~eenv:_ =
@@ -300,18 +290,9 @@ module Copy = struct
300290
;;
301291
end
302292

303-
let action ~src_dir ~dst_dir =
304-
let module M = struct
305-
type path = Path.t
306-
type target = Path.Build.t
293+
module A = Action_ext.Make (Spec)
307294

308-
module Spec = Spec
309-
310-
let v = { Spec.dst_dir; src_dir }
311-
end
312-
in
313-
Action.Extension (module M)
314-
;;
295+
let action ~src_dir ~dst_dir = A.action { Spec.dst_dir; src_dir }
315296
end
316297

317298
let fetch ~target kind (source : Source.t) =

0 commit comments

Comments
 (0)