Skip to content

Commit

Permalink
Improve copying base-frontend files between subprojects (#2970)
Browse files Browse the repository at this point in the history
  • Loading branch information
aSemy committed May 25, 2023
1 parent f55e22d commit f182a0a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 48 deletions.
@@ -0,0 +1,31 @@
package org.jetbrains.conventions

import org.gradle.api.attributes.Usage.USAGE_ATTRIBUTE

/**
* Utility for sharing the Dokka HTML frontend files between subprojects in a safe, cacheable way.
*/

plugins {
id("org.jetbrains.conventions.base")
}

/** Apply a distinct attribute to the incoming/outgoing configuration */
fun AttributeContainer.dokkaHtmlFrontendFilesAttribute() =
attribute(USAGE_ATTRIBUTE, objects.named("org.jetbrains.dokka.html-frontend-files"))

// incoming configuration
val dokkaHtmlFrontendFiles by configurations.registering {
description = "Retrieve Dokka HTML frontend files from other subprojects"
isCanBeConsumed = false
isCanBeResolved = true
attributes { dokkaHtmlFrontendFilesAttribute() }
}

// outgoing configuration
val dokkaHtmlFrontendFilesElements by configurations.registering {
description = "Provide Dokka HTML frontend files to other subprojects"
isCanBeConsumed = true
isCanBeResolved = false
attributes { dokkaHtmlFrontendFilesAttribute() }
}
1 change: 0 additions & 1 deletion build.gradle.kts
Expand Up @@ -39,7 +39,6 @@ apiValidation {
// note that subprojects are ignored by their name, not their path https://github.com/Kotlin/binary-compatibility-validator/issues/16
ignoredProjects += setOf(
// NAME PATH
"search-component", // :plugins:search-component
"frontend", // :plugins:base:frontend

"kotlin-analysis", // :kotlin-analysis
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Expand Up @@ -41,6 +41,7 @@ node = "16.13.0"
gradlePlugin-shadow = "7.1.2"
gradlePlugin-nexusPublish = "1.1.0"
gradlePlugin-gradlePluginPublish = "0.20.0"
gradlePlugin-gradleNode = "3.5.1"

## Test
junit = "5.9.2"
Expand Down Expand Up @@ -109,3 +110,4 @@ kotlinx-binaryCompatibilityValidator = { id = "org.jetbrains.kotlinx.binary-comp
shadow = { id = "com.github.johnrengelman.shadow", version.ref = "gradlePlugin-shadow" }
gradlePublish = { id = "com.gradle.plugin-publish", version.ref = "gradlePlugin-gradlePluginPublish" }
nexusPublish = { id = "io.github.gradle-nexus.publish-plugin", version.ref = "gradlePlugin-nexusPublish" }
gradleNode = { id = "com.github.node-gradle.node", version.ref = "gradlePlugin-gradleNode" }
7 changes: 0 additions & 7 deletions plugins/base/.gitignore

This file was deleted.

50 changes: 23 additions & 27 deletions plugins/base/build.gradle.kts
Expand Up @@ -3,6 +3,7 @@ import org.jetbrains.registerDokkaArtifactPublication
plugins {
id("org.jetbrains.conventions.kotlin-jvm")
id("org.jetbrains.conventions.maven-publish")
id("org.jetbrains.conventions.dokka-html-frontend-files")
}

dependencies {
Expand Down Expand Up @@ -33,45 +34,40 @@ dependencies {
testImplementation(projects.core.testApi)
testImplementation(platform(libs.junit.bom))
testImplementation(libs.junit.jupiter)

dokkaHtmlFrontendFiles(projects.plugins.base.frontend) {
because("fetch frontend files from subproject :plugins:base:frontend")
}
}

val projectDistDir = project(":plugins:base:frontend").file("dist")
val generateFrontendFiles = tasks.getByPath(":plugins:base:frontend:generateFrontendFiles")
// access the frontend files via the dependency on :plugins:base:frontend
val dokkaHtmlFrontendFiles: Provider<FileCollection> =
configurations.dokkaHtmlFrontendFiles.map { frontendFiles ->
frontendFiles.incoming.artifacts.artifactFiles
}

val preparedokkaHtmlFrontendFiles by tasks.registering(Sync::class) {
description = "copy Dokka Base frontend files into the resources directory"

val copyJsFiles by tasks.registering(Copy::class) {
from(projectDistDir) {
from(dokkaHtmlFrontendFiles) {
include("*.js")
into("dokka/scripts")
}
dependsOn(generateFrontendFiles)
destinationDir =
File(sourceSets.main.get().resources.sourceDirectories.singleFile, "dokka/scripts")
}

val copyCssFiles by tasks.registering(Copy::class) {
from(projectDistDir) {
from(dokkaHtmlFrontendFiles) {
include("*.css")
into("dokka/styles")
}
dependsOn(generateFrontendFiles)
destinationDir =
File(sourceSets.main.get().resources.sourceDirectories.singleFile, "dokka/styles")
}

val copyFrontend by tasks.registering {
dependsOn(copyJsFiles, copyCssFiles)
into(layout.buildDirectory.dir("generated/src/main/resources"))
}

tasks {
processResources {
dependsOn(copyFrontend)
}

sourcesJar {
dependsOn(processResources)
}
sourceSets.main {
resources.srcDir(preparedokkaHtmlFrontendFiles.map { it.destinationDir })
}

test {
maxHeapSize = "4G"
}
tasks.test {
maxHeapSize = "4G"
}

registerDokkaArtifactPublication("dokkaBase") {
Expand Down
43 changes: 31 additions & 12 deletions plugins/base/frontend/build.gradle.kts
@@ -1,6 +1,10 @@
import com.github.gradle.node.npm.task.NpmTask
import org.jetbrains.kotlin.util.parseSpaceSeparatedArgs

@Suppress("DSL_SCOPE_VIOLATION") // fixed in Gradle 8.1 https://github.com/gradle/gradle/pull/23639
plugins {
base
id("com.github.node-gradle.node") version "3.2.1"
id("org.jetbrains.conventions.dokka-html-frontend-files")
alias(libs.plugins.gradleNode)
}

node {
Expand All @@ -11,19 +15,34 @@ node {
distBaseUrl.set(null as String?) // Strange cast to avoid overload ambiguity
}

val npmRunBuild = tasks.getByName("npm_run_build") {
inputs.dir(file("src/main"))
inputs.files(file("package.json"), file("webpack.config.js"))
outputs.dir(file("dist/"))
val distributionDirectory = layout.projectDirectory.dir("dist")

val npmRunBuild by tasks.registering(NpmTask::class) {
dependsOn(tasks.npmInstall)

npmCommand.set(parseSpaceSeparatedArgs("run build"))

inputs.dir("src/main")
inputs.files(
"package.json",
"webpack.config.js",
)

outputs.dir(distributionDirectory)
outputs.cacheIf { true }
}

task("generateFrontendFiles") {
dependsOn(npmRunBuild)
configurations.dokkaHtmlFrontendFilesElements.configure {
outgoing {
artifact(distributionDirectory) {
builtBy(npmRunBuild)
}
}
}

tasks {
clean {
delete(file("node_modules"), file("dist"))
}
tasks.clean {
delete(
file("node_modules"),
file("dist"),
)
}
1 change: 0 additions & 1 deletion settings.gradle.kts
Expand Up @@ -68,7 +68,6 @@ include(

":plugins:base",
":plugins:base:frontend",
":plugins:base:search-component",
":plugins:base:base-test-utils",
":plugins:all-modules-page",
":plugins:templating",
Expand Down

0 comments on commit f182a0a

Please sign in to comment.