Skip to content

Commit f5881d3

Browse files
committed
use conventions
1 parent 2af3deb commit f5881d3

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

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

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -71,50 +71,45 @@ abstract class GenerateTask : DefaultTask() {
7171

7272
@TaskAction
7373
fun swaggerGenerate() {
74-
val platform = platform.orNull
75-
val specName = specName.orNull
76-
var packageName = packageName.orNull
74+
platform.convention(DEFAULT_PLATFORM)
75+
specName.convention(DEFAULT_NAME)
76+
packageName.convention(DEFAULT_PACKAGE)
77+
outputDirectory.convention(project.objects.directoryProperty().value(project.layout.buildDirectory.dir(DEFAULT_OUTPUT_DIR)))
78+
platform.convention(DEFAULT_PLATFORM)
79+
specName.convention(project.provider { readVersionFromSpecfile(inputFile.get().asFile) })
7780

78-
val inputFile = inputFile.get().asFile
79-
val outputDirectory = outputDirectory.orNull?.asFile
80-
81-
val specVersion = specVersion.getOrElse(readVersionFromSpecfile(inputFile))
82-
83-
val defaultOutputDir = File(project.buildDir, DEFAULT_OUTPUT_DIR)
84-
val headersToRemove = features?.headersToRemove?.orNull ?: emptyList()
81+
val headersToRemove = features?.headersToRemove?.get() ?: emptyList()
8582

8683
println("""
8784
####################
8885
Yelp Swagger Codegen
8986
####################
90-
Platform ${'\t'} ${platform ?: "[ DEFAULT ] $DEFAULT_PLATFORM"}
91-
Package ${'\t'} ${packageName ?: "[ DEFAULT ] $DEFAULT_PACKAGE"}
92-
specName ${'\t'} ${specName ?: "[ DEFAULT ] $DEFAULT_NAME"}
93-
specVers ${'\t'} ${specVersion ?: "[ DEFAULT ] $DEFAULT_VERSION"}
87+
Platform ${'\t'} $platform
88+
Package ${'\t'} $packageName
89+
specName ${'\t'} $specName
90+
specVers ${'\t'} $specVersion
9491
input ${"\t\t"} $inputFile
95-
output ${"\t\t"} ${outputDirectory ?: "[ DEFAULT ] $defaultOutputDir"}
96-
groupId ${'\t'} ${packageName ?: "[ DEFAULT ] default"}
97-
artifactId ${'\t'} ${packageName ?: "[ DEFAULT ] com.codegen"}
92+
output ${"\t\t"} $outputDirectory
93+
groupId ${'\t'} $packageName
94+
artifactId ${'\t'} $packageName
9895
features ${'\t'} ${headersToRemove.joinToString(separator = ",", prefix = "[", postfix = "]")}
9996
""".trimIndent())
10097

101-
packageName = this.packageName.orNull ?: DEFAULT_PACKAGE
102-
10398
val params = mutableListOf<String>()
10499
params.add("-p")
105-
params.add(platform ?: DEFAULT_PLATFORM)
100+
params.add(platform.get())
106101
params.add("-s")
107-
params.add(specName ?: DEFAULT_NAME)
102+
params.add(specName.get())
108103
params.add("-v")
109-
params.add(specVersion ?: DEFAULT_VERSION)
104+
params.add(specVersion.get())
110105
params.add("-g")
111-
params.add(packageName.substringBeforeLast('.'))
106+
params.add(packageName.get().substringBeforeLast('.'))
112107
params.add("-a")
113-
params.add(packageName.substringAfterLast('.'))
108+
params.add(packageName.get().substringAfterLast('.'))
114109
params.add("-i")
115-
params.add(inputFile.toString())
110+
params.add(inputFile.get().asFile.toString())
116111
params.add("-o")
117-
params.add((outputDirectory ?: defaultOutputDir).toString())
112+
params.add((outputDirectory.get().asFile).toString())
118113

119114
if (headersToRemove.isNotEmpty()) {
120115
params.add("-ignoreheaders")
@@ -126,10 +121,8 @@ abstract class GenerateTask : DefaultTask() {
126121

127122
// Copy over the extra files.
128123
val source = extraFiles.orNull?.asFile
129-
val destin = outputDirectory
130-
if (source != null && destin != null) {
131-
source.copyRecursively(destin, overwrite = true)
132-
}
124+
val destination = outputDirectory.get().asFile
125+
source?.copyRecursively(destination, overwrite = true)
133126
}
134127

135128
private fun readVersionFromSpecfile(specFile: File): String {

0 commit comments

Comments
 (0)