Created
July 23, 2019 10:09
-
-
Save LiewJunTung/aa4eb16a5030b247f4b6c9abcb407641 to your computer and use it in GitHub Desktop.
DefaultTask()
This file contains hidden or 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
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