diff --git a/bin/commandLockdown.ml b/bin/commandLockdown.ml index 48e452d..1473e25 100644 --- a/bin/commandLockdown.ml +++ b/bin/commandLockdown.ml @@ -30,10 +30,11 @@ let lockdown_save_command = "_build" |> Option.some in - let _env = Setup.read_environment () in + let env = Setup.read_environment () in (fun () -> Satyrographos_command.Lockdown.save_lockdown ~verbose + ~env ~buildscript_path; reprint_err_warn ()) ] @@ -50,10 +51,11 @@ let lockdown_restore_command = in Compatibility.optin (); let buildscript_path = Option.value ~default:(default_script_path ()) script in - let _env = Setup.read_environment () in + let env = Setup.read_environment () in (fun () -> Satyrographos_command.Lockdown.restore_lockdown ~verbose + ~env ~buildscript_path; reprint_err_warn ()) ] diff --git a/bin/setup.ml b/bin/setup.ml index 245ffc0..ef65612 100644 --- a/bin/setup.ml +++ b/bin/setup.ml @@ -13,9 +13,8 @@ let default_target_dir = |> Option.value ~default:(Filename.concat home_dir ".satysfi") |> (fun dir -> Filename.concat dir "dist") -let read_environment () = +let read_environment ?opam_switch () = let outf = Format.std_formatter in - let env = EnvironmentStatus.read_opam_environment () in - Format.(fprintf std_formatter !"env: %{sexp: Environment.t}\n" env); + let env = EnvironmentStatus.read_opam_environment ?opam_switch () in SatysfiDirs.read_satysfi_env ~outf env diff --git a/bin/setup.mli b/bin/setup.mli index 5b531fb..da3b53e 100644 --- a/bin/setup.mli +++ b/bin/setup.mli @@ -3,4 +3,4 @@ val default_target_dir : string (** Read current runtime-dependent information. This command SHOULD NOT affect the environment. *) -val read_environment : unit -> Satyrographos.Environment.t +val read_environment : ?opam_switch:OpamTypes.switch -> unit -> Satyrographos.Environment.t diff --git a/satyrographos.opam b/satyrographos.opam index f1e447a..25c73f7 100644 --- a/satyrographos.opam +++ b/satyrographos.opam @@ -29,13 +29,8 @@ depends: [ "ppx_deriving" "ppx_deriving_yojson" "ocamlgraph" - ( "opam-format" {>= "2.0.4" & < "2.2"} - & "opam-state" {>= "2.0.4" & < "2.2"} - & "ocaml" {< "4.12.0"} - | "opam-format" {>= "2.1.0" & < "2.2"} - & "opam-state" {>= "2.1.0" & < "2.2"} - & "ocaml" {>= "4.12.0"} - ) + "opam-format" {>= "2.0" & < "2.2"} + "opam-state" {>= "2.0" & < "2.2"} "re" { >= "1.9.0" } "stringext" {with-test} "uri" {>= "3.0.0"} diff --git a/src/command/lockdown.ml b/src/command/lockdown.ml index 6727489..ca7ce28 100644 --- a/src/command/lockdown.ml +++ b/src/command/lockdown.ml @@ -10,11 +10,12 @@ let load_lockdown_file ~buildscript_path = then Some (Satyrographos_lockdown.LockdownFile.load_file_exn path) else None -let save_lockdown ~verbose ~buildscript_path = +let save_lockdown ~verbose ~env ~buildscript_path = let buildscript = Satyrographos.BuildScript.load buildscript_path in let lockdown = Satyrographos_lockdown.Lockdown.generate_lockdown ~verbose + ~env ~buildscript in Satyrographos_lockdown.LockdownFile.save_file_exn diff --git a/src/environmentStatus.ml b/src/environmentStatus.ml index a8d29d3..ba79bbb 100644 --- a/src/environmentStatus.ml +++ b/src/environmentStatus.ml @@ -1,21 +1,10 @@ +(* open Core -(* let satysfi_opam_registry () = OpamWrapper.get_satysfi_opam_registry None |> Option.map ~f:OpamFilename.Dir.to_string *) let read_opam_environment ?opam_switch () = - let satysfi_opam_registry () = - OpamWrapper.get_satysfi_opam_registry opam_switch - |> Option.map ~f:OpamFilename.Dir.to_string - in - - let reg = satysfi_opam_registry () in - Format.(printf !"reg: %{sexp: string option}\n" reg); - - let opam_reg = - OpamSatysfiRegistry.read (reg |> Option.value_exn ~message:"Failed to read OPAM repo") - in - Environment.{ empty with opam_reg; opam_switch; } + OpamWrapper.read_opam_environment ?opam_switch Environment.empty diff --git a/src/lockdown/lockdown.ml b/src/lockdown/lockdown.ml index 9b34b87..7f1147c 100644 --- a/src/lockdown/lockdown.ml +++ b/src/lockdown/lockdown.ml @@ -1,6 +1,6 @@ open Core -let generate_lockdown ~verbose ~buildscript = +let generate_lockdown ~verbose ~env ~buildscript = let open Satyrographos in let dependent_opam_packages = BuildScript.get_opam_dependencies buildscript @@ -24,7 +24,7 @@ let generate_lockdown ~verbose ~buildscript = LockdownFile.make ~dependencies: (LockdownFile.Opam - (OpamDependencies.get_opam_dependencies ~verbose dependent_opam_packages)) + (OpamDependencies.get_opam_dependencies ~verbose ~env dependent_opam_packages)) ~autogen let restore_lockdown ~verbose (lockdown : LockdownFile.t) = diff --git a/src/lockdown/opamDependencies.ml b/src/lockdown/opamDependencies.ml index b589dea..ea70eba 100644 --- a/src/lockdown/opamDependencies.ml +++ b/src/lockdown/opamDependencies.ml @@ -8,11 +8,11 @@ let get_opam_repositories ~gt ~rt () = OpamGlobalState.repos_list gt |> List.map ~f:(OpamRepositoryState.get_repo rt) -let get_opam_dependencies ~verbose packages = +let get_opam_dependencies ~verbose ~env packages = OpamGlobalState.with_ `Lock_none @@ fun gt -> OpamRepositoryState.with_ `Lock_none gt @@ fun rt -> let package_and_repos = - OpamWrapper.opam_installed_transitive_dependencies_com ~verbose packages + OpamWrapper.opam_installed_transitive_dependencies_com ~verbose ~env packages |> P.eval in let packages = @@ -79,14 +79,14 @@ let get_opam_dependencies ~verbose packages = end; { packages; repos; } -let restore_opam_dependencies_com ~verbose (dependencies : opam_dependencies) = +let restore_opam_dependencies_com ~verbose ~env (dependencies : opam_dependencies) = let packages = dependencies.packages |> List.map ~f:(fun {name; version;} -> name, version) in - let install_com = - OpamWrapper.opam_install_com ~verbose packages + let install_com env = + OpamWrapper.opam_install_com ~env ~verbose packages in let repos = dependencies.repos @@ -94,10 +94,10 @@ let restore_opam_dependencies_com ~verbose (dependencies : opam_dependencies) = in let open P.Infix in OpamWrapper.opam_clean_up_local_switch_com () - >> OpamWrapper.opam_set_up_local_switch_com ~repos ~version:None () - >> install_com + >> OpamWrapper.opam_set_up_local_switch_com ~env ~repos ~version:None () + >>= install_com -let restore_opam_dependencies ~verbose (dependencies : opam_dependencies) = - restore_opam_dependencies_com ~verbose dependencies +let restore_opam_dependencies ~verbose ~env (dependencies : opam_dependencies) = + restore_opam_dependencies_com ~verbose ~env dependencies |> P.eval diff --git a/src/opamWrapper.ml b/src/opamWrapper.ml index 72b7901..8319339 100644 --- a/src/opamWrapper.ml +++ b/src/opamWrapper.ml @@ -22,11 +22,8 @@ let opam_package_name_satysfi = OpamPackage.Name.(of_string "satysfi") let get_satysfi_opam_registry switch = let get_reg_from_root switch = let root = OpamStateConfig.(!r.root_dir) in - Format.(OpamFilename.Dir.to_string root |> printf !"root: %s\n"); let switch_config_file = OpamPath.Switch.switch_config root switch in - Format.(OpamFile.to_string switch_config_file |> printf !"switch_config_file: %s\n"); let switch_config = OpamFile.Switch_config.read switch_config_file in - Format.(OpamFile.Switch_config.write_to_string switch_config |> printf !"switch_config: %s\n"); OpamPath.Switch.share root switch switch_config opam_package_name_satysfi in OpamGlobalState.with_ `Lock_none @@ fun gt -> @@ -73,7 +70,21 @@ let default_repo_list = "https://opam.ocaml.org/"; ] +let opam_switch_arg opam_switch = + "--switch=" ^ OpamSwitch.to_string opam_switch +let read_opam_environment ?opam_switch env = + let satysfi_opam_registry () = + get_satysfi_opam_registry opam_switch + |> Option.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") + in + Environment.{ env with opam_reg; opam_switch; } let opam_clean_up_local_switch_com ?(path="./") () = let open P.Infix in @@ -98,7 +109,9 @@ let opam_clean_up_local_switch_com ?(path="./") () = else P.return () -let opam_set_up_local_switch_com ?(repos=default_repo_list) ?(path="./") ~version () = +let opam_set_up_local_switch_com ~(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 = [ "switch"; "create"; @@ -116,6 +129,9 @@ let opam_set_up_local_switch_com ?(repos=default_repo_list) ?(path="./") ~versio ] in P.run "opam" args + >>| (fun () -> + read_opam_environment ~opam_switch env + ) type opam_dependency_wrapper = { @@ -124,10 +140,10 @@ type opam_dependency_wrapper = { version: string; } -let opam_installed_transitive_dependencies_com ~verbose:_ packages = +let opam_installed_transitive_dependencies_com ~verbose:_ ~(env: Environment.t) packages = let open P.Infix in let cmd = - P.run "opam" [ + [ "list"; "-i"; "--color=never"; @@ -136,6 +152,8 @@ let opam_installed_transitive_dependencies_com ~verbose:_ packages = "--separator=,"; "--recursive"; "--required-by"; String.concat ~sep:"," packages] + @ Option.value_map env.opam_switch ~default:[] ~f:(fun switch -> [opam_switch_arg switch]) + |> P.run "opam" |> P.capture_unit [P.Std_io.Stdout] >>| String.split_lines >>| List.filter ~f:(fun l -> String.is_prefix ~prefix:"#" l |> not) @@ -161,7 +179,7 @@ let get_opam_repositories ~gt ~rt () = OpamGlobalState.repos_list gt |> List.map ~f:(OpamRepositoryState.get_repo rt) -let opam_install_com ~verbose packages = +let opam_install_com ~(env: Environment.t) ~verbose packages = let packages = packages |> List.map ~f:(fun (name, version) -> @@ -169,7 +187,9 @@ let opam_install_com ~verbose packages = in [ ["install"; "--yes";]; - ["--switch=.";]; + Option.value_map env.opam_switch ~default:[] ~f:(fun opam_switch -> + [opam_switch_arg opam_switch] + ); if verbose then ["--verbose";] else []; @@ -179,14 +199,19 @@ let opam_install_com ~verbose packages = |> P.run "opam" |> with_dune_cache -let opam_install ~verbose packages = - opam_install_com ~verbose packages +let opam_install ~(env: Environment.t) ~verbose packages = + opam_install_com ~env ~verbose packages |> P.eval -let opam_exec_run com args = - [ - "exec"; - "--switch=."; - "--"; - ] @ [com] @ args - |> P.run "opam" + +let exec_run ~(env: Environment.t) com args = + match env.opam_switch with + | None -> + P.run com args + | Some opam_switch -> + [ + "exec"; + opam_switch_arg opam_switch; + "--"; + ] @ [com] @ args + |> P.run "opam" diff --git a/src/satysfi/satysfiDirs.ml b/src/satysfi/satysfiDirs.ml index 58e33b5..c540982 100644 --- a/src/satysfi/satysfiDirs.ml +++ b/src/satysfi/satysfiDirs.ml @@ -32,7 +32,7 @@ let option_to_list = function | Some x -> [x] | None -> [] -let dist_library_dir ?satysfi_opam_reg ~outf:_ = +let dist_library_dir ~satysfi_opam_reg ~outf:_ = let shares = ["/usr/local/share"; "/usr/share"] in let dist_dirs = @@ -51,5 +51,5 @@ let dist_library_dir ?satysfi_opam_reg ~outf:_ = let read_satysfi_env ~outf (env: Satyrographos.Environment.t) = - let dist_library_dir = dist_library_dir ?satysfi_opam_reg:(Option.map env.opam_reg ~f:(fun reg -> reg.registry_dir)) ~outf in + let dist_library_dir = dist_library_dir ~satysfi_opam_reg:(Option.map env.opam_reg ~f:(fun reg -> reg.registry_dir)) ~outf in { env with dist_library_dir } diff --git a/test/testcases/command_build__doc_with_autogen.ml b/test/testcases/command_build__doc_with_autogen.ml index fde0cf5..eabede2 100644 --- a/test/testcases/command_build__doc_with_autogen.ml +++ b/test/testcases/command_build__doc_with_autogen.ml @@ -42,6 +42,11 @@ dependencies: version: 0.0.2.7 - name: satysfi version: 0.0.5+dev2020.09.05 + repos: + - name: default + url: https://opam.ocaml.org/ + - name: satysfi-external + url: git+https://github.com/gfngfn/satysfi-external-repo.git autogen: '$today': datetime: 2020-11-06T00:46:35.000000+09:00 diff --git a/test/testcases/command_lockdown_restore__opam.ml b/test/testcases/command_lockdown_restore__opam.ml index e1f01bb..90c9053 100644 --- a/test/testcases/command_lockdown_restore__opam.ml +++ b/test/testcases/command_lockdown_restore__opam.ml @@ -16,6 +16,11 @@ dependencies: version: 0.0.2.7 - name: satysfi version: 0.0.5+dev2020.09.05 + repos: + - name: default + url: https://opam.ocaml.org/ + - name: satysfi-external + url: git+https://github.com/gfngfn/satysfi-external-repo.git autogen: '$today': time: 2020-11-06T00:46:35.000000+09:00 @@ -54,11 +59,12 @@ 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 ~verbose + ~env ~buildscript_path:(FilePath.concat temp_dir "pkg/Satyristes") in let post_dump_dirs ~dest_dir:_ ~temp_dir:_ = [] in diff --git a/test/testcases/command_lockdown_save__invalid_opam_response.ml b/test/testcases/command_lockdown_save__invalid_opam_response.ml index 8e68700..426957e 100644 --- a/test/testcases/command_lockdown_save__invalid_opam_response.ml +++ b/test/testcases/command_lockdown_save__invalid_opam_response.ml @@ -9,7 +9,7 @@ let files = Command_lockdown_save__opam.files let opam_response = { - PrepareBin.list_result = {|***invalid,response,!!!|} + PrepareBin.list_result = {|***,invalid,response,!!!|} } let env ~dest_dir:_ ~temp_dir : Satyrographos.Environment.t t = @@ -38,11 +38,12 @@ 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.save_lockdown ~verbose + ~env ~buildscript_path:(FilePath.concat temp_dir "pkg/Satyristes") in let post_dump_dirs ~dest_dir:_ ~temp_dir = diff --git a/test/testcases/command_lockdown_save__opam.ml b/test/testcases/command_lockdown_save__opam.ml index 62fb55a..1099371 100644 --- a/test/testcases/command_lockdown_save__opam.ml +++ b/test/testcases/command_lockdown_save__opam.ml @@ -65,10 +65,10 @@ let files = let opam_response = { PrepareBin.list_result = {|# Packages matching: (installed | available) & (name-match(satyrographos) | name-match(satysfi) | name-match(ocaml)) -# Name ,# Version -ocaml ,4.09.0 -satyrographos,0.0.2.7 -satysfi ,0.0.5+dev2020.09.05|} +# Name ,# Repository ,# Version +ocaml ,default ,4.09.0 +satyrographos,satysfi-external,0.0.2.7 +satysfi ,satyrographos ,0.0.5+dev2020.09.05|} } let env ~dest_dir:_ ~temp_dir : Satyrographos.Environment.t t = @@ -97,11 +97,12 @@ 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.save_lockdown ~verbose + ~env ~buildscript_path:(FilePath.concat temp_dir "pkg/Satyristes") in let post_dump_dirs ~dest_dir:_ ~temp_dir = diff --git a/test/testlib/testLib.ml b/test/testlib/testLib.ml index d49c2d8..5889ebb 100644 --- a/test/testlib/testLib.ml +++ b/test/testlib/testLib.ml @@ -80,7 +80,10 @@ let dump_dir dir : unit t = ) |> set_env "LC_ALL" "C" -let stacktrace = false +let stacktrace = + Sys.getenv_opt "SATYROGRAPHOS_TEST_STACKTRACE" + |> Option.map (String.equal "true") + |> Option.value ~default:false let filter_output f c = capture [Std_io.Stdout] c