Skip to content

Commit a0f7a3d

Browse files
authored
Quarkus Add/Change/Delete property recipe to use the custom pathExpression (#93)
- Update FindQuarkusProperties to use the custom pathExpression to support dependent Add/Change?delete property recipe - Fixes (#92)
1 parent f47c202 commit a0f7a3d

10 files changed

+291
-24
lines changed

src/main/java/org/openrewrite/quarkus/AddQuarkusProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
9191
@Override
9292
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
9393
QuarkusExecutionContextView quarkusCtx = QuarkusExecutionContextView.view(ctx);
94-
return quarkusCtx.isQuarkusConfigFile(sourceFile, null);
94+
return quarkusCtx.isQuarkusConfigFile(sourceFile, pathExpressions);
9595
}
9696

9797
@Override

src/main/java/org/openrewrite/quarkus/ChangeQuarkusPropertyKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public String getDescription() {
9090
@Override
9191
public TreeVisitor<?, ExecutionContext> getVisitor() {
9292
return Preconditions.check(
93-
new FindQuarkusProperties(oldPropertyKey, profile, changeAllProfiles).getVisitor(),
93+
new FindQuarkusProperties(oldPropertyKey, profile, changeAllProfiles, pathExpressions).getVisitor(),
9494
new ChangeQuarkusPropertyKeyVisitor(oldPropertyKey, newPropertyKey, profile, changeAllProfiles, pathExpressions)
9595
);
9696
}

src/main/java/org/openrewrite/quarkus/ChangeQuarkusPropertyValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public String getDescription() {
9696
@Override
9797
public TreeVisitor<?, ExecutionContext> getVisitor() {
9898
return Preconditions.check(
99-
new FindQuarkusProperties(propertyKey, profile, changeAllProfiles).getVisitor(),
99+
new FindQuarkusProperties(propertyKey, profile, changeAllProfiles, pathExpressions).getVisitor(),
100100
new ChangeQuarkusPropertyValueVisitor(propertyKey, newValue, oldValue, profile, changeAllProfiles, pathExpressions)
101101
);
102102
}

src/main/java/org/openrewrite/quarkus/DeleteQuarkusProperty.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public String getDescription() {
9191
@Override
9292
public TreeVisitor<?, ExecutionContext> getVisitor() {
9393
return Preconditions.check(
94-
new FindQuarkusProperties(propertyKey, profile, deleteFromAllProfiles).getVisitor(),
94+
new FindQuarkusProperties(propertyKey, profile, deleteFromAllProfiles, pathExpressions).getVisitor(),
9595
new DeleteQuarkusPropertyVisitor(propertyKey, oldValue, profile, deleteFromAllProfiles, pathExpressions)
9696
);
9797
}

src/main/java/org/openrewrite/quarkus/search/FindQuarkusProperties.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929
import org.openrewrite.yaml.tree.Yaml;
3030
import org.openrewrite.yaml.tree.YamlKey;
3131

32-
import java.util.Comparator;
33-
import java.util.Iterator;
34-
import java.util.Set;
35-
import java.util.TreeSet;
32+
import java.util.*;
3633
import java.util.regex.Pattern;
3734

3835
@Value
@@ -68,6 +65,16 @@ public String getDescription() {
6865
@Nullable
6966
Boolean searchAllProfiles;
7067

68+
@Option(displayName = "Optional list of file path matcher",
69+
description = "Each value in this list represents a glob expression that is used to match which files will " +
70+
"be modified. If this value is not present, this recipe will query the execution context for " +
71+
"reasonable defaults. (\"**/application.yml\", \"**/application.yaml\", " +
72+
"\"**/application.properties\" and \"**/META-INF/microprofile-config.properties\".",
73+
required = false,
74+
example = "[\"**/application.yaml\"]")
75+
@Nullable
76+
List<String> pathExpressions;
77+
7178
@Override
7279
public Validated<Object> validate() {
7380
Validated<Object> validated = super.validate()
@@ -153,7 +160,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
153160
@Override
154161
public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
155162
QuarkusExecutionContextView quarkusCtx = QuarkusExecutionContextView.view(ctx);
156-
return quarkusCtx.isQuarkusConfigFile(sourceFile, null);
163+
return quarkusCtx.isQuarkusConfigFile(sourceFile, pathExpressions);
157164
}
158165

159166
@Override

src/test/java/org/openrewrite/quarkus/AddQuarkusPropertyTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import org.openrewrite.DocumentExample;
2020
import org.openrewrite.test.RewriteTest;
2121

22+
import java.util.List;
23+
2224
import static org.openrewrite.properties.Assertions.properties;
2325
import static org.openrewrite.yaml.Assertions.yaml;
2426

@@ -245,4 +247,40 @@ void doNotChangeToFilesThatDoNotMatch() {
245247
)
246248
);
247249
}
250+
251+
@Test
252+
void makeChangeToMatchingFilesWithCustomPathExpression() {
253+
rewriteRun(
254+
spec -> spec.recipe(new AddQuarkusProperty("quarkus.http.root-path", "/api", "This property was added", null, List.of("**/custom.{properties,yaml,yml}"))),
255+
properties("# Sample empty properties file", s -> s.path("src/main/resources/test.properties")),
256+
//language=properties
257+
properties(
258+
"""
259+
quarkus.http.port=9090
260+
""",
261+
"""
262+
quarkus.http.port=9090
263+
# This property was added
264+
quarkus.http.root-path=/api
265+
""",
266+
s -> s.path("src/main/resources/custom.properties")
267+
),
268+
//language=yaml
269+
yaml(
270+
"""
271+
quarkus:
272+
http:
273+
port: 9090
274+
""",
275+
"""
276+
quarkus:
277+
http:
278+
port: 9090
279+
# This property was added
280+
root-path: /api
281+
""",
282+
s -> s.path("src/main/resources/custom.yml")
283+
)
284+
);
285+
}
248286
}

src/test/java/org/openrewrite/quarkus/ChangeQuarkusPropertyKeyTest.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.junit.jupiter.api.Test;
2121
import org.openrewrite.test.RewriteTest;
2222

23+
import java.util.List;
24+
2325
import static org.openrewrite.properties.Assertions.properties;
2426
import static org.openrewrite.yaml.Assertions.yaml;
2527

@@ -138,6 +140,28 @@ void changeKeyOnAllProfilesBecauseDefault() {
138140
properties(sourceProperties, after, spec -> spec.path("src/main/resources/application.properties"))
139141
);
140142
}
143+
144+
@Test
145+
void changeKeyOnCustomPath() {
146+
@Language("properties")
147+
String after = """
148+
quarkus.hibernate-search-orm.indexing.plan.synchronization.strategy=read-sync
149+
%dev.quarkus.hibernate-search-orm.automatic-indexing.synchronization.strategy=sync
150+
%staging,prod.quarkus.hibernate-search-orm.automatic-indexing.synchronization.strategy=async
151+
152+
quarkus.hibernate-search-orm."unitname".indexing.plan.synchronization.strategy=read-sync
153+
%dev.quarkus.hibernate-search-orm."unitname".automatic-indexing.synchronization.strategy=sync
154+
%staging,prod.quarkus.hibernate-search-orm."unitname".automatic-indexing.synchronization.strategy=async
155+
""";
156+
157+
rewriteRun(
158+
spec -> spec.recipe(new ChangeQuarkusPropertyKey(
159+
"quarkus\\.hibernate-search-orm(\\..*)?\\.automatic-indexing\\.synchronization\\.strategy",
160+
"quarkus.hibernate-search-orm$1.indexing.plan.synchronization.strategy",
161+
null, false, List.of("**/custom.{properties,yaml,yml}"))),
162+
properties(sourceProperties, after, spec -> spec.path("src/main/resources/custom.properties"))
163+
);
164+
}
141165
}
142166

143167
@Nested
@@ -336,5 +360,55 @@ void changeKeyOnAllProfiles() {
336360
yaml(sourceYaml, after, spec -> spec.path("src/main/resources/application.yaml"))
337361
);
338362
}
363+
364+
@Test
365+
void changeKeyOnCustomPath() {
366+
@Language("yml")
367+
String after = """
368+
quarkus:
369+
hibernate-search-orm:
370+
indexing:
371+
plan:
372+
synchronization:
373+
strategy: read-sync
374+
unitname:
375+
indexing:
376+
plan:
377+
synchronization:
378+
strategy: read-sync
379+
'%dev':
380+
quarkus:
381+
hibernate-search-orm:
382+
indexing:
383+
plan:
384+
synchronization:
385+
strategy: sync
386+
unitname:
387+
indexing:
388+
plan:
389+
synchronization:
390+
strategy: sync
391+
'%staging,prod':
392+
quarkus:
393+
hibernate-search-orm:
394+
indexing:
395+
plan:
396+
synchronization:
397+
strategy: async
398+
unitname:
399+
indexing:
400+
plan:
401+
synchronization:
402+
strategy: async
403+
""";
404+
405+
rewriteRun(
406+
spec -> spec.recipe(new ChangeQuarkusPropertyKey(
407+
"quarkus\\.hibernate-search-orm(\\..*)?\\.automatic-indexing\\.synchronization\\.strategy",
408+
"quarkus.hibernate-search-orm$1.indexing.plan.synchronization.strategy",
409+
null, true, List.of("**/custom.{properties,yaml,yml}"))),
410+
yaml(sourceYaml, after, spec -> spec.path("src/main/resources/custom.yaml"))
411+
);
412+
}
339413
}
340414
}

src/test/java/org/openrewrite/quarkus/ChangeQuarkusPropertyValueTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.junit.jupiter.api.Test;
2121
import org.openrewrite.test.RewriteTest;
2222

23+
import java.util.List;
24+
2325
import static org.openrewrite.properties.Assertions.properties;
2426
import static org.openrewrite.yaml.Assertions.yaml;
2527

