Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build logic + better publishing #760

Merged
merged 3 commits into from
Oct 30, 2023
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
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 'com.squareup.anvil.library'
id 'com.squareup.anvil.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 'com.squareup.anvil.library'
id 'com.squareup.anvil.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.

42 changes: 42 additions & 0 deletions build-logic/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktlint)
id 'java-gradle-plugin'
}

gradlePlugin {
plugins {
library {
id = "com.squareup.anvil.library"
implementationClass = "com.squareup.anvil.LibraryPlugin"
}
root {
id = "com.squareup.anvil.root"
implementationClass = "com.squareup.anvil.RootPlugin"
}
publish {
id = "com.squareup.anvil.publish"
implementationClass = "com.squareup.anvil.PublishConventionPlugin"
}
}
}

kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}

dependencies {
compileOnly(gradleApi())
implementation libs.ktlintRaw
implementation libs.kgx
implementation libs.kotlinpoet
implementation libs.kotlin.dokka
implementation libs.kotlin.gradlePlugin
implementation libs.mavenPublishRaw
}

ktlint {
version = libs.versions.ktlint.get()
}
3 changes: 3 additions & 0 deletions buildSrc/settings.gradle → build-logic/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pluginManagement {
}
}

rootProject.name = "build-logic"

dependencyResolutionManagement {
versionCatalogs {
Map<String, String> overrides = new LinkedHashMap()
Expand All @@ -29,6 +31,7 @@ dependencyResolutionManagement {

repositories {
google()
gradlePluginPortal()
mavenCentral()
if (hasProperty("anvil.allowSnapshots")) {
maven { url 'https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev' }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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.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)
}
}
}
}
20 changes: 20 additions & 0 deletions build-logic/src/main/kotlin/com/squareup/anvil/RootPlugin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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) {
target.plugins.apply(KotlinPlatformJvmPlugin::class.java)
target.plugins.apply(KtlintConventionPlugin::class.java)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.squareup.anvil.benchmark

import org.gradle.api.Plugin
import org.gradle.api.Project

open class BenchmarkPlugin : Plugin<Project> {
override fun apply(target: Project) {
target.tasks.register("createBenchmarkProject", CreateBenchmarkProjectTask::class.java)
}
}
28 changes: 10 additions & 18 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import com.squareup.anvil.benchmark.CreateBenchmarkProjectTask
import org.gradle.api.internal.project.DefaultProject
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
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 'com.squareup.anvil.root'
}

boolean isCi = (System.getenv('CI') ?: 'false').toBoolean()
Expand All @@ -40,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 @@ -129,13 +121,13 @@ 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) {
return project.hasProperty(name) ? project.getProperty(name) : defaultValue
}

// Register the task that can create the benchmark project.
tasks.register("createBenchmarkProject", CreateBenchmarkProjectTask)
19 changes: 0 additions & 19 deletions buildSrc/build.gradle

This file was deleted.

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 'com.squareup.anvil.library'
id 'com.squareup.anvil.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 'com.squareup.anvil.library'
id 'java-test-fixtures'
id 'com.squareup.anvil.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()
}
}

Loading