Skip to content

Commit

Permalink
modernize Anvil's publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
RBusarow committed Oct 27, 2023
1 parent 258dd05 commit 68c66a0
Show file tree
Hide file tree
Showing 22 changed files with 241 additions and 228 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs :
check-latest : true

- name : KtLint
run : ./gradlew ktlintCheck --no-build-cache --no-daemon --stacktrace && ./gradlew -p buildSrc ktlintCheck --no-build-cache --no-daemon --stacktrace
run : ./gradlew ktlintCheck --no-build-cache --no-daemon --stacktrace && ./gradlew -p build-logic ktlintCheck --no-build-cache --no-daemon --stacktrace

lint :
runs-on : ubuntu-latest
Expand Down
12 changes: 8 additions & 4 deletions annotations-optional/build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
plugins {
alias(libs.plugins.kotlin.jvm)
id 'library'
id 'publish'
}
apply from: rootProject.file('publishing.gradle')

kotlin {
explicitApi()
publish {
configurePom(
artifactId: 'annotations-optional',
pomName: 'Anvil Optional Annotations',
pomDescription: 'Optional annotations that we\'ve found to be helpful with managing larger dependency graphs'
)
}

dependencies {
Expand Down
3 changes: 0 additions & 3 deletions annotations-optional/gradle.properties

This file was deleted.

12 changes: 10 additions & 2 deletions annotations/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
plugins {
alias(libs.plugins.kotlin.jvm)
id 'library'
id 'publish'
}

publish {
configurePom(
artifactId: 'annotations',
pomName: 'Anvil Annotations',
pomDescription: 'Annotations used to mark classes and methods for code generation in Anvil'
)
}
apply from: rootProject.file('publishing.gradle')

kotlin {
explicitApi()
Expand Down
3 changes: 0 additions & 3 deletions annotations/gradle.properties

This file was deleted.

8 changes: 8 additions & 0 deletions build-logic/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ gradlePlugin {
id = "root"
implementationClass = "com.squareup.anvil.RootPlugin"
}
publish {
id = "publish"
implementationClass = "com.squareup.anvil.PublishConventionPlugin"
}
}
}

Expand All @@ -26,7 +30,11 @@ kotlin {
dependencies {
compileOnly(gradleApi())
implementation libs.ktlintRaw
implementation libs.kgx
implementation libs.kotlinpoet
implementation libs.kotlin.dokka
implementation libs.kotlin.gradlePlugin
implementation libs.mavenPublishRaw
}

ktlint {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.squareup.anvil

import com.rickbusarow.kgx.libsCatalog
import com.rickbusarow.kgx.version
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.jlleitschuh.gradle.ktlint.KtlintExtension
import org.jlleitschuh.gradle.ktlint.KtlintPlugin

open class KtlintConventionPlugin : Plugin<Project> {

override fun apply(target: Project) {
target.plugins.apply(KtlintPlugin::class.java)

target.plugins.apply(org.jlleitschuh.gradle.ktlint.KtlintPlugin::class.java)

target.extensions.configure(KtlintExtension::class.java) { ktlint ->
ktlint.version.set(target.libsCatalog.version("ktlint"))
ktlint.verbose.set(true)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.squareup.anvil

import com.vanniktech.maven.publish.JavadocJar
import com.vanniktech.maven.publish.KotlinJvm
import com.vanniktech.maven.publish.MavenPublishBaseExtension
import com.vanniktech.maven.publish.Platform
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.publish.PublishingExtension
import org.gradle.api.publish.maven.MavenPublication
import org.gradle.api.publish.tasks.GenerateModuleMetadata
import org.gradle.api.tasks.bundling.Jar
import javax.inject.Inject

open class PublishConventionPlugin : Plugin<Project> {
override fun apply(target: Project) {

target.plugins.apply("com.vanniktech.maven.publish.base")
target.plugins.apply("org.jetbrains.dokka")

target.extensions.create("publish", PublishExtension::class.java)

val mavenPublishing = target.extensions
.getByType(MavenPublishBaseExtension::class.java)

val pluginPublishId = "com.gradle.plugin-publish"

@Suppress("UnstableApiUsage")
mavenPublishing.pomFromGradleProperties()
mavenPublishing.signAllPublications()

target.plugins.withId("org.jetbrains.kotlin.jvm") {

when {
target.plugins.hasPlugin(pluginPublishId) -> {
// Gradle's 'plugin-publish' plugin creates its own publication. We only apply this plugin
// in order to get all the automated POM configuration.
}

else -> {
configurePublication(
target,
mavenPublishing,
KotlinJvm(javadocJar = JavadocJar.Dokka("dokkaHtml"), sourcesJar = true)
)
}
}
}
}

@Suppress("UnstableApiUsage")
private fun configurePublication(
target: Project,
mavenPublishing: MavenPublishBaseExtension,
platform: Platform
) {
mavenPublishing.configure(platform)

target.tasks.withType(GenerateModuleMetadata::class.java).configureEach {
it.mustRunAfter(target.tasks.withType(Jar::class.java))
it.mustRunAfter("dokkaJavadocJar")
it.mustRunAfter("kotlinSourcesJar")
}
}
}

open class PublishExtension @Inject constructor(
private val target: Project,
) {
fun configurePom(args: Map<String, Any>) {

val artifactId = args.getValue("artifactId") as String
val pomName = args.getValue("pomName") as String
val pomDescription = args.getValue("pomDescription") as String

target.extensions
.getByType(PublishingExtension::class.java)
.publications.withType(MavenPublication::class.java)
.matching { !it.name.endsWith("PluginMarkerMaven") }
.configureEach { publication ->

// Gradle plugin publications have their own artifactID convention,
// and that's handled automatically.
if (!publication.name.endsWith("PluginMarkerMaven")) {
publication.artifactId = artifactId
}

publication.pom {
it.name.set(pomName)
it.description.set(pomDescription)
}
}
}
}
6 changes: 4 additions & 2 deletions build-logic/src/main/kotlin/com/squareup/anvil/RootPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ package com.squareup.anvil
import com.squareup.anvil.benchmark.BenchmarkPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformJvmPlugin

open class RootPlugin : Plugin<Project> {
override fun apply(target: Project) {

target.plugins.apply(BenchmarkPlugin::class.java)
target.plugins.apply(KtlintConventionPlugin::class.java)
}
}

open class LibraryPlugin : Plugin<Project> {
override fun apply(target: Project) {
// TODO
target.plugins.apply(KotlinPlatformJvmPlugin::class.java)
target.plugins.apply(KtlintConventionPlugin::class.java)
}
}
24 changes: 10 additions & 14 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

buildscript {
dependencies {
classpath libs.mavenPublishRaw
classpath("com.squareup.anvil:gradle-plugin:${findProperty("VERSION_NAME")}",)
}
}

plugins {
alias(libs.plugins.agp.application) apply false
alias(libs.plugins.agp.library) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.gradlePublish) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.kapt) apply false
alias(libs.plugins.kotlin.dokka) apply false
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.kotlin.kapt) apply false
alias(libs.plugins.kotlin.multiplatform) apply false
alias(libs.plugins.gradlePublish) apply false
alias(libs.plugins.ktlint) apply false
alias(libs.plugins.mavenPublishBase) apply false
id 'root'
}

boolean isCi = (System.getenv('CI') ?: 'false').toBoolean()
Expand All @@ -39,14 +40,6 @@ println "Versions: " + [
boolean configureOnDemandEnabled = getProperty("org.gradle.configureondemand", "false").toBoolean()

subprojects {
if (buildFile.exists()) {
apply plugin: 'org.jlleitschuh.gradle.ktlint'

ktlint {
version = libs.versions.ktlint.get()
verbose = true
}
}

tasks.withType(Test).configureEach {
testLogging {
Expand Down Expand Up @@ -128,8 +121,11 @@ subprojects {
}
}

task clean(type: Delete) {
delete rootProject.buildDir
// For any publish task invoked in the main build, there's a corresponding task in the gradle-plugin
// included build. Gradle doesn't invoke those included build tasks when the main build's tasks are
// run, and that's annoying. This fixes that behavior.
tasks.withType(PublishToMavenRepository).configureEach { mainTask ->
mainTask.dependsOn(gradle.includedBuild("gradle-plugin").task(":${mainTask.name}"))
}

def getProperty(String name, String defaultValue) {
Expand Down
12 changes: 10 additions & 2 deletions compiler-api/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
plugins {
alias(libs.plugins.kotlin.jvm)
id 'library'
id 'publish'
}

publish {
configurePom(
artifactId: 'compiler-api',
pomName: 'Anvil Compiler API',
pomDescription: 'API definitions for creating custom code generators that integrate with Anvil'
)
}
apply from: rootProject.file('publishing.gradle')

kotlin {
explicitApi()
Expand Down
3 changes: 0 additions & 3 deletions compiler-api/gradle.properties

This file was deleted.

14 changes: 9 additions & 5 deletions compiler-utils/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.kotlin.jvm)
id 'library'
id 'java-test-fixtures'
id 'publish'
}
apply from: rootProject.file('publishing.gradle')

kotlin {
explicitApi()
publish {
configurePom(
artifactId: "compiler-utils",
pomName: "Anvil Compiler Utils",
pomDescription: 'Optional utility and extension functions for working with PSI and descriptors, ' +
'designed to simplify code generation tasks in Anvil'
)
}

def isIdeSync = getSystemProperty('idea.sync.active', 'false').toBoolean()
Expand Down Expand Up @@ -58,4 +63,3 @@ private def getSystemProperty(String name, String defaultValue = null) {
return provider.getOrNull()
}
}

3 changes: 0 additions & 3 deletions compiler-utils/gradle.properties

This file was deleted.

30 changes: 26 additions & 4 deletions compiler/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.kotlin.kapt)
id 'library'
alias libs.plugins.kotlin.kapt
alias libs.plugins.buildconfig
id 'publish'
}
apply from: rootProject.file('publishing.gradle')

apply from: project.file('generate_build_properties.gradle')
buildConfig {
className('BuildProperties')
packageName('com.squareup.anvil.compiler')
useKotlinOutput { topLevelConstants = true }

buildConfigField('boolean', 'WARNINGS_AS_ERRORS',"${rootProject.ext.warningsAsErrors}")
buildConfigField('boolean', 'FULL_TEST_RUN', "${libs.versions.config.fullTestRun.get()}")
}

publish {
configurePom(
artifactId: 'compiler',
pomName: 'Anvil Compiler',
pomDescription: 'The core implementation module for Anvil, responsible for hooking into ' +
'the Kotlin compiler and orchestrating code generation'
)
}

dependencies {
implementation project(':annotations')
Expand Down Expand Up @@ -34,6 +51,11 @@ dependencies {
testImplementation libs.truth
}

// Fixes:
// Reason: Task ':compiler:dokkaHtml' uses this output of task ':compiler:kaptKotlin'
// without declaring an explicit or implicit dependency.
tasks.named("dokkaHtml") { mustRunAfter("kaptKotlin") }

tasks.withType(KotlinCompile).configureEach {
compilerOptions {
// The flag is needed because we extend an interface that uses @JvmDefault and the Kotlin
Expand Down
Loading

0 comments on commit 68c66a0

Please sign in to comment.