Skip to content

Instantly share code, notes, and snippets.

@LiewJunTung
Created July 23, 2019 10:09
Show Gist options
  • Save LiewJunTung/aa4eb16a5030b247f4b6c9abcb407641 to your computer and use it in GitHub Desktop.
Save LiewJunTung/aa4eb16a5030b247f4b6c9abcb407641 to your computer and use it in GitHub Desktop.
DefaultTask()
abstract class SymbolicLinkTask : DefaultTask() {
var actualFilePath: String? = null
var symbolicDirPath: String? = null
var rename: String? = null
@TaskAction
fun link() {
val aPath = actualFilePath ?: throw Exception("actualPath not set")
val sPath = symbolicDirPath ?: throw Exception("symbolicPath not set")
val actualFile = File(aPath)
if (!actualFile.exists()) {
throw Exception("$aPath not found")
}
val symbolicDirFile = File(sPath)
val pb = ProcessBuilder()
if (!symbolicDirFile.exists()) {
symbolicDirFile.mkdirs()
}
pb.directory(symbolicDirFile)
val symbolicRelativePath = symbolicDirFile.toPath().relativize(actualFile.toPath())
println(symbolicRelativePath.toString())
pb.command("ln", "-s", symbolicRelativePath.toString(), rename ?: ".")
pb.directory(symbolicDirFile)
pb.start()
}
}
tasks.register("linkHiTxt", SymbolicLinkTask::class) {
actualFilePath = "$projectDir/something/hi.txt"
symbolicDirPath = "$projectDir/something/some"
rename = "hello.txt"
}
tasks.register("linkDogTxt", SymbolicLinkTask::class) {
actualFilePath = "$projectDir/something/hi.txt"
symbolicDirPath = "$projectDir/something/some"
rename = "dog.txt"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment