``` abstract class AbstractSwaggerGenTask : Defaul...
# gradle
j
Copy code
abstract class AbstractSwaggerGenTask : DefaultTask() {
    init {
        group = "Swagger"
    }

    abstract fun configuration(): CodegenConfigurator

    @InputFile
    fun inputFile(): File =
        project.file(configuration().inputSpec)

    @OutputDirectory
    fun outputDir(): File =
        project.file(configuration().outputDir)

    /**
     * Guard against deleting the directory being passed.
     * I accidentally did this to the root project directory.
     */
    private fun validateDeleteOutputDir(againstDir: File) {
        if (outputDir() == againstDir) {
            throw GradleException("You probably don't want to delete this directory: $againstDir")
        }
    }

    @TaskAction
    fun swaggerCodeGen() {
        validateDeleteOutputDir(project.projectDir)
        validateDeleteOutputDir(project.rootProject.projectDir)

        // If the spec has changed then this file will have have changed.
        outputDir()
            .deleteRecursively()
        /*
         * Since the generator sets system properties we need to ensure that two tasks don't try
         * to have system properties set in the same JVM.
         * <https://github.com/swagger-api/swagger-codegen/issues/4788>
         */
        synchronized(this::class) {
            val config = configuration()
            DefaultGenerator()
                .opts(config.toClientOptInput())
                .generate()

            // Clean up the system environment variables that have been set by the code generator.
            // <https://github.com/swagger-api/swagger-codegen/issues/4788>
            config.systemProperties.keys.forEach { System.clearProperty(it) }
        }
    }
}