Skip to content

Commit

Permalink
Install command
Browse files Browse the repository at this point in the history
  • Loading branch information
emillon committed Sep 2, 2021
1 parent 2a35516 commit dedeadc
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 4 deletions.
10 changes: 10 additions & 0 deletions doc/dune
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,13 @@
(alias runtest)
(action
(diff configure.txt configure.txt.gen)))

(rule
(with-stdout-to
install.txt.gen
(run opam-compiler install --help=plain)))

(rule
(alias runtest)
(action
(diff install.txt install.txt.gen)))
16 changes: 16 additions & 0 deletions doc/install.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
NAME
opam-compiler-install - Run make install

SYNOPSIS
opam-compiler install [OPTION]...

OPTIONS
--dry-run
Do not perform external commands. Print them and continue as if
they worked.

--help[=FMT] (default=auto)
Show this help in format FMT. The value FMT must be one of `auto',
`pager', `groff' or `plain'. With `auto', the format is `pager` or
`plain' whenever the TERM env var is `dumb' or undefined.

3 changes: 3 additions & 0 deletions doc/opam-compiler.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ COMMANDS
create
Create a switch from a compiler source

install
Run make install

OPTIONS
--help[=FMT] (default=auto)
Show this help in format FMT. The value FMT must be one of `auto',
Expand Down
16 changes: 15 additions & 1 deletion lib/cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ type t =
github_client : Github_client.t;
}
| Configure of { runner : Runner.t; args : string list }
| Install of { runner : Runner.t }

let eval = function
| Create { source; switch_name; configure_command; runner; github_client } ->
Op.create runner github_client source switch_name ~configure_command
| Configure { runner; args } -> Op.configure runner args
| Install { runner } -> Op.install runner

let configure_command_explicit =
let open Cmdliner.Arg in
Expand Down Expand Up @@ -179,13 +181,25 @@ module Configure = struct
let command = (term, info)
end

module Install = struct
let term =
let open Let_syntax.Cmdliner in
let+ runner = runner in
Install { runner }

let info = Cmdliner.Term.info ~doc:"Run make install" "install"

let command = (term, info)
end

let default =
let open Cmdliner.Term in
(ret (pure (`Help (`Auto, None))), info "opam-compiler")

let main () =
let result =
Cmdliner.Term.eval_choice default [ Create.command; Configure.command ]
Cmdliner.Term.eval_choice default
[ Create.command; Configure.command; Install.command ]
in
(match result with
| `Ok op -> eval op |> Rresult.R.failwith_error_msg
Expand Down
5 changes: 3 additions & 2 deletions lib/import.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ let pp_cmd ppf cmd =
Bos.Cmd.to_list cmd |> List.map quote_if_needed |> String.concat " "
|> Format.pp_print_string ppf

type error = [ `Command_failed of Bos.Cmd.t | `Unknown ]
type error = [ `Command_failed of Bos.Cmd.t | `Configure_needed | `Unknown ]

let translate_error s =
let open Rresult.R in
reword_error (function
| `Unknown -> msgf "%s" s
| `Command_failed cmd -> msgf "%s - command failed: %a" s pp_cmd cmd)
| `Command_failed cmd -> msgf "%s - command failed: %a" s pp_cmd cmd
| `Configure_needed -> msgf "%s - configure step is required" s)
2 changes: 1 addition & 1 deletion lib/import.mli
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ val pp_env : Format.formatter -> (string * string) list option -> unit

val pp_cmd : Format.formatter -> Bos.Cmd.t -> unit

type error = [ `Command_failed of Bos.Cmd.t | `Unknown ]
type error = [ `Command_failed of Bos.Cmd.t | `Configure_needed | `Unknown ]

val translate_error :
string -> ('a, [< error ]) result -> ('a, [> Rresult.R.msg ]) result
20 changes: 20 additions & 0 deletions lib/op.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,23 @@ let configure runner args =
let cmd = Bos.Cmd.(v "./configure" % "--prefix" % prefix %% of_list args) in
Runner.run runner cmd)
|> translate_error "Could not configure"

let file_exists runner path =
let cmd =
Bos.Cmd.(
v "grep" % "-q" % "-e" % "prefix=.*_opam" % "-e" % "prefix=.*\\.opam"
% path)
in
match Runner.run runner cmd with
| Ok () -> Ok true
| Error (`Command_failed _) -> Ok false
| Error _ as e -> e

let install runner =
let open Let_syntax.Result in
(let* exists = file_exists runner "Makefile.config" in
if not exists then Error `Configure_needed
else
let cmd = Bos.Cmd.(v "make" % "install") in
Runner.run runner cmd)
|> translate_error "Could not install"
2 changes: 2 additions & 0 deletions lib/op.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ val reinstall :
(unit, [> Rresult.R.msg ]) result

val configure : Runner.t -> string list -> (unit, [> Rresult.R.msg ]) result

val install : Runner.t -> (unit, [> Rresult.R.msg ]) result
39 changes: 39 additions & 0 deletions test/cram/install.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
$ opam-compiler install --dry-run
Run: grep -q -e prefix=.*_opam -e prefix=.*\.opam Makefile.config
Run: make install

"install" checks for a Makefile.config file.
If it does not exist, it prints an error:

$ cat > Makefile <<EOF
> .PHONY: install
> install:
> @echo installed
> EOF

$ opam-compiler install
grep: Makefile.config: No such file or directory
Fatal error: exception Failure("Could not install - configure step is required")
[2]

If it exists, it is inspected. The prefix is expected to point at an opam
switch.

Global switches are recognized:

$ echo 'prefix=/home/me/.opam/name' > Makefile.config
$ opam-compiler install
installed

Local switches are recognized:

$ echo 'prefix=/home/me/src/project/_opam' > Makefile.config
$ opam-compiler install
installed

If prefix is something else, an error message is displayed:

$ echo 'prefix=/usr/local/lib' > Makefile.config
$ opam-compiler install
Fatal error: exception Failure("Could not install - configure step is required")
[2]

0 comments on commit dedeadc

Please sign in to comment.