Skip to content

Commit

Permalink
Move user-defined index.html validation to ProcessResources task
Browse files Browse the repository at this point in the history
This makes `kobwebStart` compatible with configuration cache.
  • Loading branch information
DennisTsar authored and bitspittle committed Jun 26, 2024
1 parent 4d561bf commit de3e1d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -259,6 +262,23 @@ class KobwebApplicationPlugin @Inject constructor(

project.setupKspJs(jsTarget, appBlock.cssPrefix)

project.tasks.named<ProcessResources>(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<KobwebGenerateSiteEntryTask>(
"kobwebGenSiteEntry",
appBlock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit de3e1d1

Please sign in to comment.