From 27f1206a1aab12fc321a4993bc6b199789f767d5 Mon Sep 17 00:00:00 2001 From: Gang ZHANG Date: Wed, 28 Sep 2022 23:43:55 +0800 Subject: [PATCH] support to scan once with multi-level granularities outputs --- src/main/java/depends/DependsCommand.java | 12 ++-- src/main/java/depends/Main.java | 69 ++++++++++--------- .../generator/DependencyGenerator.java | 3 +- .../generator/FileDependencyGenerator.java | 5 ++ .../FunctionDependencyGenerator.java | 5 ++ .../StructureDependencyGenerator.java | 5 ++ 6 files changed, 59 insertions(+), 40 deletions(-) diff --git a/src/main/java/depends/DependsCommand.java b/src/main/java/depends/DependsCommand.java index 0ab20c9..7495d8f 100644 --- a/src/main/java/depends/DependsCommand.java +++ b/src/main/java/depends/DependsCommand.java @@ -24,15 +24,15 @@ SOFTWARE. package depends; -import java.util.ArrayList; -import java.util.List; - import depends.deptypes.DependencyType; import depends.extractor.LangProcessorRegistration; import picocli.CommandLine.Command; import picocli.CommandLine.Option; import picocli.CommandLine.Parameters; +import java.util.ArrayList; +import java.util.List; + @Command(name = "depends") public class DependsCommand { public static class SupportedLangs extends ArrayList { @@ -62,8 +62,8 @@ public class DependsCommand { @Option(names = {"--strip-paths"}, split=",", description = "The path(s) to be stripped. if -s enabled, the path(s) start after . " + "Otherwise, the path(s) should be valid.") private String[] strippedPaths = new String[]{}; - @Option(names = {"-g", "--granularity"}, description = "Granularity of dependency.[file(default),method,structure,L#(the level of folder. e.g. L1=1st level folder)]") - private String granularity="file"; + @Option(names = {"-g", "--granularity"}, split=",", description = "Granularity of dependency.[file(default),method,structure]") + private String[] granularity=new String[]{"file"}; @Option(names = {"-p", "--namepattern"}, description = "The name path pattern.[dot(.), unix(/) or windows(\\)") private String namePathPattern=""; @Option(names = {"-i","--includes"},split=",", description = "The files of searching path") @@ -120,7 +120,7 @@ public class DependsCommand { public boolean isHelp() { return help; } - public String getGranularity() { + public String[] getGranularity() { return granularity; } public String getNamePathPattern() { diff --git a/src/main/java/depends/Main.java b/src/main/java/depends/Main.java index 260ae11..f78d384 100644 --- a/src/main/java/depends/Main.java +++ b/src/main/java/depends/Main.java @@ -36,7 +36,6 @@ import depends.generator.FileDependencyGenerator; import depends.generator.FunctionDependencyGenerator; import depends.generator.StructureDependencyGenerator; import depends.matrix.core.DependencyMatrix; -import depends.matrix.transform.MatrixLevelReducer; import depends.relations.BindingResolver; import depends.relations.IBindingResolver; import depends.relations.RelationCounter; @@ -52,6 +51,7 @@ import picocli.CommandLine; import picocli.CommandLine.PicocliException; import java.io.File; +import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -69,6 +69,7 @@ public class Main { CommandLine.usage(new DependsCommand(), System.out); System.exit(0); } + verifyParameters(appArgs); executeCommand(appArgs); } catch (Exception e) { if (e instanceof PicocliException) { @@ -83,6 +84,16 @@ public class Main { } } + private static void verifyParameters(DependsCommand args) throws ParameterException { + String[] granularities = args.getGranularity(); + List validGranularities = Arrays.asList(new String[]{"file", "method", "structure"}); + for (String g:granularities){ + if (!validGranularities.contains(g)){ + throw new ParameterException("granularity is invalid:"+g); + } + } + } + @SuppressWarnings("unchecked") private static void executeCommand(DependsCommand args) throws ParameterException { String lang = args.getLang(); @@ -114,14 +125,13 @@ public class Main { System.out.println("Dependency done...."); //step2: generate dependencies matrix - DependencyGenerator dependencyGenerator = getDependencyGenerator(args, inputDir); - DependencyMatrix matrix = dependencyGenerator.identifyDependencies(entityRepo,args.getTypeFilter()); - //step3: output - if (args.getGranularity().startsWith("L")) { - matrix = new MatrixLevelReducer(matrix,args.getGranularity().substring(1)).shrinkToLevel(); + List dependencyGenerators = getDependencyGenerators(args, inputDir); + for (DependencyGenerator dependencyGenerator:dependencyGenerators) { + DependencyMatrix matrix = dependencyGenerator.identifyDependencies(entityRepo, args.getTypeFilter()); + DependencyDumper output = new DependencyDumper(matrix); + output.outputResult(outputName+"-"+dependencyGenerator.getType(), outputDir, outputFormat); } - DependencyDumper output = new DependencyDumper(matrix); - output.outputResult(outputName,outputDir,outputFormat); + if (args.isOutputExternalDependencies()) { Set unsolved = langProcessor.getExternalDependencies(); UnsolvedSymbolDumper unsolvedSymbolDumper = new UnsolvedSymbolDumper(unsolved,args.getOutputName(),args.getOutputDir(), @@ -147,7 +157,7 @@ public class Main { return includeDir; } - private static DependencyGenerator getDependencyGenerator(DependsCommand app, String inputDir) throws ParameterException { + private static List getDependencyGenerators(DependsCommand app, String inputDir) throws ParameterException { FilenameWritter filenameWritter = new EmptyFilenameWritter(); if (!StringUtils.isEmpty(app.getNamePathPattern())) { if (app.getNamePathPattern().equals("dot")|| @@ -164,34 +174,29 @@ public class Main { } } - - /* by default use file dependency generator */ - DependencyGenerator dependencyGenerator = new FileDependencyGenerator(); - if (!StringUtils.isEmpty(app.getGranularity())) { + List dependencyGenerators = new ArrayList<>(); + for (int i=0;i0) { - dependencyGenerator.setLeadingStripper(new LeadingNameStripper(app.isStripLeadingPath(), inputDir, app.getStrippedPaths())); + dependencyGenerators.add(dependencyGenerator); + if (app.isStripLeadingPath() || + app.getStrippedPaths().length > 0) { + dependencyGenerator.setLeadingStripper(new LeadingNameStripper(app.isStripLeadingPath(), inputDir, app.getStrippedPaths())); + } + if (app.isDetail()) { + dependencyGenerator.setGenerateDetail(true); + } + dependencyGenerator.setFilenameRewritter(filenameWritter); } - - if (app.isDetail()) { - dependencyGenerator.setGenerateDetail(true); - } - - dependencyGenerator.setFilenameRewritter(filenameWritter); - return dependencyGenerator; + return dependencyGenerators; } } diff --git a/src/main/java/depends/generator/DependencyGenerator.java b/src/main/java/depends/generator/DependencyGenerator.java index dcff091..046f913 100644 --- a/src/main/java/depends/generator/DependencyGenerator.java +++ b/src/main/java/depends/generator/DependencyGenerator.java @@ -51,11 +51,10 @@ import static depends.deptypes.DependencyType.POSSIBLE_DEP; public abstract class DependencyGenerator { private static Logger logger = LoggerFactory.getLogger(DependencyGenerator.class); - + public abstract String getType(); public DependencyMatrix identifyDependencies(EntityRepo entityRepo, List typeFilter) { System.out.println("dependencie data generating..."); DependencyMatrix dependencyMatrix = build(entityRepo, typeFilter); - entityRepo.clear(); System.out.println("reorder dependency matrix..."); dependencyMatrix = new OrderedMatrixGenerator(dependencyMatrix).build(); System.out.println("Dependencies data generating done successfully..."); diff --git a/src/main/java/depends/generator/FileDependencyGenerator.java b/src/main/java/depends/generator/FileDependencyGenerator.java index 23ee29f..85db8fe 100644 --- a/src/main/java/depends/generator/FileDependencyGenerator.java +++ b/src/main/java/depends/generator/FileDependencyGenerator.java @@ -40,6 +40,11 @@ public class FileDependencyGenerator extends DependencyGenerator{ return (entity instanceof FileEntity); } + @Override + public String getType() { + return "file"; + } + @Override protected int upToOutputLevelEntityId(EntityRepo entityRepo, Entity entity) { Entity ancestor = entity.getAncestorOfType(FileEntity.class); diff --git a/src/main/java/depends/generator/FunctionDependencyGenerator.java b/src/main/java/depends/generator/FunctionDependencyGenerator.java index f434e63..45ddf66 100644 --- a/src/main/java/depends/generator/FunctionDependencyGenerator.java +++ b/src/main/java/depends/generator/FunctionDependencyGenerator.java @@ -56,4 +56,9 @@ public class FunctionDependencyGenerator extends DependencyGenerator { return ancestor.getId(); } + @Override + public String getType() { + return "method"; + } + } diff --git a/src/main/java/depends/generator/StructureDependencyGenerator.java b/src/main/java/depends/generator/StructureDependencyGenerator.java index c211209..ea13692 100644 --- a/src/main/java/depends/generator/StructureDependencyGenerator.java +++ b/src/main/java/depends/generator/StructureDependencyGenerator.java @@ -43,6 +43,11 @@ public class StructureDependencyGenerator extends DependencyGenerator{ return false; } + @Override + public String getType() { + return "structure"; + } + @Override protected int upToOutputLevelEntityId(EntityRepo entityRepo, Entity entity) { Entity ancestor = getAncestorOfType(entity);