Search in sources :

Example 1 with AddToRuleKey

use of com.facebook.buck.rules.AddToRuleKey in project buck by facebook.

the class ReflectiveAlterKeyLoader method load.

@Override
public ImmutableCollection<AlterRuleKey> load(Class<? extends BuildRule> key) throws Exception {
    ImmutableList.Builder<AlterRuleKey> builder = ImmutableList.builder();
    for (Class<?> current = key; !Object.class.equals(current); current = current.getSuperclass()) {
        ImmutableSortedMap.Builder<ValueExtractor, AlterRuleKey> sortedExtractors = ImmutableSortedMap.orderedBy(COMPARATOR);
        for (final Field field : current.getDeclaredFields()) {
            field.setAccessible(true);
            final AddToRuleKey annotation = field.getAnnotation(AddToRuleKey.class);
            if (annotation != null) {
                ValueExtractor valueExtractor = new FieldValueExtractor(field);
                sortedExtractors.put(valueExtractor, createAlterRuleKey(valueExtractor, annotation));
            }
        }
        builder.addAll(sortedExtractors.build().values());
    }
    return builder.build();
}
Also used : Field(java.lang.reflect.Field) ImmutableList(com.google.common.collect.ImmutableList) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey)

Example 2 with AddToRuleKey

use of com.facebook.buck.rules.AddToRuleKey in project buck by facebook.

the class InputBasedRuleKeyFactoryTest method ruleKeyChangesIfInputContentsFromPathSourcePathInRuleKeyAppendableChanges.

