Skip to content

Commit a09c51f

Browse files
authored
Add (format-dune-file) action (#11166)
Signed-off-by: Nicolás Ojeda Bär <[email protected]>
1 parent 41212a5 commit a09c51f

File tree

12 files changed

+134
-30
lines changed

12 files changed

+134
-30
lines changed

doc/changes/11166.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add `(format-dune-file <src> <dst>)` action. (#11166, @nojb)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
cat
2+
---
3+
4+
.. highlight:: dune
5+
6+
.. describe:: (format-dune-file <src> <dst>)
7+
8+
Output the formatted contents of the file ``<src>`` to ``<dst>``. The source
9+
file is assumed to contain S-expressions. Note that the precise formatting
10+
can depend on the version of the Dune language used by containing project.
11+
12+
Example::
13+
14+
(format-dune-file file.sexp file.sexp.formatted)

doc/reference/actions/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ The following constructions are available:
5353
copy#
5454
write-file
5555
pipe-outputs
56+
format-dune-file
5657

5758
.. grid-item::
5859

src/dune_lang/action.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ type t =
176176
| Substitute of String_with_vars.t * String_with_vars.t
177177
| Withenv of String_with_vars.t Env_update.t list * t
178178
| When of Slang.blang * t
179+
| Format_dune_file of String_with_vars.t * String_with_vars.t
179180

180181
let is_dev_null t = String_with_vars.is_pform t (Var Dev_null)
181182

@@ -350,6 +351,11 @@ let cstrs_dune_file t =
350351
, Syntax.since Stanza.syntax (2, 7)
351352
>>> let+ script = sw in
352353
Cram script )
354+
; ( "format-dune-file"
355+
, Syntax.since Stanza.syntax (3, 18)
356+
>>> let+ src = sw
357+
and+ dst = sw in
358+
Format_dune_file (src, dst) )
353359
]
354360
;;
355361

@@ -458,6 +464,7 @@ let rec encode =
458464
List [ atom "withenv"; List (List.map ~f:Env_update.encode ops); encode t ]
459465
| When (condition, action) ->
460466
List [ atom "when"; Slang.encode_blang condition; encode action ]
467+
| Format_dune_file (src, dst) -> List [ atom "format-dune-file"; sw src; sw dst ]
461468
;;
462469

