diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Args.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Args.kt index c7dad95b..8e274a6f 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Args.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Args.kt @@ -26,6 +26,9 @@ class Args { "actually running them") var dryRun: Boolean = false + @Parameter(names = arrayOf("--gc"), description = "Delete old files") + var gc: Boolean = false + @Parameter(names = arrayOf("--help", "--usage"), description = "Display the help") var usage: Boolean = false diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/Io.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/Io.kt new file mode 100644 index 00000000..3129bc0e --- /dev/null +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/Io.kt @@ -0,0 +1,68 @@ +package com.beust.kobalt.misc + +import java.io.File +import java.nio.file.Files +import java.nio.file.Paths +import java.nio.file.StandardCopyOption + +class Io(val dryRun: Boolean = false) { + fun mkdirs(dir: String) { + log("mkdirs $dir") + if (! dryRun) { + File(dir).mkdirs() + } + } + + fun rm(path: String) { + log("rm $path") + + if (! dryRun) { + File(path).deleteRecursively() + } + } + + fun moveFile(from: File, toDir: File) { + log("mv $from $toDir") + if (! dryRun) { + require(from.exists(), { -> "$from should exist" }) + require(from.isFile, { -> "$from should be a file" }) + require(toDir.isDirectory, { -> "$toDir should be a file"}) + + val toFile = File(toDir, from.name) + Files.move(Paths.get(from.absolutePath), Paths.get(toFile.absolutePath), StandardCopyOption.ATOMIC_MOVE) + } + } + + fun copyDirectory(from: File, toDir: File) { + log("cp -r $from $toDir") + + if (! dryRun) { + KFiles.copyRecursively(from, toDir) + require(from.exists(), { -> "$from should exist" }) + require(from.isDirectory, { -> println("$from should be a directory")}) + require(toDir.isDirectory, { -> println("$toDir should be a file")}) + + } + } + + fun rmDir(dir: File) { + log("rm -rf $dir") + + if (! dryRun) { + require(dir.isDirectory, { -> println("$dir should be a directory")}) + dir.deleteRecursively() + } + } + + fun mkdir(dir: File) { + log("rm -rf $dir") + if (! dryRun) { + dir.mkdirs() + } + } + + private fun log(s: String) { + log(1, "[Io] $s") + } + +} diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index a7072a66..a1a8478f 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -9,6 +9,7 @@ import com.beust.kobalt.app.* import com.beust.kobalt.app.remote.DependencyData import com.beust.kobalt.app.remote.KobaltClient import com.beust.kobalt.app.remote.KobaltServer +import com.beust.kobalt.internal.Gc import com.beust.kobalt.internal.KobaltSettings import com.beust.kobalt.internal.PluginInfo import com.beust.kobalt.internal.TaskManager @@ -169,7 +170,9 @@ private class Main @Inject constructor( server.run() } else { // Options that don't need Build.kt to be parsed first - if (args.update) { + if (args.gc) { + Gc().run() + } else if (args.update) { // --update updateKobalt.updateKobalt() } else { diff --git a/src/main/kotlin/com/beust/kobalt/internal/Gc.kt b/src/main/kotlin/com/beust/kobalt/internal/Gc.kt new file mode 100644 index 00000000..2bcb8f6c --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/internal/Gc.kt @@ -0,0 +1,41 @@ +package com.beust.kobalt.internal + +import com.beust.kobalt.api.Kobalt +import com.beust.kobalt.misc.Io +import com.beust.kobalt.misc.KFiles +import java.io.File +import java.nio.file.Files + +/** + * Clean up old Kobalt files. + */ +class Gc { + fun run() { + // Delete all the distributions except the current one + val dist = KFiles.distributionsDir + val version = Kobalt.version + val tmpDir = Files.createTempDirectory("kobalt") + + // Zipfile + val io = Io(dryRun = true) + val zipFileName = "kobalt-$version.zip" + io.moveFile(File(KFiles.joinDir(dist, zipFileName)), tmpDir.toFile()) + + // Distribution directory + val zipDirectoryName = "kobalt-$version" + val zipDirectory = File(KFiles.joinDir(dist, zipDirectoryName)) + io.copyDirectory(zipDirectory, tmpDir.toFile()) + + // Delete the whole dist directory and recreate it + File(dist).let { distFile -> + io.rmDir(distFile) + io.mkdir(distFile) + + // Copy the files back + tmpDir.toFile().absolutePath.let { fromPath -> + io.moveFile(File(KFiles.joinDir(fromPath, zipFileName)), distFile) + io.copyDirectory(File(KFiles.joinDir(fromPath, zipDirectoryName)), distFile) + } + } + } +}