diff options
-rw-r--r-- | stage2/BasicBuild.scala | 10 | ||||
-rw-r--r-- | stage2/Lib.scala | 56 | ||||
-rw-r--r-- | test/test.scala | 24 |
3 files changed, 89 insertions, 1 deletions
diff --git a/stage2/BasicBuild.scala b/stage2/BasicBuild.scala index 42384db..410d68f 100644 --- a/stage2/BasicBuild.scala +++ b/stage2/BasicBuild.scala @@ -136,6 +136,16 @@ 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 = { + lib.clean( + target, + context.args.contains("force"), + context.args.contains("dry-run"), + context.args.contains("list"), + context.args.contains("help") + ) + } + def test: Option[ExitCode] = Some(new lib.ReflectBuild( DirectoryDependency(projectDirectory++"/test").build diff --git a/stage2/Lib.scala b/stage2/Lib.scala index 118c9a4..5b175c1 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, delete} import java.security.MessageDigest import java.util.jar._ import java.lang.reflect.Method @@ -199,6 +199,60 @@ final class Lib(logger: Logger) extends Stage1Lib(logger) with Scaffold{ } } + def clean(target: File, force: Boolean, dryRun: Boolean, list: Boolean, help: Boolean): ExitCode = { + def depthFirstFileStream(file: File): Vector[File] = { + ( + if (file.isDirectory) { + file.listFiles.toVector.flatMap(depthFirstFileStream(_)) + } else Vector() + ) :+ file + } + lazy val files = depthFirstFileStream( target ) + + if( help ){ + System.err.println( s""" + list lists files to be delete + force does not ask for confirmation + dry-run does not actually delete files +""" ) + ExitCode.Success + } else if (!target.exists){ + System.err.println( "Nothing to clean. Does not exist: " ++ target.string ) + ExitCode.Success + } else if( list ){ + files.map(_.string).foreach( println ) + ExitCode.Success + } else { + val performDelete = ( + force || { + val console = Option(System.console).getOrElse( + throw new Exception("Can't access Console. Try running `cbt direct clean` or `cbt clean list` or `cbt clean force`.") + ) + System.err.println("Files to be deleted:\n\n") + files.foreach( System.err.println ) + System.err.println("") + System.err.print("To delete the above files type 'delete': ") + console.readLine() == "delete" + } + ) + + if( !performDelete ) { + System.err.println( "Ok, not cleaning." ) + ExitCode.Failure + } else { + // use same Vector[File] that was displayed earlier as a safety measure + files.foreach{ file => + System.err.println( red("Deleting") ++ " " ++ file.string ) + if(!dryRun){ + delete( file.toPath ) + } + } + System.err.println( "Done." ) + ExitCode.Success + } + } + } + // 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..7bbf1d9 100644 --- a/test/test.scala +++ b/test/test.scala @@ -1,6 +1,7 @@ import cbt._ import java.util.concurrent.ConcurrentHashMap import java.io.File +import java.nio.file._ import java.net.URL // micro framework @@ -72,6 +73,25 @@ object Main{ // assert(res.err == "", res.err) // FIXME: enable this } + def clean(path: String)(implicit logger: Logger) = { + val res = runCbt(path, Seq("clean", "dry-run", "force")) + val debugToken = "\n"++lib.red("Deleting") ++ " " ++ Paths.get("test/"++path++"/target").toAbsolutePath.toString++"\n" + val debugToken2 = "\n"++lib.red("Deleting") ++ " " ++ Paths.get("test/"++path).toAbsolutePath.toString++"\n" + assertSuccess(res,debugToken) + assert(res.out == "", debugToken ++ " " ++ res.toString) + assert(res.err.contains(debugToken), debugToken ++ " " ++ res.toString) + assert( + !res.err.contains(debugToken2), + "Tried to delete too much: " ++ debugToken2 ++ " " ++ res.toString + ) + res.err.split("\n").filter(_.startsWith(lib.red("Deleting"))).foreach{ line => + assert( + line.size >= debugToken2.trim.size, + "Tried to delete too much: " ++ line + ) + } + } + logger.test( "Running tests " ++ _args.toList.toString ) val cache = cbtHome ++ "/cache" @@ -150,10 +170,13 @@ 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") @@ -172,6 +195,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" ) |