support to scan once with multi-level granularities outputs

This commit is contained in:
Gang ZHANG 2022-09-28 23:43:55 +08:00
parent 877c1ac1d3
commit 27f1206a1a
6 changed files with 59 additions and 40 deletions

View File

@ -24,15 +24,15 @@ SOFTWARE.
package depends; package depends;
import java.util.ArrayList;
import java.util.List;
import depends.deptypes.DependencyType; import depends.deptypes.DependencyType;
import depends.extractor.LangProcessorRegistration; import depends.extractor.LangProcessorRegistration;
import picocli.CommandLine.Command; import picocli.CommandLine.Command;
import picocli.CommandLine.Option; import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters; import picocli.CommandLine.Parameters;
import java.util.ArrayList;
import java.util.List;
@Command(name = "depends") @Command(name = "depends")
public class DependsCommand { public class DependsCommand {
public static class SupportedLangs extends ArrayList<String> { public static class SupportedLangs extends ArrayList<String> {
@ -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 <src>. " @Option(names = {"--strip-paths"}, split=",", description = "The path(s) to be stripped. if -s enabled, the path(s) start after <src>. "
+ "Otherwise, the path(s) should be valid.") + "Otherwise, the path(s) should be valid.")
private String[] strippedPaths = new String[]{}; 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)]") @Option(names = {"-g", "--granularity"}, split=",", description = "Granularity of dependency.[file(default),method,structure]")
private String granularity="file"; private String[] granularity=new String[]{"file"};
@Option(names = {"-p", "--namepattern"}, description = "The name path pattern.[dot(.), unix(/) or windows(\\)") @Option(names = {"-p", "--namepattern"}, description = "The name path pattern.[dot(.), unix(/) or windows(\\)")
private String namePathPattern=""; private String namePathPattern="";
@Option(names = {"-i","--includes"},split=",", description = "The files of searching path") @Option(names = {"-i","--includes"},split=",", description = "The files of searching path")
@ -120,7 +120,7 @@ public class DependsCommand {
public boolean isHelp() { public boolean isHelp() {
return help; return help;
} }
public String getGranularity() { public String[] getGranularity() {
return granularity; return granularity;
} }
public String getNamePathPattern() { public String getNamePathPattern() {

View File

@ -36,7 +36,6 @@ import depends.generator.FileDependencyGenerator;
import depends.generator.FunctionDependencyGenerator; import depends.generator.FunctionDependencyGenerator;
import depends.generator.StructureDependencyGenerator; import depends.generator.StructureDependencyGenerator;
import depends.matrix.core.DependencyMatrix; import depends.matrix.core.DependencyMatrix;
import depends.matrix.transform.MatrixLevelReducer;
import depends.relations.BindingResolver; import depends.relations.BindingResolver;
import depends.relations.IBindingResolver; import depends.relations.IBindingResolver;
import depends.relations.RelationCounter; import depends.relations.RelationCounter;
@ -52,6 +51,7 @@ import picocli.CommandLine;
import picocli.CommandLine.PicocliException; import picocli.CommandLine.PicocliException;
import java.io.File; import java.io.File;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -69,6 +69,7 @@ public class Main {
CommandLine.usage(new DependsCommand(), System.out); CommandLine.usage(new DependsCommand(), System.out);
System.exit(0); System.exit(0);
} }
verifyParameters(appArgs);
executeCommand(appArgs); executeCommand(appArgs);
} catch (Exception e) { } catch (Exception e) {
if (e instanceof PicocliException) { if (e instanceof PicocliException) {
@ -83,6 +84,16 @@ public class Main {
} }
} }
private static void verifyParameters(DependsCommand args) throws ParameterException {
String[] granularities = args.getGranularity();
List<String> 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") @SuppressWarnings("unchecked")
private static void executeCommand(DependsCommand args) throws ParameterException { private static void executeCommand(DependsCommand args) throws ParameterException {
String lang = args.getLang(); String lang = args.getLang();
@ -114,14 +125,13 @@ public class Main {
System.out.println("Dependency done...."); System.out.println("Dependency done....");
//step2: generate dependencies matrix //step2: generate dependencies matrix
DependencyGenerator dependencyGenerator = getDependencyGenerator(args, inputDir); List<DependencyGenerator> dependencyGenerators = getDependencyGenerators(args, inputDir);
DependencyMatrix matrix = dependencyGenerator.identifyDependencies(entityRepo,args.getTypeFilter()); for (DependencyGenerator dependencyGenerator:dependencyGenerators) {
//step3: output DependencyMatrix matrix = dependencyGenerator.identifyDependencies(entityRepo, args.getTypeFilter());
if (args.getGranularity().startsWith("L")) {
matrix = new MatrixLevelReducer(matrix,args.getGranularity().substring(1)).shrinkToLevel();
}
DependencyDumper output = new DependencyDumper(matrix); DependencyDumper output = new DependencyDumper(matrix);
output.outputResult(outputName,outputDir,outputFormat); output.outputResult(outputName+"-"+dependencyGenerator.getType(), outputDir, outputFormat);
}
if (args.isOutputExternalDependencies()) { if (args.isOutputExternalDependencies()) {
Set<UnsolvedBindings> unsolved = langProcessor.getExternalDependencies(); Set<UnsolvedBindings> unsolved = langProcessor.getExternalDependencies();
UnsolvedSymbolDumper unsolvedSymbolDumper = new UnsolvedSymbolDumper(unsolved,args.getOutputName(),args.getOutputDir(), UnsolvedSymbolDumper unsolvedSymbolDumper = new UnsolvedSymbolDumper(unsolved,args.getOutputName(),args.getOutputDir(),
@ -147,7 +157,7 @@ public class Main {
return includeDir; return includeDir;
} }
private static DependencyGenerator getDependencyGenerator(DependsCommand app, String inputDir) throws ParameterException { private static List<DependencyGenerator> getDependencyGenerators(DependsCommand app, String inputDir) throws ParameterException {
FilenameWritter filenameWritter = new EmptyFilenameWritter(); FilenameWritter filenameWritter = new EmptyFilenameWritter();
if (!StringUtils.isEmpty(app.getNamePathPattern())) { if (!StringUtils.isEmpty(app.getNamePathPattern())) {
if (app.getNamePathPattern().equals("dot")|| if (app.getNamePathPattern().equals("dot")||
@ -164,34 +174,29 @@ public class Main {
} }
} }
List<DependencyGenerator> dependencyGenerators = new ArrayList<>();
for (int i=0;i<app.getGranularity().length;i++) {
/* by default use file dependency generator */ /* by default use file dependency generator */
DependencyGenerator dependencyGenerator = new FileDependencyGenerator(); DependencyGenerator dependencyGenerator = null;
if (!StringUtils.isEmpty(app.getGranularity())) {
/* method parameter means use method generator */ /* method parameter means use method generator */
if (app.getGranularity().equals("method")) if (app.getGranularity()[i].equals("method"))
dependencyGenerator = new FunctionDependencyGenerator(); dependencyGenerator = new FunctionDependencyGenerator();
else if (app.getGranularity().equals("structure")) else if (app.getGranularity()[i].equals("file"))
dependencyGenerator = new FileDependencyGenerator();
else if (app.getGranularity()[i].equals("structure"))
dependencyGenerator = new StructureDependencyGenerator(); dependencyGenerator = new StructureDependencyGenerator();
else if (app.getGranularity().equals("file"))
/*no action*/;
else if (app.getGranularity().startsWith("L"))
/*no action*/;
else
throw new ParameterException("Unknown granularity parameter:" + app.getGranularity());
}
dependencyGenerators.add(dependencyGenerator);
if (app.isStripLeadingPath() || if (app.isStripLeadingPath() ||
app.getStrippedPaths().length>0) { app.getStrippedPaths().length > 0) {
dependencyGenerator.setLeadingStripper(new LeadingNameStripper(app.isStripLeadingPath(), inputDir, app.getStrippedPaths())); dependencyGenerator.setLeadingStripper(new LeadingNameStripper(app.isStripLeadingPath(), inputDir, app.getStrippedPaths()));
} }
if (app.isDetail()) { if (app.isDetail()) {
dependencyGenerator.setGenerateDetail(true); dependencyGenerator.setGenerateDetail(true);
} }
dependencyGenerator.setFilenameRewritter(filenameWritter); dependencyGenerator.setFilenameRewritter(filenameWritter);
return dependencyGenerator; }
return dependencyGenerators;
} }
} }

View File

@ -51,11 +51,10 @@ import static depends.deptypes.DependencyType.POSSIBLE_DEP;
public abstract class DependencyGenerator { public abstract class DependencyGenerator {
private static Logger logger = LoggerFactory.getLogger(DependencyGenerator.class); private static Logger logger = LoggerFactory.getLogger(DependencyGenerator.class);
public abstract String getType();
public DependencyMatrix identifyDependencies(EntityRepo entityRepo, List<String> typeFilter) { public DependencyMatrix identifyDependencies(EntityRepo entityRepo, List<String> typeFilter) {
System.out.println("dependencie data generating..."); System.out.println("dependencie data generating...");
DependencyMatrix dependencyMatrix = build(entityRepo, typeFilter); DependencyMatrix dependencyMatrix = build(entityRepo, typeFilter);
entityRepo.clear();
System.out.println("reorder dependency matrix..."); System.out.println("reorder dependency matrix...");
dependencyMatrix = new OrderedMatrixGenerator(dependencyMatrix).build(); dependencyMatrix = new OrderedMatrixGenerator(dependencyMatrix).build();
System.out.println("Dependencies data generating done successfully..."); System.out.println("Dependencies data generating done successfully...");

View File

@ -40,6 +40,11 @@ public class FileDependencyGenerator extends DependencyGenerator{
return (entity instanceof FileEntity); return (entity instanceof FileEntity);
} }
@Override
public String getType() {
return "file";
}
@Override @Override
protected int upToOutputLevelEntityId(EntityRepo entityRepo, Entity entity) { protected int upToOutputLevelEntityId(EntityRepo entityRepo, Entity entity) {
Entity ancestor = entity.getAncestorOfType(FileEntity.class); Entity ancestor = entity.getAncestorOfType(FileEntity.class);

View File

@ -56,4 +56,9 @@ public class FunctionDependencyGenerator extends DependencyGenerator {
return ancestor.getId(); return ancestor.getId();
} }
@Override
public String getType() {
return "method";
}
} }

View File

@ -43,6 +43,11 @@ public class StructureDependencyGenerator extends DependencyGenerator{
return false; return false;
} }
@Override
public String getType() {
return "structure";
}
@Override @Override
protected int upToOutputLevelEntityId(EntityRepo entityRepo, Entity entity) { protected int upToOutputLevelEntityId(EntityRepo entityRepo, Entity entity) {
Entity ancestor = getAncestorOfType(entity); Entity ancestor = getAncestorOfType(entity);