Skip to content

Commit b61f046

Browse files
committed
feat: expand variables in (promote (into ..))
Signed-off-by: Antonio Nuno Monteiro <anmonteiro@gmail.com>
1 parent 269c08a commit b61f046

28 files changed

+267
-113
lines changed

src/dune_lang/dune_lang.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ module Menhir_env = Menhir_env
5858
module Dune_env = Dune_env
5959
module Js_of_ocaml = Js_of_ocaml
6060
module Menhir = Menhir
61+
module Rule_mode = Rule_mode
6162
module Rule_mode_decoder = Rule_mode_decoder
6263
module Mode_conf = Mode_conf
6364
module Oxcaml = Oxcaml

src/dune_lang/rule_mode.ml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
open Import
2+
module Rule = Dune_engine.Rule
3+
4+
module Promote = struct
5+
module Into = struct
6+
type t =
7+
{ loc : Loc.t
8+
; dir : String_with_vars.t
9+
}
10+
end
11+
12+
type t =
13+
{ lifetime : Rule.Promote.Lifetime.t
14+
; into : Into.t option
15+
; only : Filename.t Predicate.t option
16+
}
17+
end
18+
19+
type t =
20+
| Standard
21+
| Fallback (** Only use this rule if the source files don't exist. *)
22+
| Promote of Promote.t (** Silently promote the targets to the source tree. *)
23+
| Ignore_source_files
24+
(** Just ignore the source files entirely. This is for cases where the
25+
targets are promoted only in a specific context, such as for
26+
.install files. *)

src/dune_lang/rule_mode.mli

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
open Import
2+
3+
module Promote : sig
4+
module Into : sig
5+
type t =
6+
{ loc : Loc.t
7+
; dir : String_with_vars.t
8+
}
9+
end
10+
11+
type t =
12+
{ lifetime : Dune_engine.Rule.Promote.Lifetime.t
13+
; into : Into.t option
14+
; only : Filename.t Predicate.t option
15+
}
16+
end
17+
18+
type t =
19+
| Standard
20+
| Fallback (** Only use this rule if the source files don't exist. *)
21+
| Promote of Promote.t (** Silently promote the targets to the source tree. *)
22+
| Ignore_source_files
23+
(** Just ignore the source files entirely. This is for cases where the
24+
targets are promoted only in a specific context, such as for
25+
.install files. *)

src/dune_lang/rule_mode_decoder.ml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ module Rule = Dune_engine.Rule
44

55
module Promote = struct
66
let into_decode =
7-
let+ loc, dir = located relative_file in
8-
{ Rule.Promote.Into.loc; dir }
7+
let+ loc, dir = located String_with_vars.decode in
8+
{ Rule_mode.Promote.Into.loc; dir }
99
;;
1010

11-
let decode : Rule.Promote.t Decoder.t =
11+
let decode : Rule_mode.Promote.t Decoder.t =
1212
fields
1313
(let+ until_clean =
1414
field_b "until-clean" ~check:(Syntax.since Stanza.syntax (1, 10))
@@ -20,27 +20,27 @@ module Promote = struct
2020
Option.map only ~f:(fun only ->
2121
Predicate.create (Predicate_lang.Glob.test only ~standard:Predicate_lang.true_))
2222
in
23-
{ Rule.Promote.lifetime = (if until_clean then Until_clean else Unlimited)
23+
{ Rule_mode.Promote.lifetime = (if until_clean then Until_clean else Unlimited)
2424
; into
2525
; only
2626
})
2727
;;
2828
end
2929

3030
let mode_decoders =
31-
[ "standard", return Rule.Mode.Standard
32-
; "fallback", return Rule.Mode.Fallback
31+
[ "standard", return Rule_mode.Standard
32+
; "fallback", return Rule_mode.Fallback
3333
; ( "promote"
3434
, let+ p = Promote.decode in
35-
Rule.Mode.Promote p )
35+
Rule_mode.Promote p )
3636
; ( "promote-until-clean"
3737
, let+ () =
3838
Syntax.deleted_in
3939
Stanza.syntax
4040
(3, 0)
4141
~extra_info:"Use the (promote (until-clean)) syntax instead."
4242
in
43-
Rule.Mode.Promote { lifetime = Until_clean; into = None; only = None } )
43+
Rule_mode.Promote { lifetime = Until_clean; into = None; only = None } )
4444
; ( "promote-into"
4545
, let+ () = Syntax.since Stanza.syntax (1, 8)
4646
and+ () =
@@ -49,7 +49,7 @@ let mode_decoders =
4949
(3, 0)
5050
~extra_info:"Use the (promote (into <dir>)) syntax instead."
5151
and+ into = Promote.into_decode in
52-
Rule.Mode.Promote { lifetime = Unlimited; into = Some into; only = None } )
52+
Rule_mode.Promote { lifetime = Unlimited; into = Some into; only = None } )
5353
; ( "promote-until-clean-into"
5454
, let+ () = Syntax.since Stanza.syntax (1, 8)
5555
and+ () =
@@ -58,9 +58,9 @@ let mode_decoders =
5858
(3, 0)
5959
~extra_info:"Use the (promote (until-clean) (into <dir>)) syntax instead."
6060
and+ into = Promote.into_decode in
61-
Rule.Mode.Promote { lifetime = Until_clean; into = Some into; only = None } )
61+
Rule_mode.Promote { lifetime = Until_clean; into = Some into; only = None } )
6262
]
6363
;;
6464