463470
(* In [Action_exec] we rely on one-to-one mapping between the cwd-relative paths
@@ -495,7 +502,8 @@ let ensure_at_most_one_dynamic_run ~loc action =
495502
| Diff _
496503
| Substitute _
497504
| Patch _
498-
| Cram _ -> false
505+
| Cram _
506+
| Format_dune_file _ -> false
499507
| Pipe (_, ts) | Progn ts | Concurrent ts ->
500508
List.fold_left ts ~init:false ~f:(fun acc t ->
501509
let have_dyn = loop t in
@@ -597,6 +605,7 @@ let rec map_string_with_vars t ~f =
597605
When
598606
( blang_map_string_with_vars condition ~f:(slang_map_string_with_vars ~f)
599607
, map_string_with_vars t ~f )
608+
| Format_dune_file (src, dst) -> Format_dune_file (f src, f dst)
600609
;;
601610

602611
let remove_locs = map_string_with_vars ~f:String_with_vars.remove_locs

src/dune_lang/action.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ type t =
119119
| Substitute of String_with_vars.t * String_with_vars.t
120120
| Withenv of String_with_vars.t Env_update.t list * t
121121
| When of Slang.blang * t
122+
| Format_dune_file of String_with_vars.t * String_with_vars.t
122123

123124
val encode : t Encoder.t
124125
val decode_dune_file : t Decoder.t

src/dune_rules/action_unexpanded.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,17 @@ let rec expand (t : Dune_lang.Action.t) : Action.t Action_expander.t =
566566
| Cram script ->
567567
let+ script = E.dep script in
568568
Cram_exec.action script
569+
| Format_dune_file (src, dst) ->
570+
A.with_expander (fun expander ->
571+
let open Memo.O in
572+
let+ version =
573+
let dir = Expander.dir expander in
574+
Dune_load.find_project ~dir >>| Dune_project.dune_version
575+
in
576+
let open Action_expander.O in
577+
let+ src = E.dep src
578+
and+ dst = E.target dst in
579+
Format_dune_file.action ~version src dst)
569580
| Withenv _ | Substitute _ | Patch _ | When _ ->
570581
(* these can only be provided by the package language which isn't expanded here *)
571582
assert false

src/dune_rules/format_dune_file.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
open Import
2+
3+
let action =
4+
let module Spec = struct
5+
type ('path, 'target) t = Dune_lang.Syntax.Version.t * 'path * 'target
6+
7+
let name = "format-dune-file"
8+
let version = 1
9+
let bimap (ver, src, dst) f g = ver, f src, g dst
10+
let is_useful_to ~memoize = memoize
11+
12+
let encode (version, src, dst) path target : Sexp.t =
13+
List
14+
[ Dune_lang.Syntax.Version.encode version |> Dune_sexp.to_sexp
15+
; path src
16+
; target dst
17+
]
18+
;;
19+
20+
let action (version, src, dst) ~ectx:_ ~eenv:_ =
21+
Dune_lang.Format.format_action ~version ~src ~dst;
22+
Fiber.return ()
23+
;;
24+
end
25+
in
26+
let module A = Action_ext.Make (Spec) in
27+
fun ~version (src : Path.t) (dst : Path.Build.t) -> A.action (version, src, dst)
28+
;;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
open Import
2+
3+
val action : version:Dune_lang.Syntax.Version.t -> Path.t -> Path.Build.t -> Action.t

src/dune_rules/format_rules.ml

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,6 @@ let depend_on_files ~named dir =
2727

2828
let formatted_dir_basename = ".formatted"
2929

30-
let action =
31-
let module Spec = struct
32-
type ('path, 'target) t = Dune_lang.Syntax.Version.t * 'path * 'target
33-
34-
let name = "format-dune-file"
35-
let version = 1
36-
let bimap (ver, src, dst) f g = ver, f src, g dst
37-
let is_useful_to ~memoize = memoize
38-
39-
let encode (version, src, dst) path target : Sexp.t =
40-
List
41-
[ Dune_lang.Syntax.Version.encode version |> Dune_sexp.to_sexp
42-
; path src
43-
; target dst
44-
]
45-
;;
46-
47-
let action (version, src, dst) ~ectx:_ ~eenv:_ =
48-
Dune_lang.Format.format_action ~version ~src ~dst;
49-
Fiber.return ()
50-
;;
51-
end
52-
in
53-
let module A = Action_ext.Make (Spec) in
54-
fun ~version (src : Path.t) (dst : Path.Build.t) -> A.action (version, src, dst)
55-
;;
56-
5730
module Alias = struct
5831
let fmt ~dir = Alias.make Alias0.fmt ~dir
5932
end
@@ -217,7 +190,7 @@ let gen_rules_output
217190
let { Action_builder.With_targets.build; targets } =
218191
(let open Action_builder.O in
219192
let+ () = Action_builder.path input in
220-
Action.Full.make (action ~version input output))
193+
Action.Full.make (Format_dune_file.action ~version input output))
221194
|> Action_builder.with_file_targets ~file_targets:[ output ]
222195
in
223196
let rule = Rule.make ~mode:Standard ~targets build in

src/dune_rules/stanzas/rule_conf.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ let atom_table =
6262
; "aliases", Field
6363
; "alias", Field
6464
; "enabled_if", Field
65+
; "format-dune-file", Since ((3, 18), Action)
6566
; "package", Since ((3, 8), Field)
6667
]
6768
;;

0 commit comments

Comments
 (0)