Instead of `--emit-kotlin`, we're adding a `--lang` option defaulting to Java. Instead of `--use-java-style-names`, we're adding `--name-style` defaulting to, well, "default", but that can also be "java" (or potentially other styles as well).

This change also attempts to infer language if it is unspecified - in this case, if `--kt-file-per-type` is specified, but `--lang` is not, we assume that `--lang=kotlin` was intended.

Fixes #209

#209 envisioned a bigger revamp of the CLI, but on further thought we don't need to go to such lengths. Just providing options (instead of boolean flags) for a few params and introducing `--lang` are enough to keep the CLI reasonable.
This commit is contained in:
Ben Bader 2018-08-06 11:46:27 -07:00 коммит произвёл GitHub
Родитель 045fdefd20
Коммит c3686b52f8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 55 добавлений и 20 удалений

Просмотреть файл

@ -28,7 +28,7 @@ allprojects {
version VERSION
project.ext {
kotlin_version = '1.2.51'
kotlin_version = '1.2.60'
libraries = [
clikt: [

Просмотреть файл

@ -59,7 +59,7 @@ task compileTestCase(type: Exec) {
dependsOn jarTask
executable 'java'
args = ['-jar', jarTask.archivePath.absolutePath, "--use-java-style-names", "--out=$projectDir/build/generated-src/gen/java", "$projectDir/TestThrift.thrift"]
args = ['-jar', jarTask.archivePath.absolutePath, "--name-style=java", "--out=$projectDir/build/generated-src/gen/java", "$projectDir/TestThrift.thrift"]
}
afterEvaluate {

Просмотреть файл

@ -24,11 +24,13 @@ import com.github.ajalt.clikt.core.CliktCommand
import com.github.ajalt.clikt.output.TermUi
import com.github.ajalt.clikt.parameters.arguments.argument
import com.github.ajalt.clikt.parameters.arguments.multiple
import com.github.ajalt.clikt.parameters.options.default
import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.multiple
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.options.required
import com.github.ajalt.clikt.parameters.options.validate
import com.github.ajalt.clikt.parameters.types.choice
import com.github.ajalt.clikt.parameters.types.path
import com.microsoft.thrifty.gen.ThriftyCodeGenerator
import com.microsoft.thrifty.kgen.KotlinCodeGenerator
@ -51,6 +53,10 @@ import java.util.ArrayList
* [--list-type=java.util.ArrayList]
* [--set-type=java.util.HashSet]
* [--map-type=java.util.HashMap]
* [--lang=[java|kotlin]]
* [--kt-file-per-type]
* [--parcelable]
* [--use-android-annotations]
* file1.thrift
* file2.thrift
* ...
@ -79,7 +85,15 @@ import java.util.ArrayList
*/
class ThriftyCompiler {
private val cli = object : CliktCommand(name = "thrifty-compiler") {
enum class Language {
JAVA,
KOTLIN
}
private val cli = object : CliktCommand(
name = "thrifty-compiler",
help = "Generate Java or Kotlin code from .thrift files"
) {
val outputDirectory: Path by option("-o", "--out", help = "the output directory for generated files")
.path(fileOkay = false, folderOkay = true)
.required()
@ -89,6 +103,16 @@ class ThriftyCompiler {
.path(exists = true, folderOkay = true, fileOkay = false)
.multiple()
val language: Language? by option(
"-l", "--lang", help = "the target language for generated code. Default is java.")
.choice("java" to Language.JAVA, "kotlin" to Language.KOTLIN)
val nameStyle: FieldNamingPolicy by option(
"--name-style",
help = "Format style for generated names. Default is to leave names unaltered.")
.choice("default" to FieldNamingPolicy.DEFAULT, "java" to FieldNamingPolicy.JAVA)
.default(FieldNamingPolicy.DEFAULT)
val listTypeName: String? by option("--list-type", help = "when specified, the concrete type to use for lists")
val setTypeName: String? by option("--set-type", help = "when specified, the concrete type to use for sets")
val mapTypeName: String? by option("--map-type", help = "when specified, the concrete type to use for maps")
@ -101,13 +125,6 @@ class ThriftyCompiler {
help = "When set, generates Parcelable implementations for structs")
.flag(default = false)
val useJavaStyleNames by option("--use-java-style-names",
help = "When set, converts field names to standard java camelCase")
.flag(default = false)
val emitKotlin: Boolean by option("--kotlin", help = "Generate Kotlin instead of Java")
.flag(default = false)
val kotlinFilePerType: Boolean by option(
"--kt-file-per-type", help = "Generate one .kt file per type; default is one per namespace.")
.flag(default = false)
@ -139,17 +156,27 @@ class ThriftyCompiler {
return
}
val fieldNamingPolicy = if (useJavaStyleNames) FieldNamingPolicy.JAVA else FieldNamingPolicy.DEFAULT
val impliedLanguage = when {
kotlinFilePerType -> Language.KOTLIN
emitNullabilityAnnotations -> Language.JAVA
else -> null
}
if (emitKotlin) {
generateKotlin(schema, fieldNamingPolicy)
} else {
generateJava(schema, fieldNamingPolicy)
if (language != null && impliedLanguage != language) {
TermUi.echo(
"You specified $language, but provided options implying $impliedLanguage (which will be ignored).",
err = true)
}
when (language ?: impliedLanguage) {
null,
Language.JAVA -> generateJava(schema)
Language.KOTLIN -> generateKotlin(schema)
}
}
private fun generateJava(schema: Schema, fieldNamingPolicy: FieldNamingPolicy) {
var gen = ThriftyCodeGenerator(schema, fieldNamingPolicy)
private fun generateJava(schema: Schema) {
var gen = ThriftyCodeGenerator(schema, nameStyle)
listTypeName?.let { gen = gen.withListType(it) }
setTypeName?.let { gen = gen.withSetType(it) }
mapTypeName?.let { gen = gen.withMapType(it) }
@ -166,8 +193,16 @@ class ThriftyCompiler {
gen.generate(outputDirectory)
}
private fun generateKotlin(schema: Schema, fieldNamingPolicy: FieldNamingPolicy) {
val gen = KotlinCodeGenerator(fieldNamingPolicy)
private fun generateKotlin(schema: Schema) {
val gen = KotlinCodeGenerator(nameStyle)
if (emitNullabilityAnnotations) {
TermUi.echo("Warning: Nullability annotations are unnecessary in Kotlin and will not be generated")
}
if (emitParcelable) {
TermUi.echo("Warning: Parcel generation not yet implemented for Kotlin codegen")
}
if (kotlinFilePerType) {
gen.filePerType()

Просмотреть файл

@ -75,7 +75,7 @@ task kompileTestThrift(type: Exec) {
dependsOn jarTask
executable 'java'
args = ['-jar', jarTask.archivePath.absolutePath, "--out=$projectDir/build/generated-src/thrifty/kotlin", "--kotlin", "$projectDir/ClientThriftTest.thrift"]
args = ['-jar', jarTask.archivePath.absolutePath, "--out=$projectDir/build/generated-src/thrifty/kotlin", "--lang=kotlin", "$projectDir/ClientThriftTest.thrift"]
}
compileTestKotlin {