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

Prevent project is already evaluated error #574

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
36 changes: 26 additions & 10 deletions src/main/kotlin/com/autonomousapps/subplugin/ProjectPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import com.autonomousapps.internal.utils.toJson
import com.autonomousapps.model.SourceSetKind
import com.autonomousapps.services.InMemoryCache
import com.autonomousapps.tasks.*
import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project
import org.gradle.api.UnknownTaskException
import org.gradle.api.file.RegularFile
import org.gradle.api.internal.project.ProjectStateInternal
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.SourceSetContainer
Expand Down Expand Up @@ -142,7 +144,7 @@ internal class ProjectPlugin(private val project: Project) {
// We need the afterEvaluate so we can get a reference to the `KotlinCompile` tasks. This is due to use of the
// pluginManager.withPlugin API. Currently configuring the com.android.application plugin, not any Kotlin plugin.
// I do not know how to wait for both plugins to be ready.
afterEvaluate {
safeAfterEvaluate {
// If kotlin-android is applied, get the Kotlin source sets
val kotlinSourceSets = findKotlinSourceSets()

Expand Down Expand Up @@ -182,7 +184,7 @@ internal class ProjectPlugin(private val project: Project) {

/** Has the `com.android.library` plugin applied. */
private fun Project.configureAndroidLibProject() {
afterEvaluate {
safeAfterEvaluate {
// If kotlin-android is applied, get the Kotlin source sets
val kotlinSourceSets = findKotlinSourceSets()

Expand Down Expand Up @@ -278,11 +280,11 @@ internal class ProjectPlugin(private val project: Project) {
* If it is applied, this is a kotlin-jvm-app project. If it isn't, a java-jvm-app project.
*/
private fun Project.configureJavaAppProject(maybeSpringBoot: Boolean = false) {
afterEvaluate {
safeAfterEvaluate {
if (maybeSpringBoot) {
if (!pluginManager.hasPlugin(SPRING_BOOT_PLUGIN)) {
// This means we only discovered the java plugin, which isn't sufficient
return@afterEvaluate
return@safeAfterEvaluate
}
logger.log("Adding JVM tasks to ${project.path}")
}
Expand All @@ -293,7 +295,7 @@ internal class ProjectPlugin(private val project: Project) {
if (!pluginManager.hasPlugin(KOTLIN_JVM_PLUGIN)) {
if (configuredForJavaProject.getAndSet(true)) {
logger.info("(dependency analysis) $path was already configured")
return@afterEvaluate
return@safeAfterEvaluate
}

val sourceSets = the<SourceSetContainer>()
Expand Down Expand Up @@ -336,7 +338,7 @@ internal class ProjectPlugin(private val project: Project) {
* Has the `java-library` plugin applied.
*/
private fun Project.configureJavaLibProject() {
afterEvaluate {
safeAfterEvaluate {
val sourceSets = the<SourceSetContainer>()

val javaFiles = sourceSets.flatMap {
Expand All @@ -353,12 +355,12 @@ internal class ProjectPlugin(private val project: Project) {
if (configuredForKotlinJvmOrJavaLibrary.getAndSet(true)) {
logger.info("(dependency analysis) $path was already configured for the kotlin-jvm plugin")
redundantPlugin.configure()
return@afterEvaluate
return@safeAfterEvaluate
}

if (configuredForJavaProject.getAndSet(true)) {
logger.info("(dependency analysis) $path was already configured")
return@afterEvaluate
return@safeAfterEvaluate
}

val testSource = if (shouldAnalyzeTests()) sourceSets.findByName(SourceSet.TEST_SOURCE_SET_NAME) else null
Expand Down Expand Up @@ -427,7 +429,7 @@ internal class ProjectPlugin(private val project: Project) {
* If it isn't, this is a library project.
*/
private fun Project.configureKotlinJvmProject() {
afterEvaluate {
safeAfterEvaluate {
val kotlin = the<KotlinProjectExtension>()

val kotlinFiles = kotlin.sourceSets
Expand All @@ -445,7 +447,7 @@ internal class ProjectPlugin(private val project: Project) {
if (configuredForKotlinJvmOrJavaLibrary.getAndSet(true)) {
logger.info("(dependency analysis) $path was already configured for the java-library plugin")
redundantPlugin.configure()
return@afterEvaluate
return@safeAfterEvaluate
}

val mainSource = kotlin.sourceSets.findByName(SourceSet.MAIN_SOURCE_SET_NAME)
Expand Down Expand Up @@ -813,4 +815,18 @@ internal class ProjectPlugin(private val project: Project) {
subExtension!!.storeAdviceOutput(advice)
}
}

/**
* Hacky way to check dependent project is evaluated (taken from gradle sources)
*/
private fun Project.safeAfterEvaluate(action: Action<in Project>) {
val internalState = this.state as? ProjectStateInternal
val evaluated = internalState?.isUnconfigured == false && !internalState.isConfiguring

if (evaluated) {
action.execute(this)
} else {
afterEvaluate(action)
}
}
}