Add ability to look up scopes by external name

This commit is contained in:
Rudi Grinberg 2018-01-30 19:44:55 +08:00
parent f6cefa40cc
commit 91f6a67850
2 changed files with 18 additions and 4 deletions

View File

@ -17,6 +17,7 @@ type t =
instalable_internal_libs : Lib.Internal.t String_map.t
; local_public_libs : Path.t String_map.t
; anonymous_root : Path.t
; by_scope_name : (string, scope) Hashtbl.t
}
let rec internal_name_scope t ~dir =
@ -273,15 +274,19 @@ let create findlib ~scopes ~root internal_libraries =
; instalable_internal_libs = String_map.empty
; local_public_libs
; anonymous_root = root
; by_scope_name = Hashtbl.create 1024
}
in
(* Initializes the scopes, including [Path.root] so that when there are no <pkg>.opam
files in parent directories, the scope is the whole workspace. *)
List.iter scopes ~f:(fun (scope : Jbuild.Scope.t) ->
Hashtbl.add t.by_internal_name ~key:scope.root
~data:{ libs = String_map.empty
; scope
});
let lib_scope = { libs = String_map.empty; scope } in
Option.iter scope.name ~f:(fun name ->
assert (name <> "");
assert (not (Hashtbl.mem t.by_scope_name name));
Hashtbl.add t.by_scope_name ~key:name ~data:lib_scope;
);
Hashtbl.add t.by_internal_name ~key:scope.root ~data:lib_scope);
List.iter internal_libraries ~f:(fun ((dir, lib) as internal) ->
let scope = internal_name_scope t ~dir in
scope.libs <- String_map.add scope.libs ~key:lib.Library.name ~data:internal;
@ -326,3 +331,8 @@ let anonymous_scope t =
lib_db = t
; scope = internal_name_scope t ~dir:t.anonymous_root
}
let find_scope_by_name_exn t ~name =
match Hashtbl.find t.by_scope_name name with
| None -> raise (Code_error (sprintf "Invalid scope '%s'" name))
| Some scope -> { Scope.scope ; lib_db = t }

View File

@ -87,3 +87,7 @@ val anonymous_scope : t -> Scope.t
(** Contains only publicly, and external (findlib) libraries *)
val external_scope : t -> Scope.t
(** Find scope by the their explicit names (opam package names) [""] corresponds
to the anonymous scope *)
val find_scope_by_name_exn : t -> name:string -> Scope.t