From fdc8d34f8274d0135ecb856b189129c30337eb69 Mon Sep 17 00:00:00 2001 From: Hubert Van De Walle Date: Mon, 26 Oct 2020 22:14:01 +0100 Subject: [PATCH] Move postcss purge config to gradle --- .../src/main/kotlin/be/simplenotes/CssTask.kt | 41 +++++++++++++++---- css/tailwind.config.js | 2 +- .../kotlin/be/simplenotes/views/NoteView.kt | 4 +- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/buildSrc/src/main/kotlin/be/simplenotes/CssTask.kt b/buildSrc/src/main/kotlin/be/simplenotes/CssTask.kt index 93d5533..870707d 100644 --- a/buildSrc/src/main/kotlin/be/simplenotes/CssTask.kt +++ b/buildSrc/src/main/kotlin/be/simplenotes/CssTask.kt @@ -2,9 +2,8 @@ package be.simplenotes import org.gradle.api.DefaultTask import org.gradle.api.GradleException -import org.gradle.api.tasks.InputDirectory -import org.gradle.api.tasks.OutputDirectory -import org.gradle.api.tasks.TaskAction +import org.gradle.api.tasks.* +import org.gradle.kotlin.dsl.getByType import java.io.File import java.lang.ProcessBuilder.Redirect.PIPE import java.util.concurrent.TimeUnit @@ -12,31 +11,57 @@ import kotlin.concurrent.thread open class CssTask : DefaultTask() { - private val root = project.file(".").parent + private val root = project.parent!!.rootDir + + private val viewsProject = project + .parent + ?.project(":simplenotes-views") + ?: error("Missing :simplenotes-views") @get:InputDirectory - val templatesDir = File(root, "simplenotes-views/src/main/kotlin/be/simplenotes/views") + val templatesDir = viewsProject.extensions + .getByType() + .asMap.getOrElse("main") { error("main sources not found") } + .allSource.srcDirs + .find { it.endsWith("kotlin") } + ?: error("kotlin sources not found") + + private val yarnRoot = File(project.rootDir, "css") @get:InputDirectory val postCssDir = File(project.rootDir, "css/src") + @get:InputFiles + val postCssConfig = listOf( + "tailwind.config.js", + "postcss.config.js", + "package.json" + ).map { File(yarnRoot, it) } + @get:OutputDirectory val outputRootDir = File(project.buildDir, "generated-resources/css") - private val yarnRoot = File(project.rootDir, "css") private val cssIndex = File(postCssDir, "styles.pcss") private val cssOutput = File(outputRootDir, "static/styles.css") private val manifestOutput = File(outputRootDir, "css-manifest.json") + private val purgeGlob = "$templatesDir/**/*.kt" + @TaskAction fun generateCss() { // TODO: auto yarn install ? - outputRootDir.listFiles()?.let { it.forEach { it.delete() } } + outputRootDir.deleteRecursively() ProcessBuilder("yarn", "run", "postcss", "build", "$cssIndex", "--output", "$cssOutput") - .apply { environment()["MANIFEST"] = "$manifestOutput" } + .apply { + environment().let { + it["MANIFEST"] = "$manifestOutput" + it["NODE_ENV"] = "production" + it["PURGE"] = purgeGlob + } + } .redirectOutput(PIPE) .redirectError(PIPE) .directory(yarnRoot) diff --git a/css/tailwind.config.js b/css/tailwind.config.js index 89b9bb2..614a2c8 100644 --- a/css/tailwind.config.js +++ b/css/tailwind.config.js @@ -1,7 +1,7 @@ module.exports = { purge: { content: [ - '../simplenotes-app/src/main/kotlin/be/simplenotes/app/views/**/*.kt' + process.env.PURGE ] }, theme: { diff --git a/simplenotes-views/src/main/kotlin/be/simplenotes/views/NoteView.kt b/simplenotes-views/src/main/kotlin/be/simplenotes/views/NoteView.kt index c14b720..0c9e944 100644 --- a/simplenotes-views/src/main/kotlin/be/simplenotes/views/NoteView.kt +++ b/simplenotes-views/src/main/kotlin/be/simplenotes/views/NoteView.kt @@ -184,8 +184,8 @@ class NoteView(styles: String) : View(styles) { type = ButtonType.submit, name = if (note.public) "private" else "public", classes = "font-semibold border-b-4 " + - if (!note.public) "border-teal-200" else "border-green-500" + - " p-2 rounded-r bg-teal-200 text-gray-800" + (if (!note.public) "border-teal-200" else "border-green-500") + + " p-2 rounded-r bg-teal-200 text-gray-800" ) { +"Public" }