Skip to content

Commit

Permalink
Try read OPAM switch from the current directory
Browse files Browse the repository at this point in the history
  • Loading branch information
na4zagin3 committed Apr 23, 2022
1 parent 094745f commit 3133df4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
7 changes: 7 additions & 0 deletions bin/setup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ let default_target_dir =

let read_environment ?opam_switch () =
let outf = Format.std_formatter in
let opam_switch = match opam_switch with
| Some _ -> opam_switch
| None ->
let dir = OpamFilename.cwd () in
(* TODO Read switch relative to Satyristes *)
Option.some_if (OpamWrapper.exists_switch_at_dir dir) (OpamSwitch.of_dirname dir)
in
let env = EnvironmentStatus.read_opam_environment ~outf ?opam_switch () in
SatysfiDirs.read_satysfi_env ~outf env

11 changes: 5 additions & 6 deletions src/command/build.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ let build ~outf ~build_dir ~verbose ~build_module ~buildscript_path ~system_font
end


let opam_pin_project ~(buildscript: BuildScript.t) ~buildscript_path =
let opam_pin_project ~(env: Environment.t) ~verbose ~(buildscript: BuildScript.t) ~buildscript_path =
let open P.Infix in
let workdir cwd =
FilePath.make_absolute cwd buildscript_path
Expand All @@ -122,10 +122,9 @@ let opam_pin_project ~(buildscript: BuildScript.t) ~buildscript_path =
let opam_name =
Lint.get_opam_name ~opam ~opam_path
in
P.run "opam" ["pin"; "add"; "--no-action"; "--yes"; opam_name; "file://" ^ workdir cwd]
>> P.set_env "OPAMSOLVERTIMEOUT" "0" (
P.run "opam" ["reinstall"; "--verbose"; "--yes"; "--"; workdir cwd]
)
let proj_dir = workdir cwd in
OpamWrapper.opam_add_pin_com ~env ~verbose opam_name proj_dir
>> OpamWrapper.opam_rebuild_com ~env ~verbose proj_dir
)
)

Expand Down Expand Up @@ -153,7 +152,7 @@ let build_command ~outf ~buildscript_path ~names ~verbose ~env =
|> List.map ~f:BuildScript.get_name
in
P.echo ("= Pin projects")
>> opam_pin_project ~buildscript ~buildscript_path
>> opam_pin_project ~env ~verbose ~buildscript ~buildscript_path
>> P.echo (Printf.sprintf !"\n= Build modules: %{sexp: string list}" module_names)
>> P.List.iter build_modules ~f:(fun build_module ->
begin match build_module with
Expand Down
16 changes: 16 additions & 0 deletions src/opamWrapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ end

let opam_package_name_satysfi = OpamPackage.Name.(of_string "satysfi")

let exists_switch_at_dir dir =
let switch = OpamSwitch.of_dirname dir in
let root = OpamStateConfig.(!r.root_dir) in
let switch_config_file = OpamPath.Switch.switch_config root switch in
OpamFile.exists switch_config_file

let get_satysfi_opam_registry switch =
let get_reg_from_root switch =
let root = OpamStateConfig.(!r.root_dir) in
Expand Down Expand Up @@ -221,3 +227,13 @@ let exec_run ~(env: Environment.t) com args =
"--";
] @ [com] @ args
|> P.run "opam"

let opam_add_pin_com ~(env: Environment.t) ~verbose:_ package proj_dir =
ignore env;
P.run "opam" ["pin"; "add"; "--no-action"; "--yes"; package; "file://" ^ proj_dir]

let opam_rebuild_com ~(env: Environment.t) ~verbose:_ proj_dir =
ignore env;
P.set_env "OPAMSOLVERTIMEOUT" "0" (
P.run "opam" ["reinstall"; "--verbose"; "--yes"; "--"; proj_dir]
)

0 comments on commit 3133df4

Please sign in to comment.