Skip to content

Commit

Permalink
refactor: switch to regular sexps for custom actions (#10807)
Browse files Browse the repository at this point in the history
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
  • Loading branch information
rgrinberg authored Aug 6, 2024
1 parent d5809e9 commit 4b25456
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 80 deletions.
2 changes: 1 addition & 1 deletion bin/print_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ let rec encode : Action.For_shell.t -> Dune_lang.t =
| Mkdir x -> List [ atom "mkdir"; target x ]
| Pipe (outputs, l) ->
List (atom (sprintf "pipe-%s" (Outputs.to_string outputs)) :: List.map l ~f:encode)
| Extension ext -> List [ atom "ext"; ext ]
| Extension ext -> List [ atom "ext"; Dune_sexp.Quoted_string (Sexp.to_string ext) ]
;;

let print_rule_sexp ppf (rule : Dune_engine.Reflection.Rule.t) =
Expand Down
8 changes: 4 additions & 4 deletions src/dune_engine/action.ml
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ module For_shell = struct
with type path = string
with type target = string
with type string = string
with type ext = Dune_sexp.t
with type ext = Sexp.t

module rec Ast : Ast = Ast
include Make (String) (String) (String) (String) (Dune_sexp) (Ast)
include Make (String) (String) (String) (String) (Sexp) (Ast)
end

module Relativise = Action_mapper.Make (Ast) (For_shell.Ast)
Expand Down Expand Up @@ -163,8 +163,8 @@ let for_shell t =
~f_ext:(fun ~dir (module A) ->
A.Spec.encode
A.v
(fun p -> Dune_sexp.atom_or_quoted_string (f_path p ~dir))
(fun p -> Dune_sexp.atom_or_quoted_string (f_target p ~dir)))
(fun p -> Sexp.Atom (f_path p ~dir))
(fun p -> Sexp.Atom (f_target p ~dir)))
~f_program:(fun ~dir x ->
match x with
| Ok p -> Path.reach p ~from:dir
Expand Down
2 changes: 1 addition & 1 deletion src/dune_engine/action.mli
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ module For_shell : sig
with type path := string
with type target := string
with type string := string
with type ext := Dune_sexp.t
with type ext := Sexp.t
end

(** Convert the action to a format suitable for printing *)
Expand Down
2 changes: 1 addition & 1 deletion src/dune_engine/action_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module Ext = struct
val name : string
val version : int
val is_useful_to : memoize:bool -> bool
val encode : ('p, 't) t -> ('p -> Dune_sexp.t) -> ('t -> Dune_sexp.t) -> Dune_sexp.t
val encode : ('p, 't) t -> ('p -> Sexp.t) -> ('t -> Sexp.t) -> Sexp.t
val bimap : ('a, 'b) t -> ('a -> 'x) -> ('b -> 'y) -> ('x, 'y) t

val action
Expand Down
5 changes: 1 addition & 4 deletions src/dune_patch/dune_patch.ml
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ module Spec = struct
let version = 1
let bimap patch f _ = f patch
let is_useful_to ~memoize = memoize

let encode patch input _ : Dune_lang.t =
List [ Dune_lang.atom_or_quoted_string name; input patch ]
;;
let encode patch input _ : Sexp.t = List [ Atom name; input patch ]

let action patch ~ectx:_ ~(eenv : Action.Ext.env) =
exec !Dune_engine.Clflags.display ~patch ~dir:eenv.working_dir ~stderr:eenv.stderr_to
Expand Down
6 changes: 3 additions & 3 deletions src/dune_rules/copy_line_directive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ module Spec = struct
let bimap (src, dst, merlin) f g = f src, g dst, merlin
let is_useful_to ~memoize = memoize

let encode (src, dst, merlin) path target : Dune_lang.t =
let encode (src, dst, merlin) path target : Sexp.t =
List
[ Dune_lang.atom_or_quoted_string "copy-line-directive"
[ Atom "copy-line-directive"
; path src
; target dst
; Dune_lang.atom_or_quoted_string (Bool.to_string (bool_of_merlin merlin))
; Atom (Bool.to_string (bool_of_merlin merlin))
]
;;

Expand Down
6 changes: 1 addition & 5 deletions src/dune_rules/cram/cram_exec.ml
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,7 @@ module Spec = struct
let version = 1
let bimap path f _ = f path
let is_useful_to ~memoize:_ = true

let encode script path _ : Dune_lang.t =
List [ Dune_lang.atom_or_quoted_string "cram"; path script ]
;;

let encode script path _ : Sexp.t = List [ Atom name; path script ]
let action script ~ectx:_ ~(eenv : Action.Ext.env) = run ~env:eenv.env ~script
end

Expand Down
18 changes: 8 additions & 10 deletions src/dune_rules/fetch_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,23 @@ module Spec = struct
let bimap t _ g = { t with target = g t.target }
let is_useful_to ~memoize = memoize

let encode { target; url = _, url; checksum; kind } _ encode_target : Dune_lang.t =
let encode { target; url = _, url; checksum; kind } _ encode_target : Sexp.t =
List
([ Dune_lang.atom_or_quoted_string name
([ Sexp.Atom name
; encode_target target
; Dune_lang.atom_or_quoted_string (OpamUrl.to_string url)
; Dune_lang.atom_or_quoted_string
; Atom (OpamUrl.to_string url)
; Atom
(match kind with
| `File -> "file"
| `Directory -> "directory")
]
@ (match OpamUrl.rev url with
| None -> []
| Some rev -> [ Dune_lang.atom_or_quoted_string rev ])
| Some rev -> [ Sexp.Atom rev ])
@
match checksum with
| None -> []
| Some (_, checksum) ->
[ Checksum.to_string checksum |> Dune_lang.atom_or_quoted_string ])
| Some (_, checksum) -> [ Atom (Checksum.to_string checksum) ])
;;

let action { target; url = loc_url, url; checksum; kind } ~ectx:_ ~eenv:_ =
Expand Down Expand Up @@ -280,9 +279,8 @@ module Copy = struct
let bimap t f g = { src_dir = f t.src_dir; dst_dir = g t.dst_dir }
let is_useful_to ~memoize = memoize

let encode { src_dir; dst_dir } path target =
Dune_lang.List
[ Dune_lang.atom_or_quoted_string name; path src_dir; target dst_dir ]
let encode { src_dir; dst_dir } path target : Sexp.t =
List [ Atom name; path src_dir; target dst_dir ]
;;

let action { src_dir; dst_dir } ~ectx:_ ~eenv:_ =
Expand Down
6 changes: 3 additions & 3 deletions src/dune_rules/format_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ let action =
let bimap (ver, src, dst) f g = ver, f src, g dst
let is_useful_to ~memoize = memoize

let encode (version, src, dst) path target : Dune_lang.t =
let encode (version, src, dst) path target : Sexp.t =
List
[ Dune_lang.atom_or_quoted_string "format-dune-file"
; Dune_lang.Syntax.Version.encode version
[ Atom "format-dune-file"
; Dune_lang.Syntax.Version.encode version |> Dune_sexp.to_sexp
; path src
; target dst
]
Expand Down
5 changes: 1 addition & 4 deletions src/dune_rules/install_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1088,10 +1088,7 @@ struct
let version = 1
let bimap (entries, dst) _ g = entries, g dst
let is_useful_to ~memoize = memoize

let encode (_entries, dst) _path target : Dune_lang.t =
List [ Dune_lang.atom_or_quoted_string name; target dst ]
;;
let encode (_entries, dst) _path target : Sexp.t = List [ Atom name; target dst ]

let make_entry entry path comps =
Install.Entry.set_src entry path
Expand Down
11 changes: 5 additions & 6 deletions src/dune_rules/ocamldep.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ module Merge_files_into = struct
let encode
(type src dst)
((sources, extras, target) : (src, dst) t)
(input : src -> Dune_sexp.t)
(output : dst -> Dune_sexp.t)
: Dune_sexp.t
(input : src -> Sexp.t)
(output : dst -> Sexp.t)
: Sexp.t
=
let open Dune_sexp in
List
[ atom_or_quoted_string name
[ Atom name
; List (List.map sources ~f:input)
; List (List.map ~f:atom_or_quoted_string extras)
; List (List.map ~f:(fun s -> Sexp.Atom s) extras)
; output target
]
;;
Expand Down
45 changes: 21 additions & 24 deletions src/dune_rules/pkg_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ module Substitute = struct
let bimap t f g = { t with src = f t.src; dst = g t.dst }
let is_useful_to ~memoize = memoize

let encode { expander; src; dst } input output : Dune_lang.t =
let encode { expander; src; dst } input output : Sexp.t =
let e =
let paths (p : Path.t Paths.t) = p.source_dir, p.target_dir, p.name in
( paths expander.paths
Expand All @@ -497,9 +497,8 @@ module Substitute = struct
, expander.env )
|> Digest.generic
|> Digest.to_string_raw
|> Dune_sexp.atom_or_quoted_string
in
List [ Dune_lang.atom_or_quoted_string name; e; input src; output dst ]
List [ Atom name; Atom e; input src; output dst ]
;;

let action { expander; src; dst } ~ectx:_ ~eenv:_ =
Expand Down Expand Up @@ -642,25 +641,21 @@ module Run_with_path = struct

let is_useful_to ~memoize:_ = true

let encode { prog; args; ocamlfind_destdir; pkg = _ } path _ : Dune_lang.t =
let prog =
Dune_lang.atom_or_quoted_string
@@
match prog with
| Ok p -> Path.reach p ~from:Path.root
| Error e -> e.program
let encode { prog; args; ocamlfind_destdir; pkg = _ } path _ : Sexp.t =
let prog : Sexp.t =
Atom
(match prog with
| Ok p -> Path.reach p ~from:Path.root
| Error e -> e.program)
in
let args =
Array.Immutable.to_list_map args ~f:(fun x ->
Dune_lang.List
Sexp.List
(Array.Immutable.to_list_map x ~f:(function
| String s -> Dune_lang.atom_or_quoted_string s
| String s -> Sexp.Atom s
| Path p -> path p)))
in
List
[ List ([ Dune_lang.atom_or_quoted_string name; prog ] @ args)
; path ocamlfind_destdir
]
List [ List ([ Sexp.Atom name; prog ] @ args); path ocamlfind_destdir ]
;;

let action
Expand Down Expand Up @@ -1352,20 +1347,22 @@ module Install_action = struct
}
path
target
: Dune_lang.t
: Sexp.t
=
List
[ Dune_lang.atom_or_quoted_string name
[ Atom name
; path install_file
; path config_file
; target target_dir
; Dune_lang.Encoder.option
Dune_lang.Encoder.string
(Option.map
; (match
Option.map
prefix_outside_build_dir
~f:Path.Outside_build_dir.to_string_maybe_quoted)
; Dune_lang.atom_or_quoted_string (Package.Name.to_string package)
; Dune_lang.atom
~f:Path.Outside_build_dir.to_string_maybe_quoted
with
| None -> List []
| Some s -> List [ Atom s ])
; Atom (Package.Name.to_string package)
; Atom
(match install_action with
| `Has_install_action -> "has_install_action"
| `No_install_action -> "no_install_action")
Expand Down
8 changes: 8 additions & 0 deletions src/dune_sexp/t.ml
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,11 @@ let rec to_dyn =
| Quoted_string s -> string s
| Template t -> variant "template" [ string (Template.to_string t) ]
;;

let rec to_sexp = function
| Atom (A s) -> Sexp.Atom s
| List s -> List (List.map ~f:to_sexp s)
| Quoted_string s -> List [ Atom "quoted"; Atom s ]
| Template ({ quoted; parts = _; loc = _ } as t) ->
List [ Atom "template"; Atom (Bool.to_string quoted); Atom (Template.to_string t) ]
;;
1 change: 1 addition & 0 deletions src/dune_sexp/t.mli
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ module Deprecated : sig
end

val to_dyn : t Dyn.builder
val to_sexp : t -> Sexp.t
21 changes: 7 additions & 14 deletions src/promote/diff_action.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,14 @@ module Spec = struct
let bimap t path target = Diff.map t ~path ~target
let is_useful_to ~memoize:_ = true

let encode { Diff.optional; mode; file1; file2 } input output : Dune_sexp.t =
let mode =
Dune_sexp.atom_or_quoted_string
@@
match mode with
| Binary -> "binary"
| Text -> "text"
let encode { Diff.optional; mode; file1; file2 } input output : Sexp.t =
let mode : Sexp.t =
Atom
(match mode with
| Binary -> "binary"
| Text -> "text")
in
List
[ Dune_sexp.atom_or_quoted_string name
; Dune_sexp.Encoder.bool optional
; mode
; input file1
; output file2
]
List [ Atom name; Atom (Bool.to_string optional); mode; input file1; output file2 ]
;;

let action diff ~(ectx : Dune_engine.Action.Ext.context) ~eenv:_ =
Expand Down

0 comments on commit 4b25456

Please sign in to comment.