Skip to content

Commit

Permalink
Migrate BindingGraphPlugin to the dagger.spi package, and add a Valid…
Browse files Browse the repository at this point in the history
…ationItem API to dagger.spi so that plugins can report high-level diagnostics to the Messager.

RELNOTES=Adds a `dagger.spi` package (in the `com.google.dagger:dagger-spi` artifact) so users can hook into the Dagger binding graph at build time to report custom validation or generate extra files at build time. Note that this is still experimental and the APIs may change.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=184681743
  • Loading branch information
ronshapiro committed Feb 7, 2018
1 parent 1925e8e commit 120a339
Show file tree
Hide file tree
Showing 22 changed files with 1,267 additions and 120 deletions.
46 changes: 38 additions & 8 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package_group(
packages = ["//..."],
)

load("//tools:javadoc.bzl", "javadoc_library")

py_test(
name = "maven_sha1_test",
srcs = ["maven_sha1_test.py"],
Expand Down Expand Up @@ -70,25 +72,53 @@ jarjar_library(
],
)

jarjar_library(
name = "shaded_compiler_src",
rules_file = "merge_all_rules.txt",
deps = [
"//java/dagger/internal/codegen:libbase-src.jar",
"//java/dagger/internal/codegen:libbinding-src.jar",
"//java/dagger/internal/codegen:libprocessor-src.jar",
"//java/dagger/internal/codegen:libshared-with-spi-src.jar",
"//java/dagger/internal/codegen:libvalidation-src.jar",
"//java/dagger/internal/codegen:libwriting-src.jar",
],
)

jarjar_library(
name = "shaded_spi",
rules_file = "shade_rules.txt",
deps = [
"//java/dagger/internal/codegen:shared-with-spi",
"//java/dagger/model",
"//java/dagger/spi",
"@com_google_auto_auto_common//jar",
],
)

jarjar_library(
name = "shaded_compiler_src",
name = "shaded_spi_src",
rules_file = "merge_all_rules.txt",
deps = [
"//java/dagger/internal/codegen:libbase-src.jar",
"//java/dagger/internal/codegen:libbinding-src.jar",
"//java/dagger/internal/codegen:libprocessor-src.jar",
"//java/dagger/internal/codegen:libvalidation-src.jar",
"//java/dagger/internal/codegen:libwriting-src.jar",
"//java/dagger/internal/codegen:libshared-with-spi-src.jar",
"//java/dagger/model:libmodel-src.jar",
"//java/dagger/spi:libspi-src.jar",
],
)

javadoc_library(
name = "spi-javadoc",
srcs = [
"//java/dagger/model:model-srcs",
"//java/dagger/spi:spi-srcs",
],
root_packages = [
"dagger.model",
"dagger.spi",
],
deps = [
"//java/dagger/model",
"//java/dagger/spi",
],
)

Expand All @@ -110,8 +140,6 @@ jarjar_library(
],
)

load("//tools:javadoc.bzl", "javadoc_library")

# coalesced javadocs used for the gh-pages site
javadoc_library(
name = "user-docs",
Expand All @@ -123,6 +151,7 @@ javadoc_library(
"//java/dagger/grpc/server/processor:javadoc-srcs",
"//java/dagger/model:model-srcs",
"//java/dagger/producers:producers-srcs",
"//java/dagger/spi:spi-srcs",
],
android_api_level = 26,
# TODO(ronshapiro): figure out how to specify the version number for release builds
Expand All @@ -141,5 +170,6 @@ javadoc_library(
"//java/dagger/grpc/server/processor",
"//java/dagger/model",
"//java/dagger/producers",
"//java/dagger/spi",
],
)
31 changes: 31 additions & 0 deletions java/dagger/example/spi/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright (C) 2018 The Dagger Authors.
#
# 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.

# Description:
# An example of the dagger.spi.BindingGraphPlugin usage

package(default_visibility = ["//:src"])

