Skip to content

Commit

Permalink
Add 'opam install -u' to allow upgrades
Browse files Browse the repository at this point in the history
there was no command to upgrade-or-install a package, which was annoying when developping
(the package may be already installed, or have previously failed).

The workaround was to use 'opam reinstall', which is not equivalent
  • Loading branch information
AltGr committed Sep 22, 2015
1 parent acc9474 commit 20cde4e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
45 changes: 28 additions & 17 deletions src/client/opamClient.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1437,7 +1437,7 @@ module API = struct
full_orphans,
orphan_versions

let install_t ?ask atoms add_to_roots deps_only t =
let install_t ?ask atoms add_to_roots ~deps_only ~upgrade t =
log "INSTALL %a" (slog OpamFormula.string_of_atoms) atoms;
let names = OpamPackage.Name.Set.of_list (List.rev_map fst atoms) in

Expand All @@ -1454,10 +1454,11 @@ module API = struct
if OpamPackage.Set.mem nv t.installed then
match add_to_roots with
| None ->
OpamConsole.note
"Package %s is already installed (current version is %s)."
(OpamPackage.Name.to_string (OpamPackage.name nv))
(OpamPackage.Version.to_string (OpamPackage.version nv));
if not upgrade then
OpamConsole.note
"Package %s is already installed (current version is %s)."
(OpamPackage.Name.to_string (OpamPackage.name nv))
(OpamPackage.Version.to_string (OpamPackage.version nv));
t
| Some true ->
if OpamPackage.Set.mem nv t.installed_roots then
Expand Down Expand Up @@ -1499,16 +1500,23 @@ module API = struct

OpamSolution.check_availability t (Lazy.force t.available_packages) atoms;

if pkg_new <> [] then (
let wish_upgrade =
if upgrade then List.filter (fun at -> not (List.mem at pkg_new)) atoms
else [] in

if pkg_new <> [] || wish_upgrade <> [] then (

let request =
preprocessed_request t full_orphans orphan_versions
~wish_install:atoms ();
~wish_install:atoms ~wish_upgrade ();
in
let action =
if add_to_roots = Some false || deps_only then
Install OpamPackage.Name.Set.empty
else Install names in
if wish_upgrade <> [] then Upgrade (OpamPackage.Set.of_list pkg_skip)
(* Fixme: the above won't properly handle setting as a root *)
else match add_to_roots, deps_only with
| Some false, _ | None, true ->
Install OpamPackage.Name.Set.empty
| _ -> Install names in
let solution =
OpamSolution.resolve t action
~orphans:(full_orphans ++ orphan_versions)
Expand All @@ -1530,11 +1538,11 @@ module API = struct
OpamSolution.check_solution t solution
)

let install names add_to_roots deps_only =
let install names add_to_roots ~deps_only ~upgrade =
with_switch_backup "install" @@ fun t ->
let atoms = OpamSolution.sanitize_atom_list ~permissive:true t names in
let t = update_dev_packages_t atoms t in
install_t atoms add_to_roots deps_only t
install_t atoms add_to_roots ~deps_only ~upgrade t

let remove_t ?ask ~autoremove ~force atoms t =
log "REMOVE autoremove:%b %a" autoremove
Expand Down Expand Up @@ -1679,9 +1687,12 @@ module API = struct
OpamConsole.msg "%s needs to be %sinstalled.\n"
(OpamPackage.Name.to_string name)
(if OpamPackage.has_name t.installed name then "re" else "");
if OpamPackage.Set.mem nv t.installed
then reinstall_t ~ask:true [name, Some (`Eq,v)] t (* same version *)
else install_t ~ask:true [name, Some (`Eq,v)] None false t (* != version or new *)
if OpamPackage.Set.mem nv t.installed then
reinstall_t ~ask:true [name, Some (`Eq,v)] t (* same version *)
else
install_t ~ask:true [name, Some (`Eq,v)] None
~deps_only:false ~upgrade:false t
(* != version or new *)
| None ->
try
let nv = OpamPackage.max_version t.installed name in
Expand Down Expand Up @@ -1829,8 +1840,8 @@ module SafeAPI = struct
let info ~fields ~raw_opam ~where regexps =
read_lock (fun () -> API.info ~fields ~raw_opam ~where regexps)

let install names add_to_roots deps_only =
switch_lock (fun () -> API.install names add_to_roots deps_only)
let install names add_to_roots ~deps_only ~upgrade =
switch_lock (fun () -> API.install names add_to_roots ~deps_only ~upgrade)

let reinstall names =
switch_lock (fun () -> API.reinstall names)
Expand Down
3 changes: 2 additions & 1 deletion src/client/opamClient.mli
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ module API: sig
(** Install the given list of packages. Second argument, if not None, specifies
that given packages should be added or removed from the roots.
Third argument installs all dependencies but not the packages themselves *)
val install: atom list -> bool option -> bool -> unit
val install:
atom list -> bool option -> deps_only:bool -> upgrade:bool -> unit

(** Reinstall the given set of packages. *)
val reinstall: atom list -> unit
Expand Down
13 changes: 9 additions & 4 deletions src/client/opamMain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -632,13 +632,18 @@ let install =
Arg.(value & flag & info ["deps-only"]
~doc:"Install all its dependencies, but don't actually install the \
package.") in
let install global_options build_options add_to_roots deps_only atoms =
let upgrade =
Arg.(value & flag & info ["u";"upgrade"]
~doc:"Upgrade the packages if already installed, rather than \
ignoring them") in
let install
global_options build_options add_to_roots deps_only upgrade atoms =
apply_global_options global_options;
apply_build_options build_options;
Client.install atoms add_to_roots deps_only
Client.install atoms add_to_roots ~deps_only ~upgrade
in
Term.(pure install $global_options $build_options
$add_to_roots $deps_only $nonempty_atom_list),
$add_to_roots $deps_only $upgrade $nonempty_atom_list),
term_info "install" ~doc ~man

(* REMOVE *)
Expand Down Expand Up @@ -1453,7 +1458,7 @@ let check_and_run_external_commands () =
(OpamRepositoryConfig.init ();
OpamSolverConfig.init ();
OpamClientConfig.init ();
Client.install [pkgname,None] None false;
Client.install [pkgname,None] None ~deps_only:false ~upgrade:false;
OpamConsole.header_msg "Carrying on to \"%s\""
(String.concat " " (Array.to_list Sys.argv));
OpamConsole.msg "\n";
Expand Down

0 comments on commit 20cde4e

Please sign in to comment.