app.cash.sqldelight.gradle.GenerateSchemaTask.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-plugin Show documentation
Show all versions of gradle-plugin Show documentation
Gradle plugin for generating kotlin interfaces for sqlite files
package app.cash.sqldelight.gradle
import app.cash.sqldelight.VERSION
import app.cash.sqldelight.core.SqlDelightCompilationUnit
import app.cash.sqldelight.core.SqlDelightDatabaseProperties
import app.cash.sqldelight.core.SqlDelightEnvironment
import app.cash.sqldelight.core.lang.SqlDelightQueriesFile
import app.cash.sqldelight.core.lang.util.forInitializationStatements
import app.cash.sqldelight.dialect.api.SqlDelightDialect
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.FileTree
import org.gradle.api.provider.Property
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.IgnoreEmptyDirectories
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.SkipWhenEmpty
import org.gradle.api.tasks.TaskAction
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import java.io.File
import java.sql.Connection
import java.sql.DriverManager
import java.sql.SQLException
import java.util.ServiceLoader
@CacheableTask
abstract class GenerateSchemaTask : SqlDelightWorkerTask() {
@Suppress("unused")
// Required to invalidate the task on version updates.
@Input
val pluginVersion = VERSION
@get:OutputDirectory
var outputDirectory: File? = null
@get:Input abstract val projectName: Property
@get:Nested abstract var properties: SqlDelightDatabasePropertiesImpl
@get:Nested abstract var compilationUnit: SqlDelightCompilationUnitImpl
@Input var verifyMigrations: Boolean = false
@TaskAction
fun generateSchemaFile() {
workQueue().submit(GenerateSchema::class.java) {
it.outputDirectory.set(outputDirectory)
it.moduleName.set(projectName)
it.properties.set(properties)
it.verifyMigrations.set(verifyMigrations)
it.compilationUnit.set(compilationUnit)
}
}
@InputFiles
@SkipWhenEmpty
@IgnoreEmptyDirectories
@PathSensitive(PathSensitivity.RELATIVE)
override fun getSource(): FileTree {
return super.getSource()
}
interface GenerateSchemaWorkParameters : WorkParameters {
val outputDirectory: DirectoryProperty
val moduleName: Property
val properties: Property
val compilationUnit: Property
val verifyMigrations: Property
}
abstract class GenerateSchema : WorkAction {
private val sourceFolders: List
get() = parameters.compilationUnit.get().sourceFolders.map { it.folder }
override fun execute() {
val environment = SqlDelightEnvironment(
sourceFolders = sourceFolders.filter { it.exists() },
dependencyFolders = emptyList(),
moduleName = parameters.moduleName.get(),
properties = parameters.properties.get(),
verifyMigrations = parameters.verifyMigrations.get(),
compilationUnit = parameters.compilationUnit.get(),
dialect = ServiceLoader.load(SqlDelightDialect::class.java).findFirst().get(),
)
var maxVersion = 1
environment.forMigrationFiles { migrationFile ->
maxVersion = maxOf(maxVersion, migrationFile.version + 1)
}
val outputDirectory = parameters.outputDirectory.get().asFile
File("$outputDirectory/$maxVersion.db").apply {
if (exists()) delete()
}
createConnection("$outputDirectory/$maxVersion.db").use { connection ->
val sourceFiles = ArrayList()
environment.forSourceFiles { file ->
if (file is SqlDelightQueriesFile) sourceFiles.add(file)
}
sourceFiles.forInitializationStatements(
environment.dialect.allowsReferenceCycles,
) { sqlText ->
connection.prepareStatement(sqlText).execute()
}
}
}
private fun createConnection(path: String): Connection {
return try {
DriverManager.getConnection("jdbc:sqlite:$path")
} catch (e: SQLException) {
DriverManager.getConnection("jdbc:sqlite:$path")
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy