5: Make plugins to have Plugin interface and add supports method to them
This commit is contained in:
@@ -1,4 +1,19 @@
|
|||||||
package com.bartek.esa.core.archetype;
|
package com.bartek.esa.core.archetype;
|
||||||
|
|
||||||
|
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
public abstract class AndroidManifestPlugin extends XmlPlugin {
|
public abstract class AndroidManifestPlugin extends XmlPlugin {
|
||||||
|
private final GlobMatcher globMatcher;
|
||||||
|
|
||||||
|
protected AndroidManifestPlugin(GlobMatcher globMatcher) {
|
||||||
|
super(globMatcher);
|
||||||
|
this.globMatcher = globMatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(File file) {
|
||||||
|
return globMatcher.fileMatchesGlobPattern(file, "**/AndroidManifest.xml");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,15 +6,17 @@ import java.io.File;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public abstract class BasePlugin {
|
public abstract class BasePlugin implements Plugin {
|
||||||
private List<Issue> issues = new ArrayList<>();
|
private List<Issue> issues = new ArrayList<>();
|
||||||
private File file;
|
private File file;
|
||||||
|
|
||||||
|
@Override
|
||||||
public void update(File file) {
|
public void update(File file) {
|
||||||
this.file = file;
|
this.file = file;
|
||||||
this.issues.clear();
|
this.issues.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public List<Issue> runForIssues() {
|
public List<Issue> runForIssues() {
|
||||||
run(file);
|
run(file);
|
||||||
return issues;
|
return issues;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bartek.esa.core.archetype;
|
package com.bartek.esa.core.archetype;
|
||||||
|
|
||||||
|
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||||
import com.github.javaparser.ast.CompilationUnit;
|
import com.github.javaparser.ast.CompilationUnit;
|
||||||
import com.github.javaparser.ast.body.BodyDeclaration;
|
import com.github.javaparser.ast.body.BodyDeclaration;
|
||||||
import com.github.javaparser.ast.body.MethodDeclaration;
|
import com.github.javaparser.ast.body.MethodDeclaration;
|
||||||
@@ -13,6 +14,10 @@ import java.util.stream.Stream;
|
|||||||
|
|
||||||
public abstract class JavaMethodBodyStatementPlugin extends JavaPlugin {
|
public abstract class JavaMethodBodyStatementPlugin extends JavaPlugin {
|
||||||
|
|
||||||
|
public JavaMethodBodyStatementPlugin(GlobMatcher globMatcher) {
|
||||||
|
super(globMatcher);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(CompilationUnit compilationUnit) {
|
public void run(CompilationUnit compilationUnit) {
|
||||||
Stream<Statement> statements = compilationUnit.getTypes().stream()
|
Stream<Statement> statements = compilationUnit.getTypes().stream()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.bartek.esa.core.archetype;
|
package com.bartek.esa.core.archetype;
|
||||||
|
|
||||||
import com.bartek.esa.error.EsaException;
|
import com.bartek.esa.error.EsaException;
|
||||||
|
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||||
import com.github.javaparser.StaticJavaParser;
|
import com.github.javaparser.StaticJavaParser;
|
||||||
import com.github.javaparser.ast.CompilationUnit;
|
import com.github.javaparser.ast.CompilationUnit;
|
||||||
import io.vavr.control.Try;
|
import io.vavr.control.Try;
|
||||||
@@ -8,6 +9,16 @@ import io.vavr.control.Try;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
public abstract class JavaPlugin extends BasePlugin {
|
public abstract class JavaPlugin extends BasePlugin {
|
||||||
|
private final GlobMatcher globMatcher;
|
||||||
|
|
||||||
|
protected JavaPlugin(GlobMatcher globMatcher) {
|
||||||
|
this.globMatcher = globMatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(File file) {
|
||||||
|
return globMatcher.fileMatchesGlobPattern(file, "**/*.java");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void run(File file) {
|
protected void run(File file) {
|
||||||
|
|||||||
12
src/main/java/com/bartek/esa/core/archetype/Plugin.java
Normal file
12
src/main/java/com/bartek/esa/core/archetype/Plugin.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package com.bartek.esa.core.archetype;
|
||||||
|
|
||||||
|
import com.bartek.esa.core.model.Issue;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface Plugin {
|
||||||
|
boolean supports(File file);
|
||||||
|
void update(File file);
|
||||||
|
List<Issue> runForIssues();
|
||||||
|
}
|
||||||
@@ -1,4 +1,19 @@
|
|||||||
package com.bartek.esa.core.archetype;
|
package com.bartek.esa.core.archetype;
|
||||||
|
|
||||||
|
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
public abstract class ResourceLayoutPlugin extends XmlPlugin {
|
public abstract class ResourceLayoutPlugin extends XmlPlugin {
|
||||||
|
private final GlobMatcher globMatcher;
|
||||||
|
|
||||||
|
public ResourceLayoutPlugin(GlobMatcher globMatcher) {
|
||||||
|
super(globMatcher);
|
||||||
|
this.globMatcher = globMatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(File file) {
|
||||||
|
return globMatcher.fileMatchesGlobPattern(file, "**/res/layout*/*.xml");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.bartek.esa.core.archetype;
|
package com.bartek.esa.core.archetype;
|
||||||
|
|
||||||
import com.bartek.esa.error.EsaException;
|
import com.bartek.esa.error.EsaException;
|
||||||
|
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||||
import io.vavr.control.Try;
|
import io.vavr.control.Try;
|
||||||
import org.w3c.dom.Document;
|
import org.w3c.dom.Document;
|
||||||
|
|
||||||
@@ -11,6 +12,16 @@ import javax.xml.xpath.XPathFactory;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
public abstract class XmlPlugin extends BasePlugin {
|
public abstract class XmlPlugin extends BasePlugin {
|
||||||
|
private final GlobMatcher globMatcher;
|
||||||
|
|
||||||
|
protected XmlPlugin(GlobMatcher globMatcher) {
|
||||||
|
this.globMatcher = globMatcher;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean supports(File file) {
|
||||||
|
return globMatcher.fileMatchesGlobPattern(file, "**/*.xml");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void run(File file) {
|
protected void run(File file) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.bartek.esa.core.executor;
|
package com.bartek.esa.core.executor;
|
||||||
|
|
||||||
import com.bartek.esa.core.archetype.BasePlugin;
|
import com.bartek.esa.core.archetype.Plugin;
|
||||||
import com.bartek.esa.core.model.Issue;
|
import com.bartek.esa.core.model.Issue;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@@ -16,15 +16,16 @@ public class PluginExecutor {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Issue> executeForFiles(List<File> files, List<BasePlugin> plugins) {
|
public List<Issue> executeForFiles(List<File> files, List<Plugin> plugins) {
|
||||||
return files.stream()
|
return files.stream()
|
||||||
.map(file -> executeForFile(file, plugins))
|
.map(file -> executeForFile(file, plugins))
|
||||||
.flatMap(List::stream)
|
.flatMap(List::stream)
|
||||||
.collect(toList());
|
.collect(toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Issue> executeForFile(File file, List<BasePlugin> plugins) {
|
private List<Issue> executeForFile(File file, List<Plugin> plugins) {
|
||||||
return plugins.stream()
|
return plugins.stream()
|
||||||
|
.filter(plugin -> plugin.supports(file))
|
||||||
.map(plugin -> {
|
.map(plugin -> {
|
||||||
plugin.update(file);
|
plugin.update(file);
|
||||||
return plugin.runForIssues();
|
return plugin.runForIssues();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.bartek.esa.file.di;
|
package com.bartek.esa.file.di;
|
||||||
|
|
||||||
|
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||||
import com.bartek.esa.file.provider.FileContentProvider;
|
import com.bartek.esa.file.provider.FileContentProvider;
|
||||||
import com.bartek.esa.file.provider.FileProvider;
|
import com.bartek.esa.file.provider.FileProvider;
|
||||||
import dagger.Module;
|
import dagger.Module;
|
||||||
@@ -9,12 +10,17 @@ import dagger.Provides;
|
|||||||
public class FileModule {
|
public class FileModule {
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
public FileProvider fileProvider() {
|
public FileProvider fileProvider(GlobMatcher globMatcher) {
|
||||||
return new FileProvider();
|
return new FileProvider(globMatcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
public FileContentProvider fileContentProvider() {
|
public FileContentProvider fileContentProvider() {
|
||||||
return new FileContentProvider();
|
return new FileContentProvider();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Provides
|
||||||
|
public GlobMatcher globMatcher() {
|
||||||
|
return new GlobMatcher();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
22
src/main/java/com/bartek/esa/file/matcher/GlobMatcher.java
Normal file
22
src/main/java/com/bartek/esa/file/matcher/GlobMatcher.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.bartek.esa.file.matcher;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.file.FileSystems;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
public class GlobMatcher {
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
public GlobMatcher() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean fileMatchesGlobPattern(File file, String globPattern) {
|
||||||
|
return pathMatchesGlobPattern(file.toPath(), globPattern);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean pathMatchesGlobPattern(Path path, String globPattern) {
|
||||||
|
return FileSystems.getDefault().getPathMatcher("glob:" + globPattern).matches(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.bartek.esa.file.provider;
|
package com.bartek.esa.file.provider;
|
||||||
|
|
||||||
import com.bartek.esa.error.EsaException;
|
import com.bartek.esa.error.EsaException;
|
||||||
|
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||||
import io.vavr.control.Try;
|
import io.vavr.control.Try;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
@@ -12,10 +13,12 @@ import java.util.Set;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class FileProvider {
|
public class FileProvider {
|
||||||
|
private final GlobMatcher globMatcher;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public FileProvider() {
|
public FileProvider(GlobMatcher globMatcher) {
|
||||||
|
|
||||||
|
this.globMatcher = globMatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
public File createTemporaryDirectory() {
|
public File createTemporaryDirectory() {
|
||||||
@@ -24,49 +27,11 @@ public class FileProvider {
|
|||||||
.toFile();
|
.toFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<File> findFilesRecursively(String path, String globPattern) {
|
public Set<File> getGlobMatchedFiles(String path, String globPattern) {
|
||||||
return findFilesRecursivelyInSubpath(path, "", globPattern);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<File> findFilesRecursivelyInSubpath(String path, String subpath, String globPattern) {
|
|
||||||
return Try.of(() -> Files.walk(Paths.get(path))
|
return Try.of(() -> Files.walk(Paths.get(path))
|
||||||
.filter(p -> p.toString().contains(subpath))
|
.filter(p -> globMatcher.pathMatchesGlobPattern(p, globPattern))
|
||||||
.filter(p -> matchesGlobPattern(p.getFileName().toString(), globPattern))
|
|
||||||
.map(Path::toFile)
|
.map(Path::toFile)
|
||||||
.collect(Collectors.toSet()))
|
.collect(Collectors.toSet()))
|
||||||
.getOrElseThrow(EsaException::new);
|
.getOrElseThrow(EsaException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean matchesGlobPattern(String filename, String globPattern) {
|
|
||||||
return filename.matches(createRegexFromGlob(globPattern));
|
|
||||||
}
|
|
||||||
|
|
||||||
private String createRegexFromGlob(String glob) {
|
|
||||||
StringBuilder out = new StringBuilder("^");
|
|
||||||
|
|
||||||
glob
|
|
||||||
.chars()
|
|
||||||
.mapToObj(i -> (char) i)
|
|
||||||
.map(this::globCharacterToRegexString)
|
|
||||||
.forEach(out::append);
|
|
||||||
|
|
||||||
out.append('$');
|
|
||||||
|
|
||||||
return out.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String globCharacterToRegexString(char character) {
|
|
||||||
switch (character) {
|
|
||||||
case '*':
|
|
||||||
return ".*";
|
|
||||||
case '?':
|
|
||||||
return ".";
|
|
||||||
case '.':
|
|
||||||
return "\\.";
|
|
||||||
case '\\':
|
|
||||||
return "\\\\";
|
|
||||||
default:
|
|
||||||
return String.valueOf(character);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user