6565
let decode = sum mode_decoders
66-
let field = field "mode" decode ~default:Rule.Mode.Standard
66+
let field = field "mode" decode ~default:Rule_mode.Standard
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
open Import
2-
module Rule := Dune_engine.Rule
32

43
module Promote : sig
5-
val decode : Rule.Promote.t Decoder.t
6-
val into_decode : Rule.Promote.Into.t Decoder.t
4+
val decode : Rule_mode.Promote.t Decoder.t
5+
val into_decode : Rule_mode.Promote.Into.t Decoder.t
76
end
87

9-
val decode : Rule.Mode.t Decoder.t
10-
val field : Rule.Mode.t Decoder.fields_parser
8+
val decode : Rule_mode.t Decoder.t
9+
val field : Rule_mode.t Decoder.fields_parser

src/dune_lang/stanzas/copy_files.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type origin =
99
type t =
1010
{ add_line_directive : bool
1111
; alias : Alias_name.t option
12-
; mode : Rule.Mode.t
12+
; mode : Rule_mode.t
1313
; enabled_if : Blang.t
1414
; files : String_with_vars.t
1515
; only_sources : Blang.t
@@ -32,7 +32,7 @@ let decode_only_sources =
3232
let long_form =
3333
let check = Syntax.since Stanza.syntax (2, 7) in
3434
let+ alias = field_o "alias" (check >>> Alias.decode)
35-
and+ mode = field "mode" ~default:Rule.Mode.Standard (check >>> Rule_mode_decoder.decode)
35+
and+ mode = field "mode" ~default:Rule_mode.Standard (check >>> Rule_mode_decoder.decode)
3636
and+ enabled_if = Enabled_if.decode ~allowed_vars:Any ~since:(Some (2, 8)) ()
3737
and+ files = field "files" (check >>> String_with_vars.decode)
3838
and+ only_sources =

src/dune_lang/stanzas/copy_files.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type origin =
77
type t =
88
{ add_line_directive : bool
99
; alias : Alias_name.t option
10-
; mode : Dune_engine.Rule.Mode.t
10+
; mode : Rule_mode.t
1111
; enabled_if : Blang.t
1212
; files : String_with_vars.t
1313
; only_sources : Blang.t

src/dune_rules/dune_file.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module Mask = struct
6060

6161
let is_promoted_rule =
6262
let is_promoted_mode version = function
63-
| Rule.Mode.Promote { only = None; lifetime; _ } ->
63+
| Rule_mode.Promote { only = None; lifetime; _ } ->
6464
if version >= (3, 5)
6565
then (
6666
match lifetime with

src/dune_rules/exe.ml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,17 @@ let link_exe
233233
; Dyn link_args
234234
]
235235
>>| Action.Full.add_sandbox sandbox
236+
and* mode =
237+
let sctx = Compilation_context.super_context cctx in
238+
let* expander = Super_context.expander sctx ~dir in
239+
let rule_mode =
240+
match promote with
241+
| None -> Rule_mode.Standard
242+
| Some p -> Promote p
243+
in
244+
Rule_mode_expand.expand_path ~expander ~dir rule_mode
236245
in
237-
Super_context.add_rule
238-
sctx
239-
~loc
240-
~dir
241-
~mode:
242-
(match promote with
243-
| None -> Standard
244-
| Some p -> Promote p)
245-
action_with_targets
246+
Super_context.add_rule sctx ~loc ~dir ~mode action_with_targets
246247
;;
247248

248249
let link_js

src/dune_rules/exe.mli

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ val link_many
6363
-> ?sandbox:Sandbox_config.t
6464
-> programs:Program.t list
6565
-> linkages:Linkage.t list
66-
-> promote:Rule.Promote.t option
66+
-> promote:Rule_mode.Promote.t option
6767
-> Compilation_context.t
6868
-> dep_graphs Memo.t
6969

@@ -74,7 +74,7 @@ val build_and_link
7474
-> ?sandbox:Sandbox_config.t
7575
-> program:Program.t
7676
-> linkages:Linkage.t list
77-
-> promote:Rule.Promote.t option
77+
-> promote:Rule_mode.Promote.t option
7878
-> Compilation_context.t
7979
-> dep_graphs Memo.t
8080

@@ -85,7 +85,7 @@ val build_and_link_many
8585
-> ?sandbox:Sandbox_config.t
8686
-> programs:Program.t list
8787
-> linkages:Linkage.t list
88-
-> promote:Rule.Promote.t option
88+
-> promote:Rule_mode.Promote.t option
8989
-> Compilation_context.t
9090
-> dep_graphs Memo.t
9191

0 commit comments

Comments
 (0)