Skip to content

Commit

Permalink
Add Docker Image reference, to be used for gitlab, docker, etc (#4793)
Browse files Browse the repository at this point in the history
* Basic implementation to find image over multiple sources

* Formatting

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Improvement

* Improvement

* Add TODO removal comments

* Add DockerImageReference

* Add DockerImageReference

* Add DockerImageReference

* Introduce AbstractProvider

* Improve DockerImageReference

* work on it

* first impl

* Extra yaml test

* working example

* working example

* working example

* revert lost

* Fix

* Support --from and --platform and ignore comments for dockerfile

* Merge branch 'main' into introduce-image-reference-and-recipe

* Remove files (will be places in rewrite-docker)

* Remove files (will be places in rewrite-docker)

* Cleanup `findMatches`

* Make SourceFileWithReferences `abstract`, so we can define `getReferences` once

* Make SourceFileWithReferences `abstract`, so we can define `getReferences` once

* Make SourceFileWithReferences `abstract`, so we can define `getReferences` once

* Make SourceFileWithReferences `abstract`, so we can define `getReferences` once

* Revert `SourceFileWithReferences` as interface

* Make matchers static singletons in the provider classes

* Update description

* Fix tests

* Revert implements of File class

* Pattern.asPredicate() acts as `find`, not `matches()`

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
3 people authored Dec 30, 2024
1 parent 2375e31 commit a8946ac
Show file tree
Hide file tree
Showing 20 changed files with 234 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,40 @@
import org.jspecify.annotations.Nullable;
import org.openrewrite.trait.Reference;

import java.lang.ref.SoftReference;
import java.util.*;

@Incubating(since = "8.39.0")
public interface SourceFileWithReferences extends SourceFile {

References getReferences();

default SoftReference<References> build(@Nullable SoftReference<@Nullable References> references) {
References cache = references == null ? null : references.get();
if (cache == null || cache.getSourceFile() != this) {
return new SoftReference<>(References.build(this));
}
return references;
}

@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
class References {
@Getter(AccessLevel.PRIVATE)
private final SourceFile sourceFile;
@Getter
private final Set<Reference> references;

public Collection<Reference> findMatches(Reference.Matcher matcher) {
return findMatchesInternal(matcher, null);
}

public Collection<Reference> findMatches(Reference.Matcher matcher, Reference.Kind kind) {
return findMatchesInternal(matcher, kind);
}

private List<Reference> findMatchesInternal(Reference.Matcher matcher, Reference.@Nullable Kind kind) {
List<Reference> list = new ArrayList<>();
for (Reference ref : references) {
if ((kind == null || ref.getKind() == kind) && ref.matches(matcher) ) {
if (ref.matches(matcher)) {
list.add(ref);
}
}
return list;
}

public static References build(SourceFile sourceFile) {
private static References build(SourceFile sourceFile) {
Set<Reference> references = new HashSet<>();
ServiceLoader<Reference.Provider> loader = ServiceLoader.load(Reference.Provider.class);
loader.forEach(provider -> {
Expand Down
10 changes: 9 additions & 1 deletion rewrite-core/src/main/java/org/openrewrite/text/Find.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.apache.commons.lang3.BooleanUtils;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.binary.Binary;
Expand Down Expand Up @@ -91,6 +92,12 @@ public String getDescription() {
@Nullable
String filePattern;

@Option(displayName = "Description",
description = "Add the matched value(s) as description on the search result marker. Default `false`.",
required = false)
@Nullable
Boolean description;

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {

Expand Down Expand Up @@ -135,7 +142,8 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
do {
int matchStart = matcher.start();
snippets.add(snippet(rawText.substring(previousEnd, matchStart)));
snippets.add(SearchResult.found(snippet(rawText.substring(matchStart, matcher.end()))));
String text = rawText.substring(matchStart, matcher.end());
snippets.add(SearchResult.found(snippet(text), BooleanUtils.isTrue(description) ? text : null));
previousEnd = matcher.end();

// For the first match, search backwards
Expand Down
24 changes: 19 additions & 5 deletions rewrite-core/src/main/java/org/openrewrite/text/PlainText.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
package org.openrewrite.text;

import lombok.*;
import lombok.experimental.NonFinal;
import org.jspecify.annotations.Nullable;
import org.openrewrite.*;
import org.openrewrite.marker.Markers;

import java.lang.ref.SoftReference;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Predicate;

Expand All @@ -36,7 +39,7 @@
@Value
@Builder
@AllArgsConstructor
public class PlainText implements SourceFile, Tree {
public class PlainText implements SourceFileWithReferences, Tree {

@Builder.Default
@With
Expand Down Expand Up @@ -79,16 +82,27 @@ public SourceFile withCharset(Charset charset) {
@Builder.Default
String text = "";

List<Snippet> snippets;

@Nullable
@NonFinal
@ToString.Exclude
transient SoftReference<References> references;

@Override
public References getReferences() {
this.references = build(this.references);
return Objects.requireNonNull(this.references.get());
}

public PlainText withText(String text) {
if (!text.equals(this.text)) {
return new PlainText(this.id, this.sourcePath, this.markers, this.charsetName, this.charsetBomMarked,
this.fileAttributes, this.checksum, text, this.snippets);
this.fileAttributes, this.checksum, text, this.snippets, this.references);
}
return this;
}

List<Snippet> snippets;

@Override
public <P> boolean isAcceptable(TreeVisitor<?, P> v, P p) {
return v.isAdaptableTo(PlainTextVisitor.class);
Expand Down Expand Up @@ -123,7 +137,7 @@ public PlainText withSnippets(@Nullable List<Snippet> snippets) {
if (this.snippets == snippets) {
return this;
}
return new PlainText(id, sourcePath, markers, charsetName, charsetBomMarked, fileAttributes, checksum, text, snippets);
return new PlainText(id, sourcePath, markers, charsetName, charsetBomMarked, fileAttributes, checksum, text, snippets, null);
}

@Value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
input.getFileAttributes(),
null,
sourceStr,
null,
null
);
parsingListener.parsed(input, plainText);
Expand Down
18 changes: 17 additions & 1 deletion rewrite-core/src/main/java/org/openrewrite/trait/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@

import org.openrewrite.*;

import java.util.HashSet;
import java.util.Set;

@Incubating(since = "8.39.0")
public interface Reference extends Trait<Tree> {

enum Kind {
TYPE,
PACKAGE
PACKAGE,
IMAGE
}

Kind getKind();
Expand Down Expand Up @@ -57,6 +59,20 @@ interface Provider {
boolean isAcceptable(SourceFile sourceFile);
}

abstract class AbstractProvider<U extends Reference> implements Provider {
protected abstract SimpleTraitMatcher<U> getMatcher();

@Override
public Set<Reference> getReferences(SourceFile sourceFile) {
Set<Reference> references = new HashSet<>();
getMatcher().asVisitor(reference -> {
references.add(reference);
return reference.getTree();
}).visit(sourceFile, 0);
return references;
}
}

interface Matcher {

boolean matchesReference(Reference value);
Expand Down
35 changes: 25 additions & 10 deletions rewrite-core/src/test/java/org/openrewrite/text/FindTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FindTest implements RewriteTest {
@Test
void dataTable() {
rewriteRun(
spec -> spec.recipe(new Find("text", null, null, null, null, null))
spec -> spec.recipe(new Find("text", null, null, null, null, null, null))
.dataTable(TextMatches.Row.class, rows -> {
assertThat(rows).hasSize(1);
assertThat(rows.get(0).getMatch()).isEqualTo("This is ~~>text.");
Expand All @@ -53,7 +53,7 @@ void dataTable() {
@Test
void regex() {
rewriteRun(
spec -> spec.recipe(new Find("[T\\s]", true, true, null, null, null)),
spec -> spec.recipe(new Find("[T\\s]", true, true, null, null, null, null)),
text(
"""
This is\ttext.
Expand All @@ -68,7 +68,7 @@ void regex() {
@Test
void plainText() {
rewriteRun(
spec -> spec.recipe(new Find("\\s", null, null, null, null, null)),
spec -> spec.recipe(new Find("\\s", null, null, null, null, null, null)),
text(
"""
This i\\s text.
Expand All @@ -83,7 +83,7 @@ void plainText() {
@Test
void caseInsensitive() {
rewriteRun(
spec -> spec.recipe(new Find("text", null, null, null, null, "**/foo/**;**/baz/**")),
spec -> spec.recipe(new Find("text", null, null, null, null, "**/foo/**;**/baz/**", null)),
dir("foo",
text(
"""
Expand Down Expand Up @@ -115,7 +115,7 @@ void caseInsensitive() {
@Test
void regexBasicMultiLine() {
rewriteRun(
spec -> spec.recipe(new Find("[T\\s]", true, true, true, null, null)),
spec -> spec.recipe(new Find("[T\\s]", true, true, true, null, null, null)),
text(
"""
This is\ttext.
Expand All @@ -132,7 +132,7 @@ void regexBasicMultiLine() {
@Test
void regexWithoutMultilineAndDotall() {
rewriteRun(
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, false, false, null)),
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, false, false, null, null)),
text(
"""
This is text.
Expand All @@ -148,7 +148,7 @@ void regexWithoutMultilineAndDotall() {
@Test
void regexMatchingWhitespaceWithoutMultilineWithDotall() {
rewriteRun(
spec -> spec.recipe(new Find("One.Two$", true, true, false, true, null)),
spec -> spec.recipe(new Find("One.Two$", true, true, false, true, null, null)),
//language=csv
text( // the `.` above matches the space character on the same line
"""
Expand All @@ -163,7 +163,7 @@ void regexMatchingWhitespaceWithoutMultilineWithDotall() {
@Test
void regexWithoutMultilineAndWithDotAll() {
rewriteRun(
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, false, true, null)),
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, false, true, null, null)),
text(
"""
This is text.
Expand All @@ -186,7 +186,7 @@ void regexWithoutMultilineAndWithDotAll() {
@Test
void regexWithMultilineAndWithoutDotall() {
rewriteRun(
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, true, false, null)),
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, true, false, null, null)),
text(
"""
This is text.
Expand All @@ -209,7 +209,7 @@ void regexWithMultilineAndWithoutDotall() {
@Test
void regexWithBothMultilineAndDotAll() {
rewriteRun(
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, true, true, null)),
spec -> spec.recipe(new Find("^This.*below\\.$", true, true, true, true, null, null)),
text(
"""
The first line.
Expand All @@ -228,4 +228,19 @@ void regexWithBothMultilineAndDotAll() {
)
);
}

@Test
void description() {
rewriteRun(
spec -> spec.recipe(new Find("text", null, null, null, null, null, true)),
text(
"""
This is text.
""",
"""
This is ~~(text)~~>text.
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
SourceFileWithReferences.References references = sourceFile.getReferences();
TypeMatcher matcher = new TypeMatcher(oldFullyQualifiedTypeName);
Map<Tree, Reference> matches = new HashMap<>();
for (Reference ref : references.findMatches(matcher, Reference.Kind.TYPE)) {
for (Reference ref : references.findMatches(matcher)) {
matches.put(ref.getTree(), ref);
}
return new ReferenceChangeTypeVisitor(matches, matcher.createRenamer(newFullyQualifiedTypeName)).visit(tree, ctx, requireNonNull(getCursor().getParent()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.openrewrite.java.table.TypeUses;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.SearchResult;
import org.openrewrite.trait.Reference;
import org.openrewrite.trait.Trait;

import java.util.HashSet;
Expand Down Expand Up @@ -80,7 +79,7 @@ public boolean isAcceptable(SourceFile sourceFile, ExecutionContext ctx) {
SourceFileWithReferences sourceFile = (SourceFileWithReferences) tree;
SourceFileWithReferences.References references = sourceFile.getReferences();
TypeMatcher matcher = new TypeMatcher(fullyQualifiedTypeName);
Set<Tree> matches = references.findMatches(matcher, Reference.Kind.TYPE).stream().map(Trait::getTree).collect(Collectors.toSet());
Set<Tree> matches = references.findMatches(matcher).stream().map(Trait::getTree).collect(Collectors.toSet());
return new ReferenceVisitor(matches).visit(tree, ctx);
}
return tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public boolean isAcceptable(SourceFile sourceFile, P p) {
SourceFileWithReferences sourceFile = (SourceFileWithReferences) tree;
SourceFileWithReferences.References references = sourceFile.getReferences();
TypeMatcher matcher = typeMatcher != null ? typeMatcher : new TypeMatcher(fullyQualifiedType);
for (Reference ignored : references.findMatches(matcher, Reference.Kind.TYPE)) {
for (Reference ignored : references.findMatches(matcher)) {
return SearchResult.found(sourceFile);
}
}
Expand Down
Loading

0 comments on commit a8946ac

Please sign in to comment.