-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8fca6b
commit 8e66538
Showing
11 changed files
with
121 additions
and
87 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
rewrite-core/src/main/java/org/openrewrite/text/DockerImageReference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package org.openrewrite.text; | ||
|
||
import lombok.Value; | ||
import org.openrewrite.Cursor; | ||
import org.openrewrite.SourceFile; | ||
import org.openrewrite.internal.StringUtils; | ||
import org.openrewrite.trait.Reference; | ||
import org.openrewrite.trait.SimpleTraitMatcher; | ||
|
||
@Value | ||
public class DockerImageReference implements Reference { | ||
Cursor cursor; | ||
String value; | ||
|
||
@Override | ||
public Kind getKind() { | ||
return Kind.IMAGE; | ||
} | ||
|
||
public static class Provider implements Reference.Provider<DockerImageReference> { | ||
@Override | ||
public boolean isAcceptable(SourceFile sourceFile) { | ||
if (sourceFile instanceof PlainText) { | ||
PlainText text = (PlainText) sourceFile; | ||
String fileName = text.getSourcePath().toFile().getName(); | ||
return (fileName.equals("Dockerfile") || fileName.equals("Containerfile")) && text.getText().contains("FROM"); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public SimpleTraitMatcher<DockerImageReference> getMatcher() { | ||
return new SimpleTraitMatcher<DockerImageReference>() { | ||
@Override | ||
protected DockerImageReference test(Cursor cursor) { | ||
String text = ((PlainText) cursor.getValue()).getText(); | ||
int index = StringUtils.indexOfNextNonWhitespace(text.indexOf("FROM") + 4, text); | ||
StringBuilder image = new StringBuilder(); | ||
for (char c : text.substring(index).toCharArray()) { | ||
if (Character.isWhitespace(c)) { | ||
break; | ||
} | ||
image.append(c); | ||
} | ||
return new DockerImageReference(cursor, image.toString()); | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
rewrite-core/src/main/resources/META-INF/services/org.openrewrite.trait.Reference$Provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.openrewrite.text.DockerImageReference$Provider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters