import org.example.*

plugins {
    id 'base'
}

task processTemplates(type: ProcessTemplates) {
    templateEngine = TemplateEngineType.FREEMARKER
    sourceFiles = fileTree("src/templates")
    templateData = new TemplateData("test", [year: 2012])
    outputDir = file("$buildDir/genOutput")
}

// tag::ad-hoc-task[]
task processTemplatesAdHoc {
    inputs.property("engine", TemplateEngineType.FREEMARKER)
    inputs.files(fileTree("src/templates"))
        .withPropertyName("sourceFiles")
        .withPathSensitivity(PathSensitivity.RELATIVE)
    inputs.property("templateData.name", "docs")
    inputs.property("templateData.variables", [year: 2013])
    outputs.dir("$buildDir/genOutput2")
        .withPropertyName("outputDir")

    doLast {
        // Process the templates here
// end::ad-hoc-task[]
        copy {
            into "$buildDir/genOutput2"
            from fileTree("src/templates")
            expand([year: 2013])
        }

// tag::ad-hoc-task[]
    }
}
// end::ad-hoc-task[]

// tag::custom-class-runtime-api[]
task processTemplatesRuntime(type: ProcessTemplatesNoAnnotations) {
    templateEngine = TemplateEngineType.FREEMARKER
    sourceFiles = fileTree("src/templates")
    templateData = new TemplateData("test", [year: 2014])
    outputDir = file("$buildDir/genOutput3")

    inputs.property("engine", templateEngine)
    inputs.files(sourceFiles)
        .withPropertyName("sourceFiles")
        .withPathSensitivity(PathSensitivity.RELATIVE)
    inputs.property("templateData.name", templateData.name)
    inputs.property("templateData.variables", templateData.variables)
    outputs.dir(outputDir)
        .withPropertyName("outputDir")
}
// end::custom-class-runtime-api[]

// tag::runtime-api-conf[]
task processTemplatesRuntimeConf(type: ProcessTemplatesNoAnnotations) {
    // ...
// end::runtime-api-conf[]
    templateEngine = TemplateEngineType.FREEMARKER
    templateData = new TemplateData("test", [year: 2014])
    outputDir = file("$buildDir/genOutput3")
// tag::runtime-api-conf[]
    sourceFiles = fileTree("src/templates") {
        include "**/*.fm"
    }

    inputs.files(sourceFiles)
        .skipWhenEmpty()
        .withPropertyName("sourceFiles")
        .withPathSensitivity(PathSensitivity.RELATIVE)
    // ...
// end::runtime-api-conf[]
    inputs.property("engine",templateEngine)
    inputs.property("templateData.name", templateData.name)
    inputs.property("templateData.variables", templateData.variables)
    outputs.dir(outputDir)
        .withPropertyName("outputDir")
// tag::runtime-api-conf[]
}
// end::runtime-api-conf[]

// tag::inferred-task-dep-via-outputs[]
task packageFiles(type: Zip) {
    from processTemplates.outputs
}
// end::inferred-task-dep-via-outputs[]

// tag::inferred-task-dep-via-task[]
task packageFiles2(type: Zip) {
    from processTemplates
}
// end::inferred-task-dep-via-task[]


// tag::adhoc-destroyable-task[]
task removeTempDir {
    destroyables.register("$projectDir/tmpDir")
    doLast {
        delete("$projectDir/tmpDir")
    }
}
// end::adhoc-destroyable-task[]

build.dependsOn processTemplates, processTemplatesAdHoc, processTemplatesRuntime, processTemplatesRuntimeConf
