live.document.mavenplugin.methods.MethodsWithEntityCsvMojo Maven / Gradle / Ivy
package live.document.mavenplugin.methods;
import live.document.generator.model.CallNode;
import live.document.generator.utils.FileUtils;
import live.document.mavenplugin.common.AnalyzeAssistant;
import live.document.mavenplugin.common.ProjectAnalyzeResult;
import live.document.scanner.MethodObject;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
/**
* 分析多个指定方法的调用链中,涉及实体Entity类和Table操作的部分。导出CSV
*/
@Mojo(name = "export-all-entities-and-tables-of-methods")
public class MethodsWithEntityCsvMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.basedir}", required = true)
private File projectRoot;
@Parameter(required = true)
private String gitRepositoryUri;
@Parameter
private String[] plsqlPaths;
@Parameter
private String[] sqlExecuteFullMethodNames;
/**
* 待扫描的方法
*/
@Parameter
private String[] methodPatternsForPlSql;
@Parameter(defaultValue = "", required = true)
private String analysisResultOutputDir;
@Override
public void execute() throws MojoExecutionException {
try {
if (methodPatternsForPlSql == null || methodPatternsForPlSql.length == 0) {
throw new MojoFailureException("Please specify methodPatternsForPlSql");
}
FileUtils fileUtils = new FileUtils();
getLog().info("scan: " + projectRoot.getAbsolutePath());
CsvExport csvExport = new CsvExport();
ProjectAnalyzeResult projectResult = AnalyzeAssistant.analyze(projectRoot,
sqlExecuteFullMethodNames,
plsqlPaths,
analysisResultOutputDir,
null);
csvExport.setMethodPatterns(Arrays.asList(methodPatternsForPlSql));
csvExport.setPlSqlExplained(projectResult.getPlSqlExplained());
String csvText = csvExport.generate(
projectResult.getCallGraph(),
projectResult.getPlSqlExplained(),
m -> true,
(node -> hasEntityOperation(node)));
Path fileName = Paths.get(analysisResultOutputDir, "methods-plsql.csv");
fileUtils.writeFile(fileName, csvText);
getLog().info("File created: file://" + fileName.toString());
} catch (Exception e) {
getLog().error(e);
throw new MojoExecutionException(e.getMessage());
}
}
private Boolean hasEntityOperation(CallNode node) {
return hasEntityOperation(node, true);
}
private Boolean hasEntityOperation(CallNode node, boolean isRoot) {
if (node.getDbOperation() != null) {
return true;
}
MethodObject methodObject = node.getMethodObject();
boolean isEntity = isEntityClass(methodObject);
if (isEntity && isRoot) {
return false;
}
if (isEntity) {
return true;
}
for (CallNode child : node.getChildren()) {
if (hasEntityOperation(child, false)) {
return true;
}
}
return false;
}
private boolean isEntityClass(MethodObject methodObject) {
if (methodObject == null) {
return false;
}
return (methodObject.getClassName().endsWith("Entity"));
}
}