Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support js only projects #3310

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

Merged
merged 2 commits into from
Jun 14, 2022
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
3 changes: 0 additions & 3 deletions drivers/sqljs-driver/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,3 @@ kotlin {
}

apply from: "$rootDir/gradle/gradle-mvn-push.gradle"

// https://github.com/Kotlin/dokka/issues/1455
tasks.getByName("dokkaGfm").dependsOn(tasks.getByName("build"))
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.gradle.api.artifacts.Dependency
import org.gradle.tooling.provider.model.ToolingModelBuilder
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSet
import java.util.concurrent.atomic.AtomicBoolean
Expand Down Expand Up @@ -65,6 +66,7 @@ abstract class SqlDelightPlugin : Plugin<Project> {
project.plugins.withId("org.jetbrains.kotlin.multiplatform", kotlinPluginHandler)
project.plugins.withId("org.jetbrains.kotlin.android", kotlinPluginHandler)
project.plugins.withId("org.jetbrains.kotlin.jvm", kotlinPluginHandler)
project.plugins.withId("org.jetbrains.kotlin.js", kotlinPluginHandler)
project.plugins.withId("kotlin2js", kotlinPluginHandler)

project.afterEvaluate {
Expand All @@ -81,6 +83,7 @@ abstract class SqlDelightPlugin : Plugin<Project> {
}

val isMultiplatform = project.plugins.hasPlugin("org.jetbrains.kotlin.multiplatform")
val isJsOnly = if (isMultiplatform) false else project.plugins.hasPlugin("org.jetbrains.kotlin.js")

val needsAsyncRuntime = extension.databases.any { it.generateAsync }
val runtimeDependencies = mutableListOf<Dependency>().apply {
Expand All @@ -97,6 +100,13 @@ abstract class SqlDelightPlugin : Plugin<Project> {
project.configurations.getByName(sourceSet.apiConfigurationName)
.dependencies.addAll(runtimeDependencies)
}
isJsOnly -> {
val sourceSets =
project.extensions.getByType(KotlinJsProjectExtension::class.java).sourceSets
val sourceSet = (sourceSets.getByName("main") as DefaultKotlinSourceSet)
project.configurations.getByName(sourceSet.apiConfigurationName)
.dependencies.addAll(runtimeDependencies)
}
else -> {
project.configurations.getByName("api").dependencies.addAll(runtimeDependencies)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.gradle.api.provider.Provider
import org.gradle.api.tasks.SourceSetContainer
import org.gradle.api.tasks.TaskContainer
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.dsl.KotlinJsProjectExtension
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.plugin.KotlinPlatformType
import java.io.File
Expand All @@ -37,6 +38,11 @@ internal fun SqlDelightDatabase.sources(): List<Source> {
return it.sources()
}

// kotlin.js only projects
project.extensions.findByType(KotlinJsProjectExtension::class.java)?.let {
return it.sources()
}

// Android project.
project.extensions.findByName("android")?.let {
return (it as BaseExtension).sources()
Expand All @@ -54,6 +60,17 @@ internal fun SqlDelightDatabase.sources(): List<Source> {
)
}

private fun KotlinJsProjectExtension.sources(): List<Source> {
return listOf(
Source(
type = KotlinPlatformType.js,
name = "main",
sourceDirectorySet = sourceSets.getByName("main").kotlin,
sourceSets = listOf("main"),
)
)
}

private fun KotlinMultiplatformExtension.sources(): List<Source> {
// For multiplatform we only support SQLDelight in commonMain - to support other source sets
// we would need to generate expect/actual SQLDelight code which at least right now doesn't
Expand Down
27 changes: 27 additions & 0 deletions sqldelight-gradle-plugin/src/test/kotlin-js/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
buildscript {
apply from: "${projectDir.absolutePath}/../buildscript.gradle"
}

apply plugin: 'org.jetbrains.kotlin.js'
apply plugin: 'app.cash.sqldelight'

repositories {
maven {
url "file://${projectDir.absolutePath}/../../../../build/localMaven"
}
mavenCentral()
}

kotlin {
js(IR) {
browser {
binaries.executable()
}
}
}

sqldelight {
Database {
packageName = "com.sample"
}
}
2 changes: 2 additions & 0 deletions sqldelight-gradle-plugin/src/test/kotlin-js/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
apply from: "../settings.gradle"

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
CREATE TABLE test (
value2 TEXT NOT NULL,
value TEXT NOT NULL
);

CREATE INDEX testIndex ON test(value);

CREATE TRIGGER testTrigger
AFTER DELETE ON test
BEGIN
INSERT INTO test VALUES ("1", "2");
END;

CREATE VIEW testView AS
SELECT value
FROM test;

select:
SELECT *
FROM test;
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ class PluginTest {
assertThat(result.output).contains("BUILD SUCCESSFUL")
}

@Test
fun `Applying the plugin works fine for js projects`() {
val runner = GradleRunner.create()
.withCommonConfiguration(File("src/test/kotlin-js"))

val result = runner
.withArguments("clean", "generateMainDatabaseInterface", "--stacktrace")
.build()
assertThat(result.output).contains("BUILD SUCCESSFUL")

// Assert the plugin added the common dependency
val dependenciesResult = runner
.withArguments("dependencies", "--stacktrace")
.build()
assertThat(dependenciesResult.output).contains("app.cash.sqldelight:runtime:")
}

@Test
fun `Applying the plugin works fine for multiplatform projects`() {
val runner = GradleRunner.create()
Expand Down