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;
}
}
}
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);
}
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")));
}
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}$");
}
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 = '')");
}
Aggregations