Skip to content

Commit 35f993a

Browse files
committed
migrate to modern api
1 parent 6d7de66 commit 35f993a

7 files changed

Lines changed: 197 additions & 127 deletions

File tree

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ import org.gradle.kotlin.dsl.property
2828
*
2929
* @author Jim Schubert
3030
*/
31-
open class OpenApiGeneratorGenerateExtension(project: Project) {
31+
open class OpenApiGeneratorGenerateExtension(private val project: Project) {
32+
3233
/**
3334
* The verbosity of generation
3435
*/
@@ -390,4 +391,40 @@ open class OpenApiGeneratorGenerateExtension(project: Project) {
390391
cleanupOutput.convention(false)
391392
dryRun.convention(false)
392393
}
394+
395+
// ========================================================================
396+
// Backwards-compatibility bridge setters for Groovy/Kotlin DSL
397+
// These allow users to continue assigning paths as standard strings.
398+
// ========================================================================
399+
400+
/** Backwards-compatibility bridge for outputDir */
401+
fun setOutputDir(path: String) {
402+
outputDir.set(project.layout.projectDirectory.dir(path))
403+
}
404+
405+
/** Backwards-compatibility bridge for inputSpec */
406+
fun setInputSpec(path: String) {
407+
inputSpec.set(project.layout.projectDirectory.file(path))
408+
}
409+
410+
/** Backwards-compatibility bridge for inputSpecRootDirectory */
411+
fun setInputSpecRootDirectory(path: String) {
412+
inputSpecRootDirectory.set(project.layout.projectDirectory.dir(path))
413+
}
414+
415+
/** Backwards-compatibility bridge for templateDir */
416+
fun setTemplateDir(path: String) {
417+
templateDir.set(project.layout.projectDirectory.dir(path))
418+
}
419+
420+
/** Backwards-compatibility bridge for configFile */
421+
fun setConfigFile(path: String) {
422+
configFile.set(project.layout.projectDirectory.file(path))
423+
}
424+
425+
/** Backwards-compatibility bridge for ignoreFileOverride */
426+
fun setIgnoreFileOverride(path: String) {
427+
ignoreFileOverride.set(project.layout.projectDirectory.file(path))
428+
}
429+
393430
}

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,6 +17,7 @@
1717
package org.openapitools.generator.gradle.plugin.extensions
1818

1919
import org.gradle.api.Project
20+
import org.gradle.api.provider.ListProperty
2021
import org.gradle.kotlin.dsl.listProperty
2122
import org.openapitools.codegen.meta.Stability
2223

@@ -29,13 +30,27 @@ open class OpenApiGeneratorGeneratorsExtension(project: Project) {
2930
/**
3031
* A list of stability indexes to include (value: all,beta,stable,experimental,deprecated). Excludes deprecated by default.
3132
*/
32-
val include = project.objects.listProperty<String>()
33+
val include: ListProperty<String> = project.objects.listProperty()
3334

3435
init {
3536
applyDefaults()
3637
}
3738

3839
@Suppress("MemberVisibilityCanBePrivate")
39-
fun applyDefaults() =
40-
include.set(Stability.values().map { it.value() }.filterNot { it == Stability.DEPRECATED.value() })
41-
}
40+
fun applyDefaults() {
41+
// Use .convention() instead of .set() so users can cleanly override this default
42+
include.convention(
43+
Stability.values()
44+
.map { it.value() }
45+
.filterNot { it == Stability.DEPRECATED.value() }
46+
)
47+
}
48+
49+
// ========================================================================
50+
// Backwards-compatibility bridge setter for Groovy/Kotlin DSL
51+
// Allows users to continue assigning lists directly via `=`
52+
// ========================================================================
53+
fun setInclude(items: Iterable<String>) {
54+
include.set(items)
55+
}
56+
}

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,32 +17,47 @@
1717
package org.openapitools.generator.gradle.plugin.extensions
1818

