Skip to content

Commit

Permalink
mjml: avoid Extension as task input
Browse files Browse the repository at this point in the history
  • Loading branch information
larsgrefer committed Aug 27, 2023
1 parent f9ba13e commit 2ed21b8
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 51 deletions.
2 changes: 1 addition & 1 deletion examples/mjml/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ mjml {
minifyOptions = "{\"minifyCSS\": true, \"removeEmptyAttributes\": false}"
juiceOptions = "{\"preserveImportant\": true}"
juicePreserveTags = "{\"myTag\": { \"start\": \"<#\", \"end\": \"</#\" }}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,9 @@ import org.gradle.api.Project
* @author Dennis Fricke
*/
class MjmlBasePlugin : Plugin<Project> {
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)

Expand All @@ -29,6 +24,13 @@ class MjmlBasePlugin : Plugin<Project> {
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)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,102 @@ 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<DefaultProjectApiHelper>()
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<Boolean>

@get:Input
@get:Optional
abstract val minify: Property<Boolean>

@get:Input
@get:Optional
abstract val juicePreserveTags: Property<String>

@get:Input
@get:Optional
abstract val minifyOptions: Property<String>

@get:Input
@get:Optional
abstract val juiceOptions: Property<String>

@get:Input
@get:Optional
abstract val validationLevel: Property<String>

@TaskAction
fun compileMjml() {
fileSystemOperations.delete {
delete(destinationDir)
}

val projectHelper: DefaultProjectApiHelper = objects.newInstance<DefaultProjectApiHelper>()

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<String> = mutableListOf("mjml", file.absolutePath, "-o", absolutOutputFile.absolutePath, *buildArgs())
val nodeExecConfiguration = NodeExecConfiguration(fullCommand, emptyMap(), buildDirectory, false, null)
val fullCommand: MutableList<String> =
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")
}
}
}


private fun buildArgs(): Array<out String> {
val result = mutableListOf<String>()
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<String>.add(vararg args: String) = addAll(args)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,8 @@ abstract class MjmlExtension {
abstract val juiceOptions: Property<String>
abstract val juicePreserveTags: Property<String>
abstract val validationMode: Property<ValidationMode>

init {
minify.convention(false)
beautify.convention(false)
minifyOptions.convention("")
juiceOptions.convention("")
juicePreserveTags.convention("")
validationMode.convention(ValidationMode.normal)
}
}

enum class ValidationMode {
strict, normal, skip
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MjmlJavaPlugin : Plugin<Project> {
val mjmlCompileProvider: TaskProvider<MjmlCompile> =
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"
}
Expand Down

0 comments on commit 2ed21b8

Please sign in to comment.