Skip to content

Commit

Permalink
Expose OpamState.contents_of_variable to resolve opam variables
Browse files Browse the repository at this point in the history
  • Loading branch information
samoht committed Aug 20, 2015
1 parent 848082a commit 7048b8d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 27 deletions.
31 changes: 4 additions & 27 deletions src/client/opamConfigCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -185,34 +185,11 @@ let set var value =
OpamFile.Dot_config.write config_f
(OpamFile.Dot_config.set config var newval)

let quick_lookup v =
if OpamVariable.Full.is_global v then (
let var = OpamVariable.Full.variable v in
let root = OpamStateConfig.(!r.root_dir) in
let switch = OpamStateConfig.(!r.current_switch) in
let config_f = OpamPath.Switch.global_config root switch in
let config = OpamFile.Dot_config.read config_f in
match OpamState.get_env_var v with
| Some _ as c -> c
| None ->
if OpamVariable.to_string var = "switch" then
Some (S (OpamSwitch.to_string switch))
else
OpamFile.Dot_config.variable config var
) else
None

let variable v =
log "config-variable";
let contents =
match quick_lookup v with
| Some c -> c
| None ->
let t = OpamState.load_state "config-variable"
OpamStateConfig.(!r.current_switch) in
OpamFilter.ident_value (OpamState.filter_env t) ~default:(S "#undefined")
(OpamFilter.ident_of_var v)
in
let t = lazy (
OpamState.load_state "config-variable" OpamStateConfig.(!r.current_switch)
) in
let contents = OpamState.contents_of_variable t v in
OpamConsole.msg "%s\n" (OpamVariable.string_of_variable_contents contents)

let setup user global =
Expand Down
26 changes: 26 additions & 0 deletions src/state/opamState.ml
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,32 @@ let consistent_available_field t opam =
OpamFilter.eval_to_bool ~default:false (filter_env ~opam t)
(OpamFile.OPAM.available opam)

let quick_var_lookup v =
if OpamVariable.Full.is_global v then (
let var = OpamVariable.Full.variable v in
let root = OpamStateConfig.(!r.root_dir) in
let switch = OpamStateConfig.(!r.current_switch) in
let config_f = OpamPath.Switch.global_config root switch in
let config = OpamFile.Dot_config.read config_f in
match get_env_var v with
| Some _ as c -> c
| None ->
if OpamVariable.to_string var = "switch" then
Some (S (OpamSwitch.to_string switch))
else
OpamFile.Dot_config.variable config var
) else
None

let contents_of_variable t v =
log "config-variable";
match quick_var_lookup v with
| Some c -> c
| None ->
let env = filter_env (Lazy.force t) in
let default = S "#undefined" in
OpamFilter.ident_value env ~default (OpamFilter.ident_of_var v)

(* List the packages which do fulfil the compiler and OS constraints *)
let available_packages t =
let filter nv =
Expand Down
6 changes: 6 additions & 0 deletions src/state/opamState.mli
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ val filter_env:
?local_variables:((variable_contents option) OpamVariable.Map.t) ->
state -> full_variable -> variable_contents option

(** [contents_of_variable t v] resolves the variable [v] using the
(lazy) state [t]. First check in the environment for overwrites,
then use an heuristic to avoid read the global state and then use
{!filter_env}. Return ["#undefined"] if [v] is not defined.*)
val contents_of_variable: state Lazy.t -> full_variable -> variable_contents

(** {2 Helpers} *)

(** Return the OPAM file for the given package *)
Expand Down

0 comments on commit 7048b8d

Please sign in to comment.