diff --git a/gradlekotlinconverter.kts b/gradlekotlinconverter.kts index 46cdedc..5026ff0 100755 --- a/gradlekotlinconverter.kts +++ b/gradlekotlinconverter.kts @@ -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 @@ -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 ?: "" @@ -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 @@ -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 { @@ -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 @@ -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 @@ -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() @@ -691,6 +742,8 @@ val convertedText = textToConvert .convertSourceSets() .convertSigningConfigs() .convertExcludeClasspath() + .convertExcludeModules() + .convertExcludeGroups() .convertJetBrainsKotlin() .convertSigningConfigBuildType() .convertExtToExtra()