Search in sources :

Example 1 with OutputsCleaner

use of org.gradle.internal.execution.history.OutputsCleaner in project gradle by gradle.

the class RemovePreviousOutputsStep method cleanupOverlappingOutputs.

private void cleanupOverlappingOutputs(BeforeExecutionContext context, UnitOfWork work) {
    context.getPreviousExecutionState().ifPresent(previousOutputs -> {
        Set<File> outputDirectoriesToPreserve = new HashSet<>();
        work.visitOutputs(context.getWorkspace(), new UnitOfWork.OutputVisitor() {

            @Override
            public void visitOutputProperty(String propertyName, TreeType type, File root, FileCollection contents) {
                switch(type) {
                    case FILE:
                        File parentFile = root.getParentFile();
                        if (parentFile != null) {
                            outputDirectoriesToPreserve.add(parentFile);
                        }
                        break;
                    case DIRECTORY:
                        outputDirectoriesToPreserve.add(root);
                        break;
                    default:
                        throw new AssertionError();
                }
            }
        });
        OutputsCleaner cleaner = new OutputsCleaner(deleter, file -> true, dir -> !outputDirectoriesToPreserve.contains(dir));
        for (FileSystemSnapshot snapshot : previousOutputs.getOutputFilesProducedByWork().values()) {
            try {
                // Previous outputs can be in a different place than the current outputs
                outputChangeListener.beforeOutputChange(SnapshotUtil.rootIndex(snapshot).keySet());
                cleaner.cleanupOutputs(snapshot);
            } catch (IOException e) {
                throw new UncheckedIOException("Failed to clean up output files for " + work.getDisplayName(), e);
            }
        }
    });
}
Also used : OutputsCleaner(org.gradle.internal.execution.history.OutputsCleaner) UnitOfWork(org.gradle.internal.execution.UnitOfWork) TreeType(org.gradle.internal.file.TreeType) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) FileCollection(org.gradle.api.file.FileCollection) FileSystemSnapshot(org.gradle.internal.snapshot.FileSystemSnapshot) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with OutputsCleaner

use of org.gradle.internal.execution.history.OutputsCleaner in project gradle by gradle.

the class StaleOutputCleaner method cleanOutputs.

/**
 * Clean up the given stale output files under the given directories.
 *
 * Any files and directories are removed that are descendants of any of the {@code directoriesToClean}.
 * Files and directories outside {@code directoriesToClean} and {@code directoriesToClean} themselves are not deleted.
 *
 * Returns {code true} if any file or directory was deleted, {@code false} otherwise.
 */
@CheckReturnValue
public static boolean cleanOutputs(Deleter deleter, Iterable<File> filesToDelete, ImmutableSet<File> directoriesToClean) {
    Set<String> prefixes = directoriesToClean.stream().map(directoryToClean -> directoryToClean.getAbsolutePath() + File.separator).collect(Collectors.toSet());
    OutputsCleaner outputsCleaner = new OutputsCleaner(deleter, file -> {
        String absolutePath = file.getAbsolutePath();
        return prefixes.stream().anyMatch(absolutePath::startsWith);
    }, dir -> !directoriesToClean.contains(dir));
    try {
        for (File f : filesToDelete) {
            if (f.isFile()) {
                outputsCleaner.cleanupOutput(f, FileType.RegularFile);
            }
        }
        outputsCleaner.cleanupDirectories();
    } catch (IOException e) {
        throw new UncheckedIOException("Failed to clean up stale outputs", e);
    }
    return outputsCleaner.getDidWork();
}
Also used : Deleter(org.gradle.internal.file.Deleter) CheckReturnValue(javax.annotation.CheckReturnValue) ImmutableSet(com.google.common.collect.ImmutableSet) OutputsCleaner(org.gradle.internal.execution.history.OutputsCleaner) Set(java.util.Set) FileType(org.gradle.internal.file.FileType) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) UncheckedIOException(org.gradle.api.UncheckedIOException) File(java.io.File) OutputsCleaner(org.gradle.internal.execution.history.OutputsCleaner) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(org.gradle.api.UncheckedIOException) File(java.io.File) CheckReturnValue(javax.annotation.CheckReturnValue)

Example 3 with OutputsCleaner

use of org.gradle.internal.execution.history.OutputsCleaner in project gradle by gradle.

the class SkipEmptyWorkStep method cleanPreviousTaskOutputs.

private boolean cleanPreviousTaskOutputs(Map<String, FileSystemSnapshot> outputFileSnapshots) {
    OutputsCleaner outputsCleaner = outputsCleanerSupplier.get();
    for (FileSystemSnapshot outputFileSnapshot : outputFileSnapshots.values()) {
        try {
            outputChangeListener.beforeOutputChange(SnapshotUtil.rootIndex(outputFileSnapshot).keySet());
            outputsCleaner.cleanupOutputs(outputFileSnapshot);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    return outputsCleaner.getDidWork();
}
Also used : OutputsCleaner(org.gradle.internal.execution.history.OutputsCleaner) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) FileSystemSnapshot(org.gradle.internal.snapshot.FileSystemSnapshot)

Aggregations

IOException (java.io.IOException)3 OutputsCleaner (org.gradle.internal.execution.history.OutputsCleaner)3 File (java.io.File)2 UncheckedIOException (java.io.UncheckedIOException)2 FileSystemSnapshot (org.gradle.internal.snapshot.FileSystemSnapshot)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CheckReturnValue (javax.annotation.CheckReturnValue)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1 FileCollection (org.gradle.api.file.FileCollection)1 UnitOfWork (org.gradle.internal.execution.UnitOfWork)1 Deleter (org.gradle.internal.file.Deleter)1 FileType (org.gradle.internal.file.FileType)1 TreeType (org.gradle.internal.file.TreeType)1