Skip to content

3.1. Variables and Contexts

Andrey Chaschev edited this page Jan 11, 2014 · 5 revisions

Bear is driven by variables. Almost any piece of Bear can be reconfigured.

A variable is typically a field in a plugin or a project.

public final DynamicVariable<Boolean> useUI = newVar(true);

A variable could be a constant value or a dynamic values - a closure. Variables are evaluated within a Global or a Session context and can be overridden in each of these contexts. If a variable is not defined in a Session context, evaluation falls back to the Global context.

To evaluate a variable value, one could do:

boolean useUI;
useUI = _.var(useUI);          // in a Session Context
useUI = global.var(useUI);     // in the Global Context

Below is an example of how a password can be read from a property. In most cases it is set globally by overriding a variable in a Global Context:

public final DynamicVariable<String> sshPassword = dynamic((_) -> {
    String password = _.getGlobal().getProperty(_.concat(bear.sessionHostname, ".password"));

    if (password == null) {
        password = $.getGlobal().getProperty(var.name());
    }

    return password;
});

To override password retrieval strategy per session, one could do:

global.put(bear.sshPassword, dynamic((_) -> { /* get the password for _.getHost() */ }))

Setting up variables

There are several ways to set up the variables

  • Set/override value in a Session Context. This value can be overridden only in the same context:
_.putConst(useUI, false)
_.put(useUI, dynamic((_) -> {false})
  • Set/override value in a Global Context. The value set can be overridden in both contexts.
  • Directly set the default value or a dynamic implementation for a variable. This value can be reassigned or overridden in both contexts
useUI.set(false)
useUI.dynamic((_) -> { false })
  • Setting up variables by name from a command line:
bear -VnodeJs.clean=false drywall.deploy

If you encounter troubles guessing a variable name, for now you can use debug to get it (global.getPlugin(NodeJsPlugin.class).clean.name()). In future there will be a feature to list variables.

  • Setting up variables by name from a *.properties file or a system property. Same as previous, with a different place to declare the value.
Clone this wiki locally