refactory: simplified main function

This commit is contained in:
Gang ZHANG 2022-09-14 21:27:14 +08:00
parent 7dfb555a41
commit 8e144f23fa
7 changed files with 67 additions and 41 deletions

View File

@ -55,18 +55,21 @@ import java.io.File;
import java.util.List;
import java.util.Set;
/**
* The entry pooint of depends
*/
public class Main {
public static void main(String[] args) {
try {
LangRegister langRegister = new LangRegister();
langRegister.register();
DependsCommand app = CommandLine.populateCommand(new DependsCommand(), args);
if (app.help) {
DependsCommand appArgs = CommandLine.populateCommand(new DependsCommand(), args);
if (appArgs.help) {
CommandLine.usage(new DependsCommand(), System.out);
System.exit(0);
}
executeCommand(app);
executeCommand(appArgs);
} catch (Exception e) {
if (e instanceof PicocliException) {
CommandLine.usage(new DependsCommand(), System.out);
@ -81,23 +84,18 @@ public class Main {
}
@SuppressWarnings("unchecked")
private static void executeCommand(DependsCommand app) throws ParameterException {
String lang = app.getLang();
String inputDir = app.getSrc();
String[] includeDir = app.getIncludes();
String outputName = app.getOutputName();
String outputDir = app.getOutputDir();
String[] outputFormat = app.getFormat();
private static void executeCommand(DependsCommand args) throws ParameterException {
String lang = args.getLang();
String inputDir = args.getSrc();
String[] includeDir = args.getIncludes();
String outputName = args.getOutputName();
String outputDir = args.getOutputDir();
String[] outputFormat = args.getFormat();
inputDir = FileUtil.uniqFilePath(inputDir);
boolean supportImplLink = false;
if (app.getLang().equals("cpp") || app.getLang().equals("python")) supportImplLink = true;
if (app.isAutoInclude()) {
FolderCollector includePathCollector = new FolderCollector();
List<String> additionalIncludePaths = includePathCollector.getFolders(inputDir);
additionalIncludePaths.addAll(Arrays.asList(includeDir));
includeDir = additionalIncludePaths.toArray(new String[] {});
if (args.isAutoInclude()) {
includeDir = appendAllFoldersToIncludePath(inputDir, includeDir);
}
AbstractLangProcessor langProcessor = LangProcessorRegistration.getRegistry().getProcessorOf(lang);
@ -105,29 +103,29 @@ public class Main {
System.err.println("Not support this language: " + lang);
return;
}
IBindingResolver bindingResolver = new BindingResolver(langProcessor.getEntityRepo(), langProcessor.getImportLookupStrategy(), langProcessor.getBuiltInType()
, app.isOutputExternalDependencies(), app.isDuckTypingDeduce());
IBindingResolver bindingResolver = new BindingResolver(langProcessor, args.isOutputExternalDependencies(), args.isDuckTypingDeduce());
long startTime = System.currentTimeMillis();
//step1: build data
EntityRepo entityRepo = langProcessor.buildDependencies(inputDir, includeDir, bindingResolver);
new RelationCounter(entityRepo,supportImplLink,langProcessor, bindingResolver).computeRelations();
new RelationCounter(entityRepo,langProcessor, bindingResolver).computeRelations();
System.out.println("Dependency done....");
//step2: generate dependencies matrix
DependencyGenerator dependencyGenerator = getDependencyGenerator(app, inputDir);
DependencyMatrix matrix = dependencyGenerator.identifyDependencies(entityRepo,app.getTypeFilter());
DependencyGenerator dependencyGenerator = getDependencyGenerator(args, inputDir);
DependencyMatrix matrix = dependencyGenerator.identifyDependencies(entityRepo,args.getTypeFilter());
//step3: output
if (app.getGranularity().startsWith("L")) {
matrix = new MatrixLevelReducer(matrix,app.getGranularity().substring(1)).shrinkToLevel();
if (args.getGranularity().startsWith("L")) {
matrix = new MatrixLevelReducer(matrix,args.getGranularity().substring(1)).shrinkToLevel();
}
DependencyDumper output = new DependencyDumper(matrix);
output.outputResult(outputName,outputDir,outputFormat);
if (app.isOutputExternalDependencies()) {
if (args.isOutputExternalDependencies()) {
Set<UnsolvedBindings> unsolved = langProcessor.getExternalDependencies();
UnsolvedSymbolDumper unsolvedSymbolDumper = new UnsolvedSymbolDumper(unsolved,app.getOutputName(),app.getOutputDir(),
new LeadingNameStripper(app.isStripLeadingPath(),inputDir,app.getStrippedPaths()));
UnsolvedSymbolDumper unsolvedSymbolDumper = new UnsolvedSymbolDumper(unsolved,args.getOutputName(),args.getOutputDir(),
new LeadingNameStripper(args.isStripLeadingPath(),inputDir,args.getStrippedPaths()));
unsolvedSymbolDumper.output();
}
long endTime = System.currentTimeMillis();
@ -135,11 +133,18 @@ public class Main {
CacheManager.create().shutdown();
System.out.println("Consumed time: " + (float) ((endTime - startTime) / 1000.00) + " s, or "
+ (float) ((endTime - startTime) / 60000.00) + " min.");
if ( app.isDv8map()) {
if ( args.isDv8map()) {
DV8MappingFileBuilder dv8MapfileBuilder = new DV8MappingFileBuilder(langProcessor.supportedRelations());
dv8MapfileBuilder.create(outputDir+ File.separator+"depends-dv8map.mapping");
}
}
private static String[] appendAllFoldersToIncludePath(String inputDir, String[] includeDir) {
FolderCollector includePathCollector = new FolderCollector();
List<String> additionalIncludePaths = includePathCollector.getFolders(inputDir);
additionalIncludePaths.addAll(Arrays.asList(includeDir));
includeDir = additionalIncludePaths.toArray(new String[] {});
return includeDir;
}
private static DependencyGenerator getDependencyGenerator(DependsCommand app, String inputDir) throws ParameterException {

View File

@ -138,8 +138,6 @@ abstract public class AbstractLangProcessor {
}
/**
*
* @param callAsImpl
* @return unsolved bindings
*/
public void resolveBindings() {
@ -233,7 +231,18 @@ abstract public class AbstractLangProcessor {
return relation;
}
/**
* Whether to resolve expression immediately during parse
* @return
*/
public boolean isEagerExpressionResolve(){
return false;
}
/**
* Call as Impl:
* implicit call (for example polymorphic in cpp)
* @return
*/
public boolean supportCallAsImpl(){return false;};
}

View File

@ -97,4 +97,9 @@ public class CppProcessor extends AbstractLangProcessor {
depedencyTypes.add(IMPLLINK);
return depedencyTypes;
}
@Override
public boolean supportCallAsImpl() {
return true;
}
}

View File

@ -52,4 +52,9 @@ public abstract class BasePythonProcessor extends AbstractLangProcessor{
depedencyTypes.add(IMPLLINK);
return depedencyTypes;
}
@Override
public boolean supportCallAsImpl() {
return true;
}
}

View File

@ -26,6 +26,7 @@ package depends.relations;
import depends.entity.*;
import depends.entity.repo.BuiltInType;
import depends.entity.repo.EntityRepo;
import depends.extractor.AbstractLangProcessor;
import depends.extractor.UnsolvedBindings;
import depends.extractor.empty.EmptyBuiltInType;
import depends.importtypes.Import;
@ -47,17 +48,18 @@ public class BindingResolver implements IBindingResolver{
private boolean isDuckTypingDeduce = true;
private static Logger logger = LoggerFactory.getLogger(IBindingResolver.class);
public BindingResolver(EntityRepo repo, ImportLookupStrategy importLookupStrategy, BuiltInType buildInTypeManager,
public BindingResolver(AbstractLangProcessor langProcessor,
boolean isCollectUnsolvedBindings, boolean isDuckTypingDeduce) {
this.repo = repo;
this.importLookupStrategy = importLookupStrategy;
this.buildInTypeManager = buildInTypeManager;
this.repo = langProcessor.getEntityRepo();
this.importLookupStrategy = langProcessor.getImportLookupStrategy();
this.buildInTypeManager = langProcessor.getBuiltInType();
this.isCollectUnsolvedBindings = isCollectUnsolvedBindings;
this.isDuckTypingDeduce = isDuckTypingDeduce;
unsolvedSymbols= new HashSet<>();
importLookupStrategy.setBindingResolver(this);
}
@Override
public Set<UnsolvedBindings> resolveAllBindings(boolean isEagerExpressionResolve) {
System.out.println("Resolve type bindings....");

View File

@ -41,11 +41,11 @@ public class RelationCounter {
private boolean callAsImpl;
private AbstractLangProcessor langProcessor;
public RelationCounter(EntityRepo repo, boolean callAsImpl, AbstractLangProcessor langProcessor, IBindingResolver bindingResolver) {
public RelationCounter(EntityRepo repo, AbstractLangProcessor langProcessor, IBindingResolver bindingResolver) {
this.entities = repo.getFileEntities();
this.bindingResolver = bindingResolver;
this.repo = repo;
this.callAsImpl = callAsImpl;
this.callAsImpl = langProcessor.supportCallAsImpl();
this.langProcessor = langProcessor;
}

View File

@ -24,27 +24,27 @@ public abstract class ParserTest {
protected void init(){
entityRepo = langProcessor.getEntityRepo();
bindingResolver = new BindingResolver(langProcessor.getEntityRepo(),langProcessor.getImportLookupStrategy(),langProcessor.getBuiltInType(),true,false);
bindingResolver = new BindingResolver(langProcessor,true,false);
langProcessor.bindingResolver = bindingResolver;
TemporaryFile.reset();
}
protected void init(boolean duckTypingDeduce){
entityRepo = langProcessor.getEntityRepo();
bindingResolver = new BindingResolver(langProcessor.getEntityRepo(),langProcessor.getImportLookupStrategy(),langProcessor.getBuiltInType(),false,duckTypingDeduce);
bindingResolver = new BindingResolver(langProcessor,false,duckTypingDeduce);
langProcessor.bindingResolver = bindingResolver;
TemporaryFile.reset();
}
public Set<UnsolvedBindings> resolveAllBindings() {
Set<UnsolvedBindings> result = bindingResolver.resolveAllBindings(langProcessor.isEagerExpressionResolve());
new RelationCounter(entityRepo,false,langProcessor, bindingResolver).computeRelations();
new RelationCounter(entityRepo,langProcessor, bindingResolver).computeRelations();
return result;
}
protected Set<UnsolvedBindings> resolveAllBindings(boolean callAsImpl) {
Set<UnsolvedBindings> result = bindingResolver.resolveAllBindings(langProcessor.isEagerExpressionResolve());
new RelationCounter(entityRepo,callAsImpl,langProcessor, bindingResolver).computeRelations();
new RelationCounter(entityRepo,langProcessor, bindingResolver).computeRelations();
return result;
}