Search in sources :

Example 1 with SkylarkDict

use of com.google.devtools.build.lib.syntax.SkylarkDict in project bazel by bazelbuild.

the class PackageFactory method callGetRulesFunction.

static SkylarkDict<String, SkylarkDict<String, Object>> callGetRulesFunction(FuncallExpression ast, Environment env) throws EvalException {
    PackageContext context = getContext(env, ast);
    Collection<Target> targets = context.pkgBuilder.getTargets();
    Location loc = ast.getLocation();
    SkylarkDict<String, SkylarkDict<String, Object>> rules = SkylarkDict.of(env);
    for (Target t : targets) {
        if (t instanceof Rule) {
            SkylarkDict<String, Object> m = targetDict(t, loc, env);
            Preconditions.checkNotNull(m);
            rules.put(t.getName(), m, loc, env);
        }
    }
    return rules;
}
Also used : ClassObject(com.google.devtools.build.lib.syntax.ClassObject) SkylarkDict(com.google.devtools.build.lib.syntax.SkylarkDict) Location(com.google.devtools.build.lib.events.Location)

Example 2 with SkylarkDict

use of com.google.devtools.build.lib.syntax.SkylarkDict in project bazel by bazelbuild.

the class SkylarkRuleContextTest method testCreatedActions.

// For created_actions() tests, the "undertest" rule represents both the code under test and the
// Skylark user test code itself.
@Test
public void testCreatedActions() throws Exception {
    // createRuleContext() gives us the context for a rule upon entry into its analysis function.
    // But we need to inspect the result of calling created_actions() after the rule context has
    // been modified by creating actions. So we'll call created_actions() from within the analysis
    // function and pass it along as a provider.
    scratch.file("test/rules.bzl", "def _undertest_impl(ctx):", "  out1 = ctx.outputs.out1", "  out2 = ctx.outputs.out2", "  ctx.action(outputs=[out1], command='echo foo123 > ' + out1.path,", "             mnemonic='foo')", "  v = ctx.created_actions().by_file", "  ctx.action(outputs=[out2], command='echo bar123 > ' + out2.path)", "  return struct(v=v, out1=out1, out2=out2)", "undertest_rule = rule(", "  implementation = _undertest_impl,", "  outputs = {'out1': '%{name}1.txt',", "             'out2': '%{name}2.txt'},", "  _skylark_testable = True,", ")", testingRuleDefinition);
    scratch.file("test/BUILD", simpleBuildDefinition);
    SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
    Object mapUnchecked = evalRuleContextCode(ruleContext, "ruleContext.attr.dep.v");
    assertThat(mapUnchecked).isInstanceOf(SkylarkDict.class);
    SkylarkDict<?, ?> map = (SkylarkDict<?, ?>) mapUnchecked;
    // Should only have the first action because created_actions() was called
    // before the second action was created.
    Object file = eval("ruleContext.attr.dep.out1");
    assertThat(map).hasSize(1);
    assertThat(map).containsKey(file);
    Object actionUnchecked = map.get(file);
    assertThat(actionUnchecked).isInstanceOf(ActionAnalysisMetadata.class);
    assertThat(((ActionAnalysisMetadata) actionUnchecked).getMnemonic()).isEqualTo("foo");
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) ActionAnalysisMetadata(com.google.devtools.build.lib.actions.ActionAnalysisMetadata) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) SkylarkDict(com.google.devtools.build.lib.syntax.SkylarkDict) Test(org.junit.Test)

Example 3 with SkylarkDict

use of com.google.devtools.build.lib.syntax.SkylarkDict in project bazel by bazelbuild.

the class SkylarkRuleContextTest method testDependencyActionsProvider.

@Test
public void testDependencyActionsProvider() throws Exception {
    scratch.file("test/rules.bzl", getSimpleUnderTestDefinition("ctx.action(outputs=[out], command='echo foo123 > ' + out.path)"), testingRuleDefinition);
    scratch.file("test/BUILD", simpleBuildDefinition);
    SkylarkRuleContext ruleContext = createRuleContext("//test:testing");
    Object provider = evalRuleContextCode(ruleContext, "ruleContext.attr.dep[Actions]");
    assertThat(provider).isInstanceOf(SkylarkClassObject.class);
    assertThat(((SkylarkClassObject) provider).getConstructor()).isEqualTo(ActionsProvider.SKYLARK_CONSTRUCTOR);
    update("actions", provider);
    Object mapping = eval("actions.by_file");
    assertThat(mapping).isInstanceOf(SkylarkDict.class);
    assertThat((SkylarkDict<?, ?>) mapping).hasSize(1);
    update("file", eval("list(ruleContext.attr.dep.files)[0]"));
    Object actionUnchecked = eval("actions.by_file[file]");
    assertThat(actionUnchecked).isInstanceOf(ActionAnalysisMetadata.class);
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) SkylarkDict(com.google.devtools.build.lib.syntax.SkylarkDict) Test(org.junit.Test)

Aggregations

SkylarkDict (com.google.devtools.build.lib.syntax.SkylarkDict)3 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)2 SkylarkRuleContext (com.google.devtools.build.lib.rules.SkylarkRuleContext)2 Test (org.junit.Test)2 ActionAnalysisMetadata (com.google.devtools.build.lib.actions.ActionAnalysisMetadata)1 Location (com.google.devtools.build.lib.events.Location)1 ClassObject (com.google.devtools.build.lib.syntax.ClassObject)1