|
| 1 | +package org.hjug.refactorfirst; |
| 2 | + |
| 3 | +import static picocli.CommandLine.Option; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.FileReader; |
| 7 | +import java.util.concurrent.Callable; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.apache.maven.model.Model; |
| 10 | +import org.apache.maven.model.io.xpp3.MavenXpp3Reader; |
| 11 | +import org.apache.maven.project.MavenProject; |
| 12 | +import org.hjug.refactorfirst.report.CsvReport; |
| 13 | +import org.hjug.refactorfirst.report.HtmlReport; |
| 14 | +import org.hjug.refactorfirst.report.json.JsonReportExecutor; |
| 15 | +import picocli.CommandLine.Command; |
| 16 | + |
| 17 | +@Command(mixinStandardHelpOptions = true, description = "Generate a report") |
| 18 | +@Slf4j |
| 19 | +public class ReportCommand implements Callable<Integer> { |
| 20 | + |
| 21 | + @Option( |
| 22 | + names = {"-d", "--details"}, |
| 23 | + description = "Show detailed report") |
| 24 | + private boolean showDetails; |
| 25 | + |
| 26 | + @Option( |
| 27 | + names = {"-p", "--project"}, |
| 28 | + description = "Project name") |
| 29 | + private String projectName; |
| 30 | + |
| 31 | + @Option( |
| 32 | + names = {"-v", "--version"}, |
| 33 | + description = "Project version") |
| 34 | + private String projectVersion; |
| 35 | + |
| 36 | + @Option( |
| 37 | + names = {"-o", "--output"}, |
| 38 | + defaultValue = ".", |
| 39 | + description = "Output directory") |
| 40 | + private String outputDirectory; |
| 41 | + |
| 42 | + @Option( |
| 43 | + names = {"-b", "--base-dir"}, |
| 44 | + defaultValue = ".", |
| 45 | + description = "Base directory of the project") |
| 46 | + private File baseDir; |
| 47 | + |
| 48 | + @Option( |
| 49 | + names = {"-t", "--type"}, |
| 50 | + description = "Report type: ${COMPLETION-CANDIDATES}", |
| 51 | + defaultValue = "HTML") |
| 52 | + private ReportType reportType; |
| 53 | + |
| 54 | + @Override |
| 55 | + public Integer call() { |
| 56 | + |
| 57 | + // TODO: add support for inferring arguments from gradle properties |
| 58 | + inferArgumentsFromMavenProject(); |
| 59 | + populateDefaultArguments(); |
| 60 | + switch (reportType) { |
| 61 | + case HTML: |
| 62 | + HtmlReport htmlReport = new HtmlReport(); |
| 63 | + htmlReport.execute(showDetails, projectName, projectVersion, outputDirectory, baseDir); |
| 64 | + return 0; |
| 65 | + case JSON: |
| 66 | + JsonReportExecutor jsonReportExecutor = new JsonReportExecutor(); |
| 67 | + jsonReportExecutor.execute(baseDir, outputDirectory); |
| 68 | + return 0; |
| 69 | + case CSV: |
| 70 | + CsvReport csvReport = new CsvReport(); |
| 71 | + csvReport.execute(showDetails, projectName, projectVersion, outputDirectory, baseDir); |
| 72 | + return 0; |
| 73 | + } |
| 74 | + |
| 75 | + return 0; |
| 76 | + } |
| 77 | + |
| 78 | + private void populateDefaultArguments() { |
| 79 | + if (projectName == null || projectName.isEmpty()) { |
| 80 | + projectName = "my-project"; |
| 81 | + } |
| 82 | + if (projectVersion == null || projectVersion.isEmpty()) { |
| 83 | + projectVersion = "0.0.0"; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private void inferArgumentsFromMavenProject() { |
| 88 | + if (baseDir.isDirectory()) { |
| 89 | + File[] potentialPomFiles = baseDir.listFiles(f -> f.getName().equals("pom.xml")); |
| 90 | + File pomFile = null; |
| 91 | + if (potentialPomFiles != null && potentialPomFiles.length > 0) { |
| 92 | + pomFile = potentialPomFiles[0]; |
| 93 | + } |
| 94 | + if (pomFile != null) { |
| 95 | + Model model; |
| 96 | + FileReader reader; |
| 97 | + MavenXpp3Reader mavenreader = new MavenXpp3Reader(); |
| 98 | + try { |
| 99 | + reader = new FileReader(pomFile); |
| 100 | + model = mavenreader.read(reader); |
| 101 | + model.setPomFile(pomFile); |
| 102 | + } catch (Exception ex) { |
| 103 | + log.info("Unable to infer arguments from pom file"); |
| 104 | + return; |
| 105 | + } |
| 106 | + MavenProject project = new MavenProject(model); |
| 107 | + |
| 108 | + // only override project name and version if they are not set |
| 109 | + if (projectName == null || projectName.isEmpty()) { |
| 110 | + projectName = project.getName(); |
| 111 | + } |
| 112 | + if (projectVersion == null || projectVersion.isEmpty()) { |
| 113 | + projectVersion = project.getVersion(); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments