Skip to content

Commit

Permalink
interfaces for classes in core.rules.graphbuilder
Browse files Browse the repository at this point in the history
Summary: Extracted interfaces for BuildRuleKey and BuildRuleContextWithEnvironment. Moved the abstract class implementation into an impl sub package.

Reviewed By: bobyangyf

fbshipit-source-id: 63e8a50
  • Loading branch information
timmych authored and facebook-github-bot committed Jun 5, 2018
1 parent 1d51479 commit 792e3b7
Show file tree
Hide file tree
Showing 10 changed files with 214 additions and 86 deletions.
3 changes: 0 additions & 3 deletions src/com/facebook/buck/core/rules/graphbuilder/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ java_immutables_library(
"//src/com/facebook/buck/core/rules:rules",
"//src/com/facebook/buck/core/rules/provider:provider",
],
tests = [
"//test/com/facebook/buck/core/rules/graphbuilder:graphbuilder",
],
visibility = ["PUBLIC"],
deps = [
"//src/com/facebook/buck/core/graph/transformation:transformation",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,8 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Maps;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import org.immutables.value.Value;
import org.immutables.value.Value.Style.ImplementationVisibility;

/**
* Context information used for construction of ActionGraph in {@link
Expand All @@ -56,36 +53,13 @@
* TransformationEnvironment)} implementation for ActionGraph construction. Hence, we have
* package-private implementation which hides constructor.
*/
@Value.Immutable(builder = false, copy = false)
@Value.Style(visibility = ImplementationVisibility.PACKAGE)
public abstract class BuildRuleContextWithEnvironment {
public interface BuildRuleContextWithEnvironment {

@Value.Parameter
protected abstract BuildRuleKey getKey();
ProjectFilesystem getProjectFilesystem();

protected BuildRuleCreationContext getCreationContext() {
return getKey().getBuildRuleCreationContext();
}
CellPathResolver getCellPathResolver();

/** @return the {@link TargetNode} of the current desired {@link BuildRule} */
protected TargetNode<?, ?> getCurrentNode() {
return getKey().getTargetNode();
}

@Value.Parameter
protected abstract TransformationEnvironment<BuildRuleKey, BuildRule> getEnv();

public ProjectFilesystem getProjectFilesystem() {
return getCreationContext().getProjectFilesystem();
}

public CellPathResolver getCellPathResolver() {
return getCreationContext().getCellPathResolver();
}

public ToolchainProvider getToolchainProvider() {
return getCreationContext().getToolchainProvider();
}
ToolchainProvider getToolchainProvider();

/**
* Access to {@link TargetGraph} and {@link TargetNode} is limited during ActionGraph
Expand All @@ -94,19 +68,13 @@ public ToolchainProvider getToolchainProvider() {
*/

/** @return The {@link TargetNode#getDeclaredDeps()} */
public ImmutableSet<BuildTarget> getDeclaredDeps() {
return getCurrentNode().getDeclaredDeps();
}
ImmutableSet<BuildTarget> getDeclaredDeps();

/** @return The {@link TargetNode#getExtraDeps()} */
public ImmutableSortedSet<BuildTarget> getExtraDeps() {
return getCurrentNode().getExtraDeps();
}
ImmutableSortedSet<BuildTarget> getExtraDeps();

/** @return The {@link TargetNode#getTargetGraphOnlyDeps()} ()} */
public ImmutableSortedSet<BuildTarget> getTargetGraphOnlyDeps() {
return getCurrentNode().getTargetGraphOnlyDeps();
}
ImmutableSortedSet<BuildTarget> getTargetGraphOnlyDeps();

/**
* Access to {@link TransformationEnvironment} and dependencies as {@link BuildRule}s is limited.
Expand All @@ -126,14 +94,9 @@ public ImmutableSortedSet<BuildTarget> getTargetGraphOnlyDeps() {
* {@link BuildRule}
* @return a future of the {@link BuildRule} to be created
*/
public CompletionStage<BuildRule> getProviderCollectionForDep(
CompletionStage<BuildRule> getProviderCollectionForDep(
BuildRuleKey depKey,
Function<BuildRuleInfoProviderCollection, BuildRule> createBuildRuleWithDep) {
return getEnv()
.evaluate(
depKey,
depBuildRule -> createBuildRuleWithDep.apply(depBuildRule.getProviderCollection()));
}
Function<BuildRuleInfoProviderCollection, BuildRule> createBuildRuleWithDep);

/**
* A method for Action Graph construction phase to access information from many dependencies by
Expand All @@ -148,17 +111,8 @@ public CompletionStage<BuildRule> getProviderCollectionForDep(
* {@link BuildRule}
* @return a future of the {@link BuildRule} to be created
*/
public CompletionStage<BuildRule> getProviderCollectionForDeps(
CompletionStage<BuildRule> getProviderCollectionForDeps(
Iterable<BuildRuleKey> depKeys,
Function<ImmutableMap<BuildRuleKey, BuildRuleInfoProviderCollection>, BuildRule>
createBuildRuleWithDeps) {
return getEnv()
.evaluateAll(
depKeys,
depBuildRules ->
createBuildRuleWithDeps.apply(
ImmutableMap.copyOf(
Maps.transformValues(
depBuildRules, rule -> rule.getProviderCollection()))));
}
createBuildRuleWithDeps);
}
22 changes: 4 additions & 18 deletions src/com/facebook/buck/core/rules/graphbuilder/BuildRuleKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.facebook.buck.core.model.targetgraph.BuildRuleCreationContextWithTargetGraph;
import com.facebook.buck.core.model.targetgraph.TargetNode;
import com.facebook.buck.core.rules.BuildRule;
import org.immutables.value.Value;

/**
* An Immutable Key to a {@link BuildRule} for computation in {@link
Expand All @@ -34,24 +33,11 @@
* about the desired {@link BuildTarget}, including flavour information, and cell path, etc.
* </ul>
*/
@Value.Immutable(builder = false, copy = false, prehash = true)
public abstract class BuildRuleKey {
public interface BuildRuleKey {

@Value.Parameter
@Value.Auxiliary
public abstract BuildTarget getBuildTarget();
BuildTarget getBuildTarget();

@Value.Derived
protected TargetNodeWrapper getTargetNodeWrapper() {
return TargetNodeWrapper.of(
getBuildRuleCreationContext().getTargetGraph().get(getBuildTarget()));
}
TargetNode<?, ?> getTargetNode();

public TargetNode<?, ?> getTargetNode() {
return getTargetNodeWrapper().getTargetNode();
}

@Value.Parameter
@Value.Auxiliary
public abstract BuildRuleCreationContextWithTargetGraph getBuildRuleCreationContext();
BuildRuleCreationContextWithTargetGraph getBuildRuleCreationContext();
}
22 changes: 22 additions & 0 deletions src/com/facebook/buck/core/rules/graphbuilder/impl/BUCK
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("//tools/build_rules:java_rules.bzl", "java_immutables_library")

java_immutables_library(
name = "impl",
srcs = glob(
["*.java"],
),
exported_deps = [
"//src/com/facebook/buck/core/rules/graphbuilder:graphbuilder",
],
tests = [
"//test/com/facebook/buck/core/rules/graphbuilder/impl:impl",
],
visibility = [
"PUBLIC",
],
deps = [
"//src/com/facebook/buck/core/graph/transformation:transformation",
"//src/com/facebook/buck/core/rules:rules",
"//third-party/java/guava:guava",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright 2018-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.facebook.buck.core.rules.graphbuilder.impl;

import com.facebook.buck.core.cell.resolver.CellPathResolver;
import com.facebook.buck.core.graph.transformation.TransformationEnvironment;
import com.facebook.buck.core.model.BuildTarget;
import com.facebook.buck.core.model.targetgraph.TargetNode;
import com.facebook.buck.core.rules.BuildRule;
import com.facebook.buck.core.rules.BuildRuleCreationContext;
import com.facebook.buck.core.rules.graphbuilder.BuildRuleContextWithEnvironment;
import com.facebook.buck.core.rules.graphbuilder.BuildRuleKey;
import com.facebook.buck.core.rules.provider.BuildRuleInfoProviderCollection;
import com.facebook.buck.io.filesystem.ProjectFilesystem;
import com.facebook.buck.toolchain.ToolchainProvider;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Maps;
import java.util.concurrent.CompletionStage;
import java.util.function.Function;
import org.immutables.value.Value;
import org.immutables.value.Value.Style.ImplementationVisibility;

/** Default abstract implementation of a {@link BuildRuleContextWithEnvironment}. */
@Value.Immutable(builder = false, copy = false)
@Value.Style(visibility = ImplementationVisibility.PACKAGE)
public abstract class DefaultBuildRuleContextWithEnvironment
implements BuildRuleContextWithEnvironment {

@Value.Parameter
protected abstract BuildRuleKey getKey();

protected BuildRuleCreationContext getCreationContext() {
return getKey().getBuildRuleCreationContext();
}

/** @return the {@link TargetNode} of the current desired {@link BuildRule} */
protected TargetNode<?, ?> getCurrentNode() {
return getKey().getTargetNode();
}

@Value.Parameter
protected abstract TransformationEnvironment<BuildRuleKey, BuildRule> getEnv();

@Override
public ProjectFilesystem getProjectFilesystem() {
return getCreationContext().getProjectFilesystem();
}

@Override
public CellPathResolver getCellPathResolver() {
return getCreationContext().getCellPathResolver();
}

@Override
public ToolchainProvider getToolchainProvider() {
return getCreationContext().getToolchainProvider();
}

@Override
public ImmutableSet<BuildTarget> getDeclaredDeps() {
return getCurrentNode().getDeclaredDeps();
}

@Override
public ImmutableSortedSet<BuildTarget> getExtraDeps() {
return getCurrentNode().getExtraDeps();
}

@Override
public ImmutableSortedSet<BuildTarget> getTargetGraphOnlyDeps() {
return getCurrentNode().getTargetGraphOnlyDeps();
}

@Override
public CompletionStage<BuildRule> getProviderCollectionForDep(
BuildRuleKey depKey,
Function<BuildRuleInfoProviderCollection, BuildRule> createBuildRuleWithDep) {
return getEnv()
.evaluate(
depKey,
depBuildRule -> createBuildRuleWithDep.apply(depBuildRule.getProviderCollection()));
}

@Override
public CompletionStage<BuildRule> getProviderCollectionForDeps(
Iterable<BuildRuleKey> depKeys,
Function<ImmutableMap<BuildRuleKey, BuildRuleInfoProviderCollection>, BuildRule>
createBuildRuleWithDeps) {
return getEnv()
.evaluateAll(
depKeys,
depBuildRules ->
createBuildRuleWithDeps.apply(
ImmutableMap.copyOf(
Maps.transformValues(
depBuildRules, rule -> rule.getProviderCollection()))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2018-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.facebook.buck.core.rules.graphbuilder.impl;

import com.facebook.buck.core.model.BuildTarget;
import com.facebook.buck.core.model.targetgraph.BuildRuleCreationContextWithTargetGraph;
import com.facebook.buck.core.model.targetgraph.TargetNode;
import com.facebook.buck.core.rules.graphbuilder.BuildRuleKey;
import org.immutables.value.Value;

/** Default abstract implementation of a {@link BuildRuleKey}. */
@Value.Immutable(builder = false, copy = false, prehash = true)
public abstract class DefaultBuildRuleKey implements BuildRuleKey {

@Value.Parameter
@Value.Auxiliary
@Override
public abstract BuildTarget getBuildTarget();

@Value.Derived
protected TargetNodeWrapper getTargetNodeWrapper() {
return TargetNodeWrapper.of(
getBuildRuleCreationContext().getTargetGraph().get(getBuildTarget()));
}

@Override
public TargetNode<?, ?> getTargetNode() {
return getTargetNodeWrapper().getTargetNode();
}

@Value.Parameter
@Value.Auxiliary
@Override
public abstract BuildRuleCreationContextWithTargetGraph getBuildRuleCreationContext();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
* under the License.
*/

package com.facebook.buck.core.rules.graphbuilder;

package com.facebook.buck.core.rules.graphbuilder.impl;

import com.facebook.buck.core.model.targetgraph.TargetNode;
import com.facebook.buck.core.rules.graphbuilder.BuildRuleKey;
import javax.annotation.concurrent.Immutable;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
load("//tools/build_rules:java_rules.bzl", "java_immutables_library", "java_test")

java_test(
name = "graphbuilder",
name = "impl",
srcs = glob(["*Test.java"]),
has_immutable_types = True,
deps = [
"//src/com/facebook/buck/core/graph/transformation:transformation",
"//src/com/facebook/buck/core/rules/graphbuilder:graphbuilder",
"//src/com/facebook/buck/core/rules/graphbuilder/impl:impl",
"//test/com/facebook/buck/core/cell:testutil",
"//test/com/facebook/buck/core/graph/transformation:testutil",
"//test/com/facebook/buck/core/model/targetgraph:testutil",
Expand Down
Loading

0 comments on commit 792e3b7

Please sign in to comment.