Skip to content

Commit

Permalink
Merge pull request #3 from overpas/add-replace-usages-flag
Browse files Browse the repository at this point in the history
Add --replace-usages flag
  • Loading branch information
overpas authored Jun 10, 2023
2 parents 267fee2 + 8d0b036 commit 859b6f2
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Sample toml value: `gradle/libs.versions.toml`

#### Options
- `-o`, `--order-lexicographically` - if you want to sort the dependencies lexicographically
- `-r`, `--replace-usages` - if you want to replace usages of old dependencies with new ones in build.gradle.kts files

#### Arguments
- `deps` - path to Kotlin dependencies file
Expand Down
2 changes: 1 addition & 1 deletion depstoml/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "by.overpass"
version = "0.1"
version = "0.2"

application {
mainClass.set("by.overpass.depstoml.MainKt")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option
import com.github.ajalt.clikt.parameters.types.file
import java.io.File
import java.nio.file.Files
import kotlin.io.path.Path
import kotlin.io.path.extension

class ConvertDependenciesToToml : CliktCommand(
name = "depstoml",
Expand All @@ -29,20 +32,35 @@ class ConvertDependenciesToToml : CliktCommand(
help = "order libraries lexicographically",
).flag(default = false)

private val replaceUsages by option(
names = arrayOf("-r", "--replace-usages"),
help = "replace artifact usages with new identifiers"
).flag(default = false)

override fun run() {
dependenciesFile.asKotlinFile()
?.extractDepsTree()
?.createTomlConfig(
orderLexicographically = orderLexicographically,
)
?.writeTo(
tomlFile.apply {
if (!exists()) {
parentFile?.mkdirs()
createNewFile()
?.apply {
createTomlConfig(orderLexicographically)
.writeTo(
tomlFile.apply {
if (!exists()) {
parentFile?.mkdirs()
createNewFile()
}
},
)
if (replaceUsages) {
val replacements = findReplacements()
Files.find(
Path(""),
Int.MAX_VALUE,
{ path, _ -> path.extension == "kts" },
).forEach { path ->
path.replaceAll(replacements)
}
},
)
}
}
?: System.err.println("Kotlin Dependencies object not found in file: ${dependenciesFile.path}")
}
}
21 changes: 20 additions & 1 deletion depstoml/src/main/kotlin/by/overpass/depstoml/DepsTreeNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private fun MutableList<MutableList<String>>.format(orderLexicographically: Bool
if (path.matches(libraryRegex) || path.matches(pluginRegex)) {
formattedList += path
} else {
formattedList += separateIds(this[i][j])
formattedList += separateIds(path)
}
}
this[i] = formattedList
Expand All @@ -106,6 +106,25 @@ private fun MutableList<MutableList<String>>.format(orderLexicographically: Bool
}
}

fun DepsTreeNode.findReplacements(): Map<String, String> = getAllLeafPaths()
.findReplacements()

private fun List<List<String>>.findReplacements(): Map<String, String> {
val map = mutableMapOf<String, String>()
for (path in this) {
val oldIdentifier = path
.subList(1, path.size - 1)
.joinToString(".")
val newPath = mutableListOf("libs")
for (j in 2 until path.size - 1) {
newPath += separateIds(path[j])
}
val newIdentifier = newPath.joinToString(".")
map += oldIdentifier to newIdentifier
}
return map
}

private fun createVersion(path: List<String>): Version {
return Version(
key = path.subList(0, path.size - 1).joinToString("-"),
Expand Down
15 changes: 15 additions & 0 deletions depstoml/src/main/kotlin/by/overpass/depstoml/ReplaceAll.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package by.overpass.depstoml

import java.nio.file.Files
import java.nio.file.Path

fun Path.replaceAll(replacements: Map<String, String>) {
var fileString = Files.readString(this)
if (replacements.keys.none(fileString::contains)) {
return
}
for ((old, new) in replacements) {
fileString = fileString.replace(old, new)
}
Files.writeString(this, fileString)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals

class DepstomlTest {
class ConversionTest {

private val tomlPath = "build/generated/main/kotlin/by/overpass/depstoml/libs.versions.toml"

Expand Down
85 changes: 85 additions & 0 deletions depstoml/src/test/kotlin/by/overpass/depstoml/ReplacementTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package by.overpass.depstoml

import java.io.File
import java.nio.file.Files
import kotlin.io.path.Path
import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@RunWith(Parameterized::class)
class ReplacementTest(
private val dependenciesFilePath: String,
buildGradlePath: String,
private val expected: String,
) {

companion object {

@JvmStatic
@Parameterized.Parameters
fun data(): Collection<Array<Any>> = listOf(
arrayOf(
"src/test/resources/DependenciesFile.kt",
"src/test/resources/gradle/dependencies/build.gradle.kts",
""" |plugins {
| alias(libs.plugins.ksp)
|}
|
|dependencies {
| implementation(libs.kotlin.gradle.plugin)
| implementation(libs.kotlin.date.time)
| implementation(libs.kotlin.coroutines.core)
| implementation(libs.log.napier)
| testImplementation(libs.kotlin.coroutines.test)
|}
|""".trimMargin(),
),
arrayOf(
"src/test/resources/VersionsAndDependenciesFile.kt",
"src/test/resources/gradle/versions-and-dependencies/build.gradle.kts",
""" |plugins {
| alias(libs.plugins.ksp)
|}
|
|dependencies {
| implementation(libs.kotlin.gradle.plugin)
| implementation(libs.kotlin.date.time)
| implementation(libs.kotlin.coroutines.core)
| implementation(libs.log.napier)
| testImplementation(libs.kotlin.coroutines.test)
|}
|""".trimMargin(),
),
)
}

private val tomlPath = "build/generated/main/kotlin/by/overpass/depstoml/libs.versions.toml"
private val buildGradlePath = Path(buildGradlePath)
private val initialBuildGradleText = Files.readString(this.buildGradlePath)

private val depstomlCommand = ConvertDependenciesToToml()

@Test
fun `usages are replaced in build gradle kts`() {
depstomlCommand.main(
arrayOf(
dependenciesFilePath,
"-r",
tomlPath,
),
)

val actual = Files.readString(buildGradlePath)

assertEquals(expected, actual)
}

@AfterTest
fun teardown() {
File(tomlPath).delete()
Files.writeString(buildGradlePath, initialBuildGradleText)
}
}
11 changes: 11 additions & 0 deletions depstoml/src/test/resources/gradle/dependencies/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
alias(Dependencies.Plugins.ksp)
}

dependencies {
implementation(Dependencies.Kotlin.gradlePlugin)
implementation(Dependencies.Kotlin.dateTime)
implementation(Dependencies.Kotlin.Coroutines.core)
implementation(Dependencies.Log.napier)
testImplementation(Dependencies.Kotlin.Coroutines.test)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
alias(Deps.Plugins.ksp)
}

dependencies {
implementation(Deps.Kotlin.gradlePlugin)
implementation(Deps.Kotlin.dateTime)
implementation(Deps.Kotlin.Coroutines.core)
implementation(Deps.Log.napier)
testImplementation(Deps.Kotlin.Coroutines.test)
}

0 comments on commit 859b6f2

Please sign in to comment.