Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support (package) in (mdx) #4691

Merged
merged 1 commit into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ Unreleased

- Add `(enabled_if ...)` to `(mdx ...)` (#4434, @emillon)

- Add `(package ...)` to `(mdx ...)` (#4691, fixes #3756, @emillon)

2.8.5 (28/03/2021)
------------------

Expand Down
9 changes: 9 additions & 0 deletions doc/dune-files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,15 @@ Where ``<optional-fields>`` are:
See `MDX's documentation <https://github.com/realworldocaml/mdx>`__ for more
details on preludes.

- ``(enabled_if <blang expression>)`` is the same as the corresponding field of
`library`_.

- ``(package <package>)`` specifies which package to attach this stanza to
(similarly to when ``(package)`` is attached to a ``(rule)`` stanza. When
``-p`` is passed, ``(mdx)`` stanzas with an other package will be ignored.
Note that this is feature is completely separate from ``(packages)``, which
specifies some dependencies.

.. _plugin:

plugin (since 2.8)
Expand Down
30 changes: 23 additions & 7 deletions src/dune_rules/mdx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ type t =
; packages : (Loc.t * Package.Name.t) list
; preludes : Prelude.t list
; enabled_if : Blang.t
; package : Package.t option
}

let enabled_if t = t.enabled_if
Expand All @@ -153,8 +154,11 @@ let decode =
and+ preludes = field ~default:[] "preludes" (repeat Prelude.decode)
and+ enabled_if =
Enabled_if.decode ~allowed_vars:Any ~since:(Some (2, 9)) ()
and+ package =
Stanza_common.Pkg.field_opt ()
~check:(Dune_lang.Syntax.since Stanza.syntax (2, 9))
in
{ loc; files; packages; preludes; enabled_if })
{ loc; files; packages; preludes; enabled_if; package })

let () =
let open Dune_lang.Decoder in
Expand Down Expand Up @@ -228,10 +232,22 @@ let gen_rules_for_single_file stanza ~sctx ~dir ~expander ~mdx_prog src =
(** Generates the rules for a given mdx stanza *)
let gen_rules t ~sctx ~dir ~expander =
let open Memo.Build.O in
let* files_to_mdx = files_to_mdx t ~sctx ~dir
and* mdx_prog =
Super_context.resolve_program sctx ~dir ~loc:(Some t.loc)
~hint:"opam install mdx" "ocaml-mdx"
let register_rules () =
let* files_to_mdx = files_to_mdx t ~sctx ~dir
and* mdx_prog =
Super_context.resolve_program sctx ~dir ~loc:(Some t.loc)
~hint:"opam install mdx" "ocaml-mdx"
in
Memo.Build.parallel_iter files_to_mdx
~f:(gen_rules_for_single_file t ~sctx ~dir ~expander ~mdx_prog)
in
let* only_packages = Only_packages.get () in
let do_it =
match (only_packages, t.package) with
| None, _
| Some _, None ->
true
| Some only, Some stanza_package ->
Package.Name.Map.mem only (Package.name stanza_package)
in
Memo.Build.parallel_iter files_to_mdx
~f:(gen_rules_for_single_file t ~sctx ~dir ~expander ~mdx_prog)
Memo.Build.if_ do_it register_rules
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```ocaml
# let x = 1;;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```ocaml
# let x = 1;;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```ocaml
# let x = 1;;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(mdx
(files doc-a.md)
(package a))

(mdx
(files doc-b.md)
(package b))

(mdx
(files doc-nopkg.md))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(lang dune 2.8)
(using mdx 0.1)
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-a.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```ocaml
# let x = 1;;
```
3 changes: 3 additions & 0 deletions test/blackbox-tests/test-cases/mdx-stanza.t/package/doc-b.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```ocaml
# let x = 1;;
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```ocaml
# let x = 1;;
```
10 changes: 10 additions & 0 deletions test/blackbox-tests/test-cases/mdx-stanza.t/package/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(mdx
(files doc-a.md)
(package a))

(mdx
(files doc-b.md)
(package b))

(mdx
(files doc-nopkg.md))
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(lang dune 2.9)
(using mdx 0.1)
36 changes: 36 additions & 0 deletions test/blackbox-tests/test-cases/mdx-stanza.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,39 @@ The mdx stanza supports (enabled_if):
Error: 'enabled_if' is only available since version 2.9 of the dune language.
Please update your dune-project file to have (lang dune 2.9).
[1]

The mdx stanza supports (package):

emillon marked this conversation as resolved.
Show resolved Hide resolved
$ dune runtest --root package
Entering directory 'package'
File "doc-a.md", line 1, characters 0-0:
Error: Files _build/default/doc-a.md and
_build/default/.mdx/doc-a.md.corrected differ.
File "doc-b.md", line 1, characters 0-0:
Error: Files _build/default/doc-b.md and
_build/default/.mdx/doc-b.md.corrected differ.
File "doc-nopkg.md", line 1, characters 0-0:
Error: Files _build/default/doc-nopkg.md and
_build/default/.mdx/doc-nopkg.md.corrected differ.
[1]

emillon marked this conversation as resolved.
Show resolved Hide resolved
$ dune runtest --root package --only-packages b
Entering directory 'package'
File "doc-b.md", line 1, characters 0-0:
Error: Files _build/default/doc-b.md and
_build/default/.mdx/doc-b.md.corrected differ.
File "doc-nopkg.md", line 1, characters 0-0:
Error: Files _build/default/doc-nopkg.md and
_build/default/.mdx/doc-nopkg.md.corrected differ.
[1]

(package) needs a recent (lang dune):

$ dune runtest --root package-old-lang-dune --only-packages b
Entering directory 'package-old-lang-dune'
File "dune", line 3, characters 1-12:
3 | (package a))
^^^^^^^^^^^
Error: 'package' is only available since version 2.9 of the dune language.
Please update your dune-project file to have (lang dune 2.9).
[1]