Skip to content

Commit

Permalink
Avoid NPEs in Kotlin 1.7.20 (#3398)
Browse files Browse the repository at this point in the history
* Avoid NPEs in Kotlin 1.7.20

Same fixes borrowed from square/wire#2310

* Add missing changes
  • Loading branch information
ZacSweers authored and AlecKazakova committed Aug 5, 2022
1 parent e8d27da commit 034d3bc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Expand Up @@ -86,7 +86,7 @@ class SqlDelightDatabase(
private val generatedSourcesDirectory
get() = File(project.buildDir, "generated/sqldelight/code/$name")

private val sources by lazy { sources() }
private val sources by lazy { sources(project) }
private val dependencies = mutableListOf<SqlDelightDatabase>()

private var recursionGuard = false
Expand Down
Expand Up @@ -6,6 +6,7 @@ import com.android.build.gradle.BaseExtension
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.api.BaseVariant
import org.gradle.api.DomainObjectSet
import org.gradle.api.Project
import org.gradle.api.Task
import org.gradle.api.file.SourceDirectorySet
import org.gradle.api.provider.Provider
Expand All @@ -32,7 +33,7 @@ import java.io.File
*
* Multiplatform environment with android target (oh boy)
*/
internal fun SqlDelightDatabase.sources(): List<Source> {
internal fun SqlDelightDatabase.sources(project: Project): List<Source> {
// Multiplatform project.
project.extensions.findByType(KotlinMultiplatformExtension::class.java)?.let {
return it.sources()
Expand All @@ -45,7 +46,7 @@ internal fun SqlDelightDatabase.sources(): List<Source> {

// Android project.
project.extensions.findByName("android")?.let {
return (it as BaseExtension).sources()
return (it as BaseExtension).sources(project)
}

// Kotlin project.
Expand All @@ -55,7 +56,7 @@ internal fun SqlDelightDatabase.sources(): List<Source> {
type = KotlinPlatformType.jvm,
name = "main",
sourceSets = listOf("main"),
sourceDirectorySet = sourceSets.getByName("main").kotlin!!,
sourceDirectorySet = sourceSets.getByName("main").kotlin ?: project.objects.sourceDirectorySet("empty", "Empty kotlin source set"),
),
)
}
Expand Down Expand Up @@ -87,7 +88,7 @@ private fun KotlinMultiplatformExtension.sources(): List<Source> {
)
}

private fun BaseExtension.sources(): List<Source> {
private fun BaseExtension.sources(project: Project): List<Source> {
val variants: DomainObjectSet<out BaseVariant> = when (this) {
is AppExtension -> applicationVariants
is LibraryExtension -> libraryVariants
Expand All @@ -104,7 +105,7 @@ private fun BaseExtension.sources(): List<Source> {
name = variant.name,
variantName = variant.name,
sourceDirectorySet = sourceSets[variant.name]
?: throw IllegalStateException("Couldn't find ${variant.name} in $sourceSets"),
?: project.objects.sourceDirectorySet(variant.name, "Empty kotlin source set"),
sourceSets = variant.sourceSets.map { it.name },
registerGeneratedDirectory = { outputDirectoryProvider ->
variant.addJavaSourceFoldersToModel(outputDirectoryProvider.get())
Expand Down

0 comments on commit 034d3bc

Please sign in to comment.