diff options
Diffstat (limited to 'stage2')
-rw-r--r-- | stage2/BasicBuild.scala | 10 | ||||
-rw-r--r-- | stage2/Lib.scala | 56 |
2 files changed, 65 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("/")) |