@Test
public void ruleKeyChangesIfInputContentsFromPathSourcePathInRuleKeyAppendableChanges() {
    BuildRuleResolver resolver = new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer());
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    final FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    final Path output = Paths.get("output");
    BuildRuleParams params = new FakeBuildRuleParamsBuilder("//:rule").setProjectFilesystem(filesystem).build();
    BuildRule rule = new NoopBuildRule(params) {

        @AddToRuleKey
        RuleKeyAppendableWithInput input = new RuleKeyAppendableWithInput(new PathSourcePath(filesystem, output));
    };
    // Build a rule key with a particular hash set for the output for the above rule.
    FakeFileHashCache hashCache = new FakeFileHashCache(ImmutableMap.of(filesystem.resolve(output), HashCode.fromInt(0)));
    RuleKey inputKey1 = new InputBasedRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(rule);
    // Now, build a rule key with a different hash for the output for the above rule.
    hashCache = new FakeFileHashCache(ImmutableMap.of(filesystem.resolve(output), HashCode.fromInt(1)));
    RuleKey inputKey2 = new InputBasedRuleKeyFactory(0, hashCache, pathResolver, ruleFinder).build(rule);
    assertThat(inputKey1, Matchers.not(Matchers.equalTo(inputKey2)));
}
Also used : SourcePath(com.facebook.buck.rules.SourcePath) Path(java.nio.file.Path) PathSourcePath(com.facebook.buck.rules.PathSourcePath) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) PathSourcePath(com.facebook.buck.rules.PathSourcePath) FakeBuildRuleParamsBuilder(com.facebook.buck.rules.FakeBuildRuleParamsBuilder) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildRuleParams(com.facebook.buck.rules.BuildRuleParams) FakeBuildRule(com.facebook.buck.rules.FakeBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 3 with AddToRuleKey

use of com.facebook.buck.rules.AddToRuleKey in project buck by facebook.

the class DefaultRuleKeyFactoryTest method fieldsFromParentClassesShouldBeAddedAndFieldsRetainOverallAlphabeticalOrdering.

@Test
public void fieldsFromParentClassesShouldBeAddedAndFieldsRetainOverallAlphabeticalOrdering() {
    BuildTarget topLevelTarget = BuildTargetFactory.newInstance("//cheese:peas");
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    BuildRule rule = new EmptyRule(topLevelTarget);
    DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, new NullFileHashCache(), pathResolver, ruleFinder);
    RuleKeyBuilder<RuleKeyResult<RuleKey>> builder = factory.newBuilderForTesting(rule);
    builder.setReflectively("exoticCheese", "bavarian smoked");
    builder.setReflectively("target", topLevelTarget);
    RuleKey expected = builder.build().result;
    class Parent extends EmptyRule {

        @AddToRuleKey
        private BuildTarget target;

        public Parent(BuildTarget target) {
            super(target);
            this.target = target;
        }
    }
    class Child extends Parent {

        @AddToRuleKey
        private String exoticCheese = "bavarian smoked";

        public Child(BuildTarget target) {
            super(target);
        }
    }
    RuleKey seen = factory.build(new Child(topLevelTarget));
    assertEquals(expected, seen);
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTarget(com.facebook.buck.model.BuildTarget) NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 4 with AddToRuleKey

use of com.facebook.buck.rules.AddToRuleKey in project buck by facebook.

the class DefaultRuleKeyFactoryTest method stringifiedRuleKeyAppendablesGetAddedToRuleKeyAsStrings.

@Test
public void stringifiedRuleKeyAppendablesGetAddedToRuleKeyAsStrings() {
    BuildTarget target = BuildTargetFactory.newInstance("//cheese:peas");
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    BuildRule rule = new EmptyRule(target);
    DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, new NullFileHashCache(), pathResolver, ruleFinder);
    RuleKeyBuilder<RuleKeyResult<RuleKey>> builder = factory.newBuilderForTesting(rule);
    builder.setReflectively("field", "cheddar");
    RuleKey expected = builder.build().result;
    class AppendingField extends EmptyRule {

        @AddToRuleKey(stringify = true)
        private Appender field = new Appender();

        public AppendingField(BuildTarget target) {
            super(target);
        }
    }
    RuleKey seen = factory.build(new AppendingField(target));
    assertEquals(expected, seen);
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTarget(com.facebook.buck.model.BuildTarget) NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Example 5 with AddToRuleKey

use of com.facebook.buck.rules.AddToRuleKey in project buck by facebook.

the class DefaultRuleKeyFactoryTest method shouldAddASingleAnnotatedFieldToRuleKey.

@Test
public void shouldAddASingleAnnotatedFieldToRuleKey() {
    BuildTarget target = BuildTargetFactory.newInstance("//cheese:peas");
    SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer()));
    SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
    BuildRule rule = new EmptyRule(target);
    DefaultRuleKeyFactory factory = new DefaultRuleKeyFactory(0, new NullFileHashCache(), pathResolver, ruleFinder);
    RuleKeyBuilder<RuleKeyResult<RuleKey>> builder = factory.newBuilderForTesting(rule);
    builder.setReflectively("field", "cake-walk");
    RuleKey expected = builder.build().result;
    class DecoratedFields extends EmptyRule {

        @AddToRuleKey
        private String field = "cake-walk";

        public DecoratedFields(BuildTarget target) {
            super(target);
        }
    }
    RuleKey seen = factory.build(new DecoratedFields(target));
    assertEquals(expected, seen);
}
Also used : RuleKey(com.facebook.buck.rules.RuleKey) AddToRuleKey(com.facebook.buck.rules.AddToRuleKey) SourcePathRuleFinder(com.facebook.buck.rules.SourcePathRuleFinder) SourcePathResolver(com.facebook.buck.rules.SourcePathResolver) BuildRuleResolver(com.facebook.buck.rules.BuildRuleResolver) BuildTarget(com.facebook.buck.model.BuildTarget) NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) NoopBuildRule(com.facebook.buck.rules.NoopBuildRule) BuildRule(com.facebook.buck.rules.BuildRule) DefaultTargetNodeToBuildRuleTransformer(com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer) Test(org.junit.Test)

Aggregations

AddToRuleKey (com.facebook.buck.rules.AddToRuleKey)13 BuildRule (com.facebook.buck.rules.BuildRule)12 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)12 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)12 NoopBuildRule (com.facebook.buck.rules.NoopBuildRule)12 RuleKey (com.facebook.buck.rules.RuleKey)12 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)12 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)12 Test (org.junit.Test)12 BuildTarget (com.facebook.buck.model.BuildTarget)10 NullFileHashCache (com.facebook.buck.util.cache.NullFileHashCache)10 FakeBuildRuleParamsBuilder (com.facebook.buck.rules.FakeBuildRuleParamsBuilder)4 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)4 DefaultFileHashCache (com.facebook.buck.util.cache.DefaultFileHashCache)4 FileHashCache (com.facebook.buck.util.cache.FileHashCache)4 StackedFileHashCache (com.facebook.buck.util.cache.StackedFileHashCache)4 PathSourcePath (com.facebook.buck.rules.PathSourcePath)3 RuleKeyObjectSink (com.facebook.buck.rules.RuleKeyObjectSink)3 SourcePath (com.facebook.buck.rules.SourcePath)3 ImmutableList (com.google.common.collect.ImmutableList)3