Skip to content

Instantly share code, notes, and snippets.

@akshay2211
Created October 7, 2023 18:05
Show Gist options
  • Save akshay2211/32d2e66cbab48e44a7f7f05e51d9f851 to your computer and use it in GitHub Desktop.
Save akshay2211/32d2e66cbab48e44a7f7f05e51d9f851 to your computer and use it in GitHub Desktop.
val currentPackageName = extra["package.old"] as String
val newPackageName = extra["package.new"] as String
val currentPackage = currentPackageName.replace(".", "/")
val newPackage = newPackageName.replace(".", "/")
infix fun String.oldPath(main: String) = "$rootDir/$this/src/$main/kotlin/$currentPackage"
infix fun String.newPath(main: String) = "$rootDir/$this/src/$main/kotlin/$newPackage"
infix fun String.delete(main: String) =
"$rootDir/$this/src/$main/kotlin/${currentPackage.split("/")[0]}"
val copyPackageFiles by tasks.registering {
doFirst {
copy {
from("androidApp" oldPath "main")
into("androidApp" newPath "main")
}
copy {
from("shared" oldPath "commonMain")
into("shared" newPath "commonMain")
}
copy {
from("shared" oldPath "androidMain")
into("shared" newPath "androidMain")
}
copy {
from("shared" oldPath "desktopMain")
into("shared" newPath "desktopMain")
}
copy {
from("shared" oldPath "iosMain")
into("shared" newPath "iosMain")
}
copy {
from("shared" oldPath "jsMain")
into("shared" newPath "jsMain")
}
}
}
val renamePackage by tasks.registering {
doFirst {
rootDir.walk().filter { it.extension == "kt" || it.name == "build.gradle.kts" }
.forEach {
if (it.readText().contains(currentPackageName)) it.writeText(
it.readText().replace(currentPackageName, newPackageName)
)
}
}
}
val deleteOldPackage by tasks.registering {
doFirst {
delete("androidApp" delete "main")
delete("shared" delete "commonMain")
delete("shared" delete "androidMain")
delete("shared" delete "desktopMain")
delete("shared" delete "iosMain")
delete("shared" delete "jsMain")
}
}
deleteOldPackage { dependsOn(copyPackageFiles) }
copyPackageFiles { dependsOn(renamePackage) }
renamePackage { shouldRunAfter(deleteOldPackage) }
tasks.register<Sync>("packageMigrationTask") {
dependsOn(renamePackage)
dependsOn(copyPackageFiles)
dependsOn(deleteOldPackage)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment