1
1
package com.yelp.codegen.plugin
2
2
3
3
import com.yelp.codegen.main
4
- import io.swagger.parser.SwaggerParser
5
- import java.io.File
6
4
import org.gradle.api.DefaultTask
5
+ import org.gradle.api.file.DirectoryProperty
6
+ import org.gradle.api.file.RegularFileProperty
7
7
import org.gradle.api.plugins.BasePlugin
8
+ import org.gradle.api.provider.Property
8
9
import org.gradle.api.tasks.Input
9
10
import org.gradle.api.tasks.InputFile
10
11
import org.gradle.api.tasks.InputFiles
@@ -14,126 +15,99 @@ import org.gradle.api.tasks.OutputDirectory
14
15
import org.gradle.api.tasks.TaskAction
15
16
import org.gradle.api.tasks.options.Option
16
17
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 () {
24
19
25
20
init {
26
21
description = " Run the Swagger Code Generation tool"
27
22
group = BasePlugin .BUILD_GROUP
28
23
}
29
24
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" ,
62
52
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
64
54
65
- @Nested
66
- @Option(option = " featureHeaderToRemove" , description = " " )
55
+ @get: Nested
56
+ @get: Option(option = " featureHeaderToRemove" , description = " " )
67
57
var features: FeatureConfiguration ? = null
68
58
69
59
@TaskAction
70
60
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()
71
67
72
- if (specVersion == null ) {
73
- readVersionFromSpecfile(inputFile)
74
- }
75
- val defaultOutputDir = File (project.buildDir, DEFAULT_OUTPUT_DIR )
68
+ val headersToRemove = features?.headersToRemove?.get() ? : emptyList()
76
69
77
70
println ("""
78
71
####################
79
72
Yelp Swagger Codegen
80
73
####################
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
85
78
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 = " ] " ) }
90
83
""" .trimIndent())
91
84
92
- val packageName = packageName ? : DEFAULT_PACKAGE
93
-
94
85
val params = mutableListOf<String >()
95
86
params.add(" -p" )
96
- params.add(platform ? : DEFAULT_PLATFORM )
87
+ params.add(platform)
97
88
params.add(" -s" )
98
- params.add(specName ? : DEFAULT_NAME )
89
+ params.add(specName)
99
90
params.add(" -v" )
100
- params.add(specVersion ? : DEFAULT_VERSION )
91
+ params.add(specVersion)
101
92
params.add(" -g" )
102
93
params.add(packageName.substringBeforeLast(' .' ))
103
94
params.add(" -a" )
104
95
params.add(packageName.substringAfterLast(' .' ))
105
96
params.add(" -i" )
106
97
params.add(inputFile.toString())
107
98
params.add(" -o" )
108
- params.add(( outputDir ? : defaultOutputDir) .toString())
99
+ params.add(outputDir.toString())
109
100
110
- if (true == features?. headersToRemove? .isNotEmpty()) {
101
+ if (headersToRemove.isNotEmpty()) {
111
102
params.add(" -ignoreheaders" )
112
- params.add(features?. headersToRemove? .joinToString(" ," ) ? : " " )
103
+ params.add(headersToRemove.joinToString(" ," ))
113
104
}
114
105
115
106
// Running the Codegen Main here
116
107
main(params.toTypedArray())
117
108
118
109
// 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 )
138
112
}
139
113
}
0 commit comments