-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkg: build and make ocamlformat dev-tool available (#10647)
Signed-off-by: Alpha DIALLO <[email protected]>
- Loading branch information
1 parent
3b9069f
commit ded1f5e
Showing
32 changed files
with
1,076 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
open Dune_config | ||
open Import | ||
|
||
let enabled = | ||
Config.make_toggle ~name:"lock_dev_tool" ~default:Dune_rules.Setup.lock_dev_tool | ||
;; | ||
|
||
let is_enabled = | ||
lazy | ||
(match Config.get enabled with | ||
| `Enabled -> true | ||
| `Disabled -> false) | ||
;; | ||
|
||
(* The solver satisfies dependencies for local packages, but dev tools | ||
are not local packages. As a workaround, create an empty local package | ||
which depends on the dev tool package. *) | ||
let make_local_package_wrapping_dev_tool ~dev_tool ~dev_tool_version | ||
: Dune_pkg.Local_package.t | ||
= | ||
let dev_tool_pkg_name = Dune_pkg.Dev_tool.package_name dev_tool in | ||
let dependency = | ||
let open Dune_lang in | ||
let open Package_dependency in | ||
let constraint_ = | ||
Option.map dev_tool_version ~f:(fun version -> | ||
Package_constraint.Uop | ||
( Relop.Eq | ||
, Package_constraint.Value.String_literal (Package_version.to_string version) )) | ||
in | ||
{ name = dev_tool_pkg_name; constraint_ } | ||
in | ||
let local_package_name = | ||
Package_name.of_string (Package_name.to_string dev_tool_pkg_name ^ "_dev_tool_wrapper") | ||
in | ||
{ Dune_pkg.Local_package.name = local_package_name | ||
; version = None | ||
; dependencies = [ dependency ] | ||
; conflicts = [] | ||
; depopts = [] | ||
; pins = Package_name.Map.empty | ||
; conflict_class = [] | ||
; loc = Loc.none | ||
} | ||
;; | ||
|
||
let solve ~local_packages ~lock_dirs = | ||
let open Fiber.O in | ||
let* solver_env_from_current_system = | ||
Dune_pkg.Sys_poll.make ~path:(Env_path.path Stdune.Env.initial) | ||
|> Dune_pkg.Sys_poll.solver_env_from_current_system | ||
>>| Option.some | ||
and* workspace = | ||
Memo.run | ||
@@ | ||
let open Memo.O in | ||
let+ workspace = Workspace.workspace () in | ||
workspace | ||
in | ||
Lock.solve | ||
workspace | ||
~local_packages | ||
~project_sources:Dune_pkg.Pin_stanza.DB.empty | ||
~solver_env_from_current_system | ||
~version_preference:None | ||
~lock_dirs | ||
;; | ||
|
||
let lock_ocamlformat () : unit Fiber.t = | ||
let version = Dune_pkg.Ocamlformat.version_of_current_project's_ocamlformat_config () in | ||
let ocamlformat_dev_tool_lock_dir = | ||
Dune_pkg.Lock_dir.dev_tool_lock_dir_path Ocamlformat | ||
in | ||
if not (Path.exists @@ Path.source ocamlformat_dev_tool_lock_dir) | ||
then ( | ||
let local_pkg = | ||
make_local_package_wrapping_dev_tool ~dev_tool:Ocamlformat ~dev_tool_version:version | ||
in | ||
let local_packages = Package_name.Map.singleton local_pkg.name local_pkg in | ||
solve ~local_packages ~lock_dirs:[ ocamlformat_dev_tool_lock_dir ]) | ||
else Fiber.return () | ||
;; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
val is_enabled : bool Lazy.t | ||
val lock_ocamlformat : unit -> unit Fiber.t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,13 @@ | ||
open Import | ||
|
||
val solve | ||
: Workspace.t | ||
-> local_packages:Dune_pkg.Local_package.t Package_name.Map.t | ||
-> project_sources:Dune_pkg.Pin_stanza.DB.t | ||
-> solver_env_from_current_system:Dune_pkg.Solver_env.t option | ||
-> version_preference:Dune_pkg.Version_preference.t option | ||
-> lock_dirs:Path.Source.t list | ||
-> unit Fiber.t | ||
|
||
(** Command to create lock directory *) | ||
val command : unit Cmd.t |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
open! Import | ||
|
||
type t = Ocamlformat | ||
|
||
let equal a b = | ||
match a, b with | ||
| Ocamlformat, Ocamlformat -> true | ||
;; | ||
|
||
let package_name = function | ||
| Ocamlformat -> Package_name.of_string "ocamlformat" | ||
;; | ||
|
||
let of_package_name package_name = | ||
match Package_name.to_string package_name with | ||
| "ocamlformat" -> Ocamlformat | ||
| other -> User_error.raise [ Pp.textf "No such dev tool: %s" other ] | ||
;; | ||
|
||
let exe_name = function | ||
| Ocamlformat -> "ocamlformat" | ||
;; | ||
|
||
let exe_path_components_within_package t = | ||
match t with | ||
| Ocamlformat -> [ "bin"; exe_name t ] | ||
;; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
open! Import | ||
|
||
type t = Ocamlformat | ||
|
||
val equal : t -> t -> bool | ||
val package_name : t -> Package_name.t | ||
val of_package_name : Package_name.t -> t | ||
val exe_name : t -> string | ||
|
||
(** Returns the path to this tool's executable relative to the root of | ||
this tool's package directory *) | ||
val exe_path_components_within_package : t -> string list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
open Import | ||
|
||
let version_of_ocamlformat_config ocamlformat_config = | ||
Io.lines_of_file ocamlformat_config | ||
|> List.find_map ~f:(fun line -> | ||
match String.split_on_char ~sep:'=' line |> List.map ~f:String.trim with | ||
| [ "version"; value ] -> Some (Package_version.of_string value) | ||
| _ -> None) | ||
;; | ||
|
||
let version_of_current_project's_ocamlformat_config () = | ||
let ocamlformat_config = Path.Source.of_string ".ocamlformat" |> Path.source in | ||
match Path.exists ocamlformat_config with | ||
| false -> None | ||
| true -> version_of_ocamlformat_config ocamlformat_config | ||
;; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
open! Import | ||
|
||
(** Returns the version from the current project's .ocamlformat file, | ||
if it exists *) | ||
val version_of_current_project's_ocamlformat_config : unit -> Package_version.t option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.