Skip to content

Commit

Permalink
Support (package) in (mdx)
Browse files Browse the repository at this point in the history
This adds a (package) field to (mdx) with the usual semantics.

The syntax is not ideal since (packages) is already supported as sugar
to specify dependencies.

See ocaml#4691

Closes ocaml#3756

Signed-off-by: Etienne Millon <me@emillon.org>
  • Loading branch information
emillon committed Jun 8, 2021
1 parent 7fbfce7 commit 01729ab
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ unreleased - 2.9 branch
- Allow to set up merlin in a variant of the default context
(#4145, @TheLortex, @voodoos)

- 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 @@ -1819,6 +1819,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
29 changes: 22 additions & 7 deletions src/dune_rules/mdx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,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 @@ -150,8 +151,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 @@ -214,10 +218,21 @@ 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 files_to_mdx = files_to_mdx t ~sctx ~dir in
let 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 in
let mdx_prog =
Super_context.resolve_program sctx ~dir ~loc:(Some t.loc)
~hint:"opam install mdx" "ocaml-mdx"
in
List.iter files_to_mdx
~f:(gen_rules_for_single_file t ~sctx ~dir ~expander ~mdx_prog)
in
let do_it =
match (!Clflags.only_packages, t.package) with
| None, _
| Some _, None ->
true
| Some only, Some stanza_package ->
Package.Name.Set.mem only (Package.name stanza_package)
in
List.iter files_to_mdx
~f:(gen_rules_for_single_file t ~sctx ~dir ~expander ~mdx_prog)
if do_it then 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 @@ -86,3 +86,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):
$ 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]
$ 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]

0 comments on commit 01729ab

Please sign in to comment.