Skip to content

Commit d3ba15f

Browse files
Only remove gradleEnterprise and develocity.buildCache (#6476)
* Only remove `gradleEnterprise` and `develocity.buildCache` * Polish --------- Co-authored-by: Shannon Pamperl <shannon@moderne.io>
1 parent 4c85a22 commit d3ba15f

2 files changed

Lines changed: 34 additions & 83 deletions

File tree

rewrite-gradle/src/main/java/org/openrewrite/gradle/plugins/RemoveDevelocityConfiguration.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
6262
J.Lambda lambda = (J.Lambda) m.getArguments().get(0);
6363
J.Block body = (J.Block) lambda.getBody();
6464

65-
List<Statement> filteredStatements = ListUtils.filter(body.getStatements(),
66-
stmt -> {
67-
if (stmt instanceof J.Return && ((J.Return) stmt).getExpression() instanceof J.MethodInvocation) {
68-
// Unpack J.Return wrapping last statement in closure
69-
stmt = (J.MethodInvocation) ((J.Return) stmt).getExpression();
70-
}
71-
if (!(stmt instanceof J.MethodInvocation)) {
72-
return true;
73-
}
74-
// Remove only 'remote' method invocations
75-
return !"remote".equals(((J.MethodInvocation) stmt).getSimpleName());
76-
});
65+
List<Statement> filteredStatements = ListUtils.filter(body.getStatements(), this::retainStatement);
7766
if (filteredStatements.isEmpty()) {
7867
return null;
7968
}
@@ -82,6 +71,29 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
8271

8372
return m;
8473
}
74+
75+
private boolean retainStatement(Statement stmt) {
76+
if (stmt instanceof J.Return && ((J.Return) stmt).getExpression() instanceof J.MethodInvocation) {
77+
// Unpack J.Return wrapping last statement in closure
78+
stmt = (J.MethodInvocation) ((J.Return) stmt).getExpression();
79+
}
80+
if (!(stmt instanceof J.MethodInvocation)) {
81+
return true;
82+
}
83+
J.MethodInvocation remote = (J.MethodInvocation) stmt;
84+
if (!"remote".equals(remote.getSimpleName())) {
85+
return true;
86+
}
87+
if (remote.getArguments().isEmpty() || !(remote.getArguments().get(0) instanceof J.FieldAccess)) {
88+
return true;
89+
}
90+
J.FieldAccess fieldAccess = (J.FieldAccess) remote.getArguments().get(0);
91+
if (!(fieldAccess.getTarget() instanceof J.Identifier)) {
92+
return true;
93+
}
94+
J.Identifier target = (J.Identifier) fieldAccess.getTarget();
95+
return !"develocity".equals(target.getSimpleName()) && !"gradleEnterprise".equals(target.getSimpleName());
96+
}
8597
});
8698
}
8799

rewrite-gradle/src/test/java/org/openrewrite/gradle/plugins/RemoveDevelocityTest.java

Lines changed: 10 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -157,76 +157,6 @@ void removeEntireBuildCacheWhenOnlyRemoteExists() {
157157
);
158158
}
159159

160-
@Test
161-
void removeHttpBuildCache() {
162-
rewriteRun(
163-
settingsGradle(
164-
"""
165-
plugins {
166-
id 'com.gradle.develocity' version '3.17'
167-
}
168-
169-
develocity {
170-
server = 'https://ge.example.com'
171-
}
172-
173-
buildCache {
174-
remote(HttpBuildCache) {
175-
url = 'https://other-cache.com/cache/'
176-
}
177-
remote(develocity.buildCache) {
178-
enabled = true
179-
}
180-
}
181-
""",
182-
""
183-
)
184-
);
185-
}
186-
187-
@Test
188-
void removeAllRemoteConfigurations() {
189-
rewriteRun(
190-
settingsGradle(
191-
"""
192-
plugins {
193-
id 'com.gradle.develocity' version '3.17'
194-
}
195-
196-
develocity {
197-
server = 'https://ge.example.com'
198-
buildScan {
199-
termsOfUseUrl = 'https://gradle.com/terms-of-service'
200-
termsOfUseAgree = 'yes'
201-
}
202-
}
203-
204-
buildCache {
205-
local {
206-
enabled = true
207-
}
208-
remote(HttpBuildCache) {
209-
url = 'https://cache.com'
210-
}
211-
remote(develocity.buildCache) {
212-
push = true
213-
}
214-
}
215-
""",
216-
"""
217-
218-
219-
buildCache {
220-
local {
221-
enabled = true
222-
}
223-
}
224-
""",
225-
SourceSpec::noTrim
226-
)
227-
);
228-
}
229-
230160
@Test
231161
void removeGradleEnterpriseConfiguration() {
232162
rewriteRun(
@@ -301,7 +231,16 @@ void removeEntireBuildCacheWithOnlyHttpBuildCache() {
301231
}
302232
}
303233
""",
304-
""
234+
"""
235+
236+
237+
buildCache {
238+
remote(HttpBuildCache) {
239+
url = 'https://cache.example.com'
240+
}
241+
}
242+
""",
243+
SourceSpec::noTrim
305244
)
306245
);
307246
}

0 commit comments

Comments
 (0)