From 094745fbc8f269c780351fc405eb8792e15f5edd Mon Sep 17 00:00:00 2001 From: na4zagin3 Date: Sat, 9 Apr 2022 23:52:20 +0900 Subject: [PATCH] Handle load failure of OPAM repo --- bin/commandLockdown.ml | 2 +- bin/setup.ml | 2 +- src/environmentStatus.ml | 4 +-- src/lockdown/opamDependencies.ml | 8 ++--- src/opamWrapper.ml | 36 +++++++++++-------- .../command_lockdown_restore__opam.ml | 3 +- 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/bin/commandLockdown.ml b/bin/commandLockdown.ml index 1473e25..95aee92 100644 --- a/bin/commandLockdown.ml +++ b/bin/commandLockdown.ml @@ -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 @@ -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; diff --git a/bin/setup.ml b/bin/setup.ml index ef65612..ae5017b 100644 --- a/bin/setup.ml +++ b/bin/setup.ml @@ -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 diff --git a/src/environmentStatus.ml b/src/environmentStatus.ml index ba79bbb..30dadb0 100644 --- a/src/environmentStatus.ml +++ b/src/environmentStatus.ml @@ -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 diff --git a/src/lockdown/opamDependencies.ml b/src/lockdown/opamDependencies.ml index ea70eba..6d847d0 100644 --- a/src/lockdown/opamDependencies.ml +++ b/src/lockdown/opamDependencies.ml @@ -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;} -> @@ -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 diff --git a/src/opamWrapper.ml b/src/opamWrapper.ml index 8319339..ff310da 100644 --- a/src/opamWrapper.ml +++ b/src/opamWrapper.ml @@ -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"; @@ -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; } @@ -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 = [ @@ -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 ) diff --git a/test/testcases/command_lockdown_restore__opam.ml b/test/testcases/command_lockdown_restore__opam.ml index 90c9053..24ff60f 100644 --- a/test/testcases/command_lockdown_restore__opam.ml +++ b/test/testcases/command_lockdown_restore__opam.ml @@ -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")