diff options
author | bcorso <bcorso@google.com> | 2020-06-17 16:29:02 -0700 |
---|---|---|
committer | Nick <565601+nick-someone@users.noreply.github.com> | 2020-06-18 11:56:21 -0400 |
commit | bc6f9a9f8b30389e3283f4509001999aa1510e3a (patch) | |
tree | c2b5f1e35883bc0e86b3f8c16b797a2ec7fff2e4 | |
parent | 3a31bf65b4c8d6ae55e7bcb2914a34d367071277 (diff) | |
download | dagger2-bc6f9a9f8b30389e3283f4509001999aa1510e3a.tar.gz |
Split Dagger, Dagger Android, and Hilt Android setup for Bazel users
This allows Dagger users to configure targets without requiring android_sdk repository.
RELNOTES=N/A
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=316989001
-rw-r--r-- | README.md | 103 | ||||
-rw-r--r-- | examples/bazel/BUILD | 4 | ||||
-rw-r--r-- | examples/bazel/WORKSPACE | 12 | ||||
-rw-r--r-- | workspace_defs.bzl | 91 |
4 files changed, 177 insertions, 33 deletions
@@ -27,12 +27,8 @@ mailing list. ### Bazel -If you build with `bazel`, you will need to configure your top-level workspace -to access Dagger/Hilt targets that export the proper dependencies and plugins. - First, import the Dagger repository into your `WORKSPACE` file using -[`http_archive`][bazel-external-deps], load the `DAGGER_ARTIFACTS` and -`DAGGER_REPOSITORIES`, and add them to your list of [`maven_install`] artifacts. +[`http_archive`][bazel-external-deps]. Note: The `http_archive` must point to a tagged release of Dagger, not just any commit. The version of the Dagger artifacts will match the version of the tagged @@ -45,6 +41,18 @@ http_archive( name = "dagger", urls = ["https://github.com/google/dagger/archive/dagger-<version>.zip"], ) +``` + +Next you will need to setup targets that export the proper dependencies +and plugins. Follow the sections below to setup the dependencies you need. + +#### Dagger Setup + +First, load the Dagger artifacts and repositories, and add them to your list of +[`maven_install`] artifacts. + +```python +# Top-level WORKSPACE file load("@dagger//:workspace_defs.bzl", "DAGGER_ARTIFACTS", "DAGGER_REPOSITORIES") @@ -54,7 +62,8 @@ maven_install( ) ``` -Next, load and call `dagger_rules` in your top-level `BUILD` file: +Next, load and call [`dagger_rules`](https://github.com/google/dagger/blob/master/workspace_defs.bzl) +in your top-level `BUILD` file: ```python # Top-level BUILD file @@ -64,7 +73,7 @@ load("@dagger//:workspace_defs.bzl", "dagger_rules") dagger_rules() ``` -This will setup Dagger and Hilt build targets of the form `:<artifact_id>`. +This will add the following Dagger build targets: (Note that these targets already export all of the dependencies and processors they need). @@ -73,8 +82,88 @@ deps = [ ":dagger", # For Dagger ":dagger-spi", # For Dagger SPI ":dagger-producers", # For Dagger Producers +] +``` + +#### Dagger Android Setup + +First, load the Dagger Android artifacts and repositories, and add them to your +list of [`maven_install`] artifacts. + +```python +# Top-level WORKSPACE file + +load( + "@dagger//:workspace_defs.bzl", + "DAGGER_ANDROID_ARTIFACTS", + "DAGGER_ANDROID_REPOSITORIES" +) + +maven_install( + artifacts = DAGGER_ANDROID_ARTIFACTS + [...], + repositories = DAGGER_ANDROID_REPOSITORIES + [...], +) +``` + +Next, load and call [`dagger_android_rules`](https://github.com/google/dagger/blob/master/workspace_defs.bzl) +in your top-level `BUILD` file: + +```python +# Top-level BUILD file + +load("@dagger//:workspace_defs.bzl", "dagger_android_rules") + +dagger_android_rules() +``` + +This will add the following Dagger Android build targets: +(Note that these targets already export all of the dependencies and processors +they need). + +```python +deps = [ ":dagger-android", # For Dagger Android ":dagger-android-support", # For Dagger Android (Support) +] +``` + +#### Hilt Android Setup + +First, load the Hilt Android artifacts and repositories, and add them to your +list of [`maven_install`] artifacts. + +```python +# Top-level WORKSPACE file + +load( + "@dagger//:workspace_defs.bzl", + "HILT_ANDROID_ARTIFACTS", + "HILT_ANDROID_REPOSITORIES" +) + +maven_install( + artifacts = HILT_ANDROID_ARTIFACTS + [...], + repositories = HILT_ANDROID_REPOSITORIES + [...], +) +``` + +Next, load and call [`hilt_android_rules`](https://github.com/google/dagger/blob/master/workspace_defs.bzl) +in your top-level `BUILD` file: + +```python +# Top-level BUILD file + +load("@dagger//:workspace_defs.bzl", "hilt_android_rules") + +hilt_android_rules() +``` + +This will add the following Hilt Android build targets: +(Note that these targets already export all of the dependencies and processors +they need). + +```python +deps = [ ":hilt-android", # For Hilt Android ":hilt-android-testing", # For Hilt Android Testing ] diff --git a/examples/bazel/BUILD b/examples/bazel/BUILD index b3342319f..0f9a621b0 100644 --- a/examples/bazel/BUILD +++ b/examples/bazel/BUILD @@ -15,6 +15,8 @@ # Description: # Dagger Bazel examples -load("@dagger//:workspace_defs.bzl", "dagger_rules") +load("@dagger//:workspace_defs.bzl", "dagger_rules", "hilt_android_rules") dagger_rules() + +hilt_android_rules() diff --git a/examples/bazel/WORKSPACE b/examples/bazel/WORKSPACE index 009f99e10..b5900f4f1 100644 --- a/examples/bazel/WORKSPACE +++ b/examples/bazel/WORKSPACE @@ -25,7 +25,13 @@ local_repository( path = "../../", ) -load("@dagger//:workspace_defs.bzl", "DAGGER_ARTIFACTS", "DAGGER_REPOSITORIES") +load( + "@dagger//:workspace_defs.bzl", + "DAGGER_ARTIFACTS", + "DAGGER_REPOSITORIES", + "HILT_ANDROID_ARTIFACTS", + "HILT_ANDROID_REPOSITORIES", +) ######################### # Load Android repository @@ -71,7 +77,7 @@ http_archive( load("@rules_jvm_external//:defs.bzl", "maven_install") maven_install( - artifacts = DAGGER_ARTIFACTS + [ + artifacts = DAGGER_ARTIFACTS + HILT_ANDROID_ARTIFACTS + [ "androidx.test.ext:junit:1.1.1", "androidx.test:runner:1.1.1", "com.google.truth:truth:1.0.1", @@ -79,5 +85,5 @@ maven_install( "org.robolectric:robolectric:4.1", "org.robolectric:annotations:4.1", ], - repositories = DAGGER_REPOSITORIES, + repositories = DAGGER_REPOSITORIES + HILT_ANDROID_REPOSITORIES, ) diff --git a/workspace_defs.bzl b/workspace_defs.bzl index 7f6ad3280..4a585df69 100644 --- a/workspace_defs.bzl +++ b/workspace_defs.bzl @@ -20,16 +20,24 @@ _DAGGER_VERSION = POM_VERSION _HILT_VERSION = POM_VERSION_ALPHA DAGGER_ARTIFACTS = [ - "androidx.test:core:1.1.0", # Export for ApplicationProvider - "javax.annotation:jsr250-api:1.0", # Export for @Generated - "androidx.annotation:annotation:1.1.0", # Export for @CallSuper/@Nullable "com.google.dagger:dagger:" + _DAGGER_VERSION, "com.google.dagger:dagger-compiler:" + _DAGGER_VERSION, + "com.google.dagger:dagger-producers:" + _DAGGER_VERSION, + "com.google.dagger:dagger-spi:" + _DAGGER_VERSION, +] + +DAGGER_ANDROID_ARTIFACTS = [ "com.google.dagger:dagger-android-processor:" + _DAGGER_VERSION, "com.google.dagger:dagger-android-support:" + _DAGGER_VERSION, "com.google.dagger:dagger-android:" + _DAGGER_VERSION, - "com.google.dagger:dagger-producers:" + _DAGGER_VERSION, - "com.google.dagger:dagger-spi:" + _DAGGER_VERSION, +] + +HILT_ANDROID_ARTIFACTS = [ + "androidx.test:core:1.1.0", # Export for ApplicationProvider + "javax.annotation:jsr250-api:1.0", # Export for @Generated + "androidx.annotation:annotation:1.1.0", # Export for @CallSuper/@Nullable + "com.google.dagger:dagger:" + _DAGGER_VERSION, + "com.google.dagger:dagger-compiler:" + _DAGGER_VERSION, "com.google.dagger:hilt-android:" + _HILT_VERSION, "com.google.dagger:hilt-android-testing:" + _HILT_VERSION, "com.google.dagger:hilt-android-compiler:" + _HILT_VERSION, @@ -40,6 +48,10 @@ DAGGER_REPOSITORIES = [ "https://repo1.maven.org/maven2", ] +DAGGER_ANDROID_REPOSITORIES = DAGGER_REPOSITORIES + +HILT_ANDROID_REPOSITORIES = DAGGER_REPOSITORIES + # https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#unnamed-macro # buildifier: disable=unnamed-macro def dagger_rules(repo_name = "@maven"): @@ -48,7 +60,7 @@ def dagger_rules(repo_name = "@maven"): The targets will be of the form ":<artifact-id>". Args: - repo_name: The name of the dependency repository (default is "@maven"). + repo_name: The name of the dependency repository (default is "@maven"). """ native.java_library( name = "dagger", @@ -69,6 +81,34 @@ def dagger_rules(repo_name = "@maven"): ], ) + native.java_library( + name = "dagger-producers", + visibility = ["//visibility:public"], + exports = [ + ":dagger", + "%s//:com_google_dagger_dagger_producers" % repo_name, + ], + ) + + native.java_library( + name = "dagger-spi", + visibility = ["//visibility:public"], + exports = [ + "%s//:com_google_dagger_dagger_spi" % repo_name, + ], + ) + +# https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#unnamed-macro +# buildifier: disable=unnamed-macro +def dagger_android_rules(repo_name = "@maven"): + """Defines the Dagger Android targets with proper exported dependencies and plugins. + + The targets will be of the form ":<artifact-id>". + + Args: + repo_name: The name of the dependency repository (default is "@maven"). + """ + # https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#native-android # buildifier: disable=native-android native.android_library( @@ -101,28 +141,23 @@ def dagger_rules(repo_name = "@maven"): ], ) - native.java_library( - name = "dagger-producers", - visibility = ["//visibility:public"], - exports = [ - ":dagger", - "%s//:com_google_dagger_dagger_producers" % repo_name, - ], - ) +# https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#unnamed-macro +# buildifier: disable=unnamed-macro +def hilt_android_rules(repo_name = "@maven"): + """Defines the Hilt Android targets with proper exported dependencies and plugins. - native.java_library( - name = "dagger-spi", - visibility = ["//visibility:public"], - exports = [ - "%s//:com_google_dagger_dagger_spi" % repo_name, - ], - ) + The targets will be of the form ":<artifact-id>". + + Args: + repo_name: The name of the dependency repository (default is "@maven"). + """ # https://github.com/bazelbuild/buildtools/blob/master/WARNINGS.md#native-android # buildifier: disable=native-android native.android_library( name = "hilt-android", exported_plugins = [ + ":hilt_dagger_compiler", ":hilt_android_entry_point_processor", ":hilt_aggregated_deps_processor", ":hilt_alias_of_processor", @@ -133,13 +168,25 @@ def dagger_rules(repo_name = "@maven"): ], visibility = ["//visibility:public"], exports = [ - ":dagger", + "%s//:com_google_dagger_dagger" % repo_name, # For Dagger APIs + "%s//:javax_inject_javax_inject" % repo_name, # For @Inject "%s//:androidx_annotation_annotation" % repo_name, # For @CallSuper "%s//:com_google_dagger_hilt_android" % repo_name, "%s//:javax_annotation_jsr250_api" % repo_name, # For @Generated ], ) + # This target is same as dagger-compiler, but we're redefining it here + # so that users don't have to call dagger_rules() first. + native.java_plugin( + name = "hilt_dagger_compiler", + generates_api = 1, + processor_class = "dagger.internal.codegen.ComponentProcessor", + deps = [ + "%s//:com_google_dagger_dagger_compiler" % repo_name, + ], + ) + native.java_plugin( name = "hilt_android_entry_point_processor", generates_api = 1, |