diff options
-rw-r--r-- | stage2/BasicBuild.scala | 5 | ||||
-rw-r--r-- | stage2/Lib.scala | 41 | ||||
-rw-r--r-- | test/test.scala | 11 |
3 files changed, 56 insertions, 1 deletions
diff --git a/stage2/BasicBuild.scala b/stage2/BasicBuild.scala index 42384db..f784299 100644 --- a/stage2/BasicBuild.scala +++ b/stage2/BasicBuild.scala @@ -136,6 +136,11 @@ trait BaseBuild extends DependencyImplementation with BuildInterface with Trigge def runClass: String = "Main" def run: ExitCode = lib.runMainIfFound( runClass, context.args, classLoader(context.classLoaderCache) ) + def clean = { + val arg = if (context.args.length > 1) context.args(1) else "" + lib.clean(scalaTarget, arg) + } + def test: Option[ExitCode] = Some(new lib.ReflectBuild( DirectoryDependency(projectDirectory++"/test").build diff --git a/stage2/Lib.scala b/stage2/Lib.scala index 118c9a4..e05085e 100644 --- a/stage2/Lib.scala +++ b/stage2/Lib.scala @@ -4,7 +4,7 @@ import java.io._ import java.net._ import java.lang.reflect.InvocationTargetException import java.nio.file.{Path =>_,_} -import java.nio.file.Files.readAllBytes +import java.nio.file.Files.{readAllBytes, deleteIfExists} import java.security.MessageDigest import java.util.jar._ import java.lang.reflect.Method @@ -199,6 +199,45 @@ final class Lib(logger: Logger) extends Stage1Lib(logger) with Scaffold{ } } + def clean (compileTarget : File, option: String) : ExitCode = { + /* recursively deletes folders*/ + def deleteRecursive(file: File): Boolean = { + if (file.isDirectory) { + file.listFiles.map(deleteRecursive(_)) + } + deleteIfExists(file.toPath) + } + + def listFilesToDelete(file: File): Unit = { + if (file.isDirectory) { + file.listFiles.map(listFilesToDelete(_)) + } + println(file.toString) + } + listFilesToDelete(compileTarget) + val delete = if (option != "-f") { + Option(System.console).getOrElse( + throw new Exception("Can't access Console. Try running `cbt direct clean` or `cbt clean -f`.") + ).readLine( + "Delete file(s) [y/n]: " + ).head.toLower + } else 'y' + + if (delete == 'y') { + logger.lib(s"""Cleaning ${compileTarget}""") + if (!compileTarget.exists) return ExitCode.Success + if (deleteRecursive(compileTarget)) { + logger.lib("Succeeded") + ExitCode.Success + } else { + logger.lib("Failed") + ExitCode.Failure + } + } else { + ExitCode.Failure + } + } + // file system helpers def basename(path: File): String = path.toString.stripSuffix("/").split("/").last def dirname(path: File): File = new File(realpath(path).string.stripSuffix("/").split("/").dropRight(1).mkString("/")) diff --git a/test/test.scala b/test/test.scala index 92d4abf..13209ff 100644 --- a/test/test.scala +++ b/test/test.scala @@ -72,6 +72,12 @@ object Main{ // assert(res.err == "", res.err) // FIXME: enable this } + def clean(path: String)(implicit logger: Logger) = { + val res = runCbt(path, Seq("clean", "-f")) + val debugToken = "clean " ++ path ++ " " + assertSuccess(res,debugToken) + } + logger.test( "Running tests " ++ _args.toList.toString ) val cache = cbtHome ++ "/cache" @@ -150,12 +156,16 @@ object Main{ usage("nothing") compile("nothing") + clean("nothing") usage("multi-build") compile("multi-build") + clean("multi-build") usage("simple") compile("simple") + clean("simple") usage("simple-fixed") compile("simple-fixed") + clean("simple-fixed") compile("../plugins/sbt_layout") compile("../plugins/scalafmt") @@ -172,6 +182,7 @@ object Main{ task("fastOptJS","../examples/scalajs-react-example/js") task("fullOptJS","../examples/scalajs-react-example/js") compile("../examples/uber-jar-example") + System.err.println(" DONE!") System.err.println( successes.toString ++ " succeeded, "++ failures.toString ++ " failed" ) |