diff --git a/examples/mjml/build.gradle.kts b/examples/mjml/build.gradle.kts index dea2da5c..11e411aa 100644 --- a/examples/mjml/build.gradle.kts +++ b/examples/mjml/build.gradle.kts @@ -12,4 +12,4 @@ mjml { minifyOptions = "{\"minifyCSS\": true, \"removeEmptyAttributes\": false}" juiceOptions = "{\"preserveImportant\": true}" juicePreserveTags = "{\"myTag\": { \"start\": \"<#\", \"end\": \" { - var mjmlExtension: MjmlExtension? = null - private set - - private var project: Project? = null override fun apply(project: Project) { - this.project = project - mjmlExtension = project.extensions.create("mjml", MjmlExtension::class.java) + val mjmlExtension = project.extensions.create("mjml", MjmlExtension::class.java) project.plugins.apply(NodePlugin::class.java) @@ -29,6 +24,13 @@ class MjmlBasePlugin : Plugin { project.tasks.withType(MjmlCompile::class.java) .configureEach { this.dependsOn("installMjml") + this.workingDir.set(project.layout.buildDirectory) + this.validationLevel.convention(mjmlExtension.validationMode.map { m -> m.name.lowercase() }) + this.beautify.convention(mjmlExtension.beautify) + this.minify.convention(mjmlExtension.minify) + this.minifyOptions.convention(mjmlExtension.minifyOptions) + this.juiceOptions.convention(mjmlExtension.juiceOptions) + this.juicePreserveTags.convention(mjmlExtension.juicePreserveTags) } } -} \ No newline at end of file +} diff --git a/mjml-plugin/src/main/kotlin/io/freefair/gradle/plugins/mjml/MjmlCompile.kt b/mjml-plugin/src/main/kotlin/io/freefair/gradle/plugins/mjml/MjmlCompile.kt index cbfb1d0c..9910c45b 100644 --- a/mjml-plugin/src/main/kotlin/io/freefair/gradle/plugins/mjml/MjmlCompile.kt +++ b/mjml-plugin/src/main/kotlin/io/freefair/gradle/plugins/mjml/MjmlCompile.kt @@ -6,59 +6,87 @@ import com.github.gradle.node.npm.exec.NpmExecRunner import com.github.gradle.node.util.DefaultProjectApiHelper import com.github.gradle.node.variant.VariantComputer import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.FileSystemOperations import org.gradle.api.model.ObjectFactory -import org.gradle.api.tasks.CacheableTask -import org.gradle.api.tasks.Internal -import org.gradle.api.tasks.OutputDirectory -import org.gradle.api.tasks.SourceTask -import org.gradle.api.tasks.TaskAction +import org.gradle.api.provider.Property +import org.gradle.api.tasks.* import org.gradle.kotlin.dsl.newInstance -import java.io.File import javax.inject.Inject @CacheableTask abstract class MjmlCompile : SourceTask() { + @get:Inject abstract val objects: ObjectFactory - @get:Internal - val projectHelper: DefaultProjectApiHelper - - @get:Internal - val mjmlExtension: MjmlExtension + @get:Inject + abstract val fileSystemOperations: FileSystemOperations @get:Internal val nodeExtension: NodeExtension @get:Internal - val buildDirectory: File + abstract val workingDir: DirectoryProperty init { include("**/*.mjml") - projectHelper = project.objects.newInstance() - mjmlExtension = project.extensions.getByType(MjmlExtension::class.java) nodeExtension = project.extensions.getByType(NodeExtension::class.java) - buildDirectory = project.layout.buildDirectory.asFile.get().absoluteFile } - @OutputDirectory - abstract fun getDestinationDir(): DirectoryProperty + @get:OutputDirectory + abstract val destinationDir: DirectoryProperty + + @get:Input + @get:Optional + abstract val beautify: Property + + @get:Input + @get:Optional + abstract val minify: Property + + @get:Input + @get:Optional + abstract val juicePreserveTags: Property + + @get:Input + @get:Optional + abstract val minifyOptions: Property + + @get:Input + @get:Optional + abstract val juiceOptions: Property + + @get:Input + @get:Optional + abstract val validationLevel: Property @TaskAction fun compileMjml() { + fileSystemOperations.delete { + delete(destinationDir) + } + + val projectHelper: DefaultProjectApiHelper = objects.newInstance() + source.visit { - if (name.endsWith(".mjml")) { - val filePath = relativePath.pathString - val absolutOutputFile = getDestinationDir().file(filePath.replace(".mjml", ".html")).get().asFile - if(!absolutOutputFile.parentFile.exists()) { + if (!this.isDirectory && name.endsWith(".mjml")) { + val absolutOutputFile = destinationDir.file(path.replace(".mjml", ".html")).get().asFile + if (!absolutOutputFile.parentFile.exists()) { absolutOutputFile.parentFile.mkdirs() } - val fullCommand: MutableList = mutableListOf("mjml", file.absolutePath, "-o", absolutOutputFile.absolutePath, *buildArgs()) - val nodeExecConfiguration = NodeExecConfiguration(fullCommand, emptyMap(), buildDirectory, false, null) + val fullCommand: MutableList = + mutableListOf("mjml", file.absolutePath, "-o", absolutOutputFile.absolutePath, *buildArgs()) + val nodeExecConfiguration = + NodeExecConfiguration(fullCommand, emptyMap(), workingDir.get().asFile, false, null) val npmExecRunner = objects.newInstance(NpmExecRunner::class.java) - val result = npmExecRunner.executeNpxCommand(projectHelper, nodeExtension, nodeExecConfiguration, VariantComputer()) - if(result.exitValue != 0) throw Exception("Mjml failed") + val result = npmExecRunner.executeNpxCommand( + projectHelper, + nodeExtension, + nodeExecConfiguration, + VariantComputer() + ) + if (result.exitValue != 0) throw Exception("Mjml failed") } } } @@ -66,14 +94,14 @@ abstract class MjmlCompile : SourceTask() { private fun buildArgs(): Array { val result = mutableListOf() - result.add("-l", mjmlExtension.validationMode.get().name.lowercase()) - if(mjmlExtension.minify.get()) result.add("--config.minify", "true") - if(mjmlExtension.beautify.get()) result.add("--config.beautify", "true") - if(mjmlExtension.minifyOptions.get().isNotBlank()) result.add("--config.minifyOptions", mjmlExtension.minifyOptions.get()) - if(mjmlExtension.juiceOptions.get().isNotBlank()) result.add("--config.juiceOptions", mjmlExtension.juiceOptions.get()) - if(mjmlExtension.juicePreserveTags.get().isNotBlank()) result.add("--config.juicePreserveTags", mjmlExtension.juicePreserveTags.get()) + if (validationLevel.isPresent) result.add("--config.validationLevel", validationLevel.get()) + if (minify.isPresent) result.add("--config.minify", minify.get().toString()) + if (beautify.isPresent) result.add("--config.beautify", beautify.get().toString()) + if (minifyOptions.isPresent) result.add("--config.minifyOptions", minifyOptions.get()) + if (juiceOptions.isPresent) result.add("--config.juiceOptions", juiceOptions.get()) + if (juicePreserveTags.isPresent) result.add("--config.juicePreserveTags", juicePreserveTags.get()) return result.toTypedArray() } private fun MutableList.add(vararg args: String) = addAll(args) -} \ No newline at end of file +} diff --git a/mjml-plugin/src/main/kotlin/io/freefair/gradle/plugins/mjml/MjmlExtension.kt b/mjml-plugin/src/main/kotlin/io/freefair/gradle/plugins/mjml/MjmlExtension.kt index 2af60c61..37168df5 100644 --- a/mjml-plugin/src/main/kotlin/io/freefair/gradle/plugins/mjml/MjmlExtension.kt +++ b/mjml-plugin/src/main/kotlin/io/freefair/gradle/plugins/mjml/MjmlExtension.kt @@ -9,17 +9,8 @@ abstract class MjmlExtension { abstract val juiceOptions: Property abstract val juicePreserveTags: Property abstract val validationMode: Property - - init { - minify.convention(false) - beautify.convention(false) - minifyOptions.convention("") - juiceOptions.convention("") - juicePreserveTags.convention("") - validationMode.convention(ValidationMode.normal) - } } enum class ValidationMode { strict, normal, skip -} \ No newline at end of file +} diff --git a/mjml-plugin/src/main/kotlin/io/freefair/gradle/plugins/mjml/MjmlJavaPlugin.kt b/mjml-plugin/src/main/kotlin/io/freefair/gradle/plugins/mjml/MjmlJavaPlugin.kt index 0971dec3..ad1a8252 100644 --- a/mjml-plugin/src/main/kotlin/io/freefair/gradle/plugins/mjml/MjmlJavaPlugin.kt +++ b/mjml-plugin/src/main/kotlin/io/freefair/gradle/plugins/mjml/MjmlJavaPlugin.kt @@ -23,7 +23,7 @@ class MjmlJavaPlugin : Plugin { val mjmlCompileProvider: TaskProvider = target.tasks.register(taskName, MjmlCompile::class.java) { source = resources - getDestinationDir().set(outputDir) + destinationDir.set(outputDir) group = org.gradle.api.plugins.BasePlugin.BUILD_GROUP description = "Compile mjml files for the " + sourceSet.name + " source set" }