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