Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7182d7b

Browse files
jdmcmahantimtebeek
andauthored
Add AddPluginRepository recipe (openrewrite#4247)
* Add `AddPluginRepository` recipe * formatting tweaks * Rework PR to add Type option to `AddRepository` * Make AddRepository.type optional for use in Yaml recipes * Make AddRepository.type optional for use in Yaml recipes --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent bd6fb4e commit 7182d7b

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

rewrite-maven/src/main/java/org/openrewrite/maven/AddRepository.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.openrewrite.maven;
1717

1818
import lombok.EqualsAndHashCode;
19+
import lombok.RequiredArgsConstructor;
1920
import lombok.Value;
2021
import org.intellij.lang.annotations.Language;
2122
import org.openrewrite.ExecutionContext;
@@ -35,7 +36,6 @@
3536
@Value
3637
@EqualsAndHashCode(callSuper = false)
3738
public class AddRepository extends Recipe {
38-
private static final XPathMatcher REPOS_MATCHER = new XPathMatcher("/project/repositories");
3939

4040
@Option(example = "repo-id", displayName = "Repository ID",
4141
description = "A unique name to describe the repository.")
@@ -93,6 +93,26 @@ public class AddRepository extends Recipe {
9393
@Nullable
9494
String releasesUpdatePolicy;
9595

96+
@Option(displayName = "Repository type",
97+
description = "The type of repository to add.",
98+
example = "Repository",
99+
required = false)
100+
@Nullable
101+
Type type;
102+
103+
@RequiredArgsConstructor
104+
public enum Type {
105+
Repository("repository", "repositories"),
106+
PluginRepository("pluginRepository", "pluginRepositories");
107+
108+
final String xmlTagSingle;
109+
final String xmlTagPlural;
110+
}
111+
112+
public Type getType() {
113+
return type == null ? Type.Repository : type;
114+
}
115+
96116
@Override
97117
public String getDisplayName() {
98118
return "Add repository";
@@ -106,11 +126,13 @@ public String getDescription() {
106126
@Override
107127
public TreeVisitor<?, ExecutionContext> getVisitor() {
108128
return new MavenIsoVisitor<ExecutionContext>() {
129+
private final XPathMatcher REPOS_MATCHER = new XPathMatcher("/project/" + getType().xmlTagPlural);
130+
109131
@Override
110132
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
111133
Xml.Tag root = document.getRoot();
112-
if (!root.getChild("repositories").isPresent()) {
113-
document = (Xml.Document) new AddToTagVisitor<>(root, Xml.Tag.build("<repositories/>"))
134+
if (!root.getChild(getType().xmlTagPlural).isPresent()) {
135+
document = (Xml.Document) new AddToTagVisitor<>(root, Xml.Tag.build("<" + getType().xmlTagPlural + "/>"))
114136
.visitNonNull(document, ctx, getCursor().getParentOrThrow());
115137
}
116138
return super.visitDocument(document, ctx);
@@ -123,7 +145,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
123145
if (REPOS_MATCHER.matches(getCursor())) {
124146
Optional<Xml.Tag> maybeRepo = repositories.getChildren().stream()
125147
.filter(repo ->
126-
"repository".equals(repo.getName()) &&
148+
getType().xmlTagSingle.equals(repo.getName()) &&
127149
(id.equals(repo.getChildValue("id").orElse(null)) || (isReleasesEqual(repo) && isSnapshotsEqual(repo))) &&
128150
url.equals(repo.getChildValue("url").orElse(null))
129151
)
@@ -171,14 +193,14 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
171193
}
172194
} else {
173195
@Language("xml")
174-
String sb = "<repository>\n" +
196+
String sb = "<" + getType().xmlTagSingle + ">\n" +
175197
assembleTagWithValue("id", id) +
176198
assembleTagWithValue("url", url) +
177199
assembleTagWithValue("name", repoName) +
178200
assembleTagWithValue("layout", layout) +
179201
assembleReleases() +
180202
assembleSnapshots() +
181-
"</repository>\n";
203+
"</" + getType().xmlTagSingle + ">\n";
182204

183205
Xml.Tag repoTag = Xml.Tag.build(sb);
184206
repositories = (Xml.Tag) new AddToTagVisitor<>(repositories, repoTag).visitNonNull(repositories, ctx, getCursor().getParentOrThrow());

rewrite-maven/src/test/java/org/openrewrite/maven/AddRepositoryTest.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void addSimpleRepo() {
2929
rewriteRun(
3030
spec -> spec.recipe(new AddRepository("myRepo", "http://myrepo.maven.com/repo", null, null,
3131
null, null, null,
32-
null, null, null)),
32+
null, null, null, null)),
3333
pomXml(
3434
"""
3535
<project>
@@ -55,12 +55,43 @@ void addSimpleRepo() {
5555
);
5656
}
5757

58+
@Test
59+
void addSimplePluginRepo() {
60+
rewriteRun(
61+
spec -> spec.recipe(new AddRepository("myRepo", "http://myrepo.maven.com/repo", null, null,
62+
null, null, null,
63+
null, null, null, AddRepository.Type.PluginRepository)),
64+
pomXml(
65+
"""
66+
<project>
67+
<groupId>com.mycompany.app</groupId>
68+
<artifactId>my-app</artifactId>
69+
<version>1</version>
70+
</project>
71+
""",
72+
"""
73+
<project>
74+
<groupId>com.mycompany.app</groupId>
75+
<artifactId>my-app</artifactId>
76+
<version>1</version>
77+
<pluginRepositories>
78+
<pluginRepository>
79+
<id>myRepo</id>
80+
<url>http://myrepo.maven.com/repo</url>
81+
</pluginRepository>
82+
</pluginRepositories>
83+
</project>
84+
"""
85+
)
86+
);
87+
}
88+
5889
@Test
5990
void updateExistingRepo() {
6091
rewriteRun(
6192
spec -> spec.recipe(new AddRepository("myRepo", "http://myrepo.maven.com/repo", "bb", null,
6293
null, null, null,
63-
null, null, null)),
94+
null, null, null, null)),
6495
pomXml(
6596
"""
6697
<project>
@@ -99,7 +130,7 @@ void doNotRemoveRepoName() {
99130
rewriteRun(
100131
spec -> spec.recipe(new AddRepository("myRepo", "http://myrepo.maven.com/repo", null, null,
101132
null, null, null,
102-
null, null, null)),
133+
null, null, null, null)),
103134
pomXml(
104135
"""
105136
<project>
@@ -124,7 +155,7 @@ void removeSnapshots() {
124155
rewriteRun(
125156
spec -> spec.recipe(new AddRepository("myRepo", "http://myrepo.maven.com/repo", null, null,
126157
null, null, null,
127-
null, null, null)),
158+
null, null, null, null)),
128159
pomXml(
129160
"""
130161
<project>
@@ -164,7 +195,7 @@ void updateSnapshots1() {
164195
rewriteRun(
165196
spec -> spec.recipe(new AddRepository("myRepo", "http://myrepo.maven.com/repo", null, null,
166197
false, "whatever", null,
167-
null, null, null)),
198+
null, null, null, null)),
168199
pomXml(
169200
"""
170201
<project>
@@ -208,7 +239,7 @@ void updateSnapshots2() {
208239
rewriteRun(
209240
spec -> spec.recipe(new AddRepository("myRepo", "http://myrepo.maven.com/repo", null, null,
210241
null, "whatever", null,
211-
null, null, null)),
242+
null, null, null, null)),
212243
pomXml(
213244
"""
214245
<project>
@@ -251,7 +282,7 @@ void noIdMatch1SameSnapshots() {
251282
rewriteRun(
252283
spec -> spec.recipe(new AddRepository("myRepo", "http://myrepo.maven.com/repo", null, null,
253284
true, null, null,
254-
null, null, null)),
285+
null, null, null, null)),
255286
pomXml(
256287
"""
257288
<project>
@@ -280,7 +311,7 @@ void updateToSpringBoot30Snapshot() {
280311
spec -> spec.recipes(
281312
new AddRepository("boot-snapshots", "https://repo.spring.io/snapshot", null, null,
282313
true, null, null,
283-
null, null, null),
314+
null, null, null, null),
284315
new UpgradeParentVersion("org.springframework.boot", "spring-boot-starter-parent", "3.0.0-SNAPSHOT", null)
285316
),
286317
pomXml(

0 commit comments

Comments
 (0)