Skip to content

Commit

Permalink
Handle load failure of OPAM repo
Browse files Browse the repository at this point in the history
  • Loading branch information
na4zagin3 committed Apr 23, 2022
1 parent 036cad0 commit 094745f
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bin/commandLockdown.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ let lockdown_file_path ~buildscript_path =
let lockdown_save_command =
let open Command.Let_syntax in
let open RenameOption in
let _outf = Format.std_formatter in
Command.basic
~summary:"Save the current environment to the lockdown file (experimental)"
[%map_open
Expand Down Expand Up @@ -54,6 +53,7 @@ let lockdown_restore_command =
let env = Setup.read_environment () in
(fun () ->
Satyrographos_command.Lockdown.restore_lockdown
~outf
~verbose
~env
~buildscript_path;
Expand Down
2 changes: 1 addition & 1 deletion bin/setup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ let default_target_dir =

let read_environment ?opam_switch () =
let outf = Format.std_formatter in
let env = EnvironmentStatus.read_opam_environment ?opam_switch () in
let env = EnvironmentStatus.read_opam_environment ~outf ?opam_switch () in
SatysfiDirs.read_satysfi_env ~outf env

4 changes: 2 additions & 2 deletions src/environmentStatus.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ let satysfi_opam_registry () =
|> Option.map ~f:OpamFilename.Dir.to_string
*)

let read_opam_environment ?opam_switch () =
OpamWrapper.read_opam_environment ?opam_switch Environment.empty
let read_opam_environment ~outf ?opam_switch () =
OpamWrapper.read_opam_environment ~outf ?opam_switch Environment.empty
8 changes: 4 additions & 4 deletions src/lockdown/opamDependencies.ml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ let get_opam_dependencies ~verbose ~env packages =
end;
{ packages; repos; }

let restore_opam_dependencies_com ~verbose ~env (dependencies : opam_dependencies) =
let restore_opam_dependencies_com ~outf ~verbose ~env (dependencies : opam_dependencies) =
let packages =
dependencies.packages
|> List.map ~f:(fun {name; version;} ->
Expand All @@ -94,10 +94,10 @@ let restore_opam_dependencies_com ~verbose ~env (dependencies : opam_dependencie
in
let open P.Infix in
OpamWrapper.opam_clean_up_local_switch_com ()
>> OpamWrapper.opam_set_up_local_switch_com ~env ~repos ~version:None ()
>> OpamWrapper.opam_set_up_local_switch_com ~outf ~env ~repos ~version:None ()
>>= install_com


let restore_opam_dependencies ~verbose ~env (dependencies : opam_dependencies) =
restore_opam_dependencies_com ~verbose ~env dependencies
let restore_opam_dependencies ~outf ~verbose ~env (dependencies : opam_dependencies) =
restore_opam_dependencies_com ~outf ~verbose ~env dependencies
|> P.eval
36 changes: 21 additions & 15 deletions src/opamWrapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,22 @@ let get_satysfi_opam_registry switch =
let switch_config = OpamFile.Switch_config.read switch_config_file in
OpamPath.Switch.share root switch switch_config opam_package_name_satysfi
in
OpamGlobalState.with_ `Lock_none @@ fun gt ->
let global_switch = OpamFile.Config.switch gt.config in
match switch, global_switch with
| Some switch, _ ->
Some (get_reg_from_root switch)
| None, Some switch ->
Some (get_reg_from_root switch)
| None, None ->
None
let get_reg () =
OpamGlobalState.with_ `Lock_none @@ fun gt ->
let global_switch = OpamFile.Config.switch gt.config in
match switch, global_switch with
| Some switch, _ ->
get_reg_from_root switch
| None, Some switch ->
get_reg_from_root switch
| None, None ->
failwith "get_reg: No switch is specified"
in
Result.try_with get_reg

let get_satysfi_opam_registry_exc switch =
get_satysfi_opam_registry switch
|> Option.value_exn ~message:"Failed to get opam directory."
|> Result.ok_exn

let dune_cache_envs = [
"DUNE_CACHE", "enabled";
Expand Down Expand Up @@ -73,16 +76,19 @@ let default_repo_list =
let opam_switch_arg opam_switch =
"--switch=" ^ OpamSwitch.to_string opam_switch

let read_opam_environment ?opam_switch env =
let read_opam_environment ~outf ?opam_switch env =
let satysfi_opam_registry () =
get_satysfi_opam_registry opam_switch
|> Option.map ~f:OpamFilename.Dir.to_string
|> Result.map ~f:OpamFilename.Dir.to_string
in

let reg = satysfi_opam_registry () in

let opam_reg =
OpamSatysfiRegistry.read (reg |> Option.value_exn ~message:"Failed to read OPAM repo")
reg
|> Result.map_error ~f:(Exn.pp outf)
|> Result.ok
|> Option.bind ~f:OpamSatysfiRegistry.read
in
Environment.{ env with opam_reg; opam_switch; }

Expand All @@ -109,7 +115,7 @@ let opam_clean_up_local_switch_com ?(path="./") () =
else P.return ()


let opam_set_up_local_switch_com ~(env: Environment.t) ?(repos=default_repo_list) ?(path="./") ~version () =
let opam_set_up_local_switch_com ~outf ~(env: Environment.t) ?(repos=default_repo_list) ?(path="./") ~version () =
let opam_switch = OpamSwitch.of_dirname @@ OpamFilename.Dir.of_string path in
let open P.Infix in
let args = [
Expand All @@ -130,7 +136,7 @@ let opam_set_up_local_switch_com ~(env: Environment.t) ?(repos=default_repo_list
in
P.run "opam" args
>>| (fun () ->
read_opam_environment ~opam_switch env
read_opam_environment ~outf ~opam_switch env
)


Expand Down
3 changes: 2 additions & 1 deletion test/testcases/command_lockdown_restore__opam.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ let env ~dest_dir:_ ~temp_dir : Satyrographos.Environment.t t =

let () =
let verbose = false in
let main env ~dest_dir:_ ~temp_dir ~outf:_ =
let main env ~dest_dir:_ ~temp_dir ~outf =
let _name = Some "example-doc" in
(* let dest_dir = FilePath.concat dest_dir "dest" in *)
Satyrographos_command.Lockdown.restore_lockdown
~outf
~verbose
~env
~buildscript_path:(FilePath.concat temp_dir "pkg/Satyristes")
Expand Down

0 comments on commit 094745f

Please sign in to comment.