Skip to content

Commit

Permalink
Fix common non-major bugs (#36)
Browse files Browse the repository at this point in the history
* Fix bug of converting some gradle dependencies

kaptTest and kaptAndroidTest have been fixed

* Support converting "apply from"

* Support exclude modules & groups from dependencies

* Support fileTree conversion

* Fix wrong conversion of Gradle dependency + "{"

By bypassing sth like:
implementation(":epoxy-annotations") { ... }

* Support converting a boolean flag properly

"transitive" to "isTransitive"
  • Loading branch information
IslamSalah authored Apr 25, 2021
1 parent 21da8d7 commit b24e3e9
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion gradlekotlinconverter.kts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ fun String.convertPlugins(): String {
}
}

// apply from: "kotlin-android"
// becomes
// apply(from = "kotlin-android")
fun String.convertPluginsFrom(): String {
val pluginsExp = """apply from: (\S+)""".toRegex()

return this.replace(pluginsExp) {
val (pluginId) = it.destructured
"apply(from = $pluginId)"
}
}

fun String.convertAndroidBuildConfigFunctions(): String {
val outerExp = """(buildConfigField|resValue|flavorDimensions|exclude|java.srcDir)\s+(".*")""".toRegex()
// packagingOptions > exclude
Expand Down Expand Up @@ -239,13 +251,15 @@ fun String.convertCompileToImplementation(): String {
fun String.convertDependencies(): String {

val testKeywords = "testImplementation|androidTestImplementation|debugImplementation|compileOnly|testCompileOnly|runtimeOnly|developmentOnly"
val gradleKeywords = "($testKeywords|implementation|api|annotationProcessor|classpath|kapt|kaptTest|kaptAndroidTest|check)".toRegex()
val gradleKeywords = "($testKeywords|implementation|api|annotationProcessor|classpath|kaptTest|kaptAndroidTest|kapt|check)".toRegex()

// ignore cases like kapt { correctErrorTypes = true } and apply plugin: ('kotlin-kapt") but pass kapt("...")
// ignore keyWord followed by a space and a { or a " and a )
val validKeywords = "(?!$gradleKeywords\\s*(\\{|\"\\)|\\.))$gradleKeywords.*".toRegex()

return this.replace(validKeywords) { substring ->
// By pass sth like: implementation(":epoxy-annotations") { ... }
if (substring.value.contains("""\)(\s*)\{""".toRegex())) return@replace substring.value

// retrieve the comment [//this is a comment], if any
val comment = "\\s*\\/\\/.*".toRegex().find(substring.value)?.value ?: ""
Expand All @@ -268,6 +282,15 @@ fun String.convertDependencies(): String {
}
}

// fileTree(dir: "libs", include: ["*.jar"])
// becomes
// fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar")))
fun String.convertFileTree(): String {
val fileTreeString = """fileTree\(dir(\s*):(\s*)"libs"(\s*),(\s*)include(\s*):(\s*)\["\*.jar"\]\)""".toRegex()

return this.replace(fileTreeString, """fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar")))""")
}


// signingConfig signingConfigs.release
// becomes
Expand Down Expand Up @@ -510,6 +533,7 @@ fun String.convertInternalBlocks(): String {
.addIsToStr("buildTypes", "debuggable")
.addIsToStr("buildTypes", "minifyEnabled")
.addIsToStr("buildTypes", "shrinkResources")
.addIsToStr("", "transitive")
}

fun String.addIsToStr(blockTitle: String, transform: String): String {
Expand Down Expand Up @@ -547,6 +571,8 @@ fun String.convertInclude(): String {
val includeExp = "include$expressionBase".toRegex()

return this.replace(includeExp) { includeBlock ->
if(includeBlock.value.contains("include\"")) return@replace includeBlock.value // exclude: "include" to

// avoid cases where some lines at the start/end are blank
val multiLine = includeBlock.value.split('\n').count { it.isNotBlank() } > 1

Expand Down Expand Up @@ -582,6 +608,29 @@ fun String.convertExcludeClasspath(): String {
}
}

// exclude module: 'module-id'
// becomes
// exclude(module = "module-id")
fun String.convertExcludeModules(): String {
val fullLineExp = """exclude module: (\S+)""".toRegex()

return this.replace(fullLineExp) {
val (moduleId) = it.destructured
"exclude(module = $moduleId)"
}
}

// exclude group: 'group-id'
// becomes
// exclude(group = "group-id")
fun String.convertExcludeGroups(): String {
val fullLineExp = """exclude group: (\S+)""".toRegex()

return this.replace(fullLineExp) {
val (groupId) = it.destructured
"exclude(group = $groupId)"
}
}

// classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// becomes
Expand Down Expand Up @@ -669,11 +718,13 @@ val convertedText = textToConvert
.replaceApostrophes()
.replaceDefWithVal()
.convertMapExpression() // Run before array
.convertFileTree()
.convertArrayExpression()
.convertManifestPlaceHoldersWithMap() // Run after convertMapExpression
.convertVariableDeclaration()
.convertPlugins()
.convertPluginsIntoOneBlock()
.convertPluginsFrom()
.convertVariantFilter()
.convertAndroidBuildConfigFunctions()
.convertCompileToImplementation()
Expand All @@ -691,6 +742,8 @@ val convertedText = textToConvert
.convertSourceSets()
.convertSigningConfigs()
.convertExcludeClasspath()
.convertExcludeModules()
.convertExcludeGroups()
.convertJetBrainsKotlin()
.convertSigningConfigBuildType()
.convertExtToExtra()
Expand Down

0 comments on commit b24e3e9

Please sign in to comment.