@@ -138,6 +140,28 @@ void changeValueAllProfilesBecauseDefault() {
138140
properties(sourceProperties, after, spec -> spec.path("src/main/resources/application.properties"))
139141
);
140142
}
143+
144+
@Test
145+
void changeValueOnCustomPath() {
146+
@Language("properties")
147+
String after = """
148+
quarkus.hibernate-search-orm.automatic-indexing.synchronization.strategy=write-sync
149+
%dev.quarkus.hibernate-search-orm.automatic-indexing.synchronization.strategy=write-sync
150+
%staging,prod.quarkus.hibernate-search-orm.automatic-indexing.synchronization.strategy=write-sync
151+
152+
quarkus.hibernate-search-orm."unitname".automatic-indexing.synchronization.strategy=write-sync
153+
%dev.quarkus.hibernate-search-orm."unitname".automatic-indexing.synchronization.strategy=write-sync
154+
%staging,prod.quarkus.hibernate-search-orm."unitname".automatic-indexing.synchronization.strategy=write-sync
155+
""";
156+
157+
rewriteRun(
158+
spec -> spec.recipe(new ChangeQuarkusPropertyValue(
159+
"quarkus\\.hibernate-search-orm(\\..*)?\\.automatic-indexing\\.synchronization\\.strategy",
160+
"write-sync",
161+
null, null, null, List.of("**/custom.{properties,yaml,yml}"))),
162+
properties(sourceProperties, after, spec -> spec.path("src/main/resources/custom.properties"))
163+
);
164+
}
141165
}
142166

143167
@Nested
@@ -326,5 +350,49 @@ void changeValueOnAllProfiles() {
326350
yaml(sourceYaml, after, spec -> spec.path("src/main/resources/application.yaml"))
327351
);
328352
}
353+
354+
@Test
355+
void changeValueOnCustomPath() {
356+
@Language("yml")
357+
String after = """
358+
quarkus:
359+
hibernate-search-orm:
360+
automatic-indexing:
361+
synchronization:
362+
strategy: write-sync
363+
unitname:
364+
automatic-indexing:
365+
synchronization:
366+
strategy: write-sync
367+
'%dev':
368+
quarkus:
369+
hibernate-search-orm:
370+
automatic-indexing:
371+
synchronization:
372+
strategy: write-sync
373+
unitname:
374+
automatic-indexing:
375+
synchronization:
376+
strategy: write-sync
377+
'%staging,prod':
378+
quarkus:
379+
hibernate-search-orm:
380+
automatic-indexing:
381+
synchronization:
382+
strategy: write-sync
383+
unitname:
384+
automatic-indexing:
385+
synchronization:
386+
strategy: write-sync
387+
""";
388+
389+
rewriteRun(
390+
spec -> spec.recipe(new ChangeQuarkusPropertyValue(
391+
"quarkus\\.hibernate-search-orm(\\..*)?\\.automatic-indexing\\.synchronization\\.strategy",
392+
"write-sync",
393+
null, null, true, List.of("**/custom.{properties,yaml,yml}"))),
394+
yaml(sourceYaml, after, spec -> spec.path("src/main/resources/custom.yaml"))
395+
);
396+
}
329397
}
330398
}

src/test/java/org/openrewrite/quarkus/DeleteQuarkusPropertyTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.junit.jupiter.api.Test;
2121
import org.openrewrite.test.RewriteTest;
2222

23+
import java.util.List;
24+
2325
import static org.openrewrite.properties.Assertions.properties;
2426
import static org.openrewrite.yaml.Assertions.yaml;
2527

@@ -110,6 +112,16 @@ void deleteValueAllProfilesBecauseDefault() {
110112
properties(sourceProperties, "", spec -> spec.path("src/main/resources/application.properties"))
111113
);
112114
}
115+
116+
@Test
117+
void deleteValueOnCustomPath() {
118+
rewriteRun(
119+
spec -> spec.recipe(new DeleteQuarkusProperty(
120+
"quarkus\\.hibernate-search-orm(\\..*)?\\.automatic-indexing\\.synchronization\\.strategy",
121+
null, null, true, List.of("**/custom.{properties,yaml,yml}"))),
122+
properties(sourceProperties, "", spec -> spec.path("src/main/resources/custom.properties"))
123+
);
124+
}
113125
}
114126

115127
@Nested
@@ -256,5 +268,15 @@ void deleteValueOnAllProfiles() {
256268
yaml(sourceYaml, "", spec -> spec.path("src/main/resources/application.yaml"))
257269
);
258270
}
271+
272+
@Test
273+
void deleteValueOnCustomPath() {
274+
rewriteRun(
275+
spec -> spec.recipe(new DeleteQuarkusProperty(
276+
"quarkus\\.hibernate-search-orm(\\..*)?\\.automatic-indexing\\.synchronization\\.strategy",
277+
null, null, true, List.of("**/custom.{properties,yaml,yml}"))),
278+
yaml(sourceYaml, "", spec -> spec.path("src/main/resources/custom.yaml"))
279+
);
280+
}
259281
}
260282
}

0 commit comments

Comments
 (0)