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

ck.wirespec.plugin.gradle.gradle.0.10.1.source-code.CompileWirespecTask.kt Maven / Gradle / Ivy

There is a newer version: 0.10.5
Show newest version
package community.flock.wirespec.plugin.gradle

import community.flock.wirespec.plugin.FilesContent
import community.flock.wirespec.plugin.Language
import community.flock.wirespec.plugin.PackageName
import community.flock.wirespec.plugin.mapEmitter
import community.flock.wirespec.plugin.parse
import community.flock.wirespec.plugin.writeToFiles
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option

abstract class CompileWirespecTask : BaseWirespecTask() {

    @get:InputDirectory
    @get:Option(option = "input", description = "input directory")
    abstract val input: DirectoryProperty

    @get:Input
    @get:Option(option = "languages", description = "languages list")
    abstract val languages: ListProperty

    @get:Optional
    @get:Input
    @get:Option(option = "shared", description = "emit shared class")
    abstract val shared: Property

    @Internal
    protected fun getFilesContent(): FilesContent = input.asFileTree
        .map { it.name.split(".").first() to it.readText(Charsets.UTF_8) }
        .map { (name, reader) -> name to reader }

    @TaskAction
    fun action() {
        val packageNameValue = packageName.map { PackageName(it) }.get()
        val ast = getFilesContent().parse(wirespecLogger)
        languages.get()
            .map { it.mapEmitter(packageNameValue, wirespecLogger) }
            .forEach { (emitter, ext, sharedData) ->
                ast.forEach { (fileName, ast) ->
                    emitter.emit(ast).forEach {
                        it.writeToFiles(
                            output = output.asFile.get(),
                            packageName = packageNameValue,
                            shared = if (shared.getOrElse(true)) sharedData else null,
                            fileName = if (emitter.split) null else fileName,
                            ext = ext
                        )
                    }
                }
            }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy