-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from overpas/add-replace-usages-flag
Add --replace-usages flag
- Loading branch information
Showing
9 changed files
with
173 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
depstoml/src/main/kotlin/by/overpass/depstoml/ReplaceAll.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
depstoml/src/test/kotlin/by/overpass/depstoml/ReplacementTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
depstoml/src/test/resources/gradle/dependencies/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
11 changes: 11 additions & 0 deletions
11
depstoml/src/test/resources/gradle/versions-and-dependencies/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |