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

[WIP] opam check-constraints #2

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions opam-build.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ depends: [
"dune" {>= "2.0"}
"opam-client" {>= "2.1.0" & < "2.2" & opam-version >= "2.1" & opam-version < "2.2"}
"opam-client" {>= "2.2" & < "2.3" & opam-version >= "2.2" & opam-version < "2.3"}
"opam-0install-cudf" {>= "0.4"}
"cmdliner" {>= "1.0"}
]
available: opam-version >= "2.1" & opam-version < "2.3"
19 changes: 19 additions & 0 deletions opam-check-constraints.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
opam-version: "2.0"
synopsis: "An opam plugin to check your projects dependency constraints"
maintainer: "Kate <[email protected]>"
authors: "Kate <[email protected]>"
license: "MIT"
homepage: "https://github.com/kit-ty-kate/opam-build"
bug-reports: "https://github.com/kit-ty-kate/opam-build/issues"
dev-repo: "git+https://github.com/kit-ty-kate/opam-build.git"
flags: plugin
build: ["dune" "build" "-p" name "-j" jobs]
depends: [
"ocaml" {>= "4.02"}
"dune" {>= "2.0"}
"opam-client" {>= "2.1.0" & < "2.2" & opam-version >= "2.1" & opam-version < "2.2"}
"opam-client" {>= "2.2" & < "2.3" & opam-version >= "2.2" & opam-version < "2.3"}
"opam-0install-cudf" {>= "0.4"}
"cmdliner" {>= "1.0"}
]
available: opam-version >= "2.1" & opam-version < "2.3"
1 change: 1 addition & 0 deletions opam-test.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ depends: [
"dune" {>= "2.0"}
"opam-client" {>= "2.1.0" & < "2.2" & opam-version >= "2.1" & opam-version < "2.2"}
"opam-client" {>= "2.2" & < "2.3" & opam-version >= "2.2" & opam-version < "2.3"}
"opam-0install-cudf" {>= "0.4"}
"cmdliner" {>= "1.0"}
]
available: opam-version >= "2.1" & opam-version < "2.3"
19 changes: 12 additions & 7 deletions src/build_test_common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ let check_switch gt dirname k =
or leave them with global switch *)
| Some _ -> OpamSwitchState.with_ `Lock_write gt k

let check_dependencies st dirname =
let check_dependencies ~upgrade st dirname =
let st, atoms =
OpamAuxCommands.simulate_autopin st ~recurse:false ~quiet:true [`Dirname dirname]
in
let missing = OpamClient.check_installed ~build:true ~post:true st atoms in
if not (OpamPackage.Map.is_empty missing) then
if upgrade then
OpamClient.install st ~deps_only:true atoms
else
st
let missing = OpamClient.check_installed ~build:true ~post:true st atoms in
if not (OpamPackage.Map.is_empty missing) then
OpamClient.install st ~deps_only:true atoms
else
st

let add_post_to_variables st =
let switch_config = st.OpamStateTypes.switch_config in
Expand All @@ -60,14 +63,16 @@ let rec iter_job = function
let result = OpamProcess.run cmd in
iter_job (k result)

let build ~with_test ~dirname =
let build ~upgrade ~lower_bounds ~with_test ~dirname =
OpamStd.Option.iter Sys.chdir dirname;
let dirname = OpamFilename.cwd () in
let solver = if lower_bounds then Some (lazy (module OpamBuiltin0install : OpamCudfSolver.S)) else None in
let solver_preferences_default = if lower_bounds then Some (lazy (Some "+removed,+count[version-lag,solution]")) else None in
(* TODO: Disable sandbox by default? Make it configurable? *)
OpamClientConfig.opam_init ~build_test:with_test ();
OpamClientConfig.opam_init ~build_test:with_test ?solver ?solver_preferences_default ();
OpamGlobalState.with_ `Lock_write @@ fun gt ->
check_switch gt dirname @@ fun st ->
let st = check_dependencies st dirname in
let st = check_dependencies ~upgrade st dirname in
let st = if with_test then add_post_to_variables st else st in
OpamAuxCommands.opams_of_dir dirname |>
List.map get_pkg |>
Expand Down
7 changes: 6 additions & 1 deletion src/build_test_common.mli
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
(* SPDX-License-Identifier: MIT *)

val build : with_test:bool -> dirname:string option -> unit
val build :
upgrade:bool ->
lower_bounds:bool ->
with_test:bool ->
dirname:string option ->
unit
16 changes: 15 additions & 1 deletion src/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(library
(name build_test_common)
(modules build_test_common)
(libraries unix opam-client))
(libraries unix opam-client opam-0install-cudf))

(executable
(name opam_build)
Expand Down Expand Up @@ -30,3 +30,17 @@
(with-stdout-to %{target}
(progn
(echo "let version = {|%{version:opam-test}|}\n")))))

(executable
(name opam_check_constraints)
(public_name opam-check-constraints)
(package opam-check-constraints)
(modules opam_check_constraints opam_check_constraints_config)
(libraries build_test_common cmdliner))

(rule
(target opam_check_constraints_config.ml)
(action
(with-stdout-to %{target}
(progn
(echo "let version = {|%{version:opam-check-constraints}|}\n")))))
2 changes: 1 addition & 1 deletion src/opam_build.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let ( $ ) = Cmdliner.Term.( $ )
let ( & ) = Cmdliner.Arg.( & )

let main dirname =
Build_test_common.build ~with_test:false ~dirname;
Build_test_common.build ~upgrade:false ~lower_bounds:false ~with_test:false ~dirname;
`Ok ()

let dirname =
Expand Down
38 changes: 38 additions & 0 deletions src/opam_check_constraints.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(* SPDX-License-Identifier: MIT *)

module Term = Cmdliner.Term
module Arg = Cmdliner.Arg
module Manpage = Cmdliner.Manpage

let ( $ ) = Cmdliner.Term.( $ )
let ( & ) = Cmdliner.Arg.( & )

let main dirname =
print_endline "Checking lower-bound constraints...";
Build_test_common.build ~upgrade:true ~lower_bounds:true ~with_test:false ~dirname;
print_endline "Checking lower-bound tests constraints...";
Build_test_common.build ~upgrade:true ~lower_bounds:true ~with_test:true ~dirname;
print_endline "Checking upper-bound constraints...";
Build_test_common.build ~upgrade:true ~lower_bounds:false ~with_test:false ~dirname;
print_endline "Checking upper-bound tests constraints...";
Build_test_common.build ~upgrade:true ~lower_bounds:false ~with_test:true ~dirname;
`Ok ()

let dirname =
let doc = "" in (* TODO *)
Arg.value &
Arg.pos 0 (Arg.some Arg.dir) None &
Arg.info [] ~docv:"DIR" ~doc

let cmd =
let doc = "" in (* TODO *)
let sdocs = Manpage.s_common_options in
let exits = Term.default_exits in
let man = [] in (* TODO *)
Term.ret (Term.const main $ dirname),
Term.info "opam-check-constraints" ~version:Opam_check_constraints_config.version ~doc ~sdocs ~exits ~man

let () =
Term.exit @@ match Term.eval cmd with
| `Ok () -> `Ok ()
| (`Error _ | `Version | `Help) as x -> x
2 changes: 1 addition & 1 deletion src/opam_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let ( $ ) = Cmdliner.Term.( $ )
let ( & ) = Cmdliner.Arg.( & )

let main dirname =
Build_test_common.build ~with_test:true ~dirname;
Build_test_common.build ~upgrade:false ~lower_bounds:false ~with_test:true ~dirname;
`Ok ()

let dirname =
Expand Down