-
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.
- Loading branch information
Showing
19 changed files
with
147 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
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 gen_rules_for_module sctx loc ocamlformat ~dir ~scope m = | ||
Format.printf "dir = %a\n" Path.pp dir; | ||
Module.iter m ~f:(fun kind src -> | ||
let flag = flag_of_kind kind in | ||
let path = Path.basename src.path in | ||
let dep x = String_with_vars.make_macro loc "dep" x in | ||
let formatted = path ^ ".formatted" in | ||
let format_action = | ||
let args = [sv flag; dep path] in | ||
Action.Unexpanded.Redirect | ||
( Stdout | ||
, target_var | ||
, Run (dep @@ Path.to_string ocamlformat, args) | ||
) | ||
in | ||
let format_rule = | ||
{ Dune_file.Rule.targets = Static [formatted] | ||
; deps = [] | ||
; action = (loc, format_action) | ||
; mode = Standard | ||
; locks = [] | ||
; loc | ||
} | ||
in | ||
let diff_action = | ||
Action.Unexpanded.Diff | ||
{ optional = false | ||
; mode = Text | ||
; file1 = dep path | ||
; file2 = sv formatted | ||
} | ||
in | ||
let _ : Path.t list = Simple_rules.user_rule sctx ~dir ~scope format_rule in | ||
add_alias_format sctx loc ~dir ~scope diff_action | ||
) | ||
|
||
let gen_rules sctx (buildable:Dune_file.Buildable.t) | ||
~dir ~scope modules = | ||
if buildable.ocamlformat then | ||
let loc = buildable.loc in | ||
match Bin.which "ocamlformat" with | ||
| None -> | ||
let msg = "Cannot find ocamlformat, skipping.\n" in | ||
add_alias_format sctx loc ~dir ~scope (Echo [sv msg]) | ||
| Some ocamlformat -> | ||
Module.Name.Map.iter | ||
modules | ||
~f:(gen_rules_for_module sctx loc ocamlformat ~dir ~scope) |
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,9 @@ | ||
open Import | ||
|
||
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,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 @@ | ||
(library | ||
(name lib2) | ||
(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 y=() |
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 |
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,22 @@ | ||
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 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 lib2/lib2.mli lib2/lib2.mli.formatted') | ||
sh (internal) (exit 1) | ||
(cd _build/default && /bin/sh -c 'false lib2/lib2.ml lib2/lib2.ml.formatted') | ||
dir = _build/default/lib2 | ||
dir = _build/default/lib | ||
[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 |