Skip to content

Commit e72fb41

Browse files
authored
Merge pull request #102 from martinbonnin/feature/lazy-config
Add Gradle Lazy Configuration take n° 2
2 parents d40777e + f85da9f commit e72fb41

File tree

10 files changed

+135
-135
lines changed

10 files changed

+135
-135
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ plugins {
4141
}
4242

4343
generateSwagger {
44-
platform = "kotlin"
45-
packageName = "com.yelp.codegen.samples"
46-
inputFile = file("./sample_specs.json")
47-
outputDir = file("./src/main/java/")
44+
platform.set("kotlin")
45+
packageName.set("com.yelp.codegen.samples")
46+
inputFile.set(file("./sample_specs.json"))
47+
outputDir.set(project.layout.buildDirectory.dir("./src/main/java/"))
4848
}
4949
```
5050

@@ -100,14 +100,14 @@ To configure the generator, please use the `generateSwagger { }` block. Here an
100100

101101
```kotlin
102102
generateSwagger {
103-
platform = "kotlin"
104-
packageName = "com.yelp.codegen.integrations"
105-
specName = "integration"
106-
specVersion = "1.0.0"
107-
inputFile = file("../sample_specs.json")
108-
outputDir = file("./src/main/java/")
103+
platform.set("kotlin")
104+
packageName.set("com.yelp.codegen.integrations")
105+
specName.set("integration")
106+
specVersion.set("1.0.0")
107+
inputFile.set(file("../sample_specs.json"))
108+
outputDir.set(project.layout.buildDirectory.dir("./src/main/java/"))
109109
features {
110-
headersToRemove = ["Accept-Language"]
110+
headersToRemove.add("Accept-Language")
111111
}
112112
}
113113
```

gradle-plugin/plugin/src/main/java/com/yelp/codegen/plugin/CodegenPlugin.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ class CodegenPlugin : Plugin<Project> {
1313
val config = project.extensions.create("generateSwagger", GenerateTaskConfiguration::class.java, project)
1414

1515
project.tasks.register("generateSwagger", GenerateTask::class.java) {
16-
it.platform = config.platform
17-
it.packageName = config.packageName
18-
it.specName = config.specName
19-
it.specVersion = config.specVersion
20-
it.inputFile = config.inputFile
21-
it.outputDir = config.outputDir
16+
it.platform.set(config.platform)
17+
it.packageName.set(config.packageName)
18+
it.specName.set(config.specName)
19+
it.specVersion.set(config.specVersion)
20+
it.inputFile.set(config.inputFile)
21+
it.outputDir.set(config.outputDir)
2222

23-
it.extraFiles = config.extraFiles
23+
it.extraFiles.set(config.extraFiles)
2424
it.features = config.features
2525
}
2626
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.yelp.codegen.plugin
22

3+
import org.gradle.api.model.ObjectFactory
34
import org.gradle.api.tasks.Input
45
import org.gradle.api.tasks.Optional
56

6-
open class FeatureConfiguration {
7-
8-
@Input
9-
@Optional
10-
var headersToRemove: Array<String> = arrayOf()
7+
class FeatureConfiguration(objectFactory: ObjectFactory) {
8+
@get:Input
9+
@get:Optional
10+
val headersToRemove = objectFactory.listProperty(String::class.java)
1111
}
Lines changed: 57 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.yelp.codegen.plugin
22

33
import com.yelp.codegen.main
4-
import io.swagger.parser.SwaggerParser
5-
import java.io.File
64
import org.gradle.api.DefaultTask
5+
import org.gradle.api.file.DirectoryProperty
6+
import org.gradle.api.file.RegularFileProperty
77
import org.gradle.api.plugins.BasePlugin
8+
import org.gradle.api.provider.Property
89
import org.gradle.api.tasks.Input
910
import org.gradle.api.tasks.InputFile
1011
import org.gradle.api.tasks.InputFiles
@@ -14,126 +15,99 @@ import org.gradle.api.tasks.OutputDirectory
1415
import org.gradle.api.tasks.TaskAction
1516
import org.gradle.api.tasks.options.Option
1617

17-
const val DEFAULT_PLATFORM = "kotlin"
18-
const val DEFAULT_VERSION = "0.0.0"
19-
const val DEFAULT_NAME = "defaultname"
20-
const val DEFAULT_PACKAGE = "com.codegen.default"
21-
const val DEFAULT_OUTPUT_DIR = "/gen"
22-
23-
open class GenerateTask : DefaultTask() {
18+
abstract class GenerateTask : DefaultTask() {
2419

2520
init {
2621
description = "Run the Swagger Code Generation tool"
2722
group = BasePlugin.BUILD_GROUP
2823
}
2924

30-
@Input
31-
@Optional
32-
@Option(option = "platform", description = "Configures the platform that is used for generating the code.")
33-
var platform: String? = null
34-
35-
@Input
36-
@Optional
37-
@Option(option = "packageName", description = "Configures the package name of the resulting code.")
38-
var packageName: String? = null
39-
40-
@Input
41-
@Optional
42-
@Option(option = "specName", description = "Configures the name of the service for the Swagger Spec.")
43-
var specName: String? = null
44-
45-
@Input
46-
@Optional
47-
@Option(option = "specVersion", description = "Configures the version of the Swagger Spec.")
48-
var specVersion: String? = null
49-
50-
@InputFile
51-
@Option(option = "inputFile", description = "Configures path of the Swagger Spec.")
52-
lateinit var inputFile: File
53-
54-
@OutputDirectory
55-
@Optional
56-
@Option(option = "outputDir", description = "Configures path of the Generated code directory.")
57-
var outputDir: File? = null
58-
59-
@InputFiles
60-
@Optional
61-
@Option(option = "extraFiles",
25+
@get:Input
26+
@get:Option(option = "platform", description = "Configures the platform that is used for generating the code.")
27+
abstract val platform: Property<String>
28+
29+
@get:Input
30+
@get:Option(option = "packageName", description = "Configures the package name of the resulting code.")
31+
abstract val packageName: Property<String>
32+
33+
@get:Input
34+
@get:Option(option = "specName", description = "Configures the name of the service for the Swagger Spec.")
35+
abstract val specName: Property<String>
36+
37+
@get:Input
38+
@get:Option(option = "specVersion", description = "Configures the version of the Swagger Spec.")
39+
abstract val specVersion: Property<String>
40+
41+
@get:InputFile
42+
@get:Option(option = "inputFile", description = "Configures path of the Swagger Spec.")
43+
abstract val inputFile: RegularFileProperty
44+
45+
@get:OutputDirectory
46+
@get:Option(option = "outputDir", description = "Configures path of the Generated code directory.")
47+
abstract val outputDir: DirectoryProperty
48+
49+
@get:InputFiles
50+
@get:Optional
51+
@get:Option(option = "extraFiles",
6252
description = "Configures path of the extra files directory to be added to the Generated code.")
63-
var extraFiles: File? = null
53+
abstract val extraFiles: DirectoryProperty
6454

65-
@Nested
66-
@Option(option = "featureHeaderToRemove", description = "")
55+
@get:Nested
56+
@get:Option(option = "featureHeaderToRemove", description = "")
6757
var features: FeatureConfiguration? = null
6858

6959
@TaskAction
7060
fun swaggerGenerate() {
61+
val platform = platform.get()
62+
val specName = specName.get()
63+
val packageName = packageName.get()
64+
val outputDir = outputDir.get().asFile
65+
val inputFile = inputFile.get().asFile
66+
val specVersion = specVersion.get()
7167

72-
if (specVersion == null) {
73-
readVersionFromSpecfile(inputFile)
74-
}
75-
val defaultOutputDir = File(project.buildDir, DEFAULT_OUTPUT_DIR)
68+
val headersToRemove = features?.headersToRemove?.get() ?: emptyList()
7669

7770
println("""
7871
####################
7972
Yelp Swagger Codegen
8073
####################
81-
Platform ${'\t'} ${platform ?: "[ DEFAULT ] $DEFAULT_PLATFORM"}
82-
Package ${'\t'} ${packageName ?: "[ DEFAULT ] $DEFAULT_PACKAGE"}
83-
specName ${'\t'} ${specName ?: "[ DEFAULT ] $DEFAULT_NAME"}
84-
specVers ${'\t'} ${specVersion ?: "[ DEFAULT ] $DEFAULT_VERSION"}
74+
Platform ${'\t'} $platform
75+
Package ${'\t'} $packageName
76+
specName ${'\t'} $specName
77+
specVers ${'\t'} $specVersion
8578
input ${"\t\t"} $inputFile
86-
output ${"\t\t"} ${outputDir ?: "[ DEFAULT ] $defaultOutputDir"}
87-
groupId ${'\t'} ${packageName ?: "[ DEFAULT ] default"}
88-
artifactId ${'\t'} ${packageName ?: "[ DEFAULT ] com.codegen"}
89-
features ${'\t'} ${features?.headersToRemove?.joinToString(", ")?.ifEmpty { "[ EMPTY ]" }}
79+
output ${"\t\t"} $outputDir
80+
groupId ${'\t'} $packageName
81+
artifactId ${'\t'} $packageName
82+
features ${'\t'} ${headersToRemove.joinToString(separator = ",", prefix = "[", postfix = "]")}
9083
""".trimIndent())
9184

92-
val packageName = packageName ?: DEFAULT_PACKAGE
93-
9485
val params = mutableListOf<String>()
9586
params.add("-p")
96-
params.add(platform ?: DEFAULT_PLATFORM)
87+
params.add(platform)
9788
params.add("-s")
98-
params.add(specName ?: DEFAULT_NAME)
89+
params.add(specName)
9990
params.add("-v")
100-
params.add(specVersion ?: DEFAULT_VERSION)
91+
params.add(specVersion)
10192
params.add("-g")
10293
params.add(packageName.substringBeforeLast('.'))
10394
params.add("-a")
10495
params.add(packageName.substringAfterLast('.'))
10596
params.add("-i")
10697
params.add(inputFile.toString())
10798
params.add("-o")
108-
params.add((outputDir ?: defaultOutputDir).toString())
99+
params.add(outputDir.toString())
109100

110-
if (true == features?.headersToRemove?.isNotEmpty()) {
101+
if (headersToRemove.isNotEmpty()) {
111102
params.add("-ignoreheaders")
112-
params.add(features?.headersToRemove?.joinToString(",") ?: "")
103+
params.add(headersToRemove.joinToString(","))
113104
}
114105

115106
// Running the Codegen Main here
116107
main(params.toTypedArray())
117108

118109
// Copy over the extra files.
119-
val source = extraFiles
120-
val destin = outputDir
121-
if (source != null && destin != null) {
122-
source.copyRecursively(destin, overwrite = true)
123-
}
124-
}
125-
126-
private fun readVersionFromSpecfile(specFile: File) {
127-
val swaggerSpec = SwaggerParser().readWithInfo(specFile.absolutePath, listOf(), false).swagger
128-
specVersion = when (val version = swaggerSpec.info.version) {
129-
is String -> {
130-
println("Successfully read version from Swagger Spec file: $version")
131-
version
132-
}
133-
else -> {
134-
println("Issue in reading version from Swagger Spec file. Falling back to $DEFAULT_VERSION")
135-
DEFAULT_VERSION
136-
}
137-
}
110+
val source = extraFiles.orNull?.asFile
111+
source?.copyRecursively(outputDir, overwrite = true)
138112
}
139113
}
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
package com.yelp.codegen.plugin
22

3-
import java.io.File
3+
import javax.inject.Inject
44
import org.gradle.api.Action
55
import org.gradle.api.Project
6+
import org.gradle.api.file.RegularFileProperty
67

7-
open class GenerateTaskConfiguration(@Suppress("UNUSED_PARAMETER") project: Project) {
8-
var platform: String? = null
9-
var packageName: String? = null
10-
var specName: String? = null
11-
var specVersion: String? = null
12-
lateinit var inputFile: File
13-
var outputDir: File? = null
14-
var extraFiles: File? = null
15-
var features: FeatureConfiguration = FeatureConfiguration()
8+
abstract class GenerateTaskConfiguration @Inject constructor(project: Project) {
9+
val objects = project.objects
10+
11+
val platform = objects.property(String::class.java).convention("kotlin")
12+
val packageName = objects.property(String::class.java).convention("com.codegen.default")
13+
val specName = objects.property(String::class.java).convention("defaultname")
14+
abstract val inputFile: RegularFileProperty
15+
val specVersion = objects.property(String::class.java).convention(project.provider {
16+
readVersionFromSpecfile(inputFile.get().asFile)
17+
})
18+
val outputDir = objects.directoryProperty().convention(project.layout.buildDirectory.dir("gen").get())
19+
20+
val extraFiles = objects.directoryProperty()
21+
val features: FeatureConfiguration = FeatureConfiguration(objects)
1622

1723
fun features(action: Action<FeatureConfiguration>) = action.execute(features)
1824
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.yelp.codegen.plugin
2+
3+
import io.swagger.parser.SwaggerParser
4+
import java.io.File
5+
6+
fun readVersionFromSpecfile(specFile: File): String {
7+
val swaggerSpec = SwaggerParser().readWithInfo(specFile.absolutePath, listOf(), false).swagger
8+
9+
return when (val version = swaggerSpec.info.version) {
10+
is String -> {
11+
println("Successfully read version from Swagger Spec file: $version")
12+
version
13+
}
14+
else -> {
15+
val defaultVersion = "0.0.0"
16+
println("Issue in reading version from Swagger Spec file. Falling back to $defaultVersion")
17+
defaultVersion
18+
}
19+
}
20+
}

gradle-plugin/plugin/src/test/testProject/build.gradle.kts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ buildscript {
2222
apply(plugin = "com.yelp.codegen.plugin")
2323

2424
configure<GenerateTaskConfiguration> {
25-
platform = "kotlin"
26-
packageName = "com.yelp.codegen.samples"
25+
platform.set("kotlin")
26+
packageName.set("com.yelp.codegen.samples")
2727
// we are in rootDir/plugin-root/plugin/build/testProject/
28-
inputFile = file("../../../../samples/junit-tests/junit_tests_specs.json")
29-
outputDir = file("./build/generatedSources")
28+
inputFile.set(file("../../../../samples/junit-tests/junit_tests_specs.json"))
29+
outputDir.set(file("./build/generatedSources"))
3030
}

samples/groovy-android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ generateSwagger {
4444
inputFile = file("../sample_specs.json")
4545
outputDir = file("./src/main/java/")
4646
features {
47-
headersToRemove = ["Accept-Language"]
47+
headersToRemove.add("Accept-Language")
4848
}
4949
}
5050

samples/kotlin-android-moshi-codegen/build.gradle.kts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ dependencies {
4444
}
4545

4646
generateSwagger {
47-
platform = "kotlin"
48-
packageName = "com.yelp.codegen.samples"
49-
specName = "sample_specs"
50-
specVersion = "1.0.0"
51-
inputFile = file("../sample_specs.json")
52-
outputDir = file("./src/main/java/")
47+
platform.set("kotlin")
48+
packageName.set("com.yelp.codegen.samples")
49+
specName.set("sample_specs")
50+
specVersion.set("1.0.0")
51+
inputFile.set(file("../sample_specs.json"))
52+
outputDir.set(file("./src/main/java/"))
5353
features {
54-
headersToRemove = arrayOf("Accept-Language")
54+
headersToRemove.add("Accept-Language")
5555
}
5656
}
5757

samples/kotlin-android/build.gradle.kts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ dependencies {
3737
}
3838

3939
generateSwagger {
40-
platform = "kotlin"
41-
packageName = "com.yelp.codegen.samples"
42-
specName = "sample_specs"
43-
specVersion = "1.0.0"
44-
inputFile = file("../sample_specs.json")
45-
outputDir = file("./src/main/java/")
40+
platform.set("kotlin")
41+
packageName.set("com.yelp.codegen.samples")
42+
specName.set("sample_specs")
43+
specVersion.set("1.0.0")
44+
inputFile.set(file("../sample_specs.json"))
45+
outputDir.set(project.layout.projectDirectory.dir("./src/main/java/"))
4646
features {
47-
headersToRemove = arrayOf("Accept-Language")
47+
headersToRemove.add("Accept-Language")
4848
}
4949
}
5050

0 commit comments

Comments
 (0)