You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Main.java 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. MIT License
  3. Copyright (c) 2018-2019 Gang ZHANG
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. package depends;
  21. import depends.addons.DV8MappingFileBuilder;
  22. import depends.entity.repo.EntityRepo;
  23. import depends.extractor.AbstractLangProcessor;
  24. import depends.extractor.LangProcessorRegistration;
  25. import depends.extractor.UnsolvedBindings;
  26. import depends.format.DependencyDumper;
  27. import depends.format.detail.UnsolvedSymbolDumper;
  28. import depends.generator.DependencyGenerator;
  29. import depends.generator.FileDependencyGenerator;
  30. import depends.generator.FunctionDependencyGenerator;
  31. import depends.generator.StructureDependencyGenerator;
  32. import depends.matrix.core.DependencyMatrix;
  33. import depends.matrix.transform.MatrixLevelReducer;
  34. import depends.relations.BindingResolver;
  35. import depends.relations.IBindingResolver;
  36. import depends.relations.RelationCounter;
  37. import edu.emory.mathcs.backport.java.util.Arrays;
  38. import multilang.depends.util.file.FileUtil;
  39. import multilang.depends.util.file.FolderCollector;
  40. import multilang.depends.util.file.TemporaryFile;
  41. import multilang.depends.util.file.path.*;
  42. import multilang.depends.util.file.strip.LeadingNameStripper;
  43. import net.sf.ehcache.CacheManager;
  44. import org.codehaus.plexus.util.StringUtils;
  45. import picocli.CommandLine;
  46. import picocli.CommandLine.PicocliException;
  47. import java.io.File;
  48. import java.util.List;
  49. import java.util.Set;
  50. /**
  51. * The entry pooint of depends
  52. */
  53. public class Main {
  54. public static void main(String[] args) {
  55. try {
  56. LangRegister langRegister = new LangRegister();
  57. langRegister.register();
  58. DependsCommand appArgs = CommandLine.populateCommand(new DependsCommand(), args);
  59. if (appArgs.help) {
  60. CommandLine.usage(new DependsCommand(), System.out);
  61. System.exit(0);
  62. }
  63. executeCommand(appArgs);
  64. } catch (Exception e) {
  65. if (e instanceof PicocliException) {
  66. CommandLine.usage(new DependsCommand(), System.out);
  67. } else if (e instanceof ParameterException){
  68. System.err.println(e.getMessage());
  69. }else {
  70. System.err.println("Exception encountered. If it is a design error, please report issue to us." );
  71. e.printStackTrace();
  72. }
  73. System.exit(0);
  74. }
  75. }
  76. @SuppressWarnings("unchecked")
  77. private static void executeCommand(DependsCommand args) throws ParameterException {
  78. String lang = args.getLang();
  79. String inputDir = args.getSrc();
  80. String[] includeDir = args.getIncludes();
  81. String outputName = args.getOutputName();
  82. String outputDir = args.getOutputDir();
  83. String[] outputFormat = args.getFormat();
  84. inputDir = FileUtil.uniqFilePath(inputDir);
  85. if (args.isAutoInclude()) {
  86. includeDir = appendAllFoldersToIncludePath(inputDir, includeDir);
  87. }
  88. AbstractLangProcessor langProcessor = LangProcessorRegistration.getRegistry().getProcessorOf(lang);
  89. if (langProcessor == null) {
  90. System.err.println("Not support this language: " + lang);
  91. return;
  92. }
  93. IBindingResolver bindingResolver = new BindingResolver(langProcessor, args.isOutputExternalDependencies(), args.isDuckTypingDeduce());
  94. long startTime = System.currentTimeMillis();
  95. //step1: build data
  96. EntityRepo entityRepo = langProcessor.buildDependencies(inputDir, includeDir, bindingResolver);
  97. new RelationCounter(entityRepo,langProcessor, bindingResolver).computeRelations();
  98. System.out.println("Dependency done....");
  99. //step2: generate dependencies matrix
  100. DependencyGenerator dependencyGenerator = getDependencyGenerator(args, inputDir);
  101. DependencyMatrix matrix = dependencyGenerator.identifyDependencies(entityRepo,args.getTypeFilter());
  102. //step3: output
  103. if (args.getGranularity().startsWith("L")) {
  104. matrix = new MatrixLevelReducer(matrix,args.getGranularity().substring(1)).shrinkToLevel();
  105. }
  106. DependencyDumper output = new DependencyDumper(matrix);
  107. output.outputResult(outputName,outputDir,outputFormat);
  108. if (args.isOutputExternalDependencies()) {
  109. Set<UnsolvedBindings> unsolved = langProcessor.getExternalDependencies();
  110. UnsolvedSymbolDumper unsolvedSymbolDumper = new UnsolvedSymbolDumper(unsolved,args.getOutputName(),args.getOutputDir(),
  111. new LeadingNameStripper(args.isStripLeadingPath(),inputDir,args.getStrippedPaths()));
  112. unsolvedSymbolDumper.output();
  113. }
  114. long endTime = System.currentTimeMillis();
  115. TemporaryFile.getInstance().delete();
  116. CacheManager.create().shutdown();
  117. System.out.println("Consumed time: " + (float) ((endTime - startTime) / 1000.00) + " s, or "
  118. + (float) ((endTime - startTime) / 60000.00) + " min.");
  119. if ( args.isDv8map()) {
  120. DV8MappingFileBuilder dv8MapfileBuilder = new DV8MappingFileBuilder(langProcessor.supportedRelations());
  121. dv8MapfileBuilder.create(outputDir+ File.separator+"depends-dv8map.mapping");
  122. }
  123. }
  124. private static String[] appendAllFoldersToIncludePath(String inputDir, String[] includeDir) {
  125. FolderCollector includePathCollector = new FolderCollector();
  126. List<String> additionalIncludePaths = includePathCollector.getFolders(inputDir);
  127. additionalIncludePaths.addAll(Arrays.asList(includeDir));
  128. includeDir = additionalIncludePaths.toArray(new String[] {});
  129. return includeDir;
  130. }
  131. private static DependencyGenerator getDependencyGenerator(DependsCommand app, String inputDir) throws ParameterException {
  132. FilenameWritter filenameWritter = new EmptyFilenameWritter();
  133. if (!StringUtils.isEmpty(app.getNamePathPattern())) {
  134. if (app.getNamePathPattern().equals("dot")||
  135. app.getNamePathPattern().equals(".")) {
  136. filenameWritter = new DotPathFilenameWritter();
  137. }else if (app.getNamePathPattern().equals("unix")||
  138. app.getNamePathPattern().equals("/")) {
  139. filenameWritter = new UnixPathFilenameWritter();
  140. }else if (app.getNamePathPattern().equals("windows")||
  141. app.getNamePathPattern().equals("\\")) {
  142. filenameWritter = new WindowsPathFilenameWritter();
  143. }else{
  144. throw new ParameterException("Unknown name pattern paremater:" + app.getNamePathPattern());
  145. }
  146. }
  147. /* by default use file dependency generator */
  148. DependencyGenerator dependencyGenerator = new FileDependencyGenerator();
  149. if (!StringUtils.isEmpty(app.getGranularity())) {
  150. /* method parameter means use method generator */
  151. if (app.getGranularity().equals("method"))
  152. dependencyGenerator = new FunctionDependencyGenerator();
  153. else if (app.getGranularity().equals("structure"))
  154. dependencyGenerator = new StructureDependencyGenerator();
  155. else if (app.getGranularity().equals("file"))
  156. /*no action*/;
  157. else if (app.getGranularity().startsWith("L"))
  158. /*no action*/;
  159. else
  160. throw new ParameterException("Unknown granularity parameter:" + app.getGranularity());
  161. }
  162. if (app.isStripLeadingPath() ||
  163. app.getStrippedPaths().length>0) {
  164. dependencyGenerator.setLeadingStripper(new LeadingNameStripper(app.isStripLeadingPath(), inputDir, app.getStrippedPaths()));
  165. }
  166. if (app.isDetail()) {
  167. dependencyGenerator.setGenerateDetail(true);
  168. }
  169. dependencyGenerator.setFilenameRewritter(filenameWritter);
  170. return dependencyGenerator;
  171. }
  172. }

人工智能研发终端

Contributors (2)