1919
import org.gradle.api.Project
20+
import org.gradle.api.file.DirectoryProperty
21+
import org.gradle.api.provider.Property
2022
import org.gradle.kotlin.dsl.property
2123

2224
/**
2325
* Gradle project level extension object definition for the meta-generator task
2426
*
2527
* @author Jim Schubert
2628
*/
27-
open class OpenApiGeneratorMetaExtension(project: Project) {
29+
open class OpenApiGeneratorMetaExtension(private val project: Project) {
2830
/**
2931
* The human-readable generator name of the newly created template generator.
3032
*/
31-
val generatorName = project.objects.property<String>()
33+
val generatorName: Property<String> = project.objects.property()
3234

3335
/**
3436
* The packageName generatorName to put the main class into (defaults to org.openapitools.codegen)
3537
*/
36-
val packageName = project.objects.property<String>()
38+
val packageName: Property<String> = project.objects.property()
3739

3840
/**
3941
* Where to write the generated files (current dir by default).
4042
*/
41-
val outputFolder = project.objects.property<String>()
43+
val outputFolder: DirectoryProperty = project.objects.directoryProperty()
4244

4345
init {
44-
generatorName.set("default")
45-
packageName.set("org.openapitools.codegen")
46-
outputFolder.set("")
46+
// Use .convention() to allow users to cleanly override these defaults
47+
generatorName.convention("default")
48+
packageName.convention("org.openapitools.codegen")
49+
50+
// Use the native layout project directory instead of an empty string
51+
outputFolder.convention(project.layout.projectDirectory)
52+
}
53+
54+
// ========================================================================
55+
// Backwards-compatibility bridge setter for Groovy/Kotlin DSL
56+
// Allows users to continue assigning paths as standard strings.
57+
// ========================================================================
58+
59+
/** Backwards-compatibility bridge for outputFolder */
60+
fun setOutputFolder(path: String) {
61+
outputFolder.set(project.layout.projectDirectory.dir(path))
4762
}
4863
}

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,26 +17,38 @@
1717
package org.openapitools.generator.gradle.plugin.extensions
1818

1919
import org.gradle.api.Project
20+
import org.gradle.api.file.RegularFileProperty
21+
import org.gradle.api.provider.Property
2022
import org.gradle.kotlin.dsl.property
2123

2224
/**
2325
* Gradle project level extension object definition for the generators task
2426
*
2527
* @author Jim Schubert
2628
*/
27-
open class OpenApiGeneratorValidateExtension(project: Project) {
29+
open class OpenApiGeneratorValidateExtension(private val project: Project) {
2830
/**
2931
* The input specification to validate. Supports all formats supported by the Parser.
3032
*/
31-
val inputSpec = project.objects.property<String>()
33+
val inputSpec: RegularFileProperty = project.objects.fileProperty()
3234

3335
/**
3436
* Whether to offer recommendations related to the validated specification document.
3537
*/
36-
val recommend = project.objects.property<Boolean>().convention(true)
38+
val recommend: Property<Boolean> = project.objects.property<Boolean>().convention(true)
3739

3840
/**
3941
* Whether to treat warnings as errors and fail the task.
4042
*/
41-
val treatWarningsAsErrors = project.objects.property<Boolean>().convention(false)
43+
val treatWarningsAsErrors: Property<Boolean> = project.objects.property<Boolean>().convention(false)
44+
45+
// ========================================================================
46+
// Backwards-compatibility bridge setters for Groovy/Kotlin DSL
47+
// These allow users to continue assigning paths as standard strings.
48+
// ========================================================================
49+
50+
/** Backwards-compatibility bridge for inputSpec */
51+
fun setInputSpec(path: String) {
52+
inputSpec.set(project.layout.projectDirectory.file(path))
53+
}
4254
}

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

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* https://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,11 +17,9 @@
1717
package org.openapitools.generator.gradle.plugin.tasks
1818

1919
import org.gradle.api.DefaultTask
20+
import org.gradle.api.provider.ListProperty
2021
import org.gradle.api.tasks.Internal
2122
import org.gradle.api.tasks.TaskAction
22-
import org.gradle.internal.logging.text.StyledTextOutput
23-
import org.gradle.internal.logging.text.StyledTextOutputFactory
24-
import org.gradle.kotlin.dsl.listProperty
2523
import org.gradle.work.DisableCachingByDefault
2624
import org.openapitools.codegen.CodegenConfigLoader
2725
import org.openapitools.codegen.CodegenType
@@ -38,66 +36,57 @@ import org.openapitools.codegen.meta.Stability
3836
* @author Jim Schubert
3937
*/
4038
@DisableCachingByDefault(because = "not worth caching")
41-
open class GeneratorsTask : DefaultTask() {
39+
abstract class GeneratorsTask : DefaultTask() {
40+
4241
/**
4342
* A list of stability indexes to include (value: all,beta,stable,experimental,deprecated). Excludes deprecated by default.
4443
*/
4544
@get:Internal
46-
val include = project.objects.listProperty<String>()
45+
abstract val include: ListProperty<String>
4746

4847
@TaskAction
4948
fun doWork() {
5049
val generators = CodegenConfigLoader.getAll()
5150

52-
val out = services.get(StyledTextOutputFactory::class.java).create("openapi")
53-
54-
StringBuilder().apply {
55-
val types = CodegenType.values()
56-
57-
val stabilities = if (include.isPresent) {
58-
when {
59-
include.get().contains("all") -> Stability.values().toList()
60-
else -> include.get().map { Stability.forDescription(it) }
61-
}
51+
// Safely extract the includes, falling back to the default if empty or not present
52+
val stabilities = include.orNull?.takeIf { it.isNotEmpty() }?.let { includes ->
53+
if (includes.contains("all")) {
54+
Stability.values().toList()
6255
} else {
63-
Stability.values().filterNot { it == Stability.DEPRECATED }
56+
includes.map { Stability.forDescription(it) }
6457
}
58+
} ?: Stability.values().filterNot { it == Stability.DEPRECATED }
6559

66-
append("The following generators are available:")
67-
68-
append(System.lineSeparator())
69-
append(System.lineSeparator())
60+
val sb = StringBuilder()
61+
sb.append("The following generators are available:\n\n")
7062

71-
for (type in types) {
72-
append(type.name).append(" generators:")
73-
append(System.lineSeparator())
63+
for (type in CodegenType.values()) {
64+
sb.append(type.name).append(" generators:\n")
7465

75-
generators.filter { it.tag == type }
76-
.sortedBy { it.name }
77-
.forEach { generator ->
66+
generators.filter { it.tag == type }
67+
.sortedBy { it.name }
68+
.forEach { generator ->
69+
val meta: GeneratorMetadata? = generator.generatorMetadata
70+
val shouldInclude = stabilities.contains(meta?.stability)
7871

79-
val meta: GeneratorMetadata? = generator.generatorMetadata
80-
val include = stabilities.contains(meta?.stability)
81-
if (include) {
82-
append(" - ")
83-
append(generator.name)
72+
if (shouldInclude) {
73+
sb.append(" - ")
74+
sb.append(generator.name)
8475

85-
meta?.stability?.let {
86-
if (it != Stability.STABLE) {
87-
append(" (${it.value()})")
88-
}
89-
}
90-
91-
append(System.lineSeparator())
76+
meta?.stability?.let { stability ->
77+
if (stability != Stability.STABLE) {
78+
sb.append(" (${stability.value()})")
9279
}
9380
}
9481

95-
append(System.lineSeparator())
96-
append(System.lineSeparator())
97-
}
82+
sb.append("\n")
83+
}
84+
}
9885

99-
out.withStyle(StyledTextOutput.Style.Success)
100-
out.formatln("%s%n", toString())
86+
sb.append("\n\n")
10187
}
88+
89+
// Use Gradle's standard lifecycle logger instead of internal StyledTextOutputFactory
90+
logger.lifecycle(sb.toString())
10291
}
10392
}

0 commit comments

Comments
 (0)