Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Last active November 18, 2022 23:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benigumocom/e517f0b2a3cc802019a470a0222ad199 to your computer and use it in GitHub Desktop.
Save benigumocom/e517f0b2a3cc802019a470a0222ad199 to your computer and use it in GitHub Desktop.
// live template expression
// groovyScript("/absolute/path/to/inline.groovy", clipboard());
def cb = _1
def ls = System.lineSeparator()
return cb.split(ls).collect { l ->
def w = l.split("\"", -1)
if (w.size() == 3) { // single quoted string per a line
def s = w[1].split(":")
switch(s.size()) {
case 2: // plugins
"${w[0]}{ id = \"${s[0]}\", version = \"${s[1]}\" }${w[2]}"
break
case 3: // libraries
"${w[0]}{ module = \"${s[0]}:${s[1]}\", version = \"${s[2]}\" }${w[2]}"
break
default: // NG?
"${l} ###"
}
} else {
l
}
}.join(ls)
@benigumocom
Copy link
Author

【Android Studio】TOML内のすべてをインライン・テーブルに書き換える Live Templates 用 Groovy スクリプト
👉 https://android.benigumo.com/20221111/live-templates-tool/

@benigumocom
Copy link
Author

// def cb = _1
def cb = '''
[libraries]
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" }
androidx-cardview = "androidx.cardview:cardview:1.0.0"

[plugins]
android-application = "com.android.application:7.3.1"
kotlin-android = "org.jetbrains.kotlin.android:1.7.20"
'''

def ls = System.lineSeparator()

return cb.split(ls).collect() { l ->

  l.replaceAll(/^([a-z0-9\-]+) = "([^\:"]+):([^\:"]+)"/) { _, k, i, v ->
    "$k = { id = \"$i\", version = \"$v\" }"
  }.replaceAll(/^([a-z0-9\-]+) = "([^\:"]+):([^\:"]+):([^\:"]+)"/) { _, k, g, a, v ->
    "$k = { module = \"$g:$a\", version = \"$v\" }"
  }

}.join(ls)

@benigumocom
Copy link
Author

benigumocom commented Nov 17, 2022

groovyScript("def ls = System.lineSeparator();return _1.split(ls).collect() { l -> l.replaceAll(/^([a-z0-9\\-]+) = \"([^\\:\"]+):([^\\:\"]+)\"/) { _, k, i, v -> \"$k = { id = \\\"$i\\\", version = \\\"$v\\\" }\"}.replaceAll(/^([a-z0-9\\-]+) = \"([^\\:\"]+):([^\\:\"]+):([^\\:\"]+)\"/) { _, k, g, a, v -> \"$k = { module = \\\"$g:$a\\\", version = \\\"$v\\\" }\"}}.join(ls)", clipboard());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment