forked from loafle/openapi-generator-original
Cleanup outputDir before openApiGenerate (#13659)
* Cleanup outputdir before openApiGenerate * Add cleanupOutput property to GenerateTask
This commit is contained in:
parent
871eda2731
commit
906ec5dfa3
@ -146,6 +146,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
|
|||||||
skipValidateSpec.set(generate.skipValidateSpec)
|
skipValidateSpec.set(generate.skipValidateSpec)
|
||||||
generateAliasAsModel.set(generate.generateAliasAsModel)
|
generateAliasAsModel.set(generate.generateAliasAsModel)
|
||||||
engine.set(generate.engine)
|
engine.set(generate.engine)
|
||||||
|
cleanupOutput.set(generate.cleanupOutput)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,8 @@
|
|||||||
package org.openapitools.generator.gradle.plugin.extensions
|
package org.openapitools.generator.gradle.plugin.extensions
|
||||||
|
|
||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
|
import org.gradle.api.tasks.Input
|
||||||
|
import org.gradle.api.tasks.Optional
|
||||||
import org.gradle.kotlin.dsl.listProperty
|
import org.gradle.kotlin.dsl.listProperty
|
||||||
import org.gradle.kotlin.dsl.mapProperty
|
import org.gradle.kotlin.dsl.mapProperty
|
||||||
import org.gradle.kotlin.dsl.property
|
import org.gradle.kotlin.dsl.property
|
||||||
@ -341,6 +343,12 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
|
|||||||
*/
|
*/
|
||||||
val engine = project.objects.property<String?>()
|
val engine = project.objects.property<String?>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines whether the output dir should be cleaned up before generating the output.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
val cleanupOutput = project.objects.property<Boolean>()
|
||||||
|
|
||||||
init {
|
init {
|
||||||
applyDefaults()
|
applyDefaults()
|
||||||
}
|
}
|
||||||
@ -362,5 +370,6 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
|
|||||||
enablePostProcessFile.set(false)
|
enablePostProcessFile.set(false)
|
||||||
skipValidateSpec.set(false)
|
skipValidateSpec.set(false)
|
||||||
generateAliasAsModel.set(false)
|
generateAliasAsModel.set(false)
|
||||||
|
cleanupOutput.set(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -490,6 +490,14 @@ open class GenerateTask : DefaultTask() {
|
|||||||
@Input
|
@Input
|
||||||
val engine = project.objects.property<String?>()
|
val engine = project.objects.property<String?>()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines whether the output dir should be cleaned up before generating the output.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Optional
|
||||||
|
@Input
|
||||||
|
val cleanupOutput = project.objects.property<Boolean>()
|
||||||
|
|
||||||
private fun <T : Any?> Property<T>.ifNotEmpty(block: Property<T>.(T) -> Unit) {
|
private fun <T : Any?> Property<T>.ifNotEmpty(block: Property<T>.(T) -> Unit) {
|
||||||
if (isPresent) {
|
if (isPresent) {
|
||||||
val item: T? = get()
|
val item: T? = get()
|
||||||
@ -512,6 +520,12 @@ open class GenerateTask : DefaultTask() {
|
|||||||
@Suppress("unused")
|
@Suppress("unused")
|
||||||
@TaskAction
|
@TaskAction
|
||||||
fun doWork() {
|
fun doWork() {
|
||||||
|
cleanupOutput.ifNotEmpty { cleanup ->
|
||||||
|
if (cleanup) {
|
||||||
|
project.delete(outputDir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
val configurator: CodegenConfigurator = if (configFile.isPresent) {
|
val configurator: CodegenConfigurator = if (configFile.isPresent) {
|
||||||
CodegenConfigurator.fromFile(configFile.get())
|
CodegenConfigurator.fromFile(configFile.get())
|
||||||
} else createDefaultCodegenConfigurator()
|
} else createDefaultCodegenConfigurator()
|
||||||
|
@ -128,6 +128,91 @@ class GenerateTaskDslTest : TestBase() {
|
|||||||
"Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}")
|
"Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `openApiGenerate should not cleanup outputDir by default`() {
|
||||||
|
// Arrange
|
||||||
|
val projectFiles = mapOf(
|
||||||
|
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0.yaml")
|
||||||
|
)
|
||||||
|
withProject(defaultBuildGradle, projectFiles)
|
||||||
|
|
||||||
|
val oldFile = File(temp, "build/kotlin/should-be-removed")
|
||||||
|
oldFile.mkdirs()
|
||||||
|
oldFile.createNewFile()
|
||||||
|
|
||||||
|
// Act
|
||||||
|
val result = GradleRunner.create()
|
||||||
|
.withProjectDir(temp)
|
||||||
|
.withArguments("openApiGenerate")
|
||||||
|
.withPluginClasspath()
|
||||||
|
.build()
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertTrue(
|
||||||
|
result.output.contains("Successfully generated code to"),
|
||||||
|
"User friendly generate notice is missing."
|
||||||
|
)
|
||||||
|
|
||||||
|
assertTrue(oldFile.exists(), "Old files should have been removed")
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
TaskOutcome.SUCCESS, result.task(":openApiGenerate")?.outcome,
|
||||||
|
"Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `openApiGenerate should cleanup outputDir`() {
|
||||||
|
// Arrange
|
||||||
|
val projectFiles = mapOf(
|
||||||
|
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0.yaml")
|
||||||
|
)
|
||||||
|
withProject(
|
||||||
|
"""
|
||||||
|
plugins {
|
||||||
|
id 'org.openapi.generator'
|
||||||
|
}
|
||||||
|
openApiGenerate {
|
||||||
|
generatorName = "kotlin"
|
||||||
|
inputSpec = file("spec.yaml").absolutePath
|
||||||
|
outputDir = file("build/kotlin").absolutePath
|
||||||
|
apiPackage = "org.openapitools.example.api"
|
||||||
|
invokerPackage = "org.openapitools.example.invoker"
|
||||||
|
modelPackage = "org.openapitools.example.model"
|
||||||
|
configOptions = [
|
||||||
|
dateLibrary: "java8"
|
||||||
|
]
|
||||||
|
cleanupOutput = true
|
||||||
|
}
|
||||||
|
""".trimIndent(),
|
||||||
|
projectFiles
|
||||||
|
)
|
||||||
|
|
||||||
|
val oldFile = File(temp, "build/kotlin/should-be-removed")
|
||||||
|
oldFile.mkdirs()
|
||||||
|
oldFile.createNewFile()
|
||||||
|
|
||||||
|
// Act
|
||||||
|
val result = GradleRunner.create()
|
||||||
|
.withProjectDir(temp)
|
||||||
|
.withArguments("openApiGenerate")
|
||||||
|
.withPluginClasspath()
|
||||||
|
.build()
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assertTrue(
|
||||||
|
result.output.contains("Successfully generated code to"),
|
||||||
|
"User friendly generate notice is missing."
|
||||||
|
)
|
||||||
|
|
||||||
|
assertFalse(oldFile.exists(), "Old files should have been removed")
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
TaskOutcome.SUCCESS, result.task(":openApiGenerate")?.outcome,
|
||||||
|
"Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `should apply prefix & suffix config parameters`() {
|
fun `should apply prefix & suffix config parameters`() {
|
||||||
// Arrange
|
// Arrange
|
||||||
@ -373,4 +458,4 @@ class GenerateTaskDslTest : TestBase() {
|
|||||||
assertEquals(TaskOutcome.FAILED, result.task(":openApiGenerate")?.outcome,
|
assertEquals(TaskOutcome.FAILED, result.task(":openApiGenerate")?.outcome,
|
||||||
"Expected a failed run, but found ${result.task(":openApiGenerate")?.outcome}")
|
"Expected a failed run, but found ${result.task(":openApiGenerate")?.outcome}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user