Skip to content

Commit

Permalink
WIP Make it possible to specify opam repo
Browse files Browse the repository at this point in the history
  • Loading branch information
na4zagin3 committed Apr 23, 2022
1 parent a8ddb74 commit 036cad0
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 66 deletions.
6 changes: 4 additions & 2 deletions bin/commandLockdown.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ())
]
Expand All @@ -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 ())
]
Expand Down
5 changes: 2 additions & 3 deletions bin/setup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion bin/setup.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 2 additions & 7 deletions satyrographos.opam
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
3 changes: 2 additions & 1 deletion src/command/lockdown.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 2 additions & 13 deletions src/environmentStatus.ml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/lockdown/lockdown.ml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) =
Expand Down
18 changes: 9 additions & 9 deletions src/lockdown/opamDependencies.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -79,25 +79,25 @@ 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
|> List.map ~f:(fun {name; url} -> name, url)
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
59 changes: 42 additions & 17 deletions src/opamWrapper.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down Expand Up @@ -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
Expand All @@ -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";
Expand All @@ -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 = {
Expand All @@ -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";
Expand All @@ -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)
Expand All @@ -161,15 +179,17 @@ 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) ->
sprintf "%s.%s" name version)
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 [];
Expand All @@ -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"
4 changes: 2 additions & 2 deletions src/satysfi/satysfiDirs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 }
5 changes: 5 additions & 0 deletions test/testcases/command_build__doc_with_autogen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion test/testcases/command_lockdown_restore__opam.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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 =
Expand Down
11 changes: 6 additions & 5 deletions test/testcases/command_lockdown_save__opam.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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 =
Expand Down
Loading

0 comments on commit 036cad0

Please sign in to comment.