Skip to content

Commit 3e55080

Browse files
committed
make task properties non-optional
1 parent 0eb141d commit 3e55080

File tree

3 files changed

+40
-41
lines changed

3 files changed

+40
-41
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CodegenPlugin : Plugin<Project> {
1010
"com.yelp.codegen.plugin requires Gradle version 5.4.1 or greater"
1111
}
1212

13-
val config = project.extensions.create("generateSwagger", GenerateTaskConfiguration::class.java)
13+
val config = project.extensions.create("generateSwagger", GenerateTaskConfiguration::class.java, project)
1414

1515
project.tasks.register("generateSwagger", GenerateTask::class.java) {
1616
it.platform.set(config.platform)

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

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
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
75
import org.gradle.api.file.DirectoryProperty
86
import org.gradle.api.file.RegularFileProperty
@@ -31,22 +29,18 @@ abstract class GenerateTask : DefaultTask() {
3129
}
3230

3331
@get:Input
34-
@get:Optional
3532
@get:Option(option = "platform", description = "Configures the platform that is used for generating the code.")
3633
abstract val platform: Property<String>
3734

3835
@get:Input
39-
@get:Optional
4036
@get:Option(option = "packageName", description = "Configures the package name of the resulting code.")
4137
abstract val packageName: Property<String>
4238

4339
@get:Input
44-
@get:Optional
4540
@get:Option(option = "specName", description = "Configures the name of the service for the Swagger Spec.")
4641
abstract val specName: Property<String>
4742

4843
@get:Input
49-
@get:Optional
5044
@get:Option(option = "specVersion", description = "Configures the version of the Swagger Spec.")
5145
abstract val specVersion: Property<String>
5246

@@ -55,7 +49,6 @@ abstract class GenerateTask : DefaultTask() {
5549
abstract val inputFile: RegularFileProperty
5650

5751
@get:OutputDirectory
58-
@get:Optional
5952
@get:Option(option = "outputDir", description = "Configures path of the Generated code directory.")
6053
abstract val outputDir: DirectoryProperty
6154

@@ -71,12 +64,12 @@ abstract class GenerateTask : DefaultTask() {
7164

7265
@TaskAction
7366
fun swaggerGenerate() {
74-
val platform = platform.getOrElse(DEFAULT_PLATFORM)
75-
val specName = specName.getOrElse(DEFAULT_NAME)
76-
val packageName = packageName.getOrElse(DEFAULT_PACKAGE)
77-
val outputDir = outputDir.getOrElse(project.layout.buildDirectory.dir(DEFAULT_OUTPUT_DIR).get()).asFile
67+
val platform = platform.get()
68+
val specName = specName.get()
69+
val packageName = packageName.get()
70+
val outputDir = outputDir.get().asFile
7871
val inputFile = inputFile.get().asFile
79-
val specVersion = specVersion.getOrElse(readVersionFromSpecfile(inputFile))
72+
val specVersion = specVersion.get()
8073

8174
val headersToRemove = features?.headersToRemove?.get() ?: emptyList()
8275

@@ -123,19 +116,4 @@ abstract class GenerateTask : DefaultTask() {
123116
val source = extraFiles.orNull?.asFile
124117
source?.copyRecursively(outputDir, overwrite = true)
125118
}
126-
127-
private fun readVersionFromSpecfile(specFile: File): String {
128-
val swaggerSpec = SwaggerParser().readWithInfo(specFile.absolutePath, listOf(), false).swagger
129-
130-
return when (val version = swaggerSpec.info.version) {
131-
is String -> {
132-
println("Successfully read version from Swagger Spec file: $version")
133-
version
134-
}
135-
else -> {
136-
println("Issue in reading version from Swagger Spec file. Falling back to $DEFAULT_VERSION")
137-
DEFAULT_VERSION
138-
}
139-
}
140-
}
141119
}
Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
package com.yelp.codegen.plugin
22

3+
import io.swagger.parser.SwaggerParser
4+
import java.io.File
35
import javax.inject.Inject
46
import org.gradle.api.Action
5-
import org.gradle.api.file.DirectoryProperty
6-
import org.gradle.api.file.RegularFileProperty
7-
import org.gradle.api.model.ObjectFactory
8-
import org.gradle.api.provider.Property
7+
import org.gradle.api.Project
98

10-
abstract class GenerateTaskConfiguration @Inject constructor(objectFactory: ObjectFactory) {
11-
abstract val platform: Property<String>
12-
abstract val packageName: Property<String>
13-
abstract val specName: Property<String>
14-
abstract val specVersion: Property<String>
15-
abstract val inputFile: RegularFileProperty
16-
abstract val outputDir: DirectoryProperty
17-
abstract val extraFiles: DirectoryProperty
9+
abstract class GenerateTaskConfiguration @Inject constructor(project: Project) {
10+
val objects = project.objects
1811

19-
val features: FeatureConfiguration = FeatureConfiguration(objectFactory)
12+
val platform = objects.property(String::class.java).convention("kotlin")
13+
val packageName = objects.property(String::class.java).convention("com.codegen.default")
14+
val specName = objects.property(String::class.java).convention("defaultname")
15+
val specVersion = objects.property(String::class.java).convention(project.provider {
16+
readVersionFromSpecfile(inputFile.get().asFile)
17+
})
18+
val inputFile = objects.fileProperty()
19+
val outputDir = objects.directoryProperty().convention(project.layout.buildDirectory.dir(DEFAULT_OUTPUT_DIR).get())
20+
21+
val extraFiles = objects.directoryProperty()
22+
val features: FeatureConfiguration = FeatureConfiguration(objects)
2023

2124
fun features(action: Action<FeatureConfiguration>) = action.execute(features)
25+
26+
companion object {
27+
private fun readVersionFromSpecfile(specFile: File): String {
28+
val swaggerSpec = SwaggerParser().readWithInfo(specFile.absolutePath, listOf(), false).swagger
29+
30+
return when (val version = swaggerSpec.info.version) {
31+
is String -> {
32+
println("Successfully read version from Swagger Spec file: $version")
33+
version
34+
}
35+
else -> {
36+
val defaultVersion = "0.0.0"
37+
println("Issue in reading version from Swagger Spec file. Falling back to $defaultVersion")
38+
defaultVersion
39+
}
40+
}
41+
}
42+
}
2243
}

0 commit comments

Comments
 (0)