bernaferrari / GradleKotlinConverter

Convert from Groovy to Kotlin DSL for Gradle, focused on Android.
https://gradle-kotlin-converter.vercel.app
Apache License 2.0
921 stars 61 forks source link

method calls conversion not done right #40

Closed nagkumar closed 2 years ago

nagkumar commented 3 years ago
minSdkVersion(24)
targetSdkVersion(30)

above should be converted to

minSdk = 24
targetSdk = 30
johanno commented 2 years ago

Almost same Issue:

android {
    compileSdk 31

    defaultConfig {
        applicationId "some.name"
        minSdk 30
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
 ...

was converted to

android {
    compileSdk 31

    defaultConfig {
        applicationId = "some.name"
        minSdk 30
        targetSdk 31
        versionCode 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    } 
...

but should be:

android {
    compileSdk = 31

    defaultConfig {
        applicationId = "some.name"
        minSdk = 30
        targetSdk = 31
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
 ...
5peak2me commented 2 years ago

it can fix by following code. in gradlekotlinconverter.kts

fun String.addEquals(): String {

    val compileSdk = "compileSdk"
    val signing = "keyAlias|keyPassword|storeFile|storePassword"
    val other = "multiDexEnabled|correctErrorTypes|javaMaxHeapSize|jumboMode|dimension|useSupportLibrary"
    val databinding = "dataBinding|viewBinding"
    val defaultConfig = "applicationId|minSdk|targetSdk|versionCode|versionName|testInstrumentationRunner"
    val negativeLookAhead = "(?!\\{)[^\\s]" // Don't want '{' as next word character

    val versionExp = """($compileSdk|$defaultConfig|$signing|$other|$databinding)\s*${negativeLookAhead}.*""".toRegex()

    return this.replace(versionExp) {
        val split = it.value.split(" ")

        // if there is more than one whitespace, the last().toIntOrNull() will find.
        if (split.lastOrNull { it.isNotBlank() } != null) {
            "${split[0]} = ${split.last()}"
        } else {
            it.value
        }
    }
}
bernaferrari commented 2 years ago

Thanks @5peak2me! I'll check and integrate your proposal.