-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
BuildScriptCompilationAndInstrumentation.java
155 lines (134 loc) · 6.47 KB
/
BuildScriptCompilationAndInstrumentation.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright 2023 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.scripts;
import org.gradle.api.internal.file.FileCollectionFactory;
import org.gradle.internal.buildoption.InternalFlag;
import org.gradle.internal.buildoption.InternalOptions;
import org.gradle.internal.classpath.transforms.ClasspathElementTransform;
import org.gradle.internal.classpath.transforms.ClasspathElementTransformFactoryForLegacy;
import org.gradle.internal.classpath.transforms.InstrumentingClassTransform;
import org.gradle.internal.classpath.types.InstrumentationTypeRegistry;
import org.gradle.internal.execution.ImmutableUnitOfWork;
import org.gradle.internal.execution.InputFingerprinter;
import org.gradle.internal.execution.UnitOfWork;
import org.gradle.internal.execution.caching.CachingDisabledReason;
import org.gradle.internal.execution.caching.CachingDisabledReasonCategory;
import org.gradle.internal.execution.history.OverlappingOutputs;
import org.gradle.internal.execution.workspace.ImmutableWorkspaceProvider;
import org.gradle.internal.file.TreeType;
import org.gradle.internal.fingerprint.CurrentFileCollectionFingerprint;
import org.gradle.internal.hash.Hasher;
import org.gradle.internal.hash.Hashing;
import org.gradle.internal.snapshot.ValueSnapshot;
import javax.annotation.Nullable;
import javax.annotation.OverridingMethodsMustInvokeSuper;
import java.io.File;
import java.util.Map;
import java.util.Optional;
import static java.util.Objects.requireNonNull;
/**
* A base class that represents a work for compilation for Kotlin and Groovy build scripts.
* This work unit first compiles the build script to a directory, and then instruments the directory for configuration cache and returns instrumented output.
*/
public abstract class BuildScriptCompilationAndInstrumentation implements ImmutableUnitOfWork {
private static final InternalFlag CACHING_DISABLED_PROPERTY = new InternalFlag("org.gradle.internal.script-caching-disabled");
private static final CachingDisabledReason CACHING_DISABLED_REASON = new CachingDisabledReason(CachingDisabledReasonCategory.NOT_CACHEABLE, "Caching of script compilation disabled by property (experimental)");
private final ImmutableWorkspaceProvider workspaceProvider;
private final InputFingerprinter inputFingerprinter;
private final ClasspathElementTransformFactoryForLegacy transformFactory;
private final boolean cachingDisabledByProperty;
protected final FileCollectionFactory fileCollectionFactory;
public BuildScriptCompilationAndInstrumentation(
ImmutableWorkspaceProvider workspaceProvider,
FileCollectionFactory fileCollectionFactory,
InputFingerprinter inputFingerprinter,
InternalOptions internalOptions,
ClasspathElementTransformFactoryForLegacy transformFactory
) {
this.workspaceProvider = workspaceProvider;
this.fileCollectionFactory = fileCollectionFactory;
this.inputFingerprinter = inputFingerprinter;
this.transformFactory = transformFactory;
this.cachingDisabledByProperty = internalOptions.getOption(CACHING_DISABLED_PROPERTY).get();
}
@Override
public Optional<CachingDisabledReason> shouldDisableCaching(@Nullable OverlappingOutputs detectedOverlappingOutputs) {
if (cachingDisabledByProperty) {
return Optional.of(CACHING_DISABLED_REASON);
}
return ImmutableUnitOfWork.super.shouldDisableCaching(detectedOverlappingOutputs);
}
@Override
public abstract void visitIdentityInputs(InputVisitor visitor);
/**
* A compile operation. It should return a File where classes are compiled to.
*/
protected abstract File compile(File workspace);
/**
* Provides a File where instrumented output will be written to.
*/
protected abstract File instrumentedOutput(File workspace);
@Override
public Identity identify(Map<String, ValueSnapshot> identityInputs, Map<String, CurrentFileCollectionFingerprint> identityFileInputs) {
Hasher hasher = Hashing.newHasher();
identityInputs.values().forEach(value -> requireNonNull(value).appendToHasher(hasher));
String identity = hasher.hash().toString();
return () -> identity;
}
@Override
@OverridingMethodsMustInvokeSuper
public void visitOutputs(File workspace, OutputVisitor visitor) {
File instrumentedOutput = instrumentedOutput(workspace);
OutputFileValueSupplier instrumentedOutputValue = OutputFileValueSupplier.fromStatic(instrumentedOutput, fileCollectionFactory.fixed(instrumentedOutput));
visitor.visitOutputProperty("instrumentedOutput", TreeType.DIRECTORY, instrumentedOutputValue);
}
@Override
public WorkOutput execute(ExecutionRequest executionRequest) {
File workspace = executionRequest.getWorkspace();
File compileOutput = compile(workspace);
instrument(compileOutput, instrumentedOutput(workspace));
return new UnitOfWork.WorkOutput() {
@Override
public WorkResult getDidWork() {
return UnitOfWork.WorkResult.DID_WORK;
}
@Nullable
@Override
public Object getOutput(File workspace) {
return loadAlreadyProducedOutput(workspace);
}
};
}
private void instrument(File sourceDir, File destination) {
ClasspathElementTransform transform = transformFactory.createTransformer(sourceDir, new InstrumentingClassTransform(), InstrumentationTypeRegistry.EMPTY);
transform.transform(destination);
}
@Nullable
@Override
public Object loadAlreadyProducedOutput(File workspace) {
return instrumentedOutput(workspace);
}
@Override
public InputFingerprinter getInputFingerprinter() {
return inputFingerprinter;
}
@Override
public ImmutableWorkspaceProvider getWorkspaceProvider() {
return workspaceProvider;
}
@Override
public abstract String getDisplayName();
}