add support for dry-run flag to Maven and Gradle plugins, fixes #15729 (#15734)

This commit is contained in:
Michael Glanznig 2023-06-03 05:06:58 +02:00 committed by GitHub
parent 2db7b0b177
commit 05b9a899e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 72 additions and 2 deletions

View File

@ -418,6 +418,12 @@ apply plugin: 'org.openapi.generator'
|Boolean |Boolean
|false |false
|Defines whether the output directory should be cleaned up before generating the output. |Defines whether the output directory should be cleaned up before generating the output.
|dryRun
|Boolean
|false
|Defines whether the generator should run in dry-run mode. In dry-run mode no files are written and a summary about
file states is output.
|=== |===
[NOTE] [NOTE]

View File

@ -149,6 +149,7 @@ class OpenApiGeneratorPlugin : Plugin<Project> {
generateAliasAsModel.set(generate.generateAliasAsModel) generateAliasAsModel.set(generate.generateAliasAsModel)
engine.set(generate.engine) engine.set(generate.engine)
cleanupOutput.set(generate.cleanupOutput) cleanupOutput.set(generate.cleanupOutput)
dryRun.set(generate.dryRun)
} }
} }
} }

View File

@ -359,6 +359,11 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
*/ */
val cleanupOutput = project.objects.property<Boolean>() val cleanupOutput = project.objects.property<Boolean>()
/**
* Defines whether the generator should run in dry-run mode.
*/
val dryRun = project.objects.property<Boolean>()
init { init {
applyDefaults() applyDefaults()
} }
@ -381,5 +386,6 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
skipValidateSpec.set(false) skipValidateSpec.set(false)
generateAliasAsModel.set(false) generateAliasAsModel.set(false)
cleanupOutput.set(false) cleanupOutput.set(false)
dryRun.set(false)
} }
} }

View File

@ -521,6 +521,13 @@ open class GenerateTask : DefaultTask() {
@Input @Input
val cleanupOutput = project.objects.property<Boolean>() val cleanupOutput = project.objects.property<Boolean>()
/**
* Defines whether the generator should run in dry-run mode.
*/
@Optional
@Input
val dryRun = 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()
@ -822,6 +829,11 @@ open class GenerateTask : DefaultTask() {
} }
} }
var dryRunSetting = false
dryRun.ifNotEmpty { setting ->
dryRunSetting = setting
}
val clientOptInput = configurator.toClientOptInput() val clientOptInput = configurator.toClientOptInput()
val codegenConfig = clientOptInput.config val codegenConfig = clientOptInput.config
@ -838,7 +850,7 @@ open class GenerateTask : DefaultTask() {
val out = services.get(StyledTextOutputFactory::class.java).create("openapi") val out = services.get(StyledTextOutputFactory::class.java).create("openapi")
out.withStyle(StyledTextOutput.Style.Success) out.withStyle(StyledTextOutput.Style.Success)
DefaultGenerator().opts(clientOptInput).generate() DefaultGenerator(dryRunSetting).opts(clientOptInput).generate()
out.println("Successfully generated code to ${outputDir.get()}") out.println("Successfully generated code to ${outputDir.get()}")
} catch (e: RuntimeException) { } catch (e: RuntimeException) {

View File

@ -458,4 +458,45 @@ 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}")
} }
@Test
fun `openapiGenerate should set dryRun flag`() {
// 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"
]
dryRun = true
}
""".trimIndent(),
projectFiles
)
// Act
val result = GradleRunner.create()
.withProjectDir(temp)
.withArguments("openApiGenerate")
.withPluginClasspath()
.build()
// Assert
assertTrue(
result.output.contains("Dry Run Results:"),
"Dry run results message is missing."
)
}
} }

View File

@ -107,6 +107,7 @@ mvn clean compile
| `skipIfSpecIsUnchanged` | `codegen.skipIfSpecIsUnchanged` | Skip the execution if the source file is older than the output folder (`false` by default. Can also be set globally through the `codegen.skipIfSpecIsUnchanged` property) | `skipIfSpecIsUnchanged` | `codegen.skipIfSpecIsUnchanged` | Skip the execution if the source file is older than the output folder (`false` by default. Can also be set globally through the `codegen.skipIfSpecIsUnchanged` property)
| `addCompileSourceRoot` | `openapi.generator.maven.plugin.addCompileSourceRoot` | Add the output directory to the project as a source root, so that the generated java types are compiled and included in the project artifact (`true` by default). Mutually exclusive with `addTestCompileSourceRoot`. | `addCompileSourceRoot` | `openapi.generator.maven.plugin.addCompileSourceRoot` | Add the output directory to the project as a source root, so that the generated java types are compiled and included in the project artifact (`true` by default). Mutually exclusive with `addTestCompileSourceRoot`.
| `addTestCompileSourceRoot` | `openapi.generator.maven.plugin.addTestCompileSourceRoot` | Add the output directory to the project as a test source root, so that the generated java types are compiled only for the test classpath of the project (`false` by default). Mutually exclusive with `addCompileSourceRoot`. | `addTestCompileSourceRoot` | `openapi.generator.maven.plugin.addTestCompileSourceRoot` | Add the output directory to the project as a test source root, so that the generated java types are compiled only for the test classpath of the project (`false` by default). Mutually exclusive with `addCompileSourceRoot`.
| `dryRun` | `openapi.generator.maven.plugin.dryRun` | Defines whether the generator should run in dry-run mode. In dry-run mode no files are written and a summary about file states is output ( `false` by default).
| `environmentVariables` | N/A | deprecated. Use globalProperties instead. | `environmentVariables` | N/A | deprecated. Use globalProperties instead.
| `globalProperties` | N/A | A **map** of items conceptually similar to "environment variables" or "system properties". These are available to all aspects of the generation flow. See [Global Properties](https://openapi-generator.tech/docs/globals/) for list of available properties. | `globalProperties` | N/A | A **map** of items conceptually similar to "environment variables" or "system properties". These are available to all aspects of the generation flow. See [Global Properties](https://openapi-generator.tech/docs/globals/) for list of available properties.
| `configHelp` | `codegen.configHelp` | dumps the configuration help for the specified library (generates no sources) | `configHelp` | `codegen.configHelp` | dumps the configuration help for the specified library (generates no sources)

View File

@ -460,6 +460,9 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(defaultValue = "false", property = "openapi.generator.maven.plugin.addTestCompileSourceRoot") @Parameter(defaultValue = "false", property = "openapi.generator.maven.plugin.addTestCompileSourceRoot")
private boolean addTestCompileSourceRoot = false; private boolean addTestCompileSourceRoot = false;
@Parameter(defaultValue = "false", property = "openapi.generator.maven.plugin.dryRun")
private Boolean dryRun = false;
// TODO: Rename to global properties in version 5.1 // TODO: Rename to global properties in version 5.1
@Parameter @Parameter
protected Map<String, String> environmentVariables = new HashMap<>(); protected Map<String, String> environmentVariables = new HashMap<>();
@ -867,7 +870,7 @@ public class CodeGenMojo extends AbstractMojo {
return; return;
} }
adjustAdditionalProperties(config); adjustAdditionalProperties(config);
new DefaultGenerator().opts(input).generate(); new DefaultGenerator(dryRun).opts(input).generate();
if (buildContext != null) { if (buildContext != null) {
buildContext.refresh(new File(getCompileSourceRoot())); buildContext.refresh(new File(getCompileSourceRoot()));