Skip to content

Commit

Permalink
[opam init -comp] fix the failure to start when there is no OCaml (ex…
Browse files Browse the repository at this point in the history
…cept .comp files in repository)
  • Loading branch information
tuong committed Jul 23, 2012
1 parent b982256 commit a4d01ec
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 49 deletions.
90 changes: 60 additions & 30 deletions src/client.ml
Expand Up @@ -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: (* We assume that we have the right ocaml-version in $opam/config. Then we:
- create $opam/$alias - create $opam/$alias
- compiles and install $opam/compiler/$descr.comp *) - compiles and install $opam/compiler/$descr.comp *)
let init_ocaml alias ocaml_version = let init_ocaml alias (default_allowed, ocaml_version) =
log "init_ocaml %s %s" (Alias.to_string alias) (OCaml_V.to_string ocaml_version); log "init_ocaml";
let t = load_state () in 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); let default = OCaml_V.of_string Globals.default_compiler_version in
add_alias t alias ocaml_version; let ocaml_version =
let t = load_state () in 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 (* we create a dummy compiler description file the the system-wide
OCaml configuration *) OCaml configuration *)
create_default_compiler_description t; create_default_compiler_description t;


end else try end else
try
let comp_f = Path.G.compiler t.global ocaml_version in 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 let comp = File.Comp.read comp_f in
if not (File.Comp.preinstalled comp) then begin if not (File.Comp.preinstalled comp) then begin


Expand Down Expand Up @@ -548,8 +569,8 @@ let init_ocaml alias ocaml_version =
with e -> with e ->
if not !Globals.debug then if not !Globals.debug then
Dirname.rmdir (Path.C.root alias_p); 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 indent_left s nb =
let nb = nb - String.length s in let nb = nb - String.length s in
Expand Down Expand Up @@ -1186,26 +1207,32 @@ let init repo alias ocaml_version cores =
if Filename.exists config_f then if Filename.exists config_f then
Globals.error_and_exit "%s already exist" (Filename.to_string config_f) Globals.error_and_exit "%s already exist" (Filename.to_string config_f)
else try 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 let repo_p = Path.R.create repo in
(* Create (possibly empty) configuration files *) (* 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_index.write (Path.G.repo_index root) N.Map.empty;
File.Repo_config.write (Path.R.config repo_p) repo; File.Repo_config.write (Path.R.config repo_p) repo;
Repositories.init repo; Repositories.init repo;
Dirname.mkdir (Path.G.opam_dir root); Dirname.mkdir (Path.G.opam_dir root);
Dirname.mkdir (Path.G.descr_dir root); Dirname.mkdir (Path.G.descr_dir root);
Dirname.mkdir (Path.G.archive_dir root); Dirname.mkdir (Path.G.archive_dir root);
Dirname.mkdir (Path.G.compiler_dir root); Dirname.mkdir (Path.G.compiler_dir root);
let alias_p = Path.C.root (Path.C.create alias) in
update_repo (); update_repo ();
if Dirname.exists alias_p then (match alias with
Globals.warning "%s does not exist and %s already exist" (Filename.to_string config_f) (Dirname.to_string alias_p) | Some alias ->
else let alias_p = Path.C.root (Path.C.create alias) in
init_ocaml alias ocaml_version; 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 (); 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 let wish_install = Heuristic.get_packages t ocaml_version Heuristic.v_any in
Heuristic.resolve `init t Heuristic.resolve `init t
[ { wish_install [ { wish_install
Expand Down Expand Up @@ -1633,10 +1660,13 @@ let switch clone alias ocaml_version =
(* [2/3] install the new OCaml version *) (* [2/3] install the new OCaml version *)
let exists = Dirname.exists (Path.C.root alias_p) in let exists = Dirname.exists (Path.C.root alias_p) in
if not exists then begin 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 -> with e ->
(* restore the previous configuration *) (* restore the previous configuration *)
File.Config.write (Path.G.config t.global) t.config; 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 if not !Globals.debug then
Dirname.rmdir (Path.C.root alias_p); Dirname.rmdir (Path.C.root alias_p);
raise e raise e
Expand Down
2 changes: 1 addition & 1 deletion src/client.mli
Expand Up @@ -23,7 +23,7 @@ open Types
- [alias] the compiler alias - [alias] the compiler alias
- [oversion] is the version of the compiler - [oversion] is the version of the compiler
- [cores] is the number of cores *) - [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. (** Displays all available packages.
If [bool] is [true], then we only display If [bool] is [true], then we only display
Expand Down
24 changes: 6 additions & 18 deletions src/opam.ml
Expand Up @@ -64,27 +64,17 @@ let parse_args fn () =
(* opam init [-kind $kind] $repo $adress *) (* opam init [-kind $kind] $repo $adress *)
let init = let init =
let kind = ref Globals.default_repository_kind in let kind = ref Globals.default_repository_kind in
let alias = ref "" in let alias = ref None in
let comp = ref "" in let comp = ref None in
let cores = ref Globals.default_cores 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"; name = "init";
usage = ""; usage = "";
synopsis = "Initial setup"; synopsis = "Initial setup";
help = "Create the initial config files"; help = "Create the initial config files";
specs = [ specs = [
("-comp" , Arg.Set_string comp , " Which compiler version to use"); ("-comp" , Arg.String (fun s -> comp := Some (OCaml_V.of_string s)), " Which compiler version to use");
("-alias", Arg.Set_string alias, " Set the compiler alias name"); ("-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"); ("-cores", Arg.Set_int cores , " Set the nomber of cores");
("-kind" , Arg.Set_string kind , " Set the repository kind"); ("-kind" , Arg.Set_string kind , " Set the repository kind");
("-no-base-packages", Arg.Clear Globals.base_packages, " Do not install the base packages"); ("-no-base-packages", Arg.Clear Globals.base_packages, " Do not install the base packages");
Expand All @@ -93,12 +83,10 @@ let init =
main = main =
parse_args (function parse_args (function
| [] -> | [] ->
let alias, comp = init () in Client.init Repository.default !alias !comp !cores
Client.init Repository.default alias comp !cores
| [name; address] -> | [name; address] ->
let alias, comp = init () in
let repo = Repository.create ~name ~address ~kind:!kind 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") | _ -> bad_argument "init" "Need a repository name and address")
} }


Expand Down

0 comments on commit a4d01ec

Please sign in to comment.