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

ck.wirespec.plugin.gradle.gradle.0.10.1.source-code.ConvertWirespecTask.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.compiler.core.emit.common.Emitter.Companion.firstToUpper
import community.flock.wirespec.openapi.v2.OpenApiV2Parser
import community.flock.wirespec.openapi.v3.OpenApiV3Parser
import community.flock.wirespec.plugin.Format
import community.flock.wirespec.plugin.Format.OpenApiV2
import community.flock.wirespec.plugin.Format.OpenApiV3
import community.flock.wirespec.plugin.Language
import community.flock.wirespec.plugin.PackageName
import community.flock.wirespec.plugin.mapEmitter
import community.flock.wirespec.plugin.writeToFiles
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option

abstract class ConvertWirespecTask : BaseWirespecTask() {

    @get:InputFile
    @get:Option(option = "input", description = "input directory")
    abstract val input: RegularFileProperty

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

    @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

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

    @TaskAction
    fun convert() {
        val packageNameValue = packageName.map { PackageName(it) }.get()
        val fileName = input.get().asFile.name.split("/")
            .last()
            .substringBeforeLast(".")
            .firstToUpper()

        val json = input.asFile.get().readText()

        val ast = when (format.get()) {
            OpenApiV2 -> OpenApiV2Parser.parse(json, strict.getOrElse(false))
            OpenApiV3 -> OpenApiV3Parser.parse(json, strict.getOrElse(false))
        }

        languages.get()
            .map { it.mapEmitter(packageNameValue, wirespecLogger) }
            .forEach { (emitter, ext, sharedData) ->
                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