-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SQLDelight client and related Gradle plugin.
- Loading branch information
1 parent
4b9186f
commit 7aa4945
Showing
42 changed files
with
1,626 additions
and
5 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,114 @@ | ||
import com.vanniktech.maven.publish.GradlePlugin | ||
import com.vanniktech.maven.publish.JavadocJar | ||
import com.vanniktech.maven.publish.MavenPublishBaseExtension | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
id("java-gradle-plugin") | ||
kotlin("jvm") | ||
`java-library` | ||
id("com.github.gmazzo.buildconfig") | ||
id("com.vanniktech.maven.publish.base") | ||
} | ||
|
||
dependencies { | ||
implementation(libs.guava) | ||
implementation(libs.kotlinGradlePlugin) | ||
implementation(libs.kotlinPoet) | ||
implementation(libs.moshiCore) | ||
implementation(libs.moshiKotlin) | ||
implementation(libs.wireRuntime) | ||
implementation(libs.guice) | ||
implementation(libs.kotlinStdLib) | ||
implementation(libs.sqldelightJdbcDriver) | ||
// /Users/mikepaw/.gradle/caches/jars-9/fd3f67ac2b6df3442dc9a5c8c256f2e1/gradle-plugin-2.0.2.jar | ||
implementation(libs.sqldelightGradlePlugin) | ||
implementation(libs.okHttp) | ||
implementation(libs.okio) | ||
implementation(libs.retrofit) | ||
implementation(libs.retrofitMock) | ||
implementation(libs.retrofitMoshi) | ||
implementation(libs.retrofitWire) | ||
implementation(libs.wireMoshiAdapter) | ||
|
||
api(project(":client")) | ||
// We do not want to leak client-base implementation details to customers. | ||
implementation(project(":client-base")) | ||
|
||
|
||
testImplementation(libs.assertj) | ||
testImplementation(libs.junitEngine) | ||
testImplementation(libs.kotlinTest) | ||
|
||
testImplementation(project(":backfila-embedded")) | ||
testImplementation(project(":client-testing")) | ||
|
||
// **************************************** | ||
// For TESTING purposes only. We only want Misk for easy testing. | ||
// DO NOT turn these into regular dependencies. | ||
// **************************************** | ||
testImplementation(libs.misk) | ||
testImplementation(libs.miskActions) | ||
testImplementation(libs.miskInject) | ||
testImplementation(libs.miskJdbc) | ||
testImplementation(testFixtures(libs.miskJdbc)) | ||
testImplementation(libs.miskTesting) | ||
testImplementation(project(":client-misk")) | ||
} | ||
|
||
tasks.withType<KotlinCompile> { | ||
dependsOn("spotlessKotlinApply") | ||
kotlinOptions { | ||
jvmTarget = "17" | ||
} | ||
} | ||
|
||
tasks.withType<JavaCompile> { | ||
sourceCompatibility = JavaVersion.VERSION_17.toString() | ||
targetCompatibility = JavaVersion.VERSION_17.toString() | ||
} | ||
|
||
gradlePlugin { | ||
plugins { | ||
create("backfila-client-sqldelight") { | ||
id = "app.cash.backfila.client.sqldelight" | ||
displayName = "backfilaClientSqlDelight" | ||
description = "Gradle plugin to generate sqldelight files for Backfila backfills." | ||
implementationClass = "app.cash.backfila.client.sqldelight.plugin.BackfilaSqlDelightGradlePlugin" | ||
} | ||
} | ||
} | ||
|
||
tasks { | ||
test { | ||
// The test in 'src/test/projects/android' needs Java 17+. | ||
javaLauncher.set( | ||
project.javaToolchains.launcherFor { | ||
languageVersion.set(JavaLanguageVersion.of(17)) | ||
} | ||
) | ||
systemProperty("backfilaVersion", rootProject.findProperty("VERSION_NAME") ?: "0.0-SNAPSHOT") | ||
dependsOn(":client:publishAllPublicationsToTestMavenRepository") | ||
dependsOn(":client-base:publishAllPublicationsToTestMavenRepository") | ||
dependsOn(":client-sqldelight:publishAllPublicationsToTestMavenRepository") | ||
dependsOn(":client-sqldelight-gradle-plugin:publishAllPublicationsToTestMavenRepository") | ||
} | ||
} | ||
|
||
buildConfig { | ||
useKotlinOutput { | ||
internalVisibility = true | ||
topLevelConstants = true | ||
} | ||
|
||
packageName("app.cash.backfila.client.sqldelight.plugin") | ||
buildConfigField("String", "VERSION", "\"${project.version}\"") | ||
} | ||
|
||
configure<MavenPublishBaseExtension> { | ||
configure( | ||
GradlePlugin( | ||
javadocJar = JavadocJar.Empty() | ||
) | ||
) | ||
} |
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,4 @@ | ||
POM_ARTIFACT_ID=client-sqldelight-gradle-plugin | ||
POM_NAME=client-sqldelight-gradle-plugin | ||
POM_DESCRIPTION=sqldelight gradle plugin for backfila | ||
POM_PACKAGING=jar |
112 changes: 112 additions & 0 deletions
112
.../main/kotlin/app/cash/backfila/client/sqldelight/plugin/BackfilaSqlDelightGradlePlugin.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,112 @@ | ||
package app.cash.backfila.client.sqldelight.plugin | ||
|
||
import java.io.Serializable | ||
import org.gradle.api.NamedDomainObjectContainer | ||
import org.gradle.api.Plugin | ||
import org.gradle.api.Project | ||
import org.gradle.api.file.Directory | ||
import org.gradle.api.provider.Property | ||
import org.jetbrains.kotlin.gradle.dsl.kotlinExtension | ||
|
||
class BackfilaSqlDelightGradlePlugin : Plugin<Project> { | ||
override fun apply(target: Project) { | ||
val backfilaExtension = target.extensions.create("backfilaSqlDelight", BackfilaSqlDelightExtension::class.java) | ||
|
||
backfilaExtension.backfills.all { backfill -> | ||
check(!backfill.name.matches(Regex("\\s"))) { "Backfill `name` is not allowed to contain whitespace." } | ||
val baseSqlDirectory = target.layout.buildDirectory.dir("backfilaSqlDelight/${backfill.name}/sql") | ||
val baseKotlinDirectory = target.layout.buildDirectory.dir("backfilaSqlDelight/${backfill.name}/kotlin") | ||
val packageProvider = backfill.backfill.map { | ||
val fullDatabaseName = it.database | ||
fullDatabaseName.substring(0, fullDatabaseName.lastIndexOf('.')) | ||
} | ||
val databaseClassNameProvider = backfill.backfill.map { | ||
val fullDatabaseName = it.database | ||
fullDatabaseName.substring(fullDatabaseName.lastIndexOf('.') + 1) | ||
} | ||
|
||
val sqlTask = target.tasks.register( | ||
"generateBackfilaRecordSourceSql${backfill.name.replaceFirstChar { it.uppercase() }}", | ||
GenerateBackfilaRecordSourceSqlTask::class.java, | ||
) { | ||
it.backfill.set(backfill.backfill) | ||
it.sqlDirectory.set(baseSqlDirectory.map { baseDir -> baseDir.dir(packageProvider.get()) }) | ||
} | ||
|
||
val sqlDelightExtension = target.extensions.findByType(app.cash.sqldelight.gradle.SqlDelightExtension::class.java) | ||
check(sqlDelightExtension != null) { | ||
"The Backfila gradle plugin requires the SqlDelight gradle plugin to function." | ||
} | ||
|
||
sqlDelightExtension.databases.all { sqlDelightDatabase -> | ||
sqlDelightDatabase.srcDirs.from( | ||
baseSqlDirectory.map<List<Directory>> { sqlDirectory -> | ||
if (packageProvider.get() == sqlDelightDatabase.packageName.get() && databaseClassNameProvider.get() == sqlDelightDatabase.name) { | ||
listOf(sqlDirectory) | ||
} else { | ||
listOf() | ||
} | ||
}, | ||
) | ||
} | ||
|
||
val kotlinTask = target.tasks.register( | ||
"generateBackfilaRecordSourceQueries${backfill.name.replaceFirstChar { it.uppercase() }}", | ||
GenerateBackfilaRecordSourceQueriesTask::class.java, | ||
) { | ||
it.dependsOn(sqlTask) | ||
it.backfill.set(backfill.backfill) | ||
it.packageName.set(packageProvider) | ||
it.kotlinDirectory.set(baseKotlinDirectory.map { baseDir -> baseDir.dir(packageProvider.get()) }) | ||
} | ||
|
||
target.kotlinExtension.sourceSets.getByName("main").kotlin.srcDir(kotlinTask) | ||
target.dependencies.add("implementation", "app.cash.backfila:client-sqldelight:$VERSION") | ||
} | ||
} | ||
} | ||
|
||
abstract class BackfilaSqlDelightExtension { | ||
abstract val backfills: NamedDomainObjectContainer<SqlDelightRecordSourceEntry> | ||
|
||
fun addRecordSource( | ||
name: String, | ||
database: String, | ||
tableName: String, | ||
keyName: String, | ||
keyType: String, | ||
recordColumns: String, | ||
recordType: String, | ||
whereClause: String = "1 = 1", | ||
) { | ||
backfills.create(name) { | ||
it.backfill.set( | ||
SqlDelightRecordSource( | ||
name = name, | ||
database = database, | ||
tableName = tableName, | ||
keyName = keyName, | ||
keyType = keyType, | ||
recordColumns = recordColumns, | ||
recordType = recordType, | ||
whereClause = whereClause, | ||
), | ||
) | ||
} | ||
} | ||
} | ||
|
||
abstract class SqlDelightRecordSourceEntry(val name: String) { | ||
abstract val backfill: Property<SqlDelightRecordSource> | ||
} | ||
|
||
data class SqlDelightRecordSource( | ||
val name: String, | ||
val database: String, | ||
val tableName: String, | ||
val keyName: String, // TODO: Eventually also support compound keys. | ||
val keyType: String, // TODO: Get this information directly from SQLDelight | ||
val recordColumns: String, | ||
val recordType: String, // TODO: Get this information directly from SQLDelight | ||
val whereClause: String, | ||
) : Serializable |
Oops, something went wrong.