Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Support OPAM local repos #288

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions 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 All @@ -30,10 +29,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 +50,12 @@ 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
~outf
~verbose
~env
~buildscript_path;
reprint_err_warn ())
]
Expand Down
6 changes: 2 additions & 4 deletions bin/commandStatus.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ let status () =
[%derive.show: string list] (SatysfiDirs.runtime_dirs ()) |> print_endline;
printf "SATySFi user directory: ";
[%derive.show: string option] (SatysfiDirs.user_dir ()) |> print_endline;
env.opam_reg |> Option.iter ~f:(
printf !"Selected SATySFi runtime distribution: %{sexp:OpamSatysfiRegistry.t}\n");
env.dist_library_dir |> Option.iter ~f:(
printf "Selected SATySFi runtime distribution: %s\n")
printf !"Selected OPAM SATySFi library registry: %{sexp:OpamSatysfiRegistry.t option}\n" env.opam_reg ;
printf !"Selected SATySFi runtime distribution: %{sexp: string option}\n" env.dist_library_dir


let status_command =
Expand Down
19 changes: 11 additions & 8 deletions bin/setup.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ let home_dir = match SatysfiDirs.home_dir () with
| Some(d) -> d
| None -> failwith "Cannot find home directory"

let reg_opam =
SatysfiDirs.opam_share_dir ~outf:Format.std_formatter
|> Option.bind ~f:(fun opam_share_dir ->
OpamSatysfiRegistry.read (Filename.concat opam_share_dir "satysfi"))

let default_target_dir =
Sys.getenv "SATYSFI_RUNTIME"
|> Option.value ~default:(Filename.concat home_dir ".satysfi")
|> (fun dir -> Filename.concat dir "dist")

let read_environment () =
let dist_library_dir = SatysfiDirs.satysfi_dist_dir ~outf:Format.std_formatter in
Environment.{ opam_reg = reg_opam; dist_library_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

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
19 changes: 11 additions & 8 deletions src/command/build.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ open Core
open Satyrographos

module P = Shexp_process
module OW = Satyrographos.OpamWrapper

let read_module ~outf ~verbose ~build_module ~buildscript_path =
let src_dir = Filename.dirname buildscript_path in
Expand Down Expand Up @@ -56,10 +57,11 @@ let build_cmd ~outf ~build_dir ~verbose ~build_module ~buildscript_path ~system_
let autogen_libraries = Library.Dependency.to_list p.autogen in
let with_build_dir build_dir c =
let satysfi_runtime_dir = FilePath.concat build_dir "satysfi" in
let project_env =
setup_project_env ~satysfi_runtime_dir ~buildscript_path ~outf ~verbose ~libraries ~env ~system_font_prefix ~autogen_libraries
in
c project_env
let open P.Infix in
P.return ()
>>| (fun () ->
setup_project_env ~satysfi_runtime_dir ~buildscript_path ~outf ~verbose ~libraries ~env ~system_font_prefix ~autogen_libraries)
>>= c
in
let with_project_env c =
match build_dir with
Expand Down Expand Up @@ -97,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 @@ -120,8 +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.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 @@ -149,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
15 changes: 9 additions & 6 deletions src/command/lockdown.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ 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
(lockdown_file_path ~buildscript_path)
lockdown

let restore_lockdown_result ~verbose ~buildscript_path =
Satyrographos_lockdown.LockdownFile.load_file_result
(lockdown_file_path ~buildscript_path)
|> Result.map (Satyrographos_lockdown.Lockdown.restore_lockdown ~verbose)

let restore_lockdown ~verbose ~buildscript_path =
let lockdown =
Satyrographos_lockdown.LockdownFile.load_file_exn
(lockdown_file_path ~buildscript_path);
in
Satyrographos_lockdown.Lockdown.restore_lockdown ~verbose lockdown;
restore_lockdown_result ~verbose ~buildscript_path
|> Result.get_ok
1 change: 1 addition & 0 deletions src/command/runSatysfi.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ open Satyrographos

module Process = Shexp_process
module P = Process
module OW = Satyrographos.OpamWrapper

module StringMap = Map.Make(String)

Expand Down
11 changes: 10 additions & 1 deletion src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
(synopsis "Internal Satyrographos Library, do not use!")
(inline_tests)
(preprocess (staged_pps ppx_deriving.std ppx_jane))
(libraries core fileutils json-derivers opam-format shexp.process uri uri-sexp yojson))
(libraries
core
fileutils
json-derivers
opam-format
opam-state
shexp.process
uri
uri-sexp
yojson))

(ocamllex
(modules glob_lexer)
Expand Down
20 changes: 18 additions & 2 deletions src/environment.ml
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
open Core

module OpamSwitch = struct
include OpamSwitch

let sexp_of_t v =
OpamSwitch.to_string v
|> [%sexp_of: string]

let t_of_sexp sexp =
[%of_sexp: string] sexp
|> OpamSwitch.of_string
end

type t = {
opam_switch: OpamSwitch.t option;
opam_reg: OpamSatysfiRegistry.t option;
dist_library_dir: string option;
}
[@@deriving sexp]


let empty = {
opam_switch=None;
opam_reg=None;
dist_library_dir=None;
}

open Core

type project_env = {
buildscript_path: string;
satysfi_runtime_dir: string;
Expand Down
6 changes: 5 additions & 1 deletion src/environment.mli
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
(** A type represents runtime environment. *)
type t = {
opam_switch: OpamSwitch.t option;
(** OPAM Switch. E.g., 4.10.0 *)

opam_reg: OpamSatysfiRegistry.t option;
(** OPAM Registry. I.e., ~/.satyrograpos *)
(** OPAM Registry. E.g., ~/.opam/4.10.0/share/satysfi *)

dist_library_dir: string option;
(** A directory with SATySFi dist for the current SATySFi compiler.
Typically, this points a directory under OPAM reg or /usr/local/share/satysfi/dist. *)
}
[@@deriving sexp]

(** An empty runtime environment. *)
val empty: t
Expand Down
10 changes: 10 additions & 0 deletions src/environmentStatus.ml
Original file line number Diff line number Diff line change
@@ -0,0 +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 ~outf ?opam_switch () =
OpamWrapper.read_opam_environment ~outf ?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
12 changes: 11 additions & 1 deletion src/lockdown/lockdownFile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ type opam_package = {
}
[@@deriving equal, sexp, yojson]

type opam_repo = {
name: string;
url: string;
}
[@@deriving equal, sexp, yojson]

let x =
opam_package_of_yojson
type opam_dependencies = {
packages: opam_package list;
repos: opam_repo list;
}
[@@deriving equal, sexp, yojson]

Expand Down Expand Up @@ -75,10 +82,13 @@ let save_file_exn f ld =
|> Out_channel.output_string oc
)

let load_file_exn f =
let load_file_result f =
In_channel.read_all f
|> Yaml.yaml_of_string
|> error_msg_to_invalid_arg
|> YamlYojson.yojson_of_yaml
|> of_yojson

let load_file_exn f =
load_file_result f
|> Result.ok_or_failwith
Loading