use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.
the class SkylarkIntegrationTest method rulesReturningDeclaredProviders.
@Test
public void rulesReturningDeclaredProviders() throws Exception {
scratch.file("test/extension.bzl", "my_provider = provider()", "def _impl(ctx):", " return [my_provider(x = 1)]", "my_rule = rule(_impl)");
scratch.file("test/BUILD", "load(':extension.bzl', 'my_rule')", "my_rule(name = 'r')");
ConfiguredTarget configuredTarget = getConfiguredTarget("//test:r");
ClassObjectConstructor.Key key = new SkylarkClassObjectConstructor.SkylarkKey(Label.create(configuredTarget.getLabel().getPackageIdentifier(), "extension.bzl"), "my_provider");
SkylarkProviders skylarkProviders = configuredTarget.getProvider(SkylarkProviders.class);
assertThat(skylarkProviders).isNotNull();
SkylarkClassObject declaredProvider = skylarkProviders.getDeclaredProvider(key);
assertThat(declaredProvider).isNotNull();
assertThat(declaredProvider.getConstructor().getKey()).isEqualTo(key);
assertThat(declaredProvider.getValue("x")).isEqualTo(1);
}
use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.
the class SkylarkAspectsTest method labelKeyedStringDictAllowsAspects.
@Test
public void labelKeyedStringDictAllowsAspects() throws Exception {
scratch.file("test/aspect.bzl", "def _aspect_impl(target, ctx):", " return struct(aspect_data=target.label.name)", "", "def _rule_impl(ctx):", " return struct(", " data=','.join(['{}:{}'.format(dep.aspect_data, val)", " for dep, val in ctx.attr.attr.items()]))", "", "MyAspect = aspect(", " implementation=_aspect_impl,", ")", "my_rule = rule(", " implementation=_rule_impl,", " attrs = { 'attr' : ", " attr.label_keyed_string_dict(aspects = [MyAspect]) },", ")");
scratch.file("test/BUILD", "load('/test/aspect', 'my_rule')", "java_library(", " name = 'yyy',", ")", "my_rule(", " name = 'xxx',", " attr = {':yyy': 'zzz'},", ")");
AnalysisResult analysisResult = update("//test:xxx");
ConfiguredTarget target = analysisResult.getTargetsToBuild().iterator().next();
SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
Object value = skylarkProviders.getValue("data");
assertThat(value).isEqualTo("yyy:zzz");
}
use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectOnLabelAttr.
@Test
public void aspectOnLabelAttr() throws Exception {
scratch.file("test/aspect.bzl", "def _aspect_impl(target, ctx):", " return struct(aspect_data='foo')", "", "def _rule_impl(ctx):", " return struct(data=ctx.attr.attr.aspect_data)", "", "MyAspect = aspect(", " implementation=_aspect_impl,", ")", "my_rule = rule(", " implementation=_rule_impl,", " attrs = { 'attr' : ", " attr.label(aspects = [MyAspect]) },", ")");
scratch.file("test/BUILD", "load('/test/aspect', 'my_rule')", "java_library(", " name = 'yyy',", ")", "my_rule(", " name = 'xxx',", " attr = ':yyy',", ")");
AnalysisResult analysisResult = update("//test:xxx");
ConfiguredTarget target = analysisResult.getTargetsToBuild().iterator().next();
SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
Object value = skylarkProviders.getValue("data");
assertThat(value).isEqualTo("foo");
}
use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.
the class SkylarkAspectsTest method aspectsFromSkylarkRules.
@Test
public void aspectsFromSkylarkRules() throws Exception {
scratch.file("test/aspect.bzl", "def _aspect_impl(target, ctx):", " s = depset([target.label])", " for i in ctx.rule.attr.deps:", " s += i.target_labels", " return struct(target_labels = s)", "", "def _rule_impl(ctx):", " s = depset([])", " for i in ctx.attr.attr:", " s += i.target_labels", " return struct(rule_deps = s)", "", "MyAspect = aspect(", " implementation=_aspect_impl,", " attr_aspects=['deps'],", ")", "my_rule = rule(", " implementation=_rule_impl,", " attrs = { 'attr' : ", " attr.label_list(mandatory=True, allow_files=True, aspects = [MyAspect]) },", ")");
scratch.file("test/BUILD", "load('/test/aspect', 'my_rule')", "java_library(", " name = 'yyy',", ")", "my_rule(", " name = 'xxx',", " attr = [':yyy'],", ")");
AnalysisResult analysisResult = update("//test:xxx");
assertThat(getLabelsToBuild(analysisResult)).containsExactly("//test:xxx");
ConfiguredTarget target = analysisResult.getTargetsToBuild().iterator().next();
SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
assertThat(skylarkProviders).isNotNull();
Object names = skylarkProviders.getValue("rule_deps");
assertThat(names).isInstanceOf(SkylarkNestedSet.class);
assertThat(transform(((SkylarkNestedSet) names).toCollection(), new Function<Object, String>() {
@Nullable
@Override
public String apply(Object o) {
assertThat(o).isInstanceOf(Label.class);
return o.toString();
}
})).containsExactly("//test:yyy");
}
use of com.google.devtools.build.lib.analysis.SkylarkProviders in project bazel by bazelbuild.
the class JavaProvider method getProvider.
/**
* Returns a provider of the specified class, fetched from the specified target or, if not found,
* from the JavaProvider of the given target. JavaProvider can be found as a declared provider
* in SkylarkProviders.
* Returns null if no such provider exists.
*
* <p>A target can either have both the specified provider and JavaProvider that encapsulates the
* same information, or just one of them.</p>
*/
@Nullable
public static <T extends TransitiveInfoProvider> T getProvider(Class<T> providerClass, TransitiveInfoCollection target) {
T provider = target.getProvider(providerClass);
if (provider != null) {
return provider;
}
SkylarkProviders skylarkProviders = target.getProvider(SkylarkProviders.class);
if (skylarkProviders == null) {
return null;
}
JavaProvider javaProvider = (JavaProvider) skylarkProviders.getDeclaredProvider(JavaProvider.JAVA_PROVIDER.getKey());
if (javaProvider == null) {
return null;
}
return javaProvider.getProvider(providerClass);
}
Aggregations