aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md16
-rwxr-xr-xcbt1
-rwxr-xr-xshell-integration/cbt-completions.bash13
-rw-r--r--stage2/BasicBuild.scala10
-rw-r--r--stage2/Lib.scala56
-rw-r--r--test/test.scala24
6 files changed, 119 insertions, 1 deletions
diff --git a/README.md b/README.md
index 7a1174e..2f2134b 100644
--- a/README.md
+++ b/README.md
@@ -176,3 +176,19 @@ solution could be code generating traits at build-time and mixing them
in ad-hoc. It's a build-tool after all. Build-time code-generation and
class loading is not rocket science. But there may be simpler solutions
for the cases at hand. And they are edge cases anyways.
+
+Bash completions
+----------------
+To auto-complete cbt task names in bash do this:
+
+```
+mkdir ~/.bash_completion.d/
+cp shell-integration/cbt-completions.bash ~/.bash_completion.d/
+```
+
+Add this to you .bashrc
+```
+for f in ~/.bash_completion.d/*; do
+ source $f
+done
+```
diff --git a/cbt b/cbt
index 63b272e..5751e69 100755
--- a/cbt
+++ b/cbt
@@ -7,6 +7,7 @@
# - reduce code size by moving more of this into type-checked Java/Scala code (if possible without performance loss).
# - reduction of dependencies
# - performance improvements
+shopt -qs extglob
seconds() {
date +"%s"
diff --git a/shell-integration/cbt-completions.bash b/shell-integration/cbt-completions.bash
new file mode 100755
index 0000000..c591321
--- /dev/null
+++ b/shell-integration/cbt-completions.bash
@@ -0,0 +1,13 @@
+#!/bin/bash
+__cbt()
+{
+ local cur prev opts
+ COMPREPLY=()
+ cur="${COMP_WORDS[COMP_CWORD]}"
+ prev="${COMP_WORDS[COMP_CWORD-1]}"
+ opts="$(cbt usage)"
+ COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
+ return 0
+}
+
+complete -F __cbt cbt
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" )