From de3e1d1d7b99c9f3d3fe4cddd30b38475394041e Mon Sep 17 00:00:00 2001 From: Dennis Tsar <45399002+DennisTsar@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:32:11 -0400 Subject: [PATCH] Move user-defined `index.html` validation to `ProcessResources` task This makes `kobwebStart` compatible with configuration cache. --- .../application/KobwebApplicationPlugin.kt | 20 +++++++++++++ .../tasks/KobwebGenerateSiteIndexTask.kt | 29 ------------------- 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/tools/gradle-plugins/application/src/main/kotlin/com/varabyte/kobweb/gradle/application/KobwebApplicationPlugin.kt b/tools/gradle-plugins/application/src/main/kotlin/com/varabyte/kobweb/gradle/application/KobwebApplicationPlugin.kt index 50860c883..ff22b5949 100644 --- a/tools/gradle-plugins/application/src/main/kotlin/com/varabyte/kobweb/gradle/application/KobwebApplicationPlugin.kt +++ b/tools/gradle-plugins/application/src/main/kotlin/com/varabyte/kobweb/gradle/application/KobwebApplicationPlugin.kt @@ -45,6 +45,7 @@ import com.varabyte.kobweb.gradle.core.tasks.KobwebTask import com.varabyte.kobweb.gradle.core.util.configureHackWorkaroundSinceWebpackTaskIsBrokenInContinuousMode import com.varabyte.kobweb.gradle.core.util.getResourceSources import com.varabyte.kobweb.gradle.core.util.getTransitiveJsDependencyResults +import com.varabyte.kobweb.gradle.core.util.isDescendantOf import com.varabyte.kobweb.gradle.core.util.kobwebCacheFile import com.varabyte.kobweb.gradle.core.util.namedOrNull import com.varabyte.kobweb.project.KobwebFolder @@ -64,8 +65,10 @@ import org.gradle.api.tasks.TaskProvider import org.gradle.build.event.BuildEventsListenerRegistry import org.gradle.jvm.tasks.Jar import org.gradle.kotlin.dsl.extra +import org.gradle.kotlin.dsl.named import org.gradle.kotlin.dsl.register import org.gradle.kotlin.dsl.withType +import org.gradle.language.jvm.tasks.ProcessResources import org.gradle.tooling.events.FailureResult import org.jetbrains.kotlin.gradle.targets.js.ir.KotlinJsIrTarget import org.jetbrains.kotlin.gradle.targets.js.webpack.KotlinWebpack @@ -259,6 +262,23 @@ class KobwebApplicationPlugin @Inject constructor( project.setupKspJs(jsTarget, appBlock.cssPrefix) + project.tasks.named(jsTarget.processResources) { + inputs.property("kobweb.publicPath", kobwebBlock.publicPath) + inputs.property("kobweb.genDir", kobwebBlock.app.genDir) + // No one should define their own root `public/index.html` files anywhere in their resources. + val generatedRoot = project.layout.buildDirectory.dir(kobwebBlock.app.genDir).get().asFile + val projectDir = project.projectDir + filesMatching("${kobwebBlock.publicPath.get()}/index.html") { + if (!file.isDescendantOf(generatedRoot)) { + throw GradleException( + "You are not supposed to define the root index file yourself. Kobweb provides its own. " + + "Use the `kobweb.app.index { ... }` block if you need to modify the generated index file. " + + "Problematic file: ${file.relativeToOrSelf(projectDir)}" + ) + } + } + } + val kobwebGenSiteEntryTask = project.tasks.register( "kobwebGenSiteEntry", appBlock, diff --git a/tools/gradle-plugins/application/src/main/kotlin/com/varabyte/kobweb/gradle/application/tasks/KobwebGenerateSiteIndexTask.kt b/tools/gradle-plugins/application/src/main/kotlin/com/varabyte/kobweb/gradle/application/tasks/KobwebGenerateSiteIndexTask.kt index 9f17bcdbb..b6723b69b 100644 --- a/tools/gradle-plugins/application/src/main/kotlin/com/varabyte/kobweb/gradle/application/tasks/KobwebGenerateSiteIndexTask.kt +++ b/tools/gradle-plugins/application/src/main/kotlin/com/varabyte/kobweb/gradle/application/tasks/KobwebGenerateSiteIndexTask.kt @@ -11,7 +11,6 @@ import com.varabyte.kobweb.gradle.core.metadata.LibraryIndexMetadata import com.varabyte.kobweb.gradle.core.metadata.LibraryMetadata import com.varabyte.kobweb.gradle.core.util.HtmlUtil import com.varabyte.kobweb.gradle.core.util.hasDependencyNamed -import com.varabyte.kobweb.gradle.core.util.isDescendantOf import com.varabyte.kobweb.gradle.core.util.searchZipFor import com.varabyte.kobweb.ksp.KOBWEB_METADATA_INDEX import com.varabyte.kobweb.ksp.KOBWEB_METADATA_LIBRARY @@ -23,7 +22,6 @@ import kotlinx.html.head import kotlinx.html.link import kotlinx.html.unsafe import kotlinx.serialization.json.Json -import org.gradle.api.GradleException import org.gradle.api.artifacts.result.ResolvedDependencyResult import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.RegularFile @@ -59,21 +57,6 @@ abstract class KobwebGenerateSiteIndexTask @Inject constructor( @get:Nested val indexBlock: AppBlock.IndexBlock = appBlock.index - // No one should define their own root `public/index.html` files anywhere in their resources. - @InputFiles - fun getUserDefinedRootIndexFiles() = - getResourceFilesJsWithRoots() - .mapNotNull { rootAndFile -> - rootAndFile.takeIf { - // Ignore files that are in our build directory, since we put them there. We are looking for index - // files explicitly added by a user, often because they don't realize that Kobweb generates one yet. - !it.file.isDescendantOf(projectLayout.buildDirectory.asFile.get()) - && it.relativeFile.invariantSeparatorsPath == "public/index.html" - } - } - .map { it.file } - .toList() - @get:InputFiles abstract val compileClasspath: ConfigurableFileCollection @@ -117,18 +100,6 @@ abstract class KobwebGenerateSiteIndexTask @Inject constructor( }) } - getUserDefinedRootIndexFiles() - .takeIf { it.isNotEmpty() } - ?.let { externalIndexFiles -> - throw GradleException( - "You are not supposed to define the root index file yourself. Kobweb provides its own. Use the `kobweb.app.index { ... }` block if you need to modify the generated index file. Problematic file(s): ${ - externalIndexFiles.joinToString( - ", " - ) { it.absoluteFile.toRelativeString(projectLayout.projectDirectory.asFile) } - }" - ) - } - compileClasspath.forEach { file -> var libraryMetadata: LibraryMetadata? = null