Skip to content

Commit

Permalink
Support conversion of Groovy list (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavi2410 authored and bernaferrari committed Dec 3, 2019
1 parent 8acb4ee commit 36643d3
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions gradlekotlinconverter.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import java.awt.Toolkit
import java.awt.datatransfer.DataFlavor
import java.awt.datatransfer.StringSelection
import java.io.File
import java.io.IOException
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import kotlin.system.exitProcess
import kotlin.text.RegexOption.*

// Bernardo Ferrari
// APACHE-2 License
Expand Down Expand Up @@ -107,20 +109,30 @@ fun String.convertVariableDeclaration(): String {
}
}

// [items...]
// becomes
// listOf(items...)
fun String.convertArrayExpression(): String {
val arrayExp = """\[([^\]]*?)\]""".toRegex(DOT_MATCHES_ALL)

return this.replace(arrayExp) {
"listOf(${it.groupValues[1]})"
}
}

// apply plugin: "kotlin-android"
// becomes
// apply(plugin = "kotlin-android")
fun String.convertPlugins(): String {
val pluginsExp = "apply plugin:((\\s*\"\\S*\")|(\\s*[(]\"\\S*\"[)]))".toRegex()
val pluginsExp = """apply plugin: (\S+)""".toRegex()

return this.replace(pluginsExp) {
val pluginIdExp = "\"\\S*\"".toRegex()
val (pluginId) = it.destructured
// it identifies the plugin id and rebuilds the line.
"apply(plugin = ${pluginIdExp.find(it.value)?.value})"
"apply(plugin = $pluginId)"
}
}


// implementation ":epoxy-annotations"
// becomes
// implementation(":epoxy-annotations")
Expand Down Expand Up @@ -342,15 +354,12 @@ fun String.convertProguardFiles(): String {
fun String.convertExtToExtra(): String {

// get ext... but not ext { ... }
val outerExp = "(?!ext\\s*\\{)ext\\..*".toRegex()
val outerExp = """ext\.(\w+)\s*=\s*(.*)""".toRegex()

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

val name = (split.firstOrNull() ?: "").replace("ext.", "")
val value = split.lastOrNull() ?: ""
val (name, value) = it.destructured

"extra.set(\"$name\", $value)"
"extra[\"$name\"] = $value"
}
}

Expand Down Expand Up @@ -559,6 +568,7 @@ print("[${currentTimeFormatted()}] -- Starting conversion.. ")
val convertedText = textToConvert
.replaceApostrophes()
.replaceDefWithVal()
.convertArrayExpression()
.convertVariableDeclaration()
.convertPlugins()
.convertPluginsIntoOneBlock()
Expand Down

0 comments on commit 36643d3

Please sign in to comment.