use of com.google.devtools.build.lib.syntax.BuiltinFunction in project bazel by bazelbuild.
the class WorkspaceFactory method newRuleFunction.
/**
* Returns a function-value implementing the build rule "ruleClass" (e.g. cc_library) in the
* specified package context.
*/
private static BuiltinFunction newRuleFunction(final RuleFactory ruleFactory, final String ruleClassName, final boolean allowOverride) {
return new BuiltinFunction(ruleClassName, FunctionSignature.KWARGS, BuiltinFunction.USE_AST_ENV) {
public Object invoke(Map<String, Object> kwargs, FuncallExpression ast, Environment env) throws EvalException, InterruptedException {
try {
Package.Builder builder = PackageFactory.getContext(env, ast).pkgBuilder;
if (!allowOverride && kwargs.containsKey("name") && builder.targets.containsKey(kwargs.get("name"))) {
throw new EvalException(ast.getLocation(), "Cannot redefine repository after any load statement in the WORKSPACE file" + " (for repository '" + kwargs.get("name") + "')");
}
RuleClass ruleClass = ruleFactory.getRuleClass(ruleClassName);
RuleClass bindRuleClass = ruleFactory.getRuleClass("bind");
Rule rule = builder.externalPackageData().createAndAddRepositoryRule(builder, ruleClass, bindRuleClass, kwargs, ast);
if (!isLegalWorkspaceName(rule.getName())) {
throw new EvalException(ast.getLocation(), rule + "'s name field must be a legal workspace name");
}
} catch (RuleFactory.InvalidRuleException | Package.NameConflictException | LabelSyntaxException e) {
throw new EvalException(ast.getLocation(), e.getMessage());
}
return NONE;
}
};
}
use of com.google.devtools.build.lib.syntax.BuiltinFunction in project bazel by bazelbuild.
the class SkylarkRuleImplementationFunctionsTest method testNoStackTraceOnInterrupt.
@Test
public void testNoStackTraceOnInterrupt() throws Exception {
setupThrowFunction(new BuiltinFunction("throw") {
@SuppressWarnings("unused")
public Object invoke() throws Exception {
throw new InterruptedException();
}
});
try {
eval("throw()");
fail("Expected an InterruptedException");
} catch (InterruptedException ex) {
// Expected.
}
}
use of com.google.devtools.build.lib.syntax.BuiltinFunction in project bazel by bazelbuild.
the class SkylarkRepositoryContextTest method buildRuleClass.
protected static RuleClass buildRuleClass(Attribute... attributes) {
RuleClass.Builder ruleClassBuilder = new RuleClass.Builder("test", RuleClassType.WORKSPACE, true);
for (Attribute attr : attributes) {
ruleClassBuilder.addOrOverrideAttribute(attr);
}
ruleClassBuilder.setWorkspaceOnly();
ruleClassBuilder.setConfiguredTargetFunction(new BuiltinFunction("test") {
});
return ruleClassBuilder.build();
}
use of com.google.devtools.build.lib.syntax.BuiltinFunction in project bazel by bazelbuild.
the class WorkspaceFactory method newBindFunction.
private static BuiltinFunction newBindFunction(final RuleFactory ruleFactory) {
return new BuiltinFunction("bind", FunctionSignature.namedOnly(1, "name", "actual"), BuiltinFunction.USE_AST_ENV) {
public Object invoke(String name, String actual, FuncallExpression ast, Environment env) throws EvalException, InterruptedException {
Label nameLabel;
try {
nameLabel = Label.parseAbsolute("//external:" + name);
try {
Package.Builder builder = PackageFactory.getContext(env, ast).pkgBuilder;
RuleClass ruleClass = ruleFactory.getRuleClass("bind");
builder.externalPackageData().addBindRule(builder, ruleClass, nameLabel, actual == null ? null : Label.parseAbsolute(actual), ast.getLocation(), ruleFactory.getAttributeContainer(ruleClass));
} catch (RuleFactory.InvalidRuleException | Package.NameConflictException | LabelSyntaxException e) {
throw new EvalException(ast.getLocation(), e.getMessage());
}
} catch (LabelSyntaxException e) {
throw new EvalException(ast.getLocation(), e.getMessage());
}
return NONE;
}
};
}
use of com.google.devtools.build.lib.syntax.BuiltinFunction in project bazel by bazelbuild.
the class SkylarkRuleImplementationFunctionsTest method testStackTraceWithoutOriginalMessage.
@Test
public void testStackTraceWithoutOriginalMessage() throws Exception {
setupThrowFunction(new BuiltinFunction("throw") {
@SuppressWarnings("unused")
public Object invoke() throws Exception {
throw new ThereIsNoMessageException();
}
});
checkEvalErrorContains("There Is No Message: SkylarkRuleImplementationFunctionsTest$3.invoke() in " + "SkylarkRuleImplementationFunctionsTest.java:", // This test skips the line number since it was not consistent across local tests and TAP.
"throw()");
}
Aggregations