23
23
import javax .inject .Inject ;
24
24
25
25
import org .gradle .api .DefaultTask ;
26
+ import org .gradle .api .GradleException ;
26
27
import org .gradle .api .Project ;
27
28
import org .gradle .api .file .DirectoryProperty ;
28
29
import org .gradle .api .model .ObjectFactory ;
35
36
36
37
import com .diffplug .common .base .Preconditions ;
37
38
import com .diffplug .common .base .Unhandled ;
39
+ import com .diffplug .spotless .FileSignature ;
38
40
import com .diffplug .spotless .Provisioner ;
39
41
40
42
/**
44
46
* apply already did).
45
47
*/
46
48
public abstract class SpotlessTaskService implements BuildService <BuildServiceParameters .None >, AutoCloseable , OperationCompletionListener {
47
- private final Map <String , SpotlessApply > apply = Collections .synchronizedMap (new HashMap <>());
48
- private final Map <String , SpotlessTask > source = Collections .synchronizedMap (new HashMap <>());
49
- private final Map <String , Provisioner > provisioner = Collections .synchronizedMap (new HashMap <>());
49
+ private boolean enableConfigCacheDaemonLocal ;
50
+ private Map <String , SpotlessApply > apply = Collections .synchronizedMap (new HashMap <>());
51
+ private Map <String , SpotlessTask > source = Collections .synchronizedMap (new HashMap <>());
52
+ private Map <String , Provisioner > provisioner = Collections .synchronizedMap (new HashMap <>());
50
53
51
- Provisioner provisionerFor (Project project ) {
52
- return provisioner .computeIfAbsent (project .getPath (), unused -> {
53
- return GradleProvisioner .newDedupingProvisioner (project );
54
- });
54
+ void registerDependenciesTask (RegisterDependenciesTask task ) {
55
+ enableConfigCacheDaemonLocal = task .getEnableConfigCacheDaemonLocal ();
55
56
}
56
57
57
58
void registerSourceAlreadyRan (SpotlessTask task ) {
58
59
source .put (task .getPath (), task );
60
+ if (enableConfigCacheDaemonLocal ) {
61
+ storeOrHydrate (task );
62
+ }
63
+ }
64
+
65
+ void hydrate (SpotlessTask task ) {
66
+ storeOrHydrate (task );
67
+ }
68
+
69
+ private void storeOrHydrate (SpotlessTask task ) {
70
+ if (!enableConfigCacheDaemonLocal ) {
71
+ return ;
72
+ }
73
+ String cacheKey = task .getProjectDir ().getAsFile ().get ().getAbsolutePath () + ">" + task .getPath ();
74
+ if (SpotlessTaskImpl .isHydrated (task )) {
75
+ daemonLocalMap .put (cacheKey , new SpotlessTaskImpl .LiveCache (task ));
76
+ } else {
77
+ SpotlessTaskImpl .LiveCache cached = daemonLocalMap .get (cacheKey );
78
+ if (cached == null ) {
79
+ throw new GradleException ("Spotless daemon-local cache is stale. Regenerate the cache with\n " +
80
+ " " + (FileSignature .machineIsWin () ? "rmdir /q /s" : "rm -rf" ) + " .gradle/configuration-cache\n " +
81
+ "For more information see #123" );
82
+ } else {
83
+ cached .hydrate (task );
84
+ }
85
+ }
86
+ }
87
+
88
+ private static final Map <String , SpotlessTaskImpl .LiveCache > daemonLocalMap = Collections .synchronizedMap (new HashMap <>());
89
+
90
+ Provisioner provisionerFor (Project project ) {
91
+ return provisioner .computeIfAbsent (project .getPath (), unused -> {
92
+ return GradleProvisioner .newDedupingProvisioner (project );
93
+ });
59
94
}
60
95
61
96
void registerApplyAlreadyRan (SpotlessApply task ) {
62
97
apply .put (task .sourceTaskPath (), task );
63
98
}
64
99
100
+ /////////////////
65
101
// <GitRatchet>
66
- private final GitRatchetGradle ratchet = new GitRatchetGradle ();
102
+ private GitRatchetGradle ratchet = new GitRatchetGradle ();
67
103
68
104
GitRatchetGradle getRatchet () {
69
105
return ratchet ;
@@ -79,6 +115,7 @@ public void close() throws Exception {
79
115
ratchet .close ();
80
116
}
81
117
// </GitRatchet>
118
+ //////////////////
82
119
83
120
static String INDEPENDENT_HELPER = "Helper" ;
84
121
0 commit comments