Skip to content

Commit 906ec5d

Browse files
authored
Cleanup outputDir before openApiGenerate (#13659)
* Cleanup outputdir before openApiGenerate * Add cleanupOutput property to GenerateTask
1 parent 871eda2 commit 906ec5d

4 files changed

Lines changed: 110 additions & 1 deletion

File tree

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/OpenApiGeneratorPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
146146
skipValidateSpec.set(generate.skipValidateSpec)
147147
generateAliasAsModel.set(generate.generateAliasAsModel)
148148
engine.set(generate.engine)
149+
cleanupOutput.set(generate.cleanupOutput)
149150
}
150151
}
151152
}

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/extensions/OpenApiGeneratorGenerateExtension.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.openapitools.generator.gradle.plugin.extensions
1818

1919
import org.gradle.api.Project
20+
import org.gradle.api.tasks.Input
21+
import org.gradle.api.tasks.Optional
2022
import org.gradle.kotlin.dsl.listProperty
2123
import org.gradle.kotlin.dsl.mapProperty
2224
import org.gradle.kotlin.dsl.property
@@ -341,6 +343,12 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
341343
*/
342344
val engine = project.objects.property<String?>()
343345

346+
/**
347+
* Defines whether the output dir should be cleaned up before generating the output.
348+
*
349+
*/
350+
val cleanupOutput = project.objects.property<Boolean>()
351+
344352
init {
345353
applyDefaults()
346354
}
@@ -362,5 +370,6 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
362370
enablePostProcessFile.set(false)
363371
skipValidateSpec.set(false)
364372
generateAliasAsModel.set(false)
373+
cleanupOutput.set(false)
365374
}
366375
}

modules/openapi-generator-gradle-plugin/src/main/kotlin/org/openapitools/generator/gradle/plugin/tasks/GenerateTask.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,14 @@ open class GenerateTask : DefaultTask() {
490490
@Input
491491
val engine = project.objects.property<String?>()
492492

493+
/**
494+
* Defines whether the output dir should be cleaned up before generating the output.
495+
*
496+
*/
497+
@Optional
498+
@Input
499+
val cleanupOutput = project.objects.property<Boolean>()
500+
493501
private fun <T : Any?> Property<T>.ifNotEmpty(block: Property<T>.(T) -> Unit) {
494502
if (isPresent) {
495503
val item: T? = get()
@@ -512,6 +520,12 @@ open class GenerateTask : DefaultTask() {
512520
@Suppress("unused")
513521
@TaskAction
514522
fun doWork() {
523+
cleanupOutput.ifNotEmpty { cleanup ->
524+
if (cleanup) {
525+
project.delete(outputDir)
526+
}
527+
}
528+
515529
val configurator: CodegenConfigurator = if (configFile.isPresent) {
516530
CodegenConfigurator.fromFile(configFile.get())
517531
} else createDefaultCodegenConfigurator()

modules/openapi-generator-gradle-plugin/src/test/kotlin/GenerateTaskDslTest.kt

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,91 @@ class GenerateTaskDslTest : TestBase() {
128128
"Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}")
129129
}
130130

131+
@Test
132+
fun `openApiGenerate should not cleanup outputDir by default`() {
133+
// Arrange
134+
val projectFiles = mapOf(
135+
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0.yaml")
136+
)
137+
withProject(defaultBuildGradle, projectFiles)
138+
139+
val oldFile = File(temp, "build/kotlin/should-be-removed")
140+
oldFile.mkdirs()
141+
oldFile.createNewFile()
142+
143+
// Act
144+
val result = GradleRunner.create()
145+
.withProjectDir(temp)
146+
.withArguments("openApiGenerate")
147+
.withPluginClasspath()
148+
.build()
149+
150+
// Assert
151+
assertTrue(
152+
result.output.contains("Successfully generated code to"),
153+
"User friendly generate notice is missing."
154+
)
155+
156+
assertTrue(oldFile.exists(), "Old files should have been removed")
157+
158+
assertEquals(
159+
TaskOutcome.SUCCESS, result.task(":openApiGenerate")?.outcome,
160+
"Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}"
161+
)
162+
}
163+
164+
@Test
165+
fun `openApiGenerate should cleanup outputDir`() {
166+
// Arrange
167+
val projectFiles = mapOf(
168+
"spec.yaml" to javaClass.classLoader.getResourceAsStream("specs/petstore-v3.0.yaml")
169+
)
170+
withProject(
171+
"""
172+
plugins {
173+
id 'org.openapi.generator'
174+
}
175+
openApiGenerate {
176+
generatorName = "kotlin"
177+
inputSpec = file("spec.yaml").absolutePath
178+
outputDir = file("build/kotlin").absolutePath
179+
apiPackage = "org.openapitools.example.api"
180+
invokerPackage = "org.openapitools.example.invoker"
181+
modelPackage = "org.openapitools.example.model"
182+
configOptions = [
183+
dateLibrary: "java8"
184+
]
185+
cleanupOutput = true
186+
}
187+
""".trimIndent(),
188+
projectFiles
189+
)
190+
191+
val oldFile = File(temp, "build/kotlin/should-be-removed")
192+
oldFile.mkdirs()
193+
oldFile.createNewFile()
194+
195+
// Act
196+
val result = GradleRunner.create()
197+
.withProjectDir(temp)
198+
.withArguments("openApiGenerate")
199+
.withPluginClasspath()
200+
.build()
201+
202+
// Assert
203+
assertTrue(
204+
result.output.contains("Successfully generated code to"),
205+
"User friendly generate notice is missing."
206+
)
207+
208+
assertFalse(oldFile.exists(), "Old files should have been removed")
209+
210+
assertEquals(
211+
TaskOutcome.SUCCESS, result.task(":openApiGenerate")?.outcome,
212+
"Expected a successful run, but found ${result.task(":openApiGenerate")?.outcome}"
213+
)
214+
}
215+
131216
@Test
132217
fun `should apply prefix & suffix config parameters`() {
133218
// Arrange
@@ -373,4 +458,4 @@ class GenerateTaskDslTest : TestBase() {
373458
assertEquals(TaskOutcome.FAILED, result.task(":openApiGenerate")?.outcome,
374459
"Expected a failed run, but found ${result.task(":openApiGenerate")?.outcome}")
375460
}
376-
}
461+
}

0 commit comments

Comments
 (0)