-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement sealed KSP + auto-service-ksp (#32)
* 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
Showing
19 changed files
with
1,001 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
164 changes: 164 additions & 0 deletions
164
...o-service-ksp/src/main/kotlin/dev/zacsweers/autoservice/ksp/AutoServiceSymbolProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
|
||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...rvice-ksp/src/test/kotlin/dev/zacsweers/autoservice/ksp/AutoServiceSymbolProcessorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.