Skip to content

Commit

Permalink
work on it
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Dec 18, 2024
1 parent 182ec9b commit 80b611a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 53 deletions.
38 changes: 37 additions & 1 deletion rewrite-java/src/main/java/org/openrewrite/java/FindImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@
*/
package org.openrewrite.java;

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.java.table.ImageSourceFiles;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.trait.Reference;
import org.openrewrite.trait.SimpleTraitMatcher;

import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// TODO: Remove this file, we will use the `FindDockerImageUses` in the rewrite-docker module
Expand All @@ -45,21 +50,52 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {

@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
// TODO improve: `if (sourceFile instanceof PlainText && references.size() > 1)` then all markers are set at beginning
Tree t = super.visit(tree, ctx);

if (tree instanceof SourceFileWithReferences) {
SourceFileWithReferences sourceFile = (SourceFileWithReferences) tree;
Path sourcePath = sourceFile.getSourcePath();
Collection<Reference> references = sourceFile.getReferences().findMatches(new ImageMatcher());

/*new JavaIsoVisitor<ExecutionContext>() {
vsit
};
new SimpleTraitMatcher<Reference>() {
@Override
protected @Nullable Reference test(Cursor cursor) {
return null;
}
}.asVisitor().visit(sourceFile, 0);*/

// TODO improve: `if (sourceFile instanceof PlainText && references.size() > 1)` then all markers are set at beginning
String value = references.stream()
.map(Reference::getValue)
.peek(it -> results.insertRow(ctx, new ImageSourceFiles.Row(sourcePath.toString(), tree.getClass().getSimpleName(), it)))
.sorted()
.collect(Collectors.joining("|"));
/*System.out.println(tree);
System.out.println(value);*/
return SearchResult.found(tree, value);
}
return tree;
}
};
}

@Value
@EqualsAndHashCode(callSuper = false)
private static class ReferenceFindSearchResultVisitor extends TreeVisitor<Tree, ExecutionContext> {
Map<Tree, Reference> matches;

@Override
public Tree postVisit(Tree tree, ExecutionContext ctx) {
Reference reference = matches.get(tree);
if (reference != null && getCursor().equals(reference.getCursor())) {
return SearchResult.found(tree, reference.getValue());
}
return tree;
}
}
}
65 changes: 13 additions & 52 deletions rewrite-java/src/test/java/org/openrewrite/java/FindImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.table.ImageSourceFiles;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.test.SourceSpecs.text;
import static org.openrewrite.yaml.Assertions.yaml;

Expand All @@ -35,64 +37,17 @@ public void defaults(RecipeSpec spec) {
@Test
void gitlabCIFile() {
rewriteRun(
spec -> spec.recipe(new FindImage())
.dataTable(ImageSourceFiles.Row.class, rows -> {
assertThat(rows).hasSize(1);
assertThat(rows.get(0).getValue()).isEqualTo("maven:latest");
}),
yaml(
"""
image: maven:latest
variables:
MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
cache:
paths:
- .m2/repository/
- target/
build:
stage: build
script:
- mvn $MAVEN_CLI_OPTS compile
test:
stage: test
script:
- mvn $MAVEN_CLI_OPTS test
deploy:
stage: deploy
script:
- mvn $MAVEN_CLI_OPTS deploy
only:
- master
""",
"""
~~(maven:latest)~~>image: maven:latest
variables:
MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
cache:
paths:
- .m2/repository/
- target/
build:
stage: build
script:
- mvn $MAVEN_CLI_OPTS compile
test:
stage: test
script:
- mvn $MAVEN_CLI_OPTS test
deploy:
stage: deploy
script:
- mvn $MAVEN_CLI_OPTS deploy
only:
- master
""",
spec -> spec.path(".gitlab-ci")
)
Expand All @@ -102,6 +57,12 @@ void gitlabCIFile() {
@Test
void dockerFile() {
rewriteRun(
spec -> spec.recipe(new FindImage())
.dataTable(ImageSourceFiles.Row.class, rows -> {
assertThat(rows).hasSize(2);
assertThat(rows.get(0).getValue()).isEqualTo("alpine:latest");
assertThat(rows.get(1).getValue()).isEqualTo("golang:1.7.3");
}),
text(
//language=Dockerfile
"""
Expand Down

0 comments on commit 80b611a

Please sign in to comment.