Skip to content

Commit

Permalink
Cleanup outputDir before openApiGenerate (#13659)
Browse files Browse the repository at this point in the history
* Cleanup outputdir before openApiGenerate

* Add cleanupOutput property to GenerateTask
  • Loading branch information
sorin-florea committed Nov 23, 2022
1 parent 871eda2 commit 906ec5d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
Expand Up @@ -146,6 +146,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
skipValidateSpec.set(generate.skipValidateSpec)
generateAliasAsModel.set(generate.generateAliasAsModel)
engine.set(generate.engine)
cleanupOutput.set(generate.cleanupOutput)
}
}
}
Expand Down
Expand Up @@ -17,6 +17,8 @@
package org.openapitools.generator.gradle.plugin.extensions

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.mapProperty
import org.gradle.kotlin.dsl.property
Expand Down Expand Up @@ -341,6 +343,12 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
*/
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 {
applyDefaults()
}
Expand All @@ -362,5 +370,6 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
enablePostProcessFile.set(false)
skipValidateSpec.set(false)
generateAliasAsModel.set(false)
cleanupOutput.set(false)
}
}
Expand Up @@ -490,6 +490,14 @@ open class GenerateTask : DefaultTask() {
@Input
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) {
if (isPresent) {
val item: T? = get()
Expand All @@ -512,6 +520,12 @@ open class GenerateTask : DefaultTask() {
@Suppress("unused")
@TaskAction
fun doWork() {
cleanupOutput.ifNotEmpty { cleanup ->
if (cleanup) {
project.delete(outputDir)
}
}

val configurator: CodegenConfigurator = if (configFile.isPresent) {
CodegenConfigurator.fromFile(configFile.get())
} else createDefaultCodegenConfigurator()
Expand Down
Expand Up @@ -128,6 +128,91 @@ class GenerateTaskDslTest : TestBase() {
"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
fun `should apply prefix & suffix config parameters`() {
// Arrange
Expand Down Expand Up @@ -373,4 +458,4 @@ class GenerateTaskDslTest : TestBase() {
assertEquals(TaskOutcome.FAILED, result.task(":openApiGenerate")?.outcome,
"Expected a failed run, but found ${result.task(":openApiGenerate")?.outcome}")
}
}
}

0 comments on commit 906ec5d

Please sign in to comment.