Skip to content

Commit

Permalink
Artifact_subsitution put all the configuration in one structure
Browse files Browse the repository at this point in the history
Group the similar arguments

Signed-off-by: François Bobot <[email protected]>
  • Loading branch information
bobot committed Mar 23, 2020
1 parent be19bd1 commit bf97510
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 23 deletions.
9 changes: 6 additions & 3 deletions bin/install_uninstall.ml
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,12 @@ module File_ops_real (W : Workspace) : File_operations = struct
| Some Dune_package ->
copy_special_file ~src ~package ~ic ~oc ~f:(process_dune_package ~get_location)
| None ->
Dune.Artifact_substitution.copy ~get_vcs
~get_location
~get_localPath:(fun _ -> None)
Dune.Artifact_substitution.copy
~conf:{
get_vcs;
get_location;
get_localPath=(fun _ -> None);
}
~input:(input ic)
~output:(output oc))

Expand Down
22 changes: 14 additions & 8 deletions src/dune/artifact_substitution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ type t =
| LocalPath of localpath
| Repeat of int * string

type conf = {
get_vcs:(Path.Source.t -> Vcs.t option);
get_location:(Section.t -> Package.Name.t -> Path.t);
get_localPath:(localpath -> Path.t option);
}

let to_dyn = function
| Vcs_describe p -> Dyn.Variant ("Vcs_describe", [ Path.Source.to_dyn p ])
| Location (kind,lib_name) ->
Expand All @@ -47,21 +53,21 @@ let to_dyn = function
Dyn.Variant ("LocalPath",[Dyn.Variant(v,[])])
| Repeat (n, s) -> Dyn.Variant ("Repeat", [ Int n; String s ])

let eval t ~get_vcs ~get_location ~get_localPath =
let eval t ~conf =
match t with
| Repeat (n, s) ->
Fiber.return (Array.make n s |> Array.to_list |> String.concat ~sep:"")
| Vcs_describe p -> (
match get_vcs p with
match conf.get_vcs p with
| None -> Fiber.return ""
| Some vcs -> Vcs.describe vcs )
| Location (name,lib_name) ->
Fiber.return (Path.to_absolute_filename (get_location name lib_name))
Fiber.return (Path.to_absolute_filename (conf.get_location name lib_name))
| LocalPath d ->
Fiber.return
(Option.value ~default:""
(let open Option.O in
let+ dir = (get_localPath d) in
let+ dir = (conf.get_localPath d) in
Path.to_absolute_filename dir))

let encode_replacement ~len ~repl:s =
Expand Down Expand Up @@ -326,7 +332,7 @@ let buf_len = max_len

let buf = Bytes.create buf_len

let copy ~get_vcs ~get_location ~get_localPath ~input ~output =
let copy ~conf ~input ~output =
let open Fiber.O in
(* The copy algorithm works as follow:
Expand Down Expand Up @@ -365,7 +371,7 @@ let copy ~get_vcs ~get_location ~get_localPath ~input ~output =
let placeholder = Bytes.sub_string buf ~pos:placeholder_start ~len in
match decode placeholder with
| Some t ->
let* s = eval t ~get_vcs ~get_location ~get_localPath in
let* s = eval t ~conf in
let s = encode_replacement ~len ~repl:s in
output (Bytes.unsafe_of_string s) 0 len;
let pos = placeholder_start + len in
Expand Down Expand Up @@ -414,10 +420,10 @@ let copy ~get_vcs ~get_location ~get_localPath ~input ~output =
| 0 -> Fiber.return ()
| n -> loop Scan0 ~beginning_of_data:0 ~pos:0 ~end_of_data:n

let copy_file ~get_vcs ~get_location ~get_localPath ?chmod ~src ~dst () =
let copy_file ~conf ?chmod ~src ~dst () =
let ic, oc = Io.setup_copy ?chmod ~src ~dst () in
Fiber.finalize
~finally:(fun () ->
Io.close_both (ic, oc);
Fiber.return ())
(fun () -> copy ~get_vcs ~get_location ~get_localPath ~input:(input ic) ~output:(output oc))
(fun () -> copy ~conf ~input:(input ic) ~output:(output oc))
14 changes: 8 additions & 6 deletions src/dune/artifact_substitution.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ type t =
(** [Repeat (n, s)] evaluates to [s] repeated [n] times. This substitution
is used for unit tests. *)

type conf = {
get_vcs:(Path.Source.t -> Vcs.t option);
get_location:(Section.t -> Package.Name.t -> Path.t);
get_localPath:(localpath -> Path.t option);
}

val to_dyn : t -> Dyn.t

(** A string encoding of a substitution. The resulting string is what should be
Expand All @@ -30,9 +36,7 @@ val decode : string -> t option

(** Copy a file, performing all required substitutions *)
val copy_file :
get_vcs:(Path.Source.t -> Vcs.t option)
-> get_location:(Section.t -> Package.Name.t -> Path.t)
-> get_localPath:(localpath -> Path.t option)
conf:conf
-> ?chmod:(int -> int)
-> src:Path.t
-> dst:Path.t
Expand All @@ -43,9 +47,7 @@ val copy_file :
output functions. Their semantic must match the ones of the [input] and
[output] functions from the OCaml standard library. *)
val copy :
get_vcs:(Path.Source.t -> Vcs.t option)
-> get_location:(Section.t -> Package.Name.t -> Path.t)
-> get_localPath:(localpath -> Path.t option)
conf:conf
-> input:(Bytes.t -> int -> int -> int)
-> output:(Bytes.t -> int -> int -> unit)
-> unit Fiber.t
Expand Down
7 changes: 4 additions & 3 deletions src/dune/build_system.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1633,9 +1633,10 @@ end = struct
Some (Path.source Path.Source.root)
in
Artifact_substitution.copy_file () ~src:path ~dst:in_source_tree
~get_vcs:File_tree.nearest_vcs
~get_location
~get_localPath:get_localPath
~conf:{ get_vcs = File_tree.nearest_vcs;
get_location;
get_localPath;
}
~chmod ))
in
t.rule_done <- t.rule_done + 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ let test input =
in
let output = Buffer.add_subbytes buf in
Artifact_substitution.copy
~get_vcs:(fun _ -> None)
~get_location:(fun _ _ -> Path.root)
~get_localPath:(fun _ -> None)
~conf:{
get_vcs=(fun _ -> None);
get_location=(fun _ _ -> Path.root);
get_localPath=(fun _ -> None);
}
~input ~output);
let result = Buffer.contents buf in
if result <> expected then
Expand Down

0 comments on commit bf97510

Please sign in to comment.