-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
SkipUpToDateStep.java
89 lines (78 loc) · 3.77 KB
/
SkipUpToDateStep.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.internal.execution.steps;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang.StringUtils;
import org.gradle.internal.Try;
import org.gradle.internal.execution.ExecutionEngine.Execution;
import org.gradle.internal.execution.UnitOfWork;
import org.gradle.internal.execution.history.ExecutionOutputState;
import org.gradle.internal.execution.history.PreviousExecutionState;
import org.gradle.internal.execution.history.impl.DefaultExecutionOutputState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Formatter;
import java.util.List;
import static org.gradle.internal.execution.ExecutionEngine.ExecutionOutcome.UP_TO_DATE;
public class SkipUpToDateStep<C extends IncrementalChangesContext> implements Step<C, UpToDateResult> {
private static final Logger LOGGER = LoggerFactory.getLogger(SkipUpToDateStep.class);
private final Step<? super C, ? extends AfterExecutionResult> delegate;
public SkipUpToDateStep(Step<? super C, ? extends AfterExecutionResult> delegate) {
this.delegate = delegate;
}
@Override
public UpToDateResult execute(UnitOfWork work, C context) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Determining if {} is up-to-date", work.getDisplayName());
}
ImmutableList<String> reasons = context.getRebuildReasons();
return context.getChanges()
.filter(__ -> reasons.isEmpty())
.map(changes -> skipExecution(work, context))
.orElseGet(() -> executeBecause(work, reasons, context));
}
private UpToDateResult skipExecution(UnitOfWork work, C context) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Skipping {} as it is up-to-date.", work.getDisplayName());
}
@SuppressWarnings("OptionalGetWithoutIsPresent")
PreviousExecutionState previousExecutionState = context.getPreviousExecutionState().get();
ExecutionOutputState executionOutputState = new DefaultExecutionOutputState(true, previousExecutionState.getOutputFilesProducedByWork(), previousExecutionState.getOriginMetadata(), true);
Try<Execution> execution = Try.successful(Execution.skipped(UP_TO_DATE, work));
return new UpToDateResult(
previousExecutionState.getOriginMetadata().getExecutionTime(),
execution,
executionOutputState,
ImmutableList.of(),
previousExecutionState.getOriginMetadata()
);
}
private UpToDateResult executeBecause(UnitOfWork work, ImmutableList<String> reasons, C context) {
logExecutionReasons(reasons, work);
AfterExecutionResult result = delegate.execute(work, context);
return new UpToDateResult(result, reasons);
}
private static void logExecutionReasons(List<String> reasons, UnitOfWork work) {
if (LOGGER.isInfoEnabled()) {
Formatter formatter = new Formatter();
formatter.format("%s is not up-to-date because:", StringUtils.capitalize(work.getDisplayName()));
for (String message : reasons) {
formatter.format("%n %s", message);
}
LOGGER.info(formatter.toString());
}
}
}