Reimplement clean and cleanFiles tasks

cleanFiles is now a task that lists exactly what will be deleted recursively.

Cherry-picked 7acb8764f5
This commit is contained in:
Eugene Yokota 2017-04-12 02:00:14 -04:00 committed by Dale Wijnand
parent a5bd564307
commit 183adc3d23
No known key found for this signature in database
GPG Key ID: 4F256E3D151DF5EF
2 changed files with 21 additions and 13 deletions

View File

@ -342,9 +342,9 @@ object Defaults extends BuildCommon {
))
lazy val projectTasks: Seq[Setting[_]] = Seq(
cleanFiles := Seq(managedDirectory.value, target.value),
cleanKeepFiles := historyPath.value.toList,
clean := doClean(cleanFiles.value, cleanKeepFiles.value),
cleanFiles := cleanFilesTask.value,
cleanKeepFiles := historyPath.value.toVector,
clean := IO.delete(cleanFiles.value),
consoleProject := consoleProjectTask.value,
watchTransitiveSources := watchTransitiveSourcesTask.value,
watch := watchSetting.value
@ -775,15 +775,23 @@ object Defaults extends BuildCommon {
pickMainClass(classes)
}
def doClean(clean: Seq[File], preserve: Seq[File]): Unit =
IO.withTemporaryDirectory { temp =>
val (dirs, files) = preserve.filter(_.exists).flatMap(_.***.get).partition(_.isDirectory)
val mappings = files.zipWithIndex map { case (f, i) => (f, new File(temp, i.toHexString)) }
IO.move(mappings)
IO.delete(clean)
IO.createDirectories(dirs) // recreate empty directories
IO.move(mappings.map(_.swap))
/** Implements `cleanFiles` task. */
def cleanFilesTask: Initialize[Task[Vector[File]]] =
Def.task {
val filesAndDirs = Vector(managedDirectory.value, target.value)
val preserve = cleanKeepFiles.value
val (dirs, fs) = filesAndDirs.filter(_.exists).partition(_.isDirectory)
val preserveSet = preserve.filter(_.exists).toSet
// performance reasons, only the direct items under `filesAndDirs` are allowed to be preserved.
val dirItems = dirs flatMap { _.*("*").get }
(preserveSet diff dirItems.toSet) match {
case xs if xs.isEmpty => ()
case xs => sys.error(s"cleanKeepFiles contains directory/file that are not directly under cleanFiles: $xs")
}
val toClean = (dirItems filterNot { preserveSet(_) }) ++ fs
toClean
}
def runMainTask(classpath: Initialize[Task[Classpath]], scalaRun: Initialize[Task[ScalaRun]]): Initialize[InputTask[Unit]] =
{
import DefaultParsers._

View File

@ -107,8 +107,8 @@ object Keys {
val classDirectory = SettingKey[File]("class-directory", "Directory for compiled classes and copied resources.", AMinusSetting)
@deprecated("Use the cacheDirectory provided by streams.", "0.13.0")
val cacheDirectory = SettingKey[File]("cache-directory", "Directory used for caching task data.", BMinusSetting)
val cleanFiles = SettingKey[Seq[File]]("clean-files", "The files to recursively delete during a clean.", BSetting)
val cleanKeepFiles = SettingKey[Seq[File]]("clean-keep-files", "Files to keep during a clean.", CSetting)
val cleanFiles = TaskKey[Seq[File]]("clean-files", "The files to recursively delete during a clean.", BSetting)
val cleanKeepFiles = SettingKey[Seq[File]]("clean-keep-files", "Files or directories to keep during a clean. Must be direct children of target.", CSetting)
val crossPaths = SettingKey[Boolean]("cross-paths", "If true, enables cross paths, which distinguish input and output directories for cross-building.", ASetting)
val taskTemporaryDirectory = SettingKey[File]("task-temporary-directory", "Directory used for temporary files for tasks that is deleted after each task execution.", DSetting)