diff --git a/src/main/java/com/bartek/esa/analyser/apk/ApkAnalyser.java b/src/main/java/com/bartek/esa/analyser/apk/ApkAnalyser.java index 2dae5f3..67dca4a 100644 --- a/src/main/java/com/bartek/esa/analyser/apk/ApkAnalyser.java +++ b/src/main/java/com/bartek/esa/analyser/apk/ApkAnalyser.java @@ -4,6 +4,9 @@ import com.bartek.esa.analyser.core.Analyser; import com.bartek.esa.core.archetype.Plugin; import com.bartek.esa.core.executor.PluginExecutor; import com.bartek.esa.decompiler.decompiler.Decompiler; +import com.bartek.esa.error.EsaException; +import com.bartek.esa.file.cleaner.FileCleaner; +import com.bartek.esa.file.matcher.GlobMatcher; import com.bartek.esa.file.provider.FileProvider; import java.io.File; @@ -15,17 +18,29 @@ public class ApkAnalyser extends Analyser { private static final String LAYOUT_GLOB = "**/" + Decompiler.XML_FILES_DIR + "/**/layout*/*.xml"; private final Decompiler decompiler; + private final FileCleaner fileCleaner; + private final GlobMatcher globMatcher; - public ApkAnalyser(PluginExecutor pluginExecutor, Set plugins, FileProvider fileProvider, Decompiler decompiler) { + public ApkAnalyser(PluginExecutor pluginExecutor, Set plugins, FileProvider fileProvider, Decompiler decompiler, FileCleaner fileCleaner, GlobMatcher globMatcher) { super(pluginExecutor, plugins, fileProvider); this.decompiler = decompiler; + this.fileCleaner = fileCleaner; + this.globMatcher = globMatcher; } @Override protected String prepareSources(String source) { + checkIfSourceIsApkFile(source); + System.out.println("Decompiling APK..."); return decompiler.decompile(new File(source)).getAbsolutePath(); } + private void checkIfSourceIsApkFile(String source) { + if (!globMatcher.fileMatchesGlobPattern(new File(source), "**/*.apk")) { + throw new EsaException("Provided source is not *.apk file. Interrupting..."); + } + } + @Override protected String getAndroidManifestGlob() { return ANDROID_MANIFEST_GLOB; @@ -40,4 +55,9 @@ public class ApkAnalyser extends Analyser { protected String getLayoutGlob() { return LAYOUT_GLOB; } + + @Override + protected void performCleaning(String source) { + fileCleaner.deleteRecursively(new File(source)); + } } diff --git a/src/main/java/com/bartek/esa/analyser/core/Analyser.java b/src/main/java/com/bartek/esa/analyser/core/Analyser.java index 0f3eb4c..9848f87 100644 --- a/src/main/java/com/bartek/esa/analyser/core/Analyser.java +++ b/src/main/java/com/bartek/esa/analyser/core/Analyser.java @@ -31,7 +31,9 @@ public abstract class Analyser { Set files = getFiles(newSource); Set selectedPlugins = getPlugins(pluginCodes, excludeCodes); - return pluginExecutor.executeForFiles(manifest, files, selectedPlugins); + List issues = pluginExecutor.executeForFiles(manifest, files, selectedPlugins); + performCleaning(newSource); + return issues; } protected abstract String prepareSources(String source); @@ -42,6 +44,8 @@ public abstract class Analyser { protected abstract String getLayoutGlob(); + protected abstract void performCleaning(String source); + private File getManifest(String source) { Set manifests = fileProvider.getGlobMatchedFiles(source, getAndroidManifestGlob()); diff --git a/src/main/java/com/bartek/esa/analyser/di/AnalyserModule.java b/src/main/java/com/bartek/esa/analyser/di/AnalyserModule.java index 3087dd5..f977f89 100644 --- a/src/main/java/com/bartek/esa/analyser/di/AnalyserModule.java +++ b/src/main/java/com/bartek/esa/analyser/di/AnalyserModule.java @@ -5,6 +5,8 @@ import com.bartek.esa.analyser.source.SourceAnalyser; import com.bartek.esa.core.archetype.Plugin; import com.bartek.esa.core.executor.PluginExecutor; import com.bartek.esa.decompiler.decompiler.Decompiler; +import com.bartek.esa.file.cleaner.FileCleaner; +import com.bartek.esa.file.matcher.GlobMatcher; import com.bartek.esa.file.provider.FileProvider; import dagger.Module; import dagger.Provides; @@ -20,7 +22,7 @@ public class AnalyserModule { } @Provides - public ApkAnalyser apkAnalyser(PluginExecutor pluginExecutor, Set plugins, FileProvider fileProvider, Decompiler decompiler) { - return new ApkAnalyser(pluginExecutor, plugins, fileProvider, decompiler); + public ApkAnalyser apkAnalyser(PluginExecutor pluginExecutor, Set plugins, FileProvider fileProvider, Decompiler decompiler, FileCleaner fileCleaner, GlobMatcher globMatcher) { + return new ApkAnalyser(pluginExecutor, plugins, fileProvider, decompiler, fileCleaner, globMatcher); } } diff --git a/src/main/java/com/bartek/esa/analyser/source/SourceAnalyser.java b/src/main/java/com/bartek/esa/analyser/source/SourceAnalyser.java index 6374ee9..97a9fed 100644 --- a/src/main/java/com/bartek/esa/analyser/source/SourceAnalyser.java +++ b/src/main/java/com/bartek/esa/analyser/source/SourceAnalyser.java @@ -3,8 +3,10 @@ package com.bartek.esa.analyser.source; import com.bartek.esa.analyser.core.Analyser; import com.bartek.esa.core.archetype.Plugin; import com.bartek.esa.core.executor.PluginExecutor; +import com.bartek.esa.error.EsaException; import com.bartek.esa.file.provider.FileProvider; +import java.io.File; import java.util.Set; public class SourceAnalyser extends Analyser { @@ -18,9 +20,16 @@ public class SourceAnalyser extends Analyser { @Override protected String prepareSources(String source) { + checkIfSourceIsDirectory(source); return source; } + private void checkIfSourceIsDirectory(String source) { + if (!new File(source).isDirectory()) { + throw new EsaException("Provided source is not a directory. Interrupting..."); + } + } + @Override protected String getAndroidManifestGlob() { return ANDROID_MANIFEST_GLOB; @@ -35,4 +44,9 @@ public class SourceAnalyser extends Analyser { protected String getLayoutGlob() { return LAYOUT_GLOB; } + + @Override + protected void performCleaning(String source) { + // do nothing + } }