-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add generated comments to Kotlin files (default true) #486
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,8 @@ import com.squareup.kotlinpoet.jvm.jvmField | |
import com.squareup.kotlinpoet.jvm.jvmStatic | ||
import com.squareup.kotlinpoet.tag | ||
import okio.ByteString | ||
import java.time.Instant | ||
import java.time.format.DateTimeFormatter | ||
import java.util.Locale | ||
|
||
private object Tags { | ||
|
@@ -149,6 +151,7 @@ class KotlinCodeGenerator( | |
private var emitJvmName: Boolean = false | ||
private var emitJvmStatic: Boolean = false | ||
private var emitBigEnums: Boolean = false | ||
private var emitFileComment: Boolean = true | ||
private var failOnUnknownEnumValues: Boolean = true | ||
private var generateServer: Boolean = false | ||
|
||
|
@@ -270,6 +273,10 @@ class KotlinCodeGenerator( | |
this.emitBigEnums = true | ||
} | ||
|
||
fun emitFileComment(emitFileComment: Boolean): KotlinCodeGenerator = apply { | ||
this.emitFileComment = emitFileComment | ||
} | ||
|
||
fun failOnUnknownEnumValues(value: Boolean = true): KotlinCodeGenerator = apply { | ||
this.failOnUnknownEnumValues = value | ||
} | ||
|
@@ -2567,6 +2574,10 @@ class KotlinCodeGenerator( | |
.addMember("%S", fileName) | ||
.build()) | ||
} | ||
|
||
if (emitFileComment) { | ||
addFileComment(FILE_COMMENT + DATE_FORMATTER.format(Instant.now())) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -2606,5 +2617,12 @@ class KotlinCodeGenerator( | |
private fun String.capitalize(): String { | ||
return this.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.US) else it.toString() } | ||
} | ||
|
||
companion object { | ||
private const val FILE_COMMENT = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how you feel about this copypasta; there wasn't an obvious shared place to put the constants but I'm happy to move. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd have imagined that after working in this codebase, you'd have seen enough copypasta from me not to worry about it 😆 (This is absolutely fine by me, thank you for asking) |
||
"Automatically generated by the Thrifty compiler; do not edit!\nGenerated on: " | ||
|
||
private val DATE_FORMATTER = DateTimeFormatter.ISO_INSTANT | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -343,7 +343,10 @@ class KotlinCodeGeneratorTest { | |
|const map<i32, list<string>> Maps = {1: [], 2: ["foo"]} | ||
""".trimMargin() | ||
|
||
val text = generate(thrift) { mapClassName("android.support.v4.util.ArrayMap") } | ||
val text = generate(thrift) { | ||
mapClassName("android.support.v4.util.ArrayMap") | ||
emitFileComment(false) | ||
} | ||
.single() | ||
.toString() | ||
|
||
|
@@ -427,6 +430,7 @@ class KotlinCodeGeneratorTest { | |
val file = generate(thrift) { | ||
emitJvmName() | ||
filePerNamespace() | ||
emitFileComment(false) | ||
} | ||
.single() | ||
file.shouldCompile() | ||
|
@@ -455,6 +459,7 @@ class KotlinCodeGeneratorTest { | |
val file = generate(thrift) { | ||
emitJvmName() | ||
filePerType() | ||
emitFileComment(false) | ||
} | ||
.single() | ||
file.shouldCompile() | ||
|
@@ -1346,8 +1351,25 @@ class KotlinCodeGeneratorTest { | |
file.shouldCompile() | ||
} | ||
|
||
@Test | ||
fun `Files should add generated comment`() { | ||
val thrift = """ | ||
|namespace kt test.comment | ||
| | ||
|const i32 FooNum = 42 | ||
""".trimMargin() | ||
|
||
val file = generate(thrift) { emitFileComment(true) } | ||
.single() | ||
file.shouldCompile() | ||
|
||
val lines = file.toString().split("\n") | ||
lines[0] shouldBe "// Automatically generated by the Thrifty compiler; do not edit!" | ||
lines[1] shouldContain "\\/\\/ Generated on: [0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\\.\\S+".toRegex() | ||
} | ||
|
||
private fun generate(thrift: String, config: (KotlinCodeGenerator.() -> KotlinCodeGenerator)? = null): List<FileSpec> { | ||
val configOrDefault = config ?: { this } | ||
val configOrDefault = config ?: { emitFileComment(false) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can also default this to true but it's a bit annoying to handle the changing timestamp. |
||
return KotlinCodeGenerator() | ||
.run(configOrDefault) | ||
.generate(load(thrift)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I'd prefer this to default to false, if only to minimize the disruption to others. AFAIK most people using thrifty are still manually checking in the generated code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@benjamin-bader I don't have a strong opinion either way, but it does default to true for Java sources, which means the default will change depending on whether you generate Java or Kotlin. I will defer to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You make a good point. I'd forgotten entirely about the Java code-generator, despite directly referring to it. Shows where our focus is, I suppose. Let's keep it on by default, then.