Skip to content

Commit

Permalink
Improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
jevanlingen committed Dec 17, 2024
1 parent d431061 commit 4d6bca8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@
*/
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 java.nio.file.Path;

@Value
@EqualsAndHashCode(callSuper = false)
public class FindImage extends Recipe {
transient ImageSourceFiles results = new ImageSourceFiles(this);

Expand All @@ -50,6 +47,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
Path sourcePath = sourceFile.getSourcePath();
for (Reference ref : sourceFile.getReferences().findMatches(new ImageMatcher())) {
results.insertRow(ctx, new ImageSourceFiles.Row(sourcePath.toString(), tree.getClass().getSimpleName(), ref.getValue()));
return SearchResult.found(tree, ref.getValue());
}
}
return tree;
Expand Down
58 changes: 46 additions & 12 deletions rewrite-java/src/test/java/org/openrewrite/java/FindImageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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;
Expand All @@ -26,15 +27,16 @@

class FindImageTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new FindImage());
}


@DocumentExample
@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
Expand Down Expand Up @@ -64,27 +66,59 @@ void gitlabCIFile() {
- 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")
)
);
}

@Test
void dockerFile() {
rewriteRun(
spec -> spec.recipe(new FindImage())
.dataTable(ImageSourceFiles.Row.class, rows -> {
assertThat(rows).hasSize(1);
assertThat(rows.get(0).getValue()).isEqualTo("openjdk:8-jdk-alpine");
}),
text(
//language=Dockerfile
"""
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
"""
""",
"""
~~(FROM openjdk:8-jdk-alpine)~~>FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
""",
spec -> spec.path("Dockerfile")
)
);
}
Expand Down

0 comments on commit 4d6bca8

Please sign in to comment.