Skip to content

Instantly share code, notes, and snippets.

@art-shen
Created August 3, 2019 02:03
Show Gist options
  • Select an option

  • Save art-shen/9c4d57dc5ff23d3b5d59617f042f5355 to your computer and use it in GitHub Desktop.

Select an option

Save art-shen/9c4d57dc5ff23d3b5d59617f042f5355 to your computer and use it in GitHub Desktop.
AndroidX migration kotlin (KTS) script
import java.io.File
import java.nio.file.*
import java.nio.file.attribute.BasicFileAttributes
// Simple helper for AndroidX migration, written as Kotlin script
// Can be better then Android Studio migration
// But use at your own risk ;)
val PROJECT_PATH = "<YOUR_PROJECT_PATH>"
// You can download a CSV files here:
// https://developer.android.com/topic/libraries/support-library/downloads/androidx-class-mapping.csv
// https://developer.android.com/topic/libraries/support-library/downloads/androidx-artifact-mapping.csv
// Put them in your project directory
val MAPPING_CLASS_FILE = "$PROJECT_PATH/androidx-class-mapping.csv"
val MAPPING_ARTIFACT_FILE = "$PROJECT_PATH/androidx-artifact-mapping.csv"
val Path.name: String get() = fileName.toString()
val Path.extension: String get() = name.substringAfterLast('.')
println("Migrating project to AndroidX: $PROJECT_PATH")
println(" Loading mapping file: $MAPPING_CLASS_FILE")
val mappingClassList = File(MAPPING_CLASS_FILE).readLines().drop(1)
.map { it.split(',', limit = 2).toTypedArray() }.toMutableList()
println(" => loaded ${mappingClassList.size} class mappings pairs")
println(" Loading mapping file: $MAPPING_ARTIFACT_FILE")
val mappingArtifactList = File(MAPPING_ARTIFACT_FILE).readLines().drop(1)
.map {
val parts = it.split(',', limit = 2)
// skip version in replacement
arrayOf(parts[0], parts[1].substringBeforeLast(':'))
}.toMutableList()
println(" => loaded ${mappingArtifactList.size} artifact mappings pairs")
val mapping = (mappingClassList + mappingArtifactList).toTypedArray()
var filesChecked = 0
var filesChanged = 0
Files.walkFileTree(Paths.get(PROJECT_PATH), object : SimpleFileVisitor<Path>() {
val ignoreDirs = hashSetOf(".idea", ".gradle", ".git", "build", "_")
val lookupExtensions = hashSetOf("kt","kts","java","xml","gradle","pro")
override fun preVisitDirectory(filePath: Path, attributes: BasicFileAttributes): FileVisitResult {
if (filePath.name in ignoreDirs) {
println(">> Skipping $filePath")
return FileVisitResult.SKIP_SUBTREE
}
return FileVisitResult.CONTINUE
}
override fun visitFile(filePath: Path, attributes: BasicFileAttributes): FileVisitResult {
if (filePath.extension in lookupExtensions) {
val file = filePath.toFile()
val origContent = file.readText()
var content = origContent
for ((a, b) in mapping)
content = content.replace(a, b)
if (content != origContent) {
file.writeText(content)
println("> Changed $filePath")
filesChanged++
}
val checked = filesChecked+1
filesChecked = checked
if (checked % 100 == 0)
println("$checked files checked")
}
return FileVisitResult.CONTINUE
}
})
println(">> $filesChecked files checked <<")
println(">> $filesChanged files changed <<")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment