From 52c9c74f52c35130781b8ea2f9df44b8d1addbb2 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Fri, 1 Mar 2019 13:32:15 +0300 Subject: [PATCH] CocoaPods: Use regex to split quoted compiler args --- .../plugin/cocoapods/KotlinCocoapodsPlugin.kt | 66 +++---------------- 1 file changed, 10 insertions(+), 56 deletions(-) diff --git a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/cocoapods/KotlinCocoapodsPlugin.kt b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/cocoapods/KotlinCocoapodsPlugin.kt index 114104071c6..6f1be8b9f75 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/cocoapods/KotlinCocoapodsPlugin.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/cocoapods/KotlinCocoapodsPlugin.kt @@ -37,68 +37,22 @@ internal class CocoapodsBuildDirs(val project: Project) { get() = root.resolve("defs") } -private enum class State { - CONSUME_ESCAPED, - CONSUME, - SKIP; -} - -/** - * Splits a string using a whitespace characters as delimiters. - * Ignores whitespaces in quotes and drops quotes, e.g. a string - * `foo "bar baz" qux="quux"` will be split into ["foo", "bar baz", "qux=quux"]. - */ -private fun String.splitQuotedArgs(): List { - if (isEmpty()) { - return emptyList() - } - - var state: State = State.SKIP - val result = mutableListOf() - val token = StringBuilder(length) - - forEachIndexed { index, char -> - when (state) { - State.CONSUME_ESCAPED -> { - when (char) { - '"' -> state = State.CONSUME // Skip `"` - else -> token.append(char) - } - } - State.CONSUME -> { - when { - char == '"' -> state = State.CONSUME_ESCAPED // Skip `"` - char.isWhitespace() -> { - state = State.SKIP - result.add(token.toString()) - token.setLength(0) - } - else -> token.append(char) - } - } - State.SKIP -> { - when { - char == '"' -> state = State.CONSUME_ESCAPED // Skip `"` - !char.isWhitespace() -> { - state = State.CONSUME - token.append(char) - } - } - } - } - } - if (token.isNotEmpty()) { - result.add(token.toString()) - } - return result -} - open class KotlinCocoapodsPlugin: Plugin { private fun KotlinMultiplatformExtension.supportedTargets() = targets .withType(KotlinNativeTarget::class.java) .matching { it.konanTarget.family == Family.IOS || it.konanTarget.family == Family.OSX } + /** + * Splits a string using a whitespace characters as delimiters. + * Ignores whitespaces in quotes and drops quotes, e.g. a string + * `foo "bar baz" qux="quux"` will be split into ["foo", "bar baz", "qux=quux"]. + */ + private fun String.splitQuotedArgs(): List = + Regex("""(?:[^\s"]|(?:"[^"]*"))+""").findAll(this).map { + it.value.replace("\"", "") + }.toList() + private fun createDefaultFrameworks(kotlinExtension: KotlinMultiplatformExtension) { kotlinExtension.supportedTargets().all { target -> target.binaries.framework {