java_plugin(
name = "binding-graph-visualizer",
srcs = glob(["*.java"]),
deps = [
"//java/dagger/model",
"//java/dagger/spi",
"//third_party:auto_service",
"//third_party:error_prone_annotations",
"//third_party:guava",
"//third_party:javapoet",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package dagger.internal.codegen;
package dagger.example.spi;

import static java.util.UUID.randomUUID;
import static java.util.regex.Matcher.quoteReplacement;
Expand All @@ -37,6 +37,8 @@
import dagger.model.BindingGraph.SubcomponentBuilderBindingEdge;
import dagger.model.BindingKind;
import dagger.model.ComponentPath;
import dagger.spi.BindingGraphPlugin;
import dagger.spi.ValidationItem;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
Expand All @@ -48,6 +50,7 @@
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.annotation.processing.Filer;
import javax.lang.model.element.TypeElement;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
Expand All @@ -63,7 +66,13 @@
* Foo_Bar.dot}.
*/
@AutoService(BindingGraphPlugin.class)
public final class BindingNetworkVisualizer extends BindingGraphPlugin {
public final class BindingGraphVisualizer implements BindingGraphPlugin {
private Filer filer;

@Override
public void initFiler(Filer filer) {
this.filer = filer;
}

/** Graphviz color names to use for binding nodes within each component. */
private static final ImmutableList<String> COMPONENT_COLORS =
Expand All @@ -82,14 +91,14 @@ public final class BindingNetworkVisualizer extends BindingGraphPlugin {
"/set312/12");

@Override
public void visitGraph(BindingGraph bindingGraph) {
public List<ValidationItem> visitGraph(BindingGraph bindingGraph) {
TypeElement componentElement =
bindingGraph.rootComponentNode().componentPath().currentComponent();
DotGraph graph = new NodesGraph(bindingGraph).graph();
ClassName componentName = ClassName.get(componentElement);
try {
FileObject file =
filer()
filer
.createResource(
StandardLocation.CLASS_OUTPUT,
componentName.packageName(),
Expand All @@ -101,6 +110,7 @@ public void visitGraph(BindingGraph bindingGraph) {
} catch (IOException e) {
throw new RuntimeException(e);
}
return ImmutableList.of();
}

private abstract static class Indented {
Expand Down Expand Up @@ -212,7 +222,7 @@ static class NodesGraph {

DotGraph graph() {
if (nodeIds.isEmpty()) {
Iterator<String> colors = Iterators.cycle(BindingNetworkVisualizer.COMPONENT_COLORS);
Iterator<String> colors = Iterators.cycle(COMPONENT_COLORS);
bindingGraph
.nodes()
.stream()
Expand Down
17 changes: 2 additions & 15 deletions java/dagger/internal/codegen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ CODEGEN_SHARED_DEPS = [
"//java/dagger:core",
"//java/dagger/producers",
"//java/dagger/model",
"//java/dagger/spi",
"//java/dagger/model:internal-proxies",
]

Expand Down Expand Up @@ -123,7 +124,6 @@ java_library(
"BindingDeclaration.java",
"BindingFactory.java",
"BindingGraph.java",
"BindingGraphPlugin.java", # TODO(ronshapiro): this should move to its own model target
"BindingType.java",
"BindingTypeMapper.java",
"BindingVariableNamer.java", # needed by FrameworkField
Expand Down Expand Up @@ -283,6 +283,7 @@ java_library(
"ModuleProcessingStep.java",
"ProcessingOptions.java",
"ProductionExecutorModuleProcessingStep.java",
"SpiDiagnosticReporter.java",
],
plugins = CODEGEN_PLUGINS,
deps = CODEGEN_DEPS + [
Expand Down Expand Up @@ -346,20 +347,6 @@ java_library(
deps = CODEGEN_DEPS,
)

java_plugin(
name = "dagger-experimental-visualizer",
srcs = EXPERIMENTAL_VISUALIZER_SRCS,
deps = [
":base",
":binding",
"//java/dagger/model",
"//third_party:auto_service",
"//third_party:error_prone_annotations",
"//third_party:guava",
"//third_party:javapoet",
],
)

java_plugin(
name = "component-codegen",
generates_api = 1,
Expand Down
68 changes: 0 additions & 68 deletions java/dagger/internal/codegen/BindingGraphPlugin.java

This file was deleted.

7 changes: 4 additions & 3 deletions java/dagger/internal/codegen/BindingGraphPluginsModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.Maps;
import dagger.Module;
import dagger.Provides;
import dagger.spi.BindingGraphPlugin;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
Expand All @@ -40,10 +41,10 @@ static ImmutableList<BindingGraphPlugin> bindingGraphPlugins(
ImmutableList<BindingGraphPlugin> bindingGraphPlugins =
ImmutableList.copyOf(ServiceLoader.load(BindingGraphPlugin.class, classLoader));
for (BindingGraphPlugin plugin : bindingGraphPlugins) {
plugin.setFiler(filer);
Set<String> supportedOptions = plugin.getSupportedOptions();
plugin.initFiler(filer);
Set<String> supportedOptions = plugin.supportedOptions();
if (!supportedOptions.isEmpty()) {
plugin.setOptions(Maps.filterKeys(processingOptions, supportedOptions::contains));
plugin.initOptions(Maps.filterKeys(processingOptions, supportedOptions::contains));
}
}
return bindingGraphPlugins;
Expand Down
Loading

0 comments on commit 120a339

Please sign in to comment.