Search in sources :

Example 31 with SkylarkRuleContext

use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.

the class SkylarkAspectFactory method create.

@Override
public ConfiguredAspect create(ConfiguredTarget base, RuleContext ruleContext, AspectParameters parameters) throws InterruptedException {
    try (Mutability mutability = Mutability.create("aspect")) {
        AspectDescriptor aspectDescriptor = new AspectDescriptor(skylarkAspect.getAspectClass(), parameters);
        SkylarkRuleContext skylarkRuleContext;
        try {
            skylarkRuleContext = new SkylarkRuleContext(ruleContext, aspectDescriptor);
        } catch (EvalException e) {
            ruleContext.ruleError(e.getMessage());
            return null;
        }
        Environment env = Environment.builder(mutability).setGlobals(skylarkAspect.getFuncallEnv().getGlobals()).setEventHandler(ruleContext.getAnalysisEnvironment().getEventHandler()).build();
        // so we do *not* setLoadingPhase().
        Object aspectSkylarkObject;
        try {
            aspectSkylarkObject = skylarkAspect.getImplementation().call(ImmutableList.<Object>of(base, skylarkRuleContext), ImmutableMap.<String, Object>of(), /*ast=*/
            null, env);
            if (ruleContext.hasErrors()) {
                return null;
            } else if (!(aspectSkylarkObject instanceof SkylarkClassObject) && !(aspectSkylarkObject instanceof Iterable)) {
                ruleContext.ruleError(String.format("Aspect implementation should return a struct or a list, but got %s", SkylarkType.typeOf(aspectSkylarkObject)));
                return null;
            }
            return createAspect(aspectSkylarkObject, aspectDescriptor, ruleContext);
        } catch (EvalException e) {
            addAspectToStackTrace(base, e);
            ruleContext.ruleError("\n" + e.print());
            return null;
        }
    }
}
Also used : SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) Mutability(com.google.devtools.build.lib.syntax.Mutability) AspectDescriptor(com.google.devtools.build.lib.packages.AspectDescriptor) Environment(com.google.devtools.build.lib.syntax.Environment) SkylarkClassObject(com.google.devtools.build.lib.packages.SkylarkClassObject) EvalException(com.google.devtools.build.lib.syntax.EvalException) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext)

Example 32 with SkylarkRuleContext

use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.

the class SkylarkFileHelperTest method testArtifactShortPath.

@Test
public void testArtifactShortPath() throws Exception {
    SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
    String result = (String) evalRuleContextCode(ruleContext, "ruleContext.files.tools[0].short_path");
    assertEquals("foo/t.exe", result);
}
Also used : SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) Test(org.junit.Test)

Example 33 with SkylarkRuleContext

use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.

the class JavaSkylarkCommon method getDefaultJavacOpts.

@SkylarkCallable(name = "default_javac_opts", // This function is experimental for now.
documented = false, // There's only one mandatory positional,the Skylark context
mandatoryPositionals = 1, parameters = { @Param(name = "java_toolchain_attr", positional = false, named = true, type = String.class) })
public static List<String> getDefaultJavacOpts(SkylarkRuleContext skylarkRuleContext, String javaToolchainAttr) {
    RuleContext ruleContext = skylarkRuleContext.getRuleContext();
    ConfiguredTarget javaToolchainConfigTarget = (ConfiguredTarget) checkNotNull(skylarkRuleContext.getAttr().getValue(javaToolchainAttr));
    JavaToolchainProvider toolchain = checkNotNull(javaToolchainConfigTarget.getProvider(JavaToolchainProvider.class));
    return ImmutableList.copyOf(Iterables.concat(toolchain.getJavacOptions(), ruleContext.getTokenizedStringListAttr("javacopts")));
}
Also used : RuleContext(com.google.devtools.build.lib.analysis.RuleContext) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) SkylarkCallable(com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)

Example 34 with SkylarkRuleContext

use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.

the class SkylarkRuleContextTest method testCreateSpawnActionArgumentsWithExecutableFilesToRunProvider.

@Test
public void testCreateSpawnActionArgumentsWithExecutableFilesToRunProvider() throws Exception {
    SkylarkRuleContext ruleContext = createRuleContext("//foo:androidlib");
    evalRuleContextCode(ruleContext, "ruleContext.action(\n" + "  inputs = ruleContext.files.srcs,\n" + "  outputs = ruleContext.files.srcs,\n" + "  arguments = ['--a','--b'],\n" + "  executable = ruleContext.executable._jarjar_bin)\n");
    SpawnAction action = (SpawnAction) Iterables.getOnlyElement(ruleContext.getRuleContext().getAnalysisEnvironment().getRegisteredActions());
    assertThat(action.getCommandFilename()).matches("^.*/jarjar_bin(\\.cmd){0,1}$");
}
Also used : SpawnAction(com.google.devtools.build.lib.analysis.actions.SpawnAction) SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) Test(org.junit.Test)

Example 35 with SkylarkRuleContext

use of com.google.devtools.build.lib.rules.SkylarkRuleContext in project bazel by bazelbuild.

the class SkylarkRuleImplementationFunctionsTest method testRunfilesBadKeywordArguments.

@Test
public void testRunfilesBadKeywordArguments() throws Exception {
    SkylarkRuleContext ruleContext = createRuleContext("//foo:foo");
    checkErrorContains(ruleContext, "unexpected keyword 'bad_keyword' in call to runfiles(self: ctx, ", "ruleContext.runfiles(bad_keyword = '')");
}
Also used : SkylarkRuleContext(com.google.devtools.build.lib.rules.SkylarkRuleContext) Test(org.junit.Test)

Aggregations

SkylarkRuleContext (com.google.devtools.build.lib.rules.SkylarkRuleContext)81 Test (org.junit.Test)79 SkylarkClassObject (com.google.devtools.build.lib.packages.SkylarkClassObject)47 Label (com.google.devtools.build.lib.cmdline.Label)6 Artifact (com.google.devtools.build.lib.actions.Artifact)5 SpawnAction (com.google.devtools.build.lib.analysis.actions.SpawnAction)5 SkylarkList (com.google.devtools.build.lib.syntax.SkylarkList)4 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)4 ActionAnalysisMetadata (com.google.devtools.build.lib.actions.ActionAnalysisMetadata)2 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)2 TemplateExpansionAction (com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction)2 SkylarkDict (com.google.devtools.build.lib.syntax.SkylarkDict)2 ImmutableList (com.google.common.collect.ImmutableList)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 RuleContext (com.google.devtools.build.lib.analysis.RuleContext)1 FileWriteAction (com.google.devtools.build.lib.analysis.actions.FileWriteAction)1 Substitution (com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction.Substitution)1 AspectDescriptor (com.google.devtools.build.lib.packages.AspectDescriptor)1 JavaSourceJarsProvider (com.google.devtools.build.lib.rules.java.JavaSourceJarsProvider)1 SkylarkCallable (com.google.devtools.build.lib.skylarkinterface.SkylarkCallable)1