5: Make plugins to have Plugin interface and add supports method to them

This commit is contained in:
Bartłomiej Pluta
2019-04-01 17:23:01 +02:00
parent 844205f280
commit 779a6255d8
11 changed files with 112 additions and 47 deletions

View File

@@ -1,4 +1,19 @@
package com.bartek.esa.core.archetype;
import com.bartek.esa.file.matcher.GlobMatcher;
import java.io.File;
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");
}
}

View File

@@ -6,15 +6,17 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
public abstract class BasePlugin {
public abstract class BasePlugin implements Plugin {
private List<Issue> issues = new ArrayList<>();
private File file;
@Override
public void update(File file) {
this.file = file;
this.issues.clear();
}
@Override
public List<Issue> runForIssues() {
run(file);
return issues;

View File

@@ -1,5 +1,6 @@
package com.bartek.esa.core.archetype;
import com.bartek.esa.file.matcher.GlobMatcher;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.BodyDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
@@ -13,6 +14,10 @@ import java.util.stream.Stream;
public abstract class JavaMethodBodyStatementPlugin extends JavaPlugin {
public JavaMethodBodyStatementPlugin(GlobMatcher globMatcher) {
super(globMatcher);
}
@Override
public void run(CompilationUnit compilationUnit) {
Stream<Statement> statements = compilationUnit.getTypes().stream()

View File

@@ -1,6 +1,7 @@
package com.bartek.esa.core.archetype;
import com.bartek.esa.error.EsaException;
import com.bartek.esa.file.matcher.GlobMatcher;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import io.vavr.control.Try;
@@ -8,6 +9,16 @@ import io.vavr.control.Try;
import java.io.File;
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
protected void run(File file) {

View 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();
}

View File

@@ -1,4 +1,19 @@
package com.bartek.esa.core.archetype;
import com.bartek.esa.file.matcher.GlobMatcher;
import java.io.File;
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");
}
}

View File

@@ -1,6 +1,7 @@
package com.bartek.esa.core.archetype;
import com.bartek.esa.error.EsaException;
import com.bartek.esa.file.matcher.GlobMatcher;
import io.vavr.control.Try;
import org.w3c.dom.Document;
@@ -11,6 +12,16 @@ import javax.xml.xpath.XPathFactory;
import java.io.File;
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
protected void run(File file) {

View File

@@ -1,6 +1,6 @@
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 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()
.map(file -> executeForFile(file, plugins))
.flatMap(List::stream)
.collect(toList());
}
private List<Issue> executeForFile(File file, List<BasePlugin> plugins) {
private List<Issue> executeForFile(File file, List<Plugin> plugins) {
return plugins.stream()
.filter(plugin -> plugin.supports(file))
.map(plugin -> {
plugin.update(file);
return plugin.runForIssues();

View File

@@ -1,5 +1,6 @@
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.FileProvider;
import dagger.Module;
@@ -9,12 +10,17 @@ import dagger.Provides;
public class FileModule {
@Provides
public FileProvider fileProvider() {
return new FileProvider();
public FileProvider fileProvider(GlobMatcher globMatcher) {
return new FileProvider(globMatcher);
}
@Provides
public FileContentProvider fileContentProvider() {
return new FileContentProvider();
}
@Provides
public GlobMatcher globMatcher() {
return new GlobMatcher();
}
}

View 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);
}
}

View File

@@ -1,6 +1,7 @@
package com.bartek.esa.file.provider;
import com.bartek.esa.error.EsaException;
import com.bartek.esa.file.matcher.GlobMatcher;
import io.vavr.control.Try;
import javax.inject.Inject;
@@ -12,10 +13,12 @@ import java.util.Set;
import java.util.stream.Collectors;
public class FileProvider {
private final GlobMatcher globMatcher;
@Inject
public FileProvider() {
public FileProvider(GlobMatcher globMatcher) {
this.globMatcher = globMatcher;
}
public File createTemporaryDirectory() {
@@ -24,49 +27,11 @@ public class FileProvider {
.toFile();
}
public Set<File> findFilesRecursively(String path, String globPattern) {
return findFilesRecursivelyInSubpath(path, "", globPattern);
}
public Set<File> findFilesRecursivelyInSubpath(String path, String subpath, String globPattern) {
public Set<File> getGlobMatchedFiles(String path, String globPattern) {
return Try.of(() -> Files.walk(Paths.get(path))
.filter(p -> p.toString().contains(subpath))
.filter(p -> matchesGlobPattern(p.getFileName().toString(), globPattern))
.filter(p -> globMatcher.pathMatchesGlobPattern(p, globPattern))
.map(Path::toFile)
.collect(Collectors.toSet()))
.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);
}
}
}