Search in sources :

Example 1 with TaskExecutionHistory

use of org.gradle.api.internal.TaskExecutionHistory in project gradle by gradle.

the class SkipEmptySourceFilesTaskExecuter method execute.

public void execute(TaskInternal task, TaskStateInternal state, TaskExecutionContext context) {
    TaskProperties taskProperties = context.getTaskProperties();
    FileCollection sourceFiles = taskProperties.getSourceFiles();
    if (taskProperties.hasSourceFiles() && sourceFiles.isEmpty()) {
        TaskArtifactState taskArtifactState = context.getTaskArtifactState();
        TaskExecutionHistory executionHistory = taskArtifactState.getExecutionHistory();
        Set<File> outputFiles = executionHistory.getOutputFiles();
        if (outputFiles.isEmpty()) {
            state.setOutcome(TaskExecutionOutcome.NO_SOURCE);
            LOGGER.info("Skipping {} as it has no source files and no previous output files.", task);
        } else {
            boolean cleanupDirectories = executionHistory.getOverlappingOutputs() == null;
            if (!cleanupDirectories) {
                LOGGER.info("No leftover directories for {} will be deleted since overlapping outputs were detected.", task);
            }
            taskOutputChangesListener.beforeTaskOutputChanged();
            boolean deletedFiles = false;
            boolean debugEnabled = LOGGER.isDebugEnabled();
            for (File file : outputFiles) {
                if (file.exists() && buildOutputCleanupRegistry.isOutputOwnedByBuild(file)) {
                    if (!cleanupDirectories && file.isDirectory()) {
                        continue;
                    }
                    if (debugEnabled) {
                        LOGGER.debug("Deleting stale output file '{}'.", file.getAbsolutePath());
                    }
                    GFileUtils.forceDelete(file);
                    deletedFiles = true;
                }
            }
            if (deletedFiles) {
                LOGGER.info("Cleaned previous output of {} as it has no source files.", task);
                state.setOutcome(TaskExecutionOutcome.EXECUTED);
            } else {
                state.setOutcome(TaskExecutionOutcome.NO_SOURCE);
            }
            taskArtifactState.snapshotAfterTaskExecution(null, buildInvocationScopeId.getId(), context);
        }
        taskInputsListener.onExecute(task, Cast.cast(FileCollectionInternal.class, sourceFiles));
        return;
    } else {
        taskInputsListener.onExecute(task, Cast.cast(FileCollectionInternal.class, taskProperties.getInputFiles()));
    }
    executer.execute(task, state, context);
}
Also used : TaskArtifactState(org.gradle.api.internal.changedetection.TaskArtifactState) FileCollectionInternal(org.gradle.api.internal.file.FileCollectionInternal) FileCollection(org.gradle.api.file.FileCollection) File(java.io.File) TaskExecutionHistory(org.gradle.api.internal.TaskExecutionHistory)

Aggregations

File (java.io.File)1 FileCollection (org.gradle.api.file.FileCollection)1 TaskExecutionHistory (org.gradle.api.internal.TaskExecutionHistory)1 TaskArtifactState (org.gradle.api.internal.changedetection.TaskArtifactState)1 FileCollectionInternal (org.gradle.api.internal.file.FileCollectionInternal)1