Skip to content

Commit

Permalink
Restingbull/compiler plugin rework (#393)
Browse files Browse the repository at this point in the history
* Add resources to android example
Rework compiler plugins:
*  require ids
* indicate which phase the plugin executes: stub and compile
* include plugin dependencies
* allow compiler plugins to be exported via the exported_compiler_plugins
* allow "bash" substitutions for plugin arguments of working directories (anvil demonstrates)
* Update bazel toolchains to 3.7
  • Loading branch information
restingbull authored Nov 17, 2020
1 parent b7f5832 commit 95b642f
Show file tree
Hide file tree
Showing 117 changed files with 2,122 additions and 472 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
examples/android
examples/node
examples/trivial
examples/anvil
30 changes: 23 additions & 7 deletions docs/kotlin.md

Large diffs are not rendered by default.

19 changes: 10 additions & 9 deletions examples/android/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ maven_install(
"androidx.test.espresso:espresso-core:3.1.1",
"org.hamcrest:hamcrest-library:1.3",
"org.jetbrains.kotlinx:kotlinx-serialization-runtime:1.0-M1-1.4.0-rc",
"com.google.dagger:dagger:2.28",
"com.google.dagger:dagger-compiler:2.28",
"com.google.dagger:dagger-producers:2.28",
],
repositories = [
"https://jcenter.bintray.com/",
Expand Down Expand Up @@ -63,17 +66,15 @@ kotlin_repositories()

kt_register_toolchains()

# Skylib, for build_test, so don't bother initializing the unit test infrastructure.
http_archive(
name = "bazel_skylib",
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
urls = [
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
],
)

http_archive(
name = "rules_pkg",
sha256 = "4ba8f4ab0ff85f2484287ab06c0d871dcb31cc54d439457d28fd4ae14b18450a",
url = "https://github.com/bazelbuild/rules_pkg/releases/download/0.2.4/rules_pkg-0.2.4.tar.gz",
)

http_archive(
name = "build_bazel_rules_android",
sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",
strip_prefix = "rules_android-0.1.1",
urls = ["https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip"],
)
13 changes: 8 additions & 5 deletions examples/android/lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_compiler_plugin", "kt_android_library")
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_android_library", "kt_compiler_plugin")

kt_compiler_plugin(
name = "serialization_plugin",
compile_phase = True,
id = "org.jetbrains.kotlin.serialization",
stubs_phase = True,
deps = [
"@com_github_jetbrains_kotlin//:kotlinx-serialization-compiler-plugin",
],
Expand All @@ -12,12 +15,12 @@ kt_android_library(
srcs = glob(["src/main/kotlin/**/*.kt"]),
custom_package = "examples.android.lib",
manifest = "src/main/AndroidManifest.xml",
plugins = [
":serialization_plugin",
],
visibility = ["//visibility:public"],
deps = [
"@maven//:org_jetbrains_kotlinx_kotlinx_serialization_runtime",
"@maven//:androidx_appcompat_appcompat",
],
plugins = [
":serialization_plugin",
"@maven//:org_jetbrains_kotlinx_kotlinx_serialization_runtime",
],
)
4 changes: 4 additions & 0 deletions examples/android/lib2/res/values-en-rAU/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="little_bat">Where you at?</string>
</resources>
4 changes: 4 additions & 0 deletions examples/android/lib2/res/values-en-rCA/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="little_bat">Where you at?</string>
</resources>
4 changes: 4 additions & 0 deletions examples/android/lib2/res/values-en-rGB/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="little_bat">Where are you?</string>
</resources>
4 changes: 4 additions & 0 deletions examples/android/lib2/res/values-es/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="little_bat">¿En dónde se encuentra?</string>
</resources>
4 changes: 4 additions & 0 deletions examples/android/lib2/res/values-fr-rCA/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="little_bat">Où êtes-vous?</string>
</resources>
4 changes: 4 additions & 0 deletions examples/android/lib2/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="little_bat">どこにいますか?</string>
</resources>
3 changes: 3 additions & 0 deletions examples/android/lib2/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="little_bat">Where you at?</string>
</resources>
18 changes: 18 additions & 0 deletions examples/android/third_party/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
java_plugin(
name = "dagger_component_plugin",
generates_api = True,
processor_class = "dagger.internal.codegen.ComponentProcessor",
visibility = ["//visibility:private"],
deps = [
"@maven//:com_google_dagger_dagger_compiler",
],
)

java_library(
name = "dagger",
exported_plugins = [":dagger_component_plugin"],
exports = [
"@maven//:com_google_dagger_dagger",
"@maven//:javax_inject_javax_inject",
],
)
Empty file added examples/anvil/BUILD.bazel
Empty file.
1 change: 1 addition & 0 deletions examples/anvil/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Forked from https://github.com/square/anvil/tree/main/sample
122 changes: 122 additions & 0 deletions examples/anvil/WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

RULES_JVM_EXTERNAL_TAG = "2.8"

RULES_JVM_EXTERNAL_SHA = "79c9850690d7614ecdb72d68394f994fef7534b292c4867ce5e7dec0aa7bdfad"

http_archive(
name = "rules_jvm_external",
sha256 = RULES_JVM_EXTERNAL_SHA,
strip_prefix = "rules_jvm_external-%s" % RULES_JVM_EXTERNAL_TAG,
url = "https://github.com/bazelbuild/rules_jvm_external/archive/%s.zip" % RULES_JVM_EXTERNAL_TAG,
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = [
"com.google.dagger:dagger:2.28",
"com.google.dagger:dagger-compiler:2.28",
"com.google.dagger:dagger-producers:2.28",
"com.squareup.anvil:compiler:2.0.1",
"com.squareup.anvil:annotations:2.0.1",
"androidx.activity:activity:1.0.0",
"androidx.annotation:annotation:1.1.0",
"androidx.appcompat:appcompat-resources:1.1.0",
"androidx.appcompat:appcompat:1.1.0",
"androidx.arch.core:core-common:2.1.0",
"androidx.arch.core:core-runtime:2.0.0",
"androidx.cardview:cardview:1.0.0",
"androidx.collection:collection:1.1.0",
"androidx.coordinatorlayout:coordinatorlayout:1.1.0",
"androidx.core:core-ktx:1.3.0",
"androidx.core:core:1.3.0",
"androidx.cursoradapter:cursoradapter:1.0.0",
"androidx.customview:customview:1.0.0",
"androidx.drawerlayout:drawerlayout:1.0.0",
"androidx.fragment:fragment:1.0.0",
"androidx.fragment:fragment:1.1.0",
"androidx.interpolator:interpolator:1.0.0",
"androidx.lifecycle:lifecycle-common:2.1.0",
"androidx.lifecycle:lifecycle-livedata-core:2.0.0",
"androidx.lifecycle:lifecycle-livedata:2.0.0",
"androidx.lifecycle:lifecycle-runtime:2.1.0",
"androidx.lifecycle:lifecycle-viewmodel:2.1.0",
"androidx.loader:loader:1.0.0",
"androidx.recyclerview:recyclerview:1.1.0",
"androidx.savedstate:savedstate:1.0.0",
"androidx.transition:transition:1.2.0",
"androidx.vectordrawable:vectordrawable-animated:1.1.0",
"androidx.vectordrawable:vectordrawable:1.1.0",
"androidx.versionedparcelable:versionedparcelable:1.1.0",
"androidx.viewpager2:viewpager2:1.0.0",
"androidx.viewpager:viewpager:1.0.0",
"com.google.android.material:material:1.1.0",
"javax.inject:javax.inject:1",
"junit:junit:4.13",
"org.jetbrains.kotlin:kotlin-test:1.3.50",
"com.google.truth:truth:1.0.1",
"org.jetbrains:annotations:13.0",
"org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.72",
],
excluded_artifacts = [
"org.jetbrains.kotlin:kotlin-stdlib",
"org.jetbrains.kotlin:kotlin-stdlib-jdk8",
"org.jetbrains.kotlin:kotlin-stdlib-jdk7",
],
repositories = [
"https://jcenter.bintray.com/",
"https://maven.google.com",
"https://repo1.maven.org/maven2",
],
)

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "build_bazel_rules_android",
sha256 = "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806",
strip_prefix = "rules_android-0.1.1",
urls = ["https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip"],
)

load(
"@build_bazel_rules_android//android:rules.bzl",
"android_ndk_repository",
"android_sdk_repository",
)

android_sdk_repository(name = "androidsdk")

android_ndk_repository(name = "androidndk") # Required. Name *must* be "androidndk".

# Directly load the kotlin rules from the parent repo.
local_repository(
name = "io_bazel_rules_kotlin",
path = "../..",
)

load("@io_bazel_rules_kotlin//kotlin:dependencies.bzl", "kt_download_local_dev_dependencies")

kt_download_local_dev_dependencies()

load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kotlin_repositories", "kt_register_toolchains")

kotlin_repositories()

kt_register_toolchains()

# Skylib, for build_test, so don't bother initializing the unit test infrastructure.
http_archive(
name = "bazel_skylib",
sha256 = "97e70364e9249702246c0e9444bccdc4b847bed1eb03c5a3ece4f83dfe6abc44",
urls = [
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz",
],
)

http_archive(
name = "rules_pkg",
sha256 = "4ba8f4ab0ff85f2484287ab06c0d871dcb31cc54d439457d28fd4ae14b18450a",
url = "https://github.com/bazelbuild/rules_pkg/releases/download/0.2.4/rules_pkg-0.2.4.tar.gz",
)
11 changes: 11 additions & 0 deletions examples/anvil/app/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@build_bazel_rules_android//android:rules.bzl", "android_binary")

android_binary(
name = "app",
custom_package = "com.squareup.anvil.sample",
manifest = "src/main/AndroidManifest.xml",
visibility = ["//app:__subpackages__"],
deps = [
"//app/src/main/java/com/squareup/anvil/sample",
],
)
61 changes: 61 additions & 0 deletions examples/anvil/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'org.jetbrains.kotlin.kapt'
id 'com.squareup.anvil'
}

android {
compileSdkVersion 29

defaultConfig {
applicationId "com.squareup.anvil.sample"
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.0.0"

testInstrumentationRunner "com.squareup.anvil.sample.TestRunner"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

lintOptions {
warningsAsErrors true
}

variantFilter { variant ->
//noinspection UnnecessaryQualifiedReference
if (variant.buildType.name == com.android.builder.core.BuilderConstants.RELEASE) {
variant.setIgnore(true)
}
}
}

dependencies {
implementation project(':sample:library')
implementation project(':sample:scopes')

implementation deps.kotlin.stdlib
implementation deps.androidx.appcompat
implementation deps.androidx.core
implementation deps.androidx.material
implementation deps.dagger2.dagger

kapt deps.dagger2.compiler

testImplementation deps.junit
testImplementation deps.truth

androidTestImplementation deps.androidx.test.core
androidTestImplementation deps.androidx.test.espresso.core
androidTestImplementation deps.androidx.test.junit
androidTestImplementation deps.androidx.test.rules
androidTestImplementation deps.androidx.test.runner
androidTestImplementation deps.junit

kaptAndroidTest deps.dagger2.compiler
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.squareup.anvil.sample

import androidx.test.espresso.Espresso
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class AnvilSampleTest {

@Rule
@JvmField
val activityRule = ActivityTestRule(MainActivity::class.java)

@Test
fun text_is_displayed() {
Espresso.onView(ViewMatchers.withText("Hephaestus, son of (No Father) and Hera"))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO: Implement tests when android_test works on more than linux.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.squareup.anvil.sample

import com.squareup.anvil.sample.father.FatherProvider
import javax.inject.Inject

class FakeFatherProvider @Inject constructor() : FatherProvider {
override fun father(god: God): String = "(No Father)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.squareup.anvil.sample

import com.squareup.anvil.annotations.ContributesTo
import com.squareup.anvil.sample.father.FatherProvider
import com.squareup.anvil.sample.father.FatherProviderModule
import com.squareup.scopes.AppScope
import dagger.Binds
import dagger.Module

@Module
@ContributesTo(
scope = AppScope::class,
replaces = [FatherProviderModule::class]
)
abstract class FakeFatherProviderModule {
@Binds
abstract fun bindFatherProvider(provider: FakeFatherProvider): FatherProvider
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.squareup.anvil.sample

import android.app.Application
import com.squareup.scopes.ComponentHolder

class TestApp : Application() {

override fun onCreate() {
super.onCreate()

ComponentHolder.components += DaggerTestAppComponent.create()
}
}
Loading

0 comments on commit 95b642f

Please sign in to comment.