Permalink
Browse files

[opam init -comp] fix the failure to start when there is no OCaml (ex…

…cept .comp files in repository)
  • Loading branch information...
tuong committed Jul 23, 2012
1 parent b982256 commit a4d01ec881102c78df09b9ea7da1aa5f0ba2261a
Showing with 67 additions and 49 deletions.
  1. +60 −30 src/client.ml
  2. +1 −1 src/client.mli
  3. +6 −18 src/opam.ml
View
@@ -482,33 +482,54 @@ let add_alias t alias ocaml_version =
(* We assume that we have the right ocaml-version in $opam/config. Then we:
- create $opam/$alias
- compiles and install $opam/compiler/$descr.comp *)
-let init_ocaml alias ocaml_version =
- log "init_ocaml %s %s" (Alias.to_string alias) (OCaml_V.to_string ocaml_version);
+let init_ocaml alias (default_allowed, ocaml_version) =
+ log "init_ocaml";
let t = load_state () in
- let alias_p = Path.C.create alias in
- let aliases = t.aliases in
- Dirname.mkdir (Path.C.root alias_p);
- add_alias t alias ocaml_version;
- let t = load_state () in
+ let default = OCaml_V.of_string Globals.default_compiler_version in
+ let ocaml_version =
+ let current () =
+ match OCaml_V.current () with
+ | None -> Globals.error_and_exit "No OCaml compiler found in path"
+ | Some _ -> default in
+
+ match ocaml_version with
+ | None -> current ()
+ | Some ocaml_version when default_allowed && ocaml_version = default -> current ()
+ | Some ocaml_version ->
+ let comp_f = Path.G.compiler t.global ocaml_version in
+ if not default_allowed && ocaml_version = default || not (Filename.exists comp_f) then
+ begin
+ Globals.msg
+ " %S is not a valid compiler description%s. The available compilers descriptions are:\n"
+ (OCaml_V.to_string ocaml_version)
+ (if ocaml_version = default then " (because it is a reserved name)" else "");
+ OCaml_V.Set.iter
+ (fun v -> Globals.msg " - %s\n" (OCaml_V.to_string v))
+ (Path.G.compiler_list t.global);
+ Globals.exit 2
+ end
+ else
+ ocaml_version in
- if ocaml_version = OCaml_V.of_string Globals.default_compiler_version then begin
+ let alias =
+ match alias with
+ | None -> Alias.of_string (OCaml_V.to_string ocaml_version)
+ | Some alias -> alias in
+
+ log "init_ocaml (alias=%s, ocaml_version=%s)" (Alias.to_string alias) (OCaml_V.to_string ocaml_version);
+
+ let alias_p = Path.C.create alias in
+ Dirname.mkdir (Path.C.root alias_p);
+ (if ocaml_version = default then begin
(* we create a dummy compiler description file the the system-wide
OCaml configuration *)
create_default_compiler_description t;
- end else try
+ end else
+ try
let comp_f = Path.G.compiler t.global ocaml_version in
- if not (Filename.exists comp_f) then begin
- Globals.msg
- " %S is not a valid compiler description. The available compilers descriptions are:\n"
- (OCaml_V.to_string ocaml_version);
- OCaml_V.Set.iter
- (fun v -> Globals.msg " - %s\n" (OCaml_V.to_string v))
- (Path.G.compiler_list t.global);
- Globals.exit 2
- end;
let comp = File.Comp.read comp_f in
if not (File.Comp.preinstalled comp) then begin
@@ -548,8 +569,8 @@ let init_ocaml alias ocaml_version =
with e ->
if not !Globals.debug then
Dirname.rmdir (Path.C.root alias_p);
- File.Aliases.write (Path.G.aliases t.global) aliases;
- raise e
+ raise e);
+ alias, ocaml_version
let indent_left s nb =
let nb = nb - String.length s in
@@ -1186,26 +1207,32 @@ let init repo alias ocaml_version cores =
if Filename.exists config_f then
Globals.error_and_exit "%s already exist" (Filename.to_string config_f)
else try
- let opam_version = OPAM_V.of_string Globals.opam_version in
- let config = File.Config.create opam_version [repo] alias cores in
let repo_p = Path.R.create repo in
(* Create (possibly empty) configuration files *)
- File.Config.write config_f config;
+ File.Config.write config_f (File.Config.with_repositories File.Config.empty [repo]);
File.Repo_index.write (Path.G.repo_index root) N.Map.empty;
File.Repo_config.write (Path.R.config repo_p) repo;
Repositories.init repo;
Dirname.mkdir (Path.G.opam_dir root);
Dirname.mkdir (Path.G.descr_dir root);
Dirname.mkdir (Path.G.archive_dir root);
Dirname.mkdir (Path.G.compiler_dir root);
- let alias_p = Path.C.root (Path.C.create alias) in
update_repo ();
- if Dirname.exists alias_p then
- Globals.warning "%s does not exist and %s already exist" (Filename.to_string config_f) (Dirname.to_string alias_p)
- else
- init_ocaml alias ocaml_version;
+ (match alias with
+ | Some alias ->
+ let alias_p = Path.C.root (Path.C.create alias) in
+ if Dirname.exists alias_p then
+ Globals.error_and_exit "%s does not exist and %s already exist" (Filename.to_string config_f) (Dirname.to_string alias_p)
+ else
+ ()
+ | None -> ());
+ let alias, ocaml_version = init_ocaml alias (false, ocaml_version) in
+ let opam_version = OPAM_V.of_string Globals.opam_version in
+ File.Config.write config_f (File.Config.create opam_version [repo] alias cores);
+ let t = load_state () in
+ add_alias t alias ocaml_version;
update_package ();
- let t = update_available_current (load_state ()) in
+ let t = update_available_current t in
let wish_install = Heuristic.get_packages t ocaml_version Heuristic.v_any in
Heuristic.resolve `init t
[ { wish_install
@@ -1633,10 +1660,13 @@ let switch clone alias ocaml_version =
(* [2/3] install the new OCaml version *)
let exists = Dirname.exists (Path.C.root alias_p) in
if not exists then begin
- try init_ocaml alias ocaml_version;
+ try
+ let alias, ocaml_version = init_ocaml (Some alias) (true, Some ocaml_version) in
+ add_alias t alias ocaml_version
with e ->
(* restore the previous configuration *)
File.Config.write (Path.G.config t.global) t.config;
+ File.Aliases.write (Path.G.aliases t.global) t.aliases;
if not !Globals.debug then
Dirname.rmdir (Path.C.root alias_p);
raise e
View
@@ -23,7 +23,7 @@ open Types
- [alias] the compiler alias
- [oversion] is the version of the compiler
- [cores] is the number of cores *)
-val init : repository -> Alias.t -> OCaml_V.t -> int -> unit
+val init : repository -> Alias.t option -> OCaml_V.t option -> int -> unit
(** Displays all available packages.
If [bool] is [true], then we only display
View
@@ -64,27 +64,17 @@ let parse_args fn () =
(* opam init [-kind $kind] $repo $adress *)
let init =
let kind = ref Globals.default_repository_kind in
- let alias = ref "" in
- let comp = ref "" in
+ let alias = ref None in
+ let comp = ref None in
let cores = ref Globals.default_cores in
- let init () =
- let comp =
- if !comp <> "" then OCaml_V.of_string !comp
- else match OCaml_V.current () with
- | None -> bad_argument "init" "No OCaml compiler found in path"
- | Some _ -> OCaml_V.of_string Globals.default_compiler_version in
- let alias =
- if !alias <> "" then Alias.of_string !alias
- else Alias.of_string (OCaml_V.to_string comp) in
- alias, comp in
{
name = "init";
usage = "";
synopsis = "Initial setup";
help = "Create the initial config files";
specs = [
- ("-comp" , Arg.Set_string comp , " Which compiler version to use");
- ("-alias", Arg.Set_string alias, " Set the compiler alias name");
+ ("-comp" , Arg.String (fun s -> comp := Some (OCaml_V.of_string s)), " Which compiler version to use");
+ ("-alias", Arg.String (fun s -> alias := Some (Alias.of_string s)), " Set the compiler alias name");
("-cores", Arg.Set_int cores , " Set the nomber of cores");
("-kind" , Arg.Set_string kind , " Set the repository kind");
("-no-base-packages", Arg.Clear Globals.base_packages, " Do not install the base packages");
@@ -93,12 +83,10 @@ let init =
main =
parse_args (function
| [] ->
- let alias, comp = init () in
- Client.init Repository.default alias comp !cores
+ Client.init Repository.default !alias !comp !cores
| [name; address] ->
- let alias, comp = init () in
let repo = Repository.create ~name ~address ~kind:!kind in
- Client.init repo alias comp !cores
+ Client.init repo !alias !comp !cores
| _ -> bad_argument "init" "Need a repository name and address")
}

0 comments on commit a4d01ec

Please sign in to comment.