Skip to content

Commit

Permalink
Rework PR to add Type option to AddRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Jun 13, 2024
1 parent 07a3371 commit 6f77692
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 562 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.maven;

import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.intellij.lang.annotations.Language;
import org.openrewrite.ExecutionContext;
Expand All @@ -35,7 +36,6 @@
@Value
@EqualsAndHashCode(callSuper = false)
public class AddRepository extends Recipe {
private static final XPathMatcher REPOS_MATCHER = new XPathMatcher("/project/repositories");

@Option(example = "repo-id", displayName = "Repository ID",
description = "A unique name to describe the repository.")
Expand Down Expand Up @@ -93,6 +93,20 @@ public class AddRepository extends Recipe {
@Nullable
String releasesUpdatePolicy;

@Option(displayName = "Repository type",
description = "The type of repository to add.",
example = "Repository")
Type type;

@RequiredArgsConstructor
public enum Type {
Repository("repository", "repositories"),
PluginRepository("pluginRepository", "pluginRepositories");

final String xmlTagSingle;
final String xmlTagPlural;
}

@Override
public String getDisplayName() {
return "Add repository";
Expand All @@ -106,11 +120,13 @@ public String getDescription() {
@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new MavenIsoVisitor<ExecutionContext>() {
private final XPathMatcher REPOS_MATCHER = new XPathMatcher("/project/" + type.xmlTagPlural);

@Override
public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
Xml.Tag root = document.getRoot();
if (!root.getChild("repositories").isPresent()) {
document = (Xml.Document) new AddToTagVisitor<>(root, Xml.Tag.build("<repositories/>"))
if (!root.getChild(type.xmlTagPlural).isPresent()) {
document = (Xml.Document) new AddToTagVisitor<>(root, Xml.Tag.build("<" + type.xmlTagPlural + "/>"))
.visitNonNull(document, ctx, getCursor().getParentOrThrow());
}
return super.visitDocument(document, ctx);
Expand All @@ -123,7 +139,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
if (REPOS_MATCHER.matches(getCursor())) {
Optional<Xml.Tag> maybeRepo = repositories.getChildren().stream()
.filter(repo ->
"repository".equals(repo.getName()) &&
type.xmlTagSingle.equals(repo.getName()) &&
(id.equals(repo.getChildValue("id").orElse(null)) || (isReleasesEqual(repo) && isSnapshotsEqual(repo))) &&
url.equals(repo.getChildValue("url").orElse(null))
)
Expand Down Expand Up @@ -171,14 +187,14 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
}
} else {
@Language("xml")
String sb = "<repository>\n" +
String sb = "<" + type.xmlTagSingle + ">\n" +
assembleTagWithValue("id", id) +
assembleTagWithValue("url", url) +
assembleTagWithValue("name", repoName) +
assembleTagWithValue("layout", layout) +
assembleReleases() +
assembleSnapshots() +
"</repository>\n";
"</" + type.xmlTagSingle + ">\n";

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

0 comments on commit 6f77692

Please sign in to comment.