Skip to content
This repository was archived by the owner on Sep 19, 2022. It is now read-only.

Implement an alternate compiler using KSP #2

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,38 @@ apply(plugin = "my-plugin")

Setup
---
To get started you'll need to include the api as regular dependency and the compiler as an annotation processor:

### KSP
If using Kotlin it's preferred to use the Gradle plugin which will use [KSP](https://github.com/google/ksp) to generate
the file:
```kotlin
plugins {
kotlin("jvm")
id("symbol-processing") version "<version>"
id("se.ansman.autoplugin") version "0.1.1"
}

// You can optionally configure it:
autoPlugin {
// By default he plugin verifies that the Plugin ID is valid. If there are issues with the validation it can
// be disabled like this.
verificationEnabled.set(false)
}
```

If you do not want to use the plugin you need to duplicate [what the plugin does](https://github.com/ansman/auto-plugin/tree/main/gradle-plugin/src/main/kotlin/se/ansman/autoplugin/gradle/AutoPluginGradlePlugin.kt).

### Annotations Processing
If you aren't using Kotlin or does not want to use KSP you can add it as an annotation processor:
```kotlin
dependencies {
implementation("se.ansman.autoplugin:api:0.1.1")
kapt("se.ansman.autoplugin:compile:0.1.1")
// For non kotlin projects you'll use something like this
annotationsProcessor("se.ansman.autoplugin:compile:0.1.1")
// For kotlin projects you'll use this instead
kapt("se.ansman.autoplugin:compile:0.1.1")
}
```

Then simply annotate each plugin with `@AutoPlugin` and assign it an ID.

Attribution
---
This library is heavily influenced by [Auto Service](https://github.com/google/auto/tree/master/service).
Expand Down
2 changes: 1 addition & 1 deletion RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
3. Update the `README.md` with the new version.
4. `git commit -am "Prepare for release X.Y.Z"` (where X.Y.Z is the new version)
5. `git push origin`
6. `./gradlew clean bintrayUpload`.
6. `./gradlew clean bintrayUpload publishPlugins`.
7. Visit the [api artifact](https://bintray.com/ansman/auto-plugin/api#central) and [compiler artifact](https://bintray.com/ansman/auto-plugin/compiler#central) and publish to maven central.
8. Release on GitHub
9. Update the `gradle.properties` to the next SNAPSHOT version.
Expand Down
2 changes: 1 addition & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
library
`published-library`
}

dependencies {
Expand Down
11 changes: 9 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
@file:Suppress("UnstableApiUsage")
buildscript {
repositories {
jcenter()
google()
mavenLocal()
}
}

allprojects {
repositories {
mavenLocal()
jcenter()
google()
mavenLocal()
}
}
6 changes: 5 additions & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
}

dependencies {
val kotlinVersion = providers.gradleProperty("kotlinVersion").forUseAtConfigurationTime().get()
val kotlinVersion = "1.4.10"
api("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion")
api("org.jetbrains.dokka:dokka-gradle-plugin:$kotlinVersion")
api("com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5")
Expand All @@ -23,5 +23,9 @@ gradlePlugin {
id = name
implementationClass = "se.ansman.autoplugin.gradle.LibraryPlugin"
}
register("published-library") {
id = name
implementationClass = "se.ansman.autoplugin.gradle.PublishedLibraryPlugin"
}
}
}
1 change: 0 additions & 1 deletion buildSrc/gradle.properties

This file was deleted.

46 changes: 46 additions & 0 deletions buildSrc/src/main/kotlin/Projects.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

import org.gradle.api.Project
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.tasks.Delete
import java.io.File

/*
* Copyright (c) 2020. Nicklas Ansman Giertz
*
* 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.
*/


val Project.testMavenRepo: File
get() = rootProject.buildDir.resolve("repo")

fun Project.setupTestPublishing() {
with(extensions.getByType(PublishingExtension::class.java)) {
repositories {
it.maven { maven ->
maven.name = "test"
maven.url = uri(testMavenRepo)
}
}

publications.register("testLibrary", MavenPublication::class.java) { publication ->
publication.from(project.components.getByName("java"))
publication.artifactId = project.path.removePrefix(":").replace(':', '-')
}
}
val cleanTestRepo = tasks.register("cleanTestRepo", Delete::class.java) {
it.delete = setOf(testMavenRepo)
}
tasks.named("clean") {
it.dependsOn(cleanTestRepo)
}
}
16 changes: 15 additions & 1 deletion buildSrc/src/main/kotlin/deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
@Suppress("ClassName", "SpellCheckingInspection", "MemberVisibilityCanBePrivate")
object deps {

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

object auto {
const val common = "com.google.auto:auto-common:0.11"
object service {
Expand All @@ -36,5 +44,11 @@ object deps {
}

const val truth = "com.google.truth:truth:1.0.1"
const val compileTesting = "com.github.tschuchortdev:kotlin-compile-testing:1.2.11"
object compileTesting {
const val version = "1.3.1"
const val core = "com.github.tschuchortdev:kotlin-compile-testing:$version"
const val ksp = "com.github.tschuchortdev:kotlin-compile-testing-ksp:$version"
}

const val kotlinPoet = "com.squareup:kotlinpoet:1.6.0"
}
110 changes: 10 additions & 100 deletions buildSrc/src/main/kotlin/se/ansman/autoplugin/gradle/LibraryPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,128 +14,38 @@

package se.ansman.autoplugin.gradle

import com.jfrog.bintray.gradle.BintrayExtension
import deps
import org.gradle.api.JavaVersion
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.plugins.JavaPluginExtension
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.jvm.tasks.Jar
import org.gradle.api.tasks.testing.Test
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
import java.util.*

@Suppress("UnstableApiUsage")
abstract class LibraryPlugin : Plugin<Project> {
override fun apply(target: Project) {
with(target) {
plugins.apply("org.jetbrains.kotlin.jvm")
plugins.apply("org.jetbrains.dokka")
plugins.apply("maven-publish")
plugins.apply("com.jfrog.bintray")
group = "se.ansman.autoplugin"
version = providers.gradleProperty("version").forUseAtConfigurationTime().get()

extensions.configure(JavaPluginExtension::class.java) {
it.sourceCompatibility = JavaVersion.VERSION_1_8
it.targetCompatibility = JavaVersion.VERSION_1_8
}

extensions.configure<KotlinJvmProjectExtension>("kotlin") { kotlin ->
kotlin.target.compilations.configureEach {
it.kotlinOptions.jvmTarget = "1.8"
}
}

val sourcesJar = tasks.register("sourcesJar", Jar::class.java) { task ->
task.from(project.sourceSets.getByName("main").allSource)
task.archiveClassifier.set("sources")
}

val dokkaJar = tasks.register("dokkaJar", Jar::class.java) { task ->
task.from(tasks.named("dokkaJavadoc"))
task.archiveClassifier.set("javadoc")
with(dependencies) {
add("testImplementation", platform(deps.junit.bom))
}

val publication = publishing.publications.register("library", MavenPublication::class.java) { publication ->
publication.from(project.components.getByName("java"))
publication.artifactId = project.path.removePrefix(":").replace(':', '-')
publication.artifact(sourcesJar)
publication.artifact(dokkaJar)

with(publication.pom) {
name.set("AutoPlugin")
description.set("Generates configuration files for Gradle Plugins.")
url.set("https://github.com/ansman/auto-plugin")
issueManagement {
it.system.set("GitHub Issues")
it.url.set("https://github.com/ansman/auto-plugin/issues")
}
licenses {
it.license { license ->
license.name.set("Apache 2.0")
license.url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
}
}
developers {
it.developer { developer ->
developer.id.set("nicklas.ansman")
developer.name.set("Nicklas Ansman Giertz")
developer.email.set("[email protected]")
}
}
scm { scm ->
scm.url.set("https://github.com/ansman/auto-plugin")
scm.connection.set("scm:git:git://github.com/ansman/auto-plugin.git")
scm.developerConnection.set("scm:git:ssh://github.com/ansman/auto-plugin.git")
}
}
tasks.named("test", Test::class.java) { test ->
test.useJUnitPlatform()
test.testLogging.events("passed", "skipped", "failed")
}

extensions.configure<BintrayExtension>("bintray") { bintray ->
with(bintray) {
user = providers.gradleProperty("BINTRAY_USER").forUseAtConfigurationTime().orNull
key = providers.gradleProperty("BINTRAY_API_KEY").forUseAtConfigurationTime().orNull
setPublications(publication.name)
with(pkg) {
val pub = publication.get()
val pom = pub.pom
repo = "auto-plugin"
name = pub.artifactId
desc = pom.description.get()
issueTrackerUrl = "https://github.com/ansman/auto-plugin/issues"
websiteUrl = pom.url.get()
vcsUrl = pom.url.get()
publish = true
publicDownloadNumbers = true
with(version) {
desc = pom.description.get()
released = Date().toString()
with(gpg) {
sign = true
passphrase = providers.gradleProperty("BINTRAY_GPG_PASSWORD").forUseAtConfigurationTime().orNull
}
}
}
extensions.configure<KotlinJvmProjectExtension>("kotlin") { kotlin ->
kotlin.target.compilations.configureEach {
it.kotlinOptions.jvmTarget = "1.8"
}
}

tasks.named("bintrayUpload") {
it.doLast { printPublishedPublications() }
}

tasks.named("publishToMavenLocal") {
it.doLast { printPublishedPublications() }
}
}
}

private fun Project.printPublishedPublications() {
extensions.getByType(PublishingExtension::class.java)
.publications
.filterIsInstance<MavenPublication>()
.forEach { publication ->
println("Published artifact ${publication.groupId}:${publication.artifactId}:${publication.version}")
}
}
}
Loading