Skip to content

Commit

Permalink
mdx: add colors to console logs
Browse files Browse the repository at this point in the history
If Dune should ouput ANSI colors to stderr, so can mdx.

Signed-off-by: Antonin Décimo <[email protected]>
  • Loading branch information
MisterDA committed Dec 29, 2022
1 parent fa3aa28 commit c911f8e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Unreleased
----------

- Use colored output with MDX when Dune colors are enabled.
(#6462, @MisterDA)

- Make `dune describe workspace` return consistent dependencies for
executables and for libraries. By default, compile-time dependencies
towards PPX-rewriters are from now not taken into account (but
Expand Down
20 changes: 14 additions & 6 deletions src/dune_rules/mdx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ open Import

let mdx_version_required = "1.6.0"

let color_always =
lazy
(if Lazy.force Ansi_color.stderr_supports_color then
[ Command.Args.A "--color=always" ]
else [])

module Files = struct
type t =
{ src : Path.Build.t
Expand Down Expand Up @@ -64,8 +70,10 @@ module Deps = struct
]

let rule ~dir ~mdx_prog files =
let open Command.Args in
Command.run ~dir:(Path.build dir) mdx_prog
[ A "deps"; Dep (Path.build files.Files.src) ]
([ A "deps" ] @ Lazy.force color_always
@ [ Dep (Path.build files.Files.src) ])
~stdout_to:files.Files.deps

let path_escapes_dir str =
Expand Down Expand Up @@ -315,7 +323,7 @@ let gen_rules_for_single_file stanza ~sctx ~dir ~expander ~mdx_prog
List.concat_map stanza.preludes ~f:(Prelude.to_args ~dir)
in
( mdx_prog
, [ A "test" ] @ prelude_args
, [ A "test" ] @ prelude_args @ Lazy.force color_always
@ [ A "-o"; Target files.corrected; Dep (Path.build files.src) ] )
in
let deps, sandbox =
Expand Down Expand Up @@ -365,13 +373,13 @@ let mdx_prog_gen t ~sctx ~dir ~scope ~expander ~mdx_prog =
in
S args
in
let prelude_args =
Command.Args.S (List.concat_map t.preludes ~f:(Prelude.to_args ~dir))
in
let open Command.Args in
let prelude_args = S (List.concat_map t.preludes ~f:(Prelude.to_args ~dir)) in
(* We call mdx to generate the testing executable source *)
let action =
Command.run ~dir:(Path.build dir) mdx_prog ~stdout_to:file
[ A "dune-gen"; prelude_args; Resolve.Memo.args directory_args ]
([ A "dune-gen"; prelude_args; Resolve.Memo.args directory_args ]
@ Lazy.force color_always)
in
let open Memo.O in
let* () = Super_context.add_rule sctx ~loc ~dir action in
Expand Down

0 comments on commit c911f8e

Please sign in to comment.