Skip to content

Commit

Permalink
Implement sealed KSP + auto-service-ksp (#32)
Browse files Browse the repository at this point in the history
* Add moshi-sealed KSP implementation

* Add KSP deps

* Add KSP to sample testing

* Add KSP matrix to CI

* Add auto-service-ksp

* Wire in auto-service-ksp

* Update sonatype bits

* Disable for now

* More updates

* Add caching

* Enable on PRs

* Make autoservice annotations on the classpath

* Fix deps

* Use appropriate annotation for java version

* Tweak versions
  • Loading branch information
ZacSweers authored Sep 26, 2020
1 parent fda7063 commit d13aff7
Show file tree
Hide file tree
Showing 19 changed files with 1,001 additions and 16 deletions.
28 changes: 18 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
name: CI

on: [push]
on: [push, pull_request]

jobs:
build:
name: JDK ${{ matrix.java_version }}
name: JDK ${{ matrix.java_version }} - KSP ${{ matrix.ksp_enabled }}
runs-on: ubuntu-latest
strategy:
matrix:
java_version: [1.8, 12, 13, 14, 15]
# No 9, 10, or 12 because Kapt
java_version: [1.8, 11, 13, 14, 15]
ksp_enabled: [true, false]
fail-fast: false
steps:
- name: Checkout
uses: actions/checkout@v1
- name: Gradle Wrapper Validation
uses: gradle/wrapper-validation-action@v1
- name: Generate cache key
run: ./checksum.sh checksum.txt
- uses: actions/cache@v2
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ matrix.java_version }}-${{ matrix.job }}-${{ hashFiles('checksum.txt') }}
restore-keys: |
${{ runner.os }}-gradle-${{ matrix.java_version }}-${{ matrix.job }}-
- name: Install JDK
uses: actions/setup-java@v1
with:
Expand All @@ -21,11 +33,7 @@ jobs:
# Initial gradle configuration, install dependencies, etc
run: ./gradlew help
- name: Build project
run: ./gradlew assemble --stacktrace
- name: Run tests
run: ./gradlew test --stacktrace
- name: Final checks
run: ./gradlew check --stacktrace
run: ./gradlew build check -Pmoshix.useKsp=${{ matrix.ksp_enabled }} --stacktrace
- name: Upload snapshot (main only)
run: ./gradlew uploadArchives -PSONATYPE_NEXUS_USERNAME=${{ secrets.SonatypeUsername }} -PSONATYPE_NEXUS_PASSWORD=${{ secrets.SonatypePassword }}
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request' && matrix.java_version == '1.8'
run: ./gradlew uploadArchives -PSONATYPE_NEXUS_USERNAME=${{ secrets.SONATYPE_USERNAME }} -PSONATYPE_NEXUS_PASSWORD=${{ secrets.SONATYPE_PASSWORD }}
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request' && matrix.java_version == '1.8' && matrix.ksp_enabled == 'false'
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ plugins {
subprojects {
repositories {
mavenCentral()
google()
jcenter()
}
pluginManager.withPlugin("java") {
Expand Down
7 changes: 7 additions & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ object Dependencies {
const val metadata = "org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.1.0"
val jvmTarget = "1.8"
val defaultFreeCompilerArgs = listOf("-Xjsr305=strict", "-progressive")

object Ksp {
const val version = "1.4.10-dev-experimental-20200924"
const val api = "com.google.devtools.ksp:symbol-processing-api:$version"
const val ksp = "com.google.devtools.ksp:symbol-processing:$version"
}
}

object KotlinPoet {
Expand All @@ -60,6 +66,7 @@ object Dependencies {

object Testing {
const val compileTesting = "com.github.tschuchortdev:kotlin-compile-testing:1.2.10"
const val kspCompileTesting = "com.github.tschuchortdev:kotlin-compile-testing-ksp:1.2.10"
const val junit = "junit:junit:4.12"
const val truth = "com.google.truth:truth:1.0"
}
Expand Down
24 changes: 24 additions & 0 deletions checksum.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

RESULT_FILE=$1

if [[ -f ${RESULT_FILE} ]]; then
rm ${RESULT_FILE}
fi
touch ${RESULT_FILE}

checksum_file() {
echo $(openssl md5 $1 | awk '{print $2}')
}

FILES=()
while read -r -d ''; do
FILES+=("$REPLY")
done < <(find . -type f \( -name "build.gradle*" -o -name "settings.gradle*" -o -name "gradle-wrapper.properties" \) -print0)

# Loop through files and append MD5 to result file
for FILE in ${FILES[@]}; do
echo $(checksum_file ${FILE}) >> ${RESULT_FILE}
done
# Now sort the file so that it is idempotent.
sort ${RESULT_FILE} -o ${RESULT_FILE}
34 changes: 34 additions & 0 deletions misc/auto-service-ksp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2019 Zac Sweers
*
* 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.
*/
plugins {
id("symbol-processing") version Dependencies.Kotlin.Ksp.version
kotlin("jvm")
kotlin("kapt")
}

dependencies {
kapt(Dependencies.AutoService.processor)
compileOnly(Dependencies.Kotlin.Ksp.api)

implementation(Dependencies.AutoService.annotations)
implementation(Dependencies.KotlinPoet.kotlinPoet)
implementation("com.google.guava:guava:29.0-jre")

testImplementation(Dependencies.Kotlin.Ksp.api)
testImplementation(Dependencies.Testing.truth)
testImplementation(Dependencies.Testing.junit)
testImplementation(Dependencies.Testing.kspCompileTesting)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package dev.zacsweers.autoservice.ksp

import com.google.auto.service.AutoService
import com.google.common.collect.HashMultimap
import com.google.common.collect.Multimap
import com.google.common.collect.Sets
import com.google.devtools.ksp.closestClassDeclaration
import com.google.devtools.ksp.getAllSuperTypes
import com.google.devtools.ksp.isLocal
import com.google.devtools.ksp.processing.CodeGenerator
import com.google.devtools.ksp.processing.KSPLogger
import com.google.devtools.ksp.processing.Resolver
import com.google.devtools.ksp.processing.SymbolProcessor
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.squareup.kotlinpoet.ClassName
import java.io.IOException
import java.util.SortedSet

@AutoService(SymbolProcessor::class)
class AutoServiceSymbolProcessor : SymbolProcessor {

companion object {
val AUTO_SERVICE_NAME = AutoService::class.qualifiedName!!
}

/**
* Maps the class names of service provider interfaces to the
* class names of the concrete classes which implement them.
*
* For example,
* ```
* "com.google.apphosting.LocalRpcService" -> "com.google.apphosting.datastore.LocalDatastoreService"
* ```
*/
private val providers: Multimap<String, String> = HashMultimap.create()

private lateinit var codeGenerator: CodeGenerator
private lateinit var logger: KSPLogger
private var verify = false
private var verbose = false

override fun init(
options: Map<String, String>,
kotlinVersion: KotlinVersion,
codeGenerator: CodeGenerator,
logger: KSPLogger
) {
this.codeGenerator = codeGenerator
this.logger = logger
verify = options["autoserviceKsp.verify"]?.toBoolean() == true
verbose = options["autoserviceKsp.verbose"]?.toBoolean() == true
}

/**
* - For each class annotated with [AutoService
* - Verify the [AutoService] interface value is correct
* - Categorize the class by its service interface
* - For each [AutoService] interface
* - Create a file named `META-INF/services/<interface>`
* - For each [AutoService] annotated class for this interface
* - Create an entry in the file
*/
override fun process(resolver: Resolver) {
val autoServiceType = resolver.getClassDeclarationByName(
resolver.getKSNameFromString(AUTO_SERVICE_NAME))
?.asType(emptyList())
?: error("@AutoService type not found on the classpath.")

resolver.getSymbolsWithAnnotation(AUTO_SERVICE_NAME)
.asSequence()
.filterIsInstance<KSClassDeclaration>()
.forEach { providerImplementer ->
val annotation = providerImplementer.annotations.find { it.annotationType.resolve() == autoServiceType }
?: error("@AutoService annotation not found")

@Suppress("UNCHECKED_CAST")
val providerInterfaces = annotation.arguments
.find { it.name?.getShortName() == "value" }!!
.value as? List<KSType>
?: error("No 'value' member value found!")

if (providerInterfaces.isEmpty()) {
error("No service interfaces provided for element! $providerImplementer")
}

for (providerType in providerInterfaces) {
val providerDecl = providerType.declaration.closestClassDeclaration()!!
if (checkImplementer(providerImplementer, providerType)) {
providers.put(providerDecl.toBinaryName(), providerImplementer.toBinaryName())
} else {
val message = "ServiceProviders must implement their service provider interface. " +
providerImplementer.qualifiedName +
" does not implement " +
providerDecl.qualifiedName
error(message)
}
}
}
generateConfigFiles()
}

private fun checkImplementer(
providerImplementer: KSClassDeclaration,
providerType: KSType
): Boolean {
if (!verify) {
return true
}
return providerImplementer.getAllSuperTypes().any { it.isAssignableFrom(providerType) }
}

private fun generateConfigFiles() {
for (providerInterface in providers.keySet()) {
val resourceFile = "META-INF/services/$providerInterface"
log("Working on resource file: $resourceFile")
try {
val allServices: SortedSet<String> = Sets.newTreeSet()
val newServices: Set<String> = HashSet(providers[providerInterface])
allServices.addAll(newServices)
log("New service file contents: $allServices")
codeGenerator.createNewFile("", resourceFile, "").bufferedWriter().use { writer ->
for (service in allServices) {
writer.write(service)
writer.newLine()
}
}
log("Wrote to: $resourceFile")
} catch (e: IOException) {
error("Unable to create $resourceFile, $e")
}
}
}

private fun log(message: String) {
if (verbose) {
logger.logging(message)
}
}

/**
* Returns the binary name of a reference type. For example,
* {@code com.google.Foo$Bar}, instead of {@code com.google.Foo.Bar}.
*/
private fun KSClassDeclaration.toBinaryName(): String {
return toClassName().reflectionName()
}

private fun KSClassDeclaration.toClassName(): ClassName {
require(!isLocal()) {
"Local/anonymous classes are not supported!"
}
val pkgName = packageName.asString()
val typesString = qualifiedName!!.asString().removePrefix("$pkgName.")

val simpleNames = typesString
.split(".")
return ClassName(pkgName, simpleNames)
}

override fun finish() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package dev.zacsweers.autoservice.ksp

import com.google.common.truth.Truth.assertThat
import com.tschuchort.compiletesting.KotlinCompilation
import com.tschuchort.compiletesting.KotlinCompilation.ExitCode
import com.tschuchort.compiletesting.SourceFile
import com.tschuchort.compiletesting.kspSourcesDir
import org.junit.Ignore
import org.junit.Test
import java.io.File

@Ignore("Disabled until kotlin-compile-testing updates")
class AutoServiceSymbolProcessorTest {
@Test
fun smokeTest() {
val source = SourceFile.kotlin("CustomCallable.kt", """
package test
import com.google.auto.service.AutoService
import java.util.concurrent.Callable
@AutoService(Callable::class)
class CustomCallable : Callable<String> {
override fun call(): String = "Hello world!"
}
""")

val compilation = KotlinCompilation().apply {
sources = listOf(source)
inheritClassPath = true
// symbolProcessors = listOf(AutoServiceSymbolProcessor())
}
val result = compilation.compile()
assertThat(result.exitCode).isEqualTo(ExitCode.OK)
val generatedSourcesDir = compilation.kspSourcesDir
val generatedFile = File(generatedSourcesDir,
"resources/META-INF/services/java.util.concurrent.Callable")
assertThat(generatedFile.exists()).isTrue()
assertThat(generatedFile.readText()).isEqualTo("test.CustomCallable\n")
}
}
50 changes: 50 additions & 0 deletions moshi-sealed/codegen-ksp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2019 Zac Sweers
*
* 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.
*/
plugins {
id("symbol-processing") version Dependencies.Kotlin.Ksp.version
kotlin("jvm")
id("com.vanniktech.maven.publish")
}

// Necessary to ensure the generated service file is included in the jar
sourceSets {
main {
resources {
srcDir("build/generated/ksp/src/main/resources")
}
}
}

val compileKotlin = tasks.named("compileKotlin")
tasks.named<ProcessResources>("processResources").configure {
dependsOn(compileKotlin)
}

dependencies {
ksp(project(":misc:auto-service-ksp"))
implementation(Dependencies.AutoService.annotations)
compileOnly(Dependencies.Kotlin.Ksp.api)

implementation(Dependencies.KotlinPoet.kotlinPoet)
implementation(Dependencies.Moshi.adapters)
implementation(Dependencies.Moshi.moshi)
implementation(project(":moshi-sealed:annotations"))

testImplementation(Dependencies.Kotlin.Ksp.api)
testImplementation(Dependencies.Testing.truth)
testImplementation(Dependencies.Testing.junit)
testImplementation(Dependencies.Testing.kspCompileTesting)
}
Loading

0 comments on commit d13aff7

Please sign in to comment.