Skip to content

Commit

Permalink
#51: Provide a gradle plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kwiens12357 committed May 26, 2023
1 parent 684e6ef commit 846e6fc
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 3 deletions.
14 changes: 13 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
plugins {
java
`maven-publish`
`java-gradle-plugin`
id("com.diffplug.spotless") version "6.12.0"
id("org.jetbrains.kotlin.jvm") version "1.8.10"
id("org.jlleitschuh.gradle.ktlint") version "11.2.0"
Expand All @@ -22,9 +23,11 @@ java.sourceCompatibility = JavaVersion.VERSION_17

dependencies {
implementation("org.camunda.bpm.model:camunda-bpmn-model:7.18.0")
compileOnly("org.apache.maven.plugin-tools:maven-plugin-annotations:3.7.1")
implementation("org.freemarker:freemarker:2.3.32")
implementation("org.slf4j:slf4j-api:2.0.7")
dokkaHtmlPlugin("org.jetbrains.dokka:kotlin-as-java-plugin:1.7.20")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.2")
testImplementation("org.junit.jupiter:junit-jupiter-engine:5.9.2")
}

publishing {
Expand Down Expand Up @@ -58,3 +61,12 @@ configure<com.diffplug.gradle.spotless.SpotlessExtension> {
ktlint()
}
}

gradlePlugin {
plugins {
create("CamundaBpmnDocumentationGenerator") {
id = "info.novatec.cbdg"
implementationClass = "info.novatec.cbdg.plugin.CamundaBpmnDocumentationGenerator"
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fun main() {
val bpmnFile = "TestDiagram.bpmn"
val bpmnFileAsPng = "TestDiagram.png"

val bpmnObject = BpmnParser.parseBpmnFile(
val bpmnObject = BpmnParser().parseBpmnFile(
File(bpmnPath + bpmnFile),
bpmnPath + bpmnFileAsPng
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package info.novatec.cbdg.plugin

import info.novatec.docu.generator.DocuGenerator
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.provider.Property
import java.io.File

interface CamundaBpmnDocumentationGeneratorConfiguration {
val templateFile: Property<File>
val camundaBpmnDir: Property<File>
val resultOutputDir: Property<File>
val bpmnDiagramImageDir: Property<File>?
}

class CamundaBpmnDocumentationGenerator : Plugin<Project> {

override fun apply(project: Project) {
val configuration = project.extensions.create("cbdg", CamundaBpmnDocumentationGeneratorConfiguration::class.java)
configuration.templateFile.convention(project.layout.buildDirectory.file("resources/main/templates/default.ftl").get().asFile)
configuration.camundaBpmnDir.convention(project.layout.buildDirectory.dir("resources/main/bpmn").get().asFile)
configuration.resultOutputDir.convention(project.layout.buildDirectory.dir("cbdg/html").get().asFile)
configuration.bpmnDiagramImageDir?.convention(project.layout.buildDirectory.dir("resources/main/images").get().asFile)

project.task("generate").doLast {
DocuGenerator().parseAndGenerate(
configuration.templateFile.get(),
configuration.camundaBpmnDir.get(),
configuration.resultOutputDir.get(),
configuration.bpmnDiagramImageDir?.get()
)
}
}
}
58 changes: 58 additions & 0 deletions src/main/kotlin/info/novatec/docu/generator/DocuGenerator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package info.novatec.docu.generator

import info.novatec.cbdg.FreeMarkerService
import info.novatec.docu.parser.main.BpmnParser
import org.slf4j.LoggerFactory
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import kotlin.io.path.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.exists

class DocuGenerator {

fun parseAndGenerate(
templateFile: File,
camundaBpmnDir: File,
resultOutputDir: File,
bpmnDiagramImageDir: File? = null
) {
if (templateFile.name.equals("default.ftl")) {
FileOutputStream(templateFile, false).use {
javaClass.classLoader.getResourceAsStream("templates/default.ftl")
?.transferTo(it) ?: throw FileNotFoundException("templates/default.ftl don't exist.")
}
}

val log = LoggerFactory.getLogger("cbdg")
val parser = BpmnParser()

camundaBpmnDir.listFiles()?.forEach {
log.info("Generating documentation for file ${it.absolutePath}")
log.info("Using template ${templateFile.absolutePath}")

val imageSrcPath = Path("${bpmnDiagramImageDir?.absolutePath}/${it.nameWithoutExtension}.png")
val imageTargetPath = Path("${resultOutputDir.absolutePath}/images/${it.nameWithoutExtension}.png")
imageTargetPath.parent.createDirectories()
if (imageSrcPath.exists()) {
Files.copy(imageSrcPath, imageTargetPath, StandardCopyOption.REPLACE_EXISTING)
}

val bpmnObject = parser.parseBpmnFile(it, "${it.nameWithoutExtension}.png")
FreeMarkerService.writeTemplate(
bpmnObject,
templateFile.name,
"${resultOutputDir.absolutePath}/${it.nameWithoutExtension}.html"
) {
setDirectoryForTemplateLoading(templateFile.parentFile)
}
log.info("Output report into path ${resultOutputDir.absolutePath}")
} ?: throw FileNotFoundException("${camundaBpmnDir.absolutePath} don't exist.")
resultOutputDir.listFiles()?.forEach {
log.info("Output: " + it.absolutePath)
} ?: throw FileNotFoundException("${resultOutputDir.absolutePath} don't exist.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.camunda.bpm.model.bpmn.instance.FlowElement
import org.camunda.bpm.model.bpmn.instance.Process
import java.io.File

object BpmnParser {
class BpmnParser {

/**
* Parses a bpmn file into a BpmnObject.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package info.novatec.cbdg.plugin

import org.gradle.api.Project
import org.gradle.testfixtures.ProjectBuilder
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

class CamundaBpmnDocumentationGeneratorTest {

@Test
fun generateTest() {
val project: Project = ProjectBuilder.builder().build()

project.getPluginManager().apply("info.novatec.cbdg")

assertTrue(project.getPluginManager().hasPlugin("info.novatec.cbdg"))
assertNotNull(project.getTasks().getByName("generate"))
}
}

0 comments on commit 846e6fc

Please sign in to comment.