-
Notifications
You must be signed in to change notification settings - Fork 409
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a "fmt" extension in dune-project files. When used, it will setup a `@fmt` alias that will call `ocamlformat` on ocaml source code, and `refmt` on reason source code. The tools are not configured by dune. Signed-off-by: Etienne Millon <me@emillon.org>
- Loading branch information
Showing
23 changed files
with
248 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
open Import | ||
|
||
let flag_of_kind : Ml_kind.t -> _ = | ||
function | ||
| Impl -> "--impl" | ||
| Intf -> "--intf" | ||
|
||
let dep loc x = String_with_vars.make_macro loc "dep" x | ||
|
||
type outcome = | ||
| Format_using of Action.Unexpanded.t | ||
| Not_formattable | ||
| Missing_tool of string | ||
|
||
let ocamlformat_bin = lazy (Bin.which "ocamlformat") | ||
|
||
let ocamlformat_action ofmt kind loc path = | ||
let flag = flag_of_kind kind in | ||
let exe = dep loc @@ Path.to_string ofmt in | ||
let args = | ||
[ String_with_vars.make_text loc flag | ||
; dep loc path | ||
; String_with_vars.make_text loc "-o" | ||
; String_with_vars.make_var loc "targets" | ||
] | ||
in | ||
Action.Unexpanded.Run (exe, args) | ||
|
||
let ocamlformat kind loc path = | ||
match Lazy.force ocamlformat_bin with | ||
| Some ofmt -> Format_using (ocamlformat_action ofmt kind loc path) | ||
| None -> Missing_tool "ocamlformat" | ||
|
||
let refmt_action rfmt loc path = | ||
let exe = dep loc @@ Path.to_string rfmt in | ||
let args = [dep loc path] in | ||
Action.Unexpanded.Redirect | ||
( Stdout | ||
, String_with_vars.make_var loc "targets" | ||
, Run (exe, args) | ||
) | ||
|
||
let refmt_bin = lazy (Bin.which "refmt") | ||
|
||
let refmt loc path = | ||
match Lazy.force refmt_bin with | ||
| Some rfmt -> Format_using (refmt_action rfmt loc path) | ||
| None -> Missing_tool "refmt" | ||
|
||
let formatter_for_path loc path = | ||
match Filename.extension path with | ||
| ".ml" -> ocamlformat Ml_kind.Impl loc path | ||
| ".mli" -> ocamlformat Ml_kind.Intf loc path | ||
| ".re" | ||
| ".rei" -> refmt loc path | ||
| _ -> Not_formattable | ||
|
||
let add_alias_format sctx loc ~dir ~scope action = | ||
let alias_conf = | ||
{ Dune_file.Alias_conf.name = "fmt" | ||
; deps = [] | ||
; action = Some (loc, action) | ||
; locks = [] | ||
; package = None | ||
; enabled_if = None | ||
; loc | ||
} | ||
in | ||
Simple_rules.alias sctx ~dir ~scope alias_conf | ||
|
||
let run_rule ~target ~action ~loc = | ||
{ Dune_file.Rule.targets = Static [target] | ||
; action = (loc, action) | ||
; mode = Standard | ||
; deps = [] | ||
; locks = [] | ||
; loc | ||
} | ||
|
||
let diff file1 file2 = | ||
Action.Unexpanded.Diff | ||
{ optional = false | ||
; mode = Text | ||
; file1 | ||
; file2 | ||
} | ||
|
||
let rules_for_file action loc path = | ||
let target = path ^ ".formatted" in | ||
let format_rule = run_rule ~loc ~target ~action in | ||
let diff_action = | ||
diff | ||
(dep loc path) | ||
(String_with_vars.make_text loc target) | ||
in | ||
(format_rule, diff_action) | ||
|
||
let setup_formatters files loc ~setup_rules = | ||
Path.Set.fold files ~init:String.Set.empty ~f:(fun file acc -> | ||
let path = Path.basename file in | ||
match formatter_for_path loc path with | ||
| Format_using action -> | ||
setup_rules (rules_for_file action loc path); | ||
acc | ||
| Not_formattable -> acc | ||
| Missing_tool tool -> String.Set.add acc tool) | ||
|
||
let gen_rules sctx ~loc ~dir ~scope = | ||
let add_alias action = add_alias_format sctx loc ~dir ~scope action in | ||
let setup_rules (format_rule, diff_action) = | ||
let _ : Path.t list = | ||
Simple_rules.user_rule sctx ~dir ~scope format_rule | ||
in | ||
add_alias diff_action | ||
in | ||
let files = | ||
File_tree.files_of | ||
(Super_context.file_tree sctx) | ||
(Path.drop_build_context_exn dir) | ||
in | ||
let unknown_tools = setup_formatters files loc ~setup_rules in | ||
if not (String.Set.is_empty unknown_tools) then | ||
let msg = | ||
Printf.sprintf | ||
"Cannot find the following tools, skipping associated files: %s\n" | ||
(String.concat ~sep:", " (String.Set.to_list unknown_tools)) | ||
in | ||
add_alias (Echo [String_with_vars.make_text loc msg]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
open Import | ||
|
||
(** Setup automatic format rules for the given dir. | ||
If ocamlformat is not available in $PATH, just display an error message | ||
when the alias is built. *) | ||
val gen_rules: | ||
Super_context.t | ||
-> loc:Loc.t | ||
-> dir:Path.t | ||
-> scope:Scope.t | ||
-> unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(library | ||
(name lib) | ||
) |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/disabled/dune-project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
(lang dune 1.2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let x = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
val x : int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(library | ||
(name lib_reason) | ||
) |
2 changes: 2 additions & 0 deletions
2
test/blackbox-tests/test-cases/formatting/enabled/dune-project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
(lang dune 1.2) | ||
(using fmt 1.0) |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/enabled/ocaml_file.ml.orig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let y=() |
2 changes: 2 additions & 0 deletions
2
test/blackbox-tests/test-cases/formatting/enabled/ocaml_file.mli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
val y : | ||
unit |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/enabled/reason_file.re.orig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let y = (); |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/enabled/reason_file.rei
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let y : unit; |
3 changes: 3 additions & 0 deletions
3
test/blackbox-tests/test-cases/formatting/enabled/subdir/dune
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(library | ||
(name lib) | ||
) |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/formatting/enabled/subdir/lib.ml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
let x = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
Formatting can be checked using the @fmt target: | ||
|
||
$ cp enabled/ocaml_file.ml.orig enabled/ocaml_file.ml | ||
$ cp enabled/reason_file.re.orig enabled/reason_file.re | ||
$ dune build --root enabled @fmt | ||
Entering directory 'enabled' | ||
File "reason_file.rei", line 1, characters 0-0: | ||
Files _build/default/reason_file.rei and _build/default/reason_file.rei.formatted differ. | ||
File "reason_file.re", line 1, characters 0-0: | ||
Files _build/default/reason_file.re and _build/default/reason_file.re.formatted differ. | ||
File "ocaml_file.mli", line 1, characters 0-0: | ||
Files _build/default/ocaml_file.mli and _build/default/ocaml_file.mli.formatted differ. | ||
File "ocaml_file.ml", line 1, characters 0-0: | ||
Files _build/default/ocaml_file.ml and _build/default/ocaml_file.ml.formatted differ. | ||
File "subdir/lib.ml", line 1, characters 0-0: | ||
Files _build/default/subdir/lib.ml and _build/default/subdir/lib.ml.formatted differ. | ||
[1] | ||
|
||
And fixable files can be promoted: | ||
|
||
$ cd enabled; dune promote ocaml_file.ml reason_file.re | ||
Promoting _build/default/ocaml_file.ml.formatted to ocaml_file.ml. | ||
Promoting _build/default/reason_file.re.formatted to reason_file.re. | ||
$ cat enabled/ocaml_file.ml | ||
let y = () | ||
$ cat enabled/reason_file.re | ||
let y = (); | ||
|
||
For projects without (using fmt), this does nothing: | ||
|
||
$ dune build --root disabled @fmt | ||
Entering directory 'disabled' | ||
From the command line: | ||
Error: Alias "fmt" is empty. | ||
It is not defined in . or any of its descendants. | ||
[1] |