Minor: reformat + clean warnings in org.jetbrains.kotlin.generators.arguments.test

This commit is contained in:
Nikolay Krasko
2021-12-13 13:38:00 +03:00
committed by teamcity
parent e733aa21d5
commit 00f0978b1d
2 changed files with 89 additions and 68 deletions
@@ -39,17 +39,19 @@ interface AdditionalGradleProperties {
object EmptyList : DefaultValues("emptyList()") object EmptyList : DefaultValues("emptyList()")
} }
fun generateKotlinGradleOptions(withPrinterToFile: (targetFile: File, Printer.()->Unit)->Unit) { fun generateKotlinGradleOptions(withPrinterToFile: (targetFile: File, Printer.() -> Unit) -> Unit) {
val apiSrcDir = File("libraries/tools/kotlin-gradle-plugin-api/src/main/kotlin") val apiSrcDir = File("libraries/tools/kotlin-gradle-plugin-api/src/main/kotlin")
val srcDir = File("libraries/tools/kotlin-gradle-plugin/src/main/kotlin") val srcDir = File("libraries/tools/kotlin-gradle-plugin/src/main/kotlin")
// common interface // common interface
val commonInterfaceFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinCommonToolOptions") val commonInterfaceFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinCommonToolOptions")
val commonOptions = gradleOptions<CommonToolArguments>() val commonOptions = gradleOptions<CommonToolArguments>()
val additionalOptions = gradleOptions<AdditionalGradleProperties>() val additionalOptions = gradleOptions<AdditionalGradleProperties>()
withPrinterToFile(File(apiSrcDir, commonInterfaceFqName)) { withPrinterToFile(file(apiSrcDir, commonInterfaceFqName)) {
generateInterface(commonInterfaceFqName, generateInterface(
commonOptions + additionalOptions) commonInterfaceFqName,
commonOptions + additionalOptions
)
} }
println("### Attributes common for JVM, JS, and JS DCE\n") println("### Attributes common for JVM, JS, and JS DCE\n")
@@ -57,10 +59,12 @@ fun generateKotlinGradleOptions(withPrinterToFile: (targetFile: File, Printer.()
val commonCompilerInterfaceFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions") val commonCompilerInterfaceFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions")
val commonCompilerOptions = gradleOptions<CommonCompilerArguments>() val commonCompilerOptions = gradleOptions<CommonCompilerArguments>()
withPrinterToFile(File(apiSrcDir, commonCompilerInterfaceFqName)) { withPrinterToFile(file(apiSrcDir, commonCompilerInterfaceFqName)) {
generateInterface(commonCompilerInterfaceFqName, generateInterface(
commonCompilerOptions, commonCompilerInterfaceFqName,
parentType = commonInterfaceFqName) commonCompilerOptions,
parentType = commonInterfaceFqName
)
} }
println("\n### Attributes common for JVM and JS\n") println("\n### Attributes common for JVM and JS\n")
@@ -69,20 +73,24 @@ fun generateKotlinGradleOptions(withPrinterToFile: (targetFile: File, Printer.()
// generate jvm interface // generate jvm interface
val jvmInterfaceFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions") val jvmInterfaceFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptions")
val jvmOptions = gradleOptions<K2JVMCompilerArguments>() val jvmOptions = gradleOptions<K2JVMCompilerArguments>()
withPrinterToFile(File(srcDir, jvmInterfaceFqName)) { withPrinterToFile(file(srcDir, jvmInterfaceFqName)) {
generateInterface(jvmInterfaceFqName, generateInterface(
jvmOptions, jvmInterfaceFqName,
parentType = commonCompilerInterfaceFqName) jvmOptions,
parentType = commonCompilerInterfaceFqName
)
} }
// generate jvm impl // generate jvm impl
val k2JvmCompilerArgumentsFqName = FqName(K2JVMCompilerArguments::class.qualifiedName!!) val k2JvmCompilerArgumentsFqName = FqName(K2JVMCompilerArguments::class.qualifiedName!!)
val jvmImplFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptionsBase") val jvmImplFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptionsBase")
withPrinterToFile(File(srcDir, jvmImplFqName)) { withPrinterToFile(file(srcDir, jvmImplFqName)) {
generateImpl(jvmImplFqName, generateImpl(
jvmInterfaceFqName, jvmImplFqName,
k2JvmCompilerArgumentsFqName, jvmInterfaceFqName,
commonOptions + commonCompilerOptions + jvmOptions) k2JvmCompilerArgumentsFqName,
commonOptions + commonCompilerOptions + jvmOptions
)
} }
println("\n### Attributes specific for JVM\n") println("\n### Attributes specific for JVM\n")
@@ -91,19 +99,23 @@ fun generateKotlinGradleOptions(withPrinterToFile: (targetFile: File, Printer.()
// generate js interface // generate js interface
val jsInterfaceFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinJsOptions") val jsInterfaceFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinJsOptions")
val jsOptions = gradleOptions<K2JSCompilerArguments>() val jsOptions = gradleOptions<K2JSCompilerArguments>()
withPrinterToFile(File(srcDir, jsInterfaceFqName)) { withPrinterToFile(file(srcDir, jsInterfaceFqName)) {
generateInterface(jsInterfaceFqName, generateInterface(
jsOptions, jsInterfaceFqName,
parentType = commonCompilerInterfaceFqName) jsOptions,
parentType = commonCompilerInterfaceFqName
)
} }
val k2JsCompilerArgumentsFqName = FqName(K2JSCompilerArguments::class.qualifiedName!!) val k2JsCompilerArgumentsFqName = FqName(K2JSCompilerArguments::class.qualifiedName!!)
val jsImplFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinJsOptionsBase") val jsImplFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinJsOptionsBase")
withPrinterToFile(File(srcDir, jsImplFqName)) { withPrinterToFile(file(srcDir, jsImplFqName)) {
generateImpl(jsImplFqName, generateImpl(
jsInterfaceFqName, jsImplFqName,
k2JsCompilerArgumentsFqName, jsInterfaceFqName,
commonOptions + commonCompilerOptions + jsOptions) k2JsCompilerArgumentsFqName,
commonOptions + commonCompilerOptions + jsOptions
)
} }
println("\n### Attributes specific for JS\n") println("\n### Attributes specific for JS\n")
@@ -112,43 +124,51 @@ fun generateKotlinGradleOptions(withPrinterToFile: (targetFile: File, Printer.()
// generate JS DCE interface and implementation // generate JS DCE interface and implementation
val jsDceInterfaceFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinJsDceOptions") val jsDceInterfaceFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinJsDceOptions")
val jsDceOptions = gradleOptions<K2JSDceArguments>() val jsDceOptions = gradleOptions<K2JSDceArguments>()
withPrinterToFile(File(srcDir, jsDceInterfaceFqName)) { withPrinterToFile(file(srcDir, jsDceInterfaceFqName)) {
generateInterface(jsDceInterfaceFqName, generateInterface(
jsDceOptions, jsDceInterfaceFqName,
parentType = commonInterfaceFqName) jsDceOptions,
parentType = commonInterfaceFqName
)
} }
val k2JsDceArgumentsFqName = FqName(K2JSDceArguments::class.qualifiedName!!) val k2JsDceArgumentsFqName = FqName(K2JSDceArguments::class.qualifiedName!!)
val jsDceImplFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinJsDceOptionsBase") val jsDceImplFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinJsDceOptionsBase")
withPrinterToFile(File(srcDir, jsDceImplFqName)) { withPrinterToFile(file(srcDir, jsDceImplFqName)) {
generateImpl(jsDceImplFqName, generateImpl(
jsDceInterfaceFqName, jsDceImplFqName,
k2JsDceArgumentsFqName, jsDceInterfaceFqName,
commonOptions + jsDceOptions) k2JsDceArgumentsFqName,
commonOptions + jsDceOptions
)
} }
// generate multiplatform common interface and implementation // generate multiplatform common interface and implementation
val multiplatformCommonInterfaceFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformCommonOptions") val multiplatformCommonInterfaceFqName = FqName("org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformCommonOptions")
val multiplatformCommonOptions = gradleOptions<K2MetadataCompilerArguments>() val multiplatformCommonOptions = gradleOptions<K2MetadataCompilerArguments>()
withPrinterToFile(File(srcDir, multiplatformCommonInterfaceFqName)) { withPrinterToFile(file(srcDir, multiplatformCommonInterfaceFqName)) {
generateInterface(multiplatformCommonInterfaceFqName, generateInterface(
multiplatformCommonOptions, multiplatformCommonInterfaceFqName,
parentType = commonCompilerInterfaceFqName) multiplatformCommonOptions,
parentType = commonCompilerInterfaceFqName
)
} }
val k2metadataCompilerArgumentsFqName = FqName(K2MetadataCompilerArguments::class.qualifiedName!!) val k2metadataCompilerArgumentsFqName = FqName(K2MetadataCompilerArguments::class.qualifiedName!!)
val multiplatformCommonImplFqName = FqName(multiplatformCommonInterfaceFqName.asString() + "Base") val multiplatformCommonImplFqName = FqName(multiplatformCommonInterfaceFqName.asString() + "Base")
withPrinterToFile(File(srcDir, multiplatformCommonImplFqName)) { withPrinterToFile(file(srcDir, multiplatformCommonImplFqName)) {
generateImpl(multiplatformCommonImplFqName, generateImpl(
multiplatformCommonInterfaceFqName, multiplatformCommonImplFqName,
k2metadataCompilerArgumentsFqName, multiplatformCommonInterfaceFqName,
commonOptions + commonCompilerOptions + multiplatformCommonOptions) k2metadataCompilerArgumentsFqName,
commonOptions + commonCompilerOptions + multiplatformCommonOptions
)
} }
} }
fun main() { fun main() {
fun getPrinter(file: File, fn: Printer.()->Unit) { fun getPrinter(file: File, fn: Printer.() -> Unit) {
if (!file.exists()) { if (!file.exists()) {
file.parentFile.mkdirs() file.parentFile.mkdirs()
file.createNewFile() file.createNewFile()
@@ -170,9 +190,9 @@ private inline fun <reified T : Any> List<KProperty1<T, *>>.filterToBeDeleted()
} }
private inline fun <reified T : Any> gradleOptions(): List<KProperty1<T, *>> = private inline fun <reified T : Any> gradleOptions(): List<KProperty1<T, *>> =
T::class.declaredMemberProperties.filter { it.findAnnotation<GradleOption>() != null }.filterToBeDeleted().sortedBy { it.name } T::class.declaredMemberProperties.filter { it.findAnnotation<GradleOption>() != null }.filterToBeDeleted().sortedBy { it.name }
private fun File(baseDir: File, fqName: FqName): File { private fun file(baseDir: File, fqName: FqName): File {
val fileRelativePath = fqName.asString().replace(".", "/") + ".kt" val fileRelativePath = fqName.asString().replace(".", "/") + ".kt"
return File(baseDir, fileRelativePath) return File(baseDir, fileRelativePath)
} }
@@ -190,10 +210,10 @@ private fun Printer.generateInterface(type: FqName, properties: List<KProperty1<
} }
private fun Printer.generateImpl( private fun Printer.generateImpl(
type: FqName, type: FqName,
parentType: FqName, parentType: FqName,
argsType: FqName, argsType: FqName,
properties: List<KProperty1<*, *>> properties: List<KProperty1<*, *>>
) { ) {
generateDeclaration("internal abstract class", type, afterType = ": $parentType") { generateDeclaration("internal abstract class", type, afterType = ": $parentType") {
fun KProperty1<*, *>.backingField(): String = "${this.name}Field" fun KProperty1<*, *>.backingField(): String = "${this.name}Field"
@@ -241,10 +261,10 @@ private fun Printer.generateImpl(
} }
private fun Printer.generateDeclaration( private fun Printer.generateDeclaration(
modifiers: String, modifiers: String,
type: FqName, type: FqName,
afterType: String? = null, afterType: String? = null,
generateBody: Printer.()->Unit generateBody: Printer.() -> Unit
) { ) {
println("// DO NOT EDIT MANUALLY!") println("// DO NOT EDIT MANUALLY!")
println("// Generated by org/jetbrains/kotlin/generators/arguments/GenerateGradleOptions.kt") println("// Generated by org/jetbrains/kotlin/generators/arguments/GenerateGradleOptions.kt")
@@ -288,7 +308,7 @@ private fun Printer.generateDoc(property: KProperty1<*, *>) {
println(" */") println(" */")
} }
private inline fun Printer.withIndent(fn: Printer.()->Unit) { private inline fun Printer.withIndent(fn: Printer.() -> Unit) {
pushIndent() pushIndent()
fn() fn()
popIndent() popIndent()
@@ -315,10 +335,10 @@ private fun generateMarkdown(properties: List<KProperty1<*, *>>) {
} }
private val KProperty1<*, *>.gradleValues: DefaultValues private val KProperty1<*, *>.gradleValues: DefaultValues
get() = findAnnotation<GradleOption>()!!.value.objectInstance!! get() = findAnnotation<GradleOption>()!!.value.objectInstance!!
private val KProperty1<*, *>.gradleDefaultValue: String private val KProperty1<*, *>.gradleDefaultValue: String
get() = gradleValues.defaultValue get() = gradleValues.defaultValue
private val KProperty1<*, *>.gradleBackingFieldVisibility: KVisibility private val KProperty1<*, *>.gradleBackingFieldVisibility: KVisibility
get() { get() {
@@ -330,17 +350,17 @@ private val KProperty1<*, *>.gradleBackingFieldVisibility: KVisibility
} }
private val KProperty1<*, *>.gradleReturnType: String private val KProperty1<*, *>.gradleReturnType: String
get() { get() {
// Set nullability based on Gradle default value // Set nullability based on Gradle default value
var type = returnType.withNullability(false).toString().substringBeforeLast("!") var type = returnType.withNullability(false).toString().substringBeforeLast("!")
if (gradleDefaultValue == "null") { if (gradleDefaultValue == "null") {
type += "?" type += "?"
}
return type
} }
return type
}
private inline fun <reified T> KAnnotatedElement.findAnnotation(): T? = private inline fun <reified T> KAnnotatedElement.findAnnotation(): T? =
annotations.filterIsInstance<T>().firstOrNull() annotations.filterIsInstance<T>().firstOrNull()
object DeprecatedOptionAnnotator { object DeprecatedOptionAnnotator {
fun generateOptionAnnotation(annotation: DeprecatedOption): String { fun generateOptionAnnotation(annotation: DeprecatedOption): String {
@@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
@file:Suppress("PackageDirectoryMismatch")
package org.jetbrains.kotlin.generators.arguments.test package org.jetbrains.kotlin.generators.arguments.test
import junit.framework.TestCase import junit.framework.TestCase
@@ -26,7 +27,7 @@ import java.io.PrintStream
class GenerateKotlinGradleOptionsTest : TestCase() { class GenerateKotlinGradleOptionsTest : TestCase() {
fun testKotlinGradleOptionsAreUpToDate() { fun testKotlinGradleOptionsAreUpToDate() {
fun getPrinter(file: File, fn: Printer.()->Unit) { fun getPrinter(file: File, fn: Printer.() -> Unit) {
val bytesOut = ByteArrayOutputStream() val bytesOut = ByteArrayOutputStream()
PrintStream(bytesOut).use { PrintStream(bytesOut).use {