|
| 1 | + |
| 2 | +package com.alipay.codequery.properties; |
| 3 | + |
| 4 | +import com.alipay.codequery.properties.core.CorefExtractor; |
| 5 | +import com.alipay.codequery.properties.model.Folder; |
| 6 | +import com.alipay.codequery.properties.model.Node; |
| 7 | +import com.alipay.codequery.properties.model.Program; |
| 8 | +import com.alipay.codequery.properties.storage.CorefStorage; |
| 9 | +import com.alipay.codequery.properties.core.CorefURI; |
| 10 | +import com.alipay.codequery.properties.util.LoggerUtil; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | +import org.apache.logging.log4j.Level; |
| 14 | +import org.apache.logging.log4j.LogManager; |
| 15 | +import org.apache.logging.log4j.Logger; |
| 16 | +import picocli.CommandLine; |
| 17 | +import picocli.CommandLine.Command; |
| 18 | +import picocli.CommandLine.Parameters; |
| 19 | + |
| 20 | +import java.io.*; |
| 21 | +import java.util.concurrent.Callable; |
| 22 | + |
| 23 | + |
| 24 | +@Command(name = "extract", mixinStandardHelpOptions = true, version = "extract 1.0", |
| 25 | + description = "extract COREF-Properties db from a src directory.") |
| 26 | +@Slf4j |
| 27 | +public class Extractor implements Callable<Integer> { |
| 28 | + |
| 29 | + private static final Logger logger = LogManager.getLogger(Extractor.class); |
| 30 | + @Parameters(index = "0", description = "The source directory to extract.") |
| 31 | + private File srcRootDir; |
| 32 | + |
| 33 | + @Parameters(index = "1", description = "The output directory for the DB file.") |
| 34 | + private File dbDir; |
| 35 | + |
| 36 | + @CommandLine.Option(names = {"--corpus"}, description = "Specify the corpus of the codebase.") |
| 37 | + private String corpus = ""; |
| 38 | + |
| 39 | + /** |
| 40 | + * |
| 41 | + * main method. |
| 42 | + */ |
| 43 | + public static void main(String[] args) { |
| 44 | + int exitCode = new CommandLine(new Extractor()).execute(args); |
| 45 | + System.exit(exitCode); |
| 46 | + } |
| 47 | + |
| 48 | + private void parse(File rootDir, CorefStorage corefStorage, CorefURI corefURI) throws IOException{ |
| 49 | + File[] files = rootDir.listFiles(); |
| 50 | + for (File file : files) { |
| 51 | + if (file.isDirectory()) { |
| 52 | + parse(file, corefStorage, corefURI); |
| 53 | + } else { |
| 54 | + |
| 55 | + // Support extracting file's extension is 'properties' or 'properties.vm'. |
| 56 | + if (file.getName().endsWith(".properties") || file.getName().endsWith(".properties.vm")) { |
| 57 | + logger.info("Start Extracting properties file: {}", file.getAbsolutePath()); |
| 58 | + try { |
| 59 | + CorefExtractor extractor = new CorefExtractor(file, corefStorage, srcRootDir.getAbsolutePath(), corefURI); |
| 60 | + extractor.parse(); |
| 61 | + } catch (Exception e) { |
| 62 | + logger.error("Extraction failed, error message:{} on file {}", e.getMessage(), file.getAbsolutePath()); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private Program createProgramNode(String repoDir, CorefStorage corefStorage, CorefURI corefURI) { |
| 70 | + Program program = new Program(); |
| 71 | + program.oid = corefURI.generateCorpusOid(); |
| 72 | + program.prefix = repoDir; |
| 73 | + corefStorage.storeProgram(program.extractProgram()); |
| 74 | + return program; |
| 75 | + } |
| 76 | + |
| 77 | + private void visitDirectory(String repoDir, Node parent, CorefStorage corefStorage, CorefURI corefURI) { |
| 78 | + File file = new File(repoDir); |
| 79 | + |
| 80 | + // Ignore the folder starts with "." |
| 81 | + if (file.isDirectory() && !(file.getName().startsWith("."))) { |
| 82 | + String absolutePath = file.getAbsolutePath(); |
| 83 | + |
| 84 | + Folder parentFolder = new Folder(); |
| 85 | + parentFolder.name = file.getName(); |
| 86 | + parentFolder.parent = parent; |
| 87 | + |
| 88 | + // Calculate the relative path of the folder. |
| 89 | + if (absolutePath.endsWith(srcRootDir.getAbsolutePath())) { |
| 90 | + parentFolder.relativePath = "ROOT"; |
| 91 | + } else { |
| 92 | + char head = repoDir.charAt(0); |
| 93 | + switch (head) { |
| 94 | + case '/': |
| 95 | + parentFolder.relativePath = absolutePath.substring(srcRootDir.getAbsolutePath().length() + 1); |
| 96 | + break; |
| 97 | + case '.': |
| 98 | + parentFolder.relativePath = absolutePath.substring(absolutePath.indexOf(repoDir) + 2); |
| 99 | + break; |
| 100 | + default: |
| 101 | + parentFolder.relativePath = absolutePath.substring(absolutePath.indexOf(repoDir)); |
| 102 | + } |
| 103 | + } |
| 104 | + corefURI.setPath(parentFolder.relativePath); |
| 105 | + parentFolder.oid = corefURI.generateFileOid(); |
| 106 | + corefStorage.storeFolder(parentFolder.extractFolder()); |
| 107 | + |
| 108 | + // Recursively visit the sub folders. |
| 109 | + for (File f : file.listFiles()) { |
| 110 | + if (f.isDirectory()) { |
| 111 | + visitDirectory(f.getAbsolutePath(), parentFolder, corefStorage, corefURI); |
| 112 | + } else if (f.getName().endsWith(".properties")) { |
| 113 | + CorefURI.fileMap.put(f.getAbsolutePath(), parentFolder.oid); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Override the call method. |
| 121 | + * @return |
| 122 | + * @throws Exception |
| 123 | + */ |
| 124 | + @Override |
| 125 | + public Integer call() throws Exception { |
| 126 | + LoggerUtil.initLogger(Level.INFO); |
| 127 | + |
| 128 | + long start = System.currentTimeMillis(); |
| 129 | + CorefStorage corefStorage = new CorefStorage(dbDir.getAbsolutePath()); |
| 130 | + CorefURI corefURI = StringUtils.isBlank(corpus) ? new CorefURI(srcRootDir.getAbsolutePath()) : new CorefURI(corpus); |
| 131 | + Program program = createProgramNode(srcRootDir.getAbsolutePath(), corefStorage, corefURI); |
| 132 | + visitDirectory(srcRootDir.getAbsolutePath(), program, corefStorage, corefURI); |
| 133 | + |
| 134 | + parse(srcRootDir, corefStorage, corefURI); |
| 135 | + corefStorage.store(); |
| 136 | + |
| 137 | + logger.info("Time to completion (TTC): " + (System.currentTimeMillis() - start)); |
| 138 | + |
| 139 | + return 0; |
| 140 | + } |
| 141 | +} |
0 commit comments