-
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 (ocamlformat) field to libraries and executables. When present, it will setup a `@format` alias that will call `ocamlformat` on source files. Reason files are ignored. It is possible to exclude more modules using OSL. The ocamlformat configuration is determined by ocamlformat itself. Signed-off-by: Etienne Millon <me@emillon.org>
- Loading branch information
Showing
30 changed files
with
252 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,112 @@ | ||
open Import | ||
|
||
let flag_of_kind : Ml_kind.t -> _ = | ||
function | ||
| Impl -> "--impl" | ||
| Intf -> "--intf" | ||
|
||
let add_alias_format sctx loc ~dir ~scope action = | ||
let alias_conf = | ||
{ Dune_file.Alias_conf.name = "format" | ||
; deps = [] | ||
; action = Some (loc, action) | ||
; locks = [] | ||
; package = None | ||
; enabled_if = None | ||
; loc | ||
} | ||
in | ||
Simple_rules.alias sctx ~dir ~scope alias_conf | ||
|
||
let sv = String_with_vars.virt_text __POS__ | ||
|
||
let target_var = String_with_vars.virt_var __POS__ "targets" | ||
|
||
let run_rule ~target ~exe ~args ~loc = | ||
{ Dune_file.Rule.targets = Static [target] | ||
; action = (loc, Run (exe, args)) | ||
; mode = Standard | ||
; deps = [] | ||
; locks = [] | ||
; loc | ||
} | ||
|
||
let diff file1 file2 = | ||
Action.Unexpanded.Diff | ||
{ optional = false | ||
; mode = Text | ||
; file1 | ||
; file2 | ||
} | ||
|
||
let rules_for_ocaml_file loc ocamlformat kind path = | ||
let flag = flag_of_kind kind in | ||
let path = Path.basename path in | ||
let formatted = path ^ ".formatted" in | ||
let dep x = String_with_vars.make_macro loc "dep" x in | ||
let format_rule = | ||
run_rule | ||
~loc | ||
~target:formatted | ||
~exe:(dep @@ Path.to_string ocamlformat) | ||
~args:[sv flag; dep path; sv "-o"; target_var] | ||
in | ||
let diff_action = diff (dep path) (sv formatted) in | ||
(format_rule, diff_action) | ||
|
||
let rules_for_file loc ocamlformat kind (src:Module.File.t) = | ||
match src.syntax with | ||
| Reason -> None | ||
| OCaml -> Some (rules_for_ocaml_file loc ocamlformat kind src.path) | ||
|
||
module Value = struct | ||
type t = Module.t | ||
type key = Module.Name.t | ||
let key m = Module.name m | ||
end | ||
|
||
module OSL = Ordered_set_lang.Make(Module.Name)(Value) | ||
|
||
let iter_modules loc all_modules modules_to_format ~f = | ||
let parse ~loc:_ s = | ||
let name = Module.Name.of_string s in | ||
match Module.Name.Map.find all_modules name with | ||
| Some m -> m | ||
| None -> Errors.fail loc "Cannot find module %s" s | ||
in | ||
let modules = | ||
OSL.eval | ||
modules_to_format | ||
~standard:(Module.Name.Map.values all_modules) | ||
~parse | ||
in | ||
List.iter modules ~f | ||
|
||
let gen_rules sctx (buildable:Dune_file.Buildable.t) ~dir ~scope all_modules = | ||
let loc = buildable.loc in | ||
let add_alias action = add_alias_format sctx loc ~dir ~scope action in | ||
let gen_rules_for_module ocamlformat m = | ||
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 | ||
Module.iter m ~f:(fun kind src -> | ||
Option.iter | ||
~f:setup_rules | ||
(rules_for_file loc ocamlformat kind src)) | ||
in | ||
match buildable.ocamlformat with | ||
| No_format -> () | ||
| Format modules_to_format -> | ||
match Bin.which "ocamlformat" with | ||
| None -> | ||
let msg = "Cannot find ocamlformat, skipping.\n" in | ||
add_alias (Echo [sv msg]) | ||
| Some ocamlformat -> | ||
iter_modules | ||
loc | ||
all_modules | ||
modules_to_format | ||
~f:(gen_rules_for_module ocamlformat) |
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,12 @@ | ||
open Import | ||
|
||
(** Setup ocamlformat rules for the given [Buildable.t]. | ||
If ocamlformat is not available in $PATH, just display an error message | ||
when the alias is built. *) | ||
val gen_rules : | ||
Super_context.t | ||
-> Dune_file.Buildable.t | ||
-> dir:Path.t | ||
-> scope:Scope.t | ||
-> Module.t Module.Name.Map.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
(executable | ||
(name ignored) | ||
) |
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,4 @@ | ||
(executable | ||
(name hello) | ||
(ocamlformat) | ||
) |
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 @@ | ||
let () = print_endline | ||
"hello" |
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 @@ | ||
let () = | ||
print_endline | ||
"hello" |
Empty file.
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,4 @@ | ||
(library | ||
(name lib) | ||
(ocamlformat) | ||
) |
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,4 @@ | ||
let | ||
n | ||
= | ||
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,4 @@ | ||
let | ||
n | ||
= | ||
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,6 @@ | ||
(library | ||
(name lib_osl) | ||
(ocamlformat | ||
(modules :standard \ excluded) | ||
) | ||
) |
4 changes: 4 additions & 0 deletions
4
test/blackbox-tests/test-cases/ocamlformat/lib_osl/excluded.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,4 @@ | ||
let | ||
n | ||
= | ||
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,4 @@ | ||
(library | ||
(name lib_reason) | ||
(ocamlformat) | ||
) |
1 change: 1 addition & 0 deletions
1
test/blackbox-tests/test-cases/ocamlformat/lib_reason/ocaml_file.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 y=() |
2 changes: 2 additions & 0 deletions
2
test/blackbox-tests/test-cases/ocamlformat/lib_reason/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/ocamlformat/lib_reason/reason_file.re
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/ocamlformat/lib_reason/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; |
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,28 @@ | ||
Formatting can be checked using the @format target: | ||
|
||
$ cp lib/lib.ml.orig lib/lib.ml | ||
$ dune build @format --diff-command false | ||
sh (internal) (exit 1) | ||
(cd _build/default && /bin/sh -c 'false exe/hello.ml exe/hello.ml.formatted') | ||
sh (internal) (exit 1) | ||
(cd _build/default && /bin/sh -c 'false lib/lib.ml lib/lib.ml.formatted') | ||
sh (internal) (exit 1) | ||
(cd _build/default && /bin/sh -c 'false lib/lib.mli lib/lib.mli.formatted') | ||
sh (internal) (exit 1) | ||
(cd _build/default && /bin/sh -c 'false lib_osl/b.ml lib_osl/b.ml.formatted') | ||
sh (internal) (exit 1) | ||
(cd _build/default && /bin/sh -c 'false lib_osl/a.ml lib_osl/a.ml.formatted') | ||
sh (internal) (exit 1) | ||
(cd _build/default && /bin/sh -c 'false lib_reason/ocaml_file.ml lib_reason/ocaml_file.ml.formatted') | ||
sh (internal) (exit 1) | ||
(cd _build/default && /bin/sh -c 'false lib_reason/ocaml_file.mli lib_reason/ocaml_file.mli.formatted') | ||
sh (internal) (exit 1) | ||
(cd _build/default && /bin/sh -c 'false test/test.ml test/test.ml.formatted') | ||
[1] | ||
|
||
And fixable files can be promoted: | ||
|
||
$ dune promote lib/lib.ml | ||
Promoting _build/default/lib/lib.ml.formatted to lib/lib.ml. | ||
$ cat lib/lib.ml | ||
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,4 @@ | ||
(test | ||
(name test) | ||
(ocamlformat) | ||
) |
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 @@ | ||
let () = | ||
() |