All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.danilopianini.gradle.latex.LatexExtension.kt Maven / Gradle / Ivy

Go to download

A plugin for compiling LaTeX, inspired by https://github.com/csabasulyok/gradle-latex

There is a newer version: 0.2.7
Show newest version
package org.danilopianini.gradle.latex

import org.gradle.api.DefaultTask
import org.gradle.api.Project
import org.gradle.api.provider.Property
import org.gradle.kotlin.dsl.getValue
import org.gradle.kotlin.dsl.provideDelegate
import org.gradle.kotlin.dsl.register
import java.util.concurrent.TimeUnit
import java.io.Serializable

inline fun  Project.propertyWithDefault(default: T): Property =
    objects.property(T::class.java).apply { convention(default) }

inline fun  Project.propertyWithDefault(noinline default: () -> T): Property =
    objects.property(T::class.java).apply { convention(default()) }

/**
 * Gradle extension to create new dynamic tasks & maintain and manage latex artifacts.
 * Registered to Gradle as extension in LatexPlugin. Thereafter the instance can be accessed via project.latex
 *
 */
open class LatexExtension @JvmOverloads constructor(
    private val project: Project,
    // val auxDir: Property = project.propertyWithDefault(project.file(".gradle/latex-temp")),
    val terminalEmulator: Property = project.propertyWithDefault("bash"),
    val waitTime: Property = project.propertyWithDefault(1),
    val waitUnit: Property = project.propertyWithDefault(TimeUnit.MINUTES),
    val pdfLatexCommand: Property = project.propertyWithDefault("pdflatex"),
    val bibTexCommand: Property = project.propertyWithDefault("bibtex"),
    val inkscapeCommand: Property = project.propertyWithDefault("inkscape"),
    val gitLatexdiffCommand: Property = project.propertyWithDefault("git latexdiff")
) : Serializable {

    val runAll by project.tasks.register("buildLatex") {
        group = Latex.TASK_GROUP
        description = "Run all LaTeX tasks"
    }

    @JvmOverloads operator fun String.invoke(configuration: LatexDSL.() -> Unit = { }): LatexArtifact =
        LatexDSL(project, this)
            .also(configuration)
            .let { builder ->
                LatexArtifact(
                    this.replace("""[\/\\:<>"?\*|]""".toRegex(), "-"),
                    tex = project.file(with(builder.name) { if (endsWith(".tex")) this else "$this.tex" }),
                    pdf = builder.fileFromName("pdf"),
                    aux = builder.fileFromName("aux"),
                    bbl = builder.fileFromName("bbl"),
                    imageFiles = builder.images,
                    extraArgs = builder.extraArguments,
                    watching = builder.watching,
                    diffs = emptyList()
                )
            }
            .also { artifact ->
                project.logger.debug("Produced {}", artifact)
                // All compilation tasks of this document
                val buildName = "buildLatex.${artifact.name}"
                val completionTask by project.tasks.register(buildName, artifact)
                completionTask.description = "Builds LaTeX project ${artifact.name}"
                runAll.dependsOn(completionTask)
                // pdflatex, first run
                val pdfLatexTask by project.tasks.register("pdfLatex.${artifact.name}", artifact)
                completionTask.dependsOn(pdfLatexTask)
                // bibtex
                val bibTexTask by project.tasks.register("bibtex.${artifact.name}", artifact)
                bibTexTask.dependsOn(pdfLatexTask)
                // pdflatex, second run
                val pdfLatexPass2 by project.tasks.register(
                    "pdfLatexAfterBibtex.${artifact.name}",
                    artifact
                )
                pdfLatexPass2.dependsOn(bibTexTask)
                completionTask.dependsOn(pdfLatexPass2)
            }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy