Skip to content

Commit cd1ce5a

Browse files
Merge pull request #74 from jimbethancourt/cli
CLI and other upgrades
2 parents bcbc9d4 + f8e8374 commit cd1ce5a

File tree

29 files changed

+1146
-750
lines changed

29 files changed

+1146
-750
lines changed

.github/workflows/maven-publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ jobs:
1717
with:
1818
ref: main
1919

20-
- name: Set up JDK 1.8
20+
- name: Set up JDK 11
2121
uses: actions/setup-java@v1
2222
with:
23-
java-version: 1.8
23+
java-version: 11
2424

2525
- name: Configure Git user
2626
run: |

README.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ It runs PMD's God Class Rule and Coupling Between Objects rule and scans your Gi
66
The graphs generated in the report will look similar to this one:
77
![image info](./RefactorFirst_Sample_Report.png)
88

9+
## Please Note: Java 11 is now required to run RefactorFirst
10+
The change to require Java 11 is needed to address vulnerability CVE-2023-4759 in JGit
11+
Java 21 features are not yet integrated since PMD APIs have changed.
12+
913
## There are several ways to run the analysis on your codebase:
1014

1115
### From The Command Line
1216
Run the following command from the root of your project (the source code does not need to be built):
1317

1418
```bash
15-
mvn org.hjug.refactorfirst.plugin:refactor-first-maven-plugin:0.5.0:report
19+
mvn org.hjug.refactorfirst.plugin:refactor-first-maven-plugin:0.5.0-M1:report
1620
```
1721

1822
### As Part of a Build
@@ -24,7 +28,7 @@ Add the following to your project in the build section. **showDetails** will sh
2428
<plugin>
2529
<groupId>org.hjug.refactorfirst.plugin</groupId>
2630
<artifactId>refactor-first-maven-plugin</artifactId>
27-
<version>0.5.0</version>
31+
<version>0.5.0-M1</version>
2832
<!-- optional -->
2933
<configuration>
3034
<showDetails>true</showDetails>
@@ -44,7 +48,7 @@ Add the following to your project in the reports section.
4448
<plugin>
4549
<groupId>org.hjug.refactorfirst.plugin</groupId>
4650
<artifactId>refactor-first-maven-plugin</artifactId>
47-
<version>0.5.0</version>
51+
<version>0.5.0-M1</version>
4852
</plugin>
4953
...
5054
</plugins>
@@ -75,8 +79,9 @@ you will need to add the following to your pom.xml:
7579

7680
### As an HTML Report
7781
```bash
78-
mvn org.hjug.refactorfirst.plugin:refactor-first-maven-plugin:0.5.0:htmlReport
82+
mvn org.hjug.refactorfirst.plugin:refactor-first-maven-plugin:0.5.0-M1:htmlReport
7983
```
84+
View the report at ```target/site/refactor-first-report.html```
8085

8186
## But I'm using Gradle / my project layout isn't typical!
8287
I would like to create a Gradle plugin and (possibly) support non-conventional projects in the future, but in the meantime you can create a dummy POM file in the same directory as your .git directory:
@@ -94,10 +99,11 @@ I would like to create a Gradle plugin and (possibly) support non-conventional p
9499
and then (assuming Maven is installed) run
95100

96101
```bash
97-
mvn org.hjug.refactorfirst.plugin:refactor-first-maven-plugin:0.4.0:report
102+
mvn org.hjug.refactorfirst.plugin:refactor-first-maven-plugin:0.5.0-M1:htmlReport
98103
```
99104

100105
## Viewing the Report
106+
View the report at ```target/site/refactor-first-report.html```
101107
Once the plugin finishes executing (it may take a while for a large / old codebase), open the file **target/site/refactor-first-report.html** in the root of the project. It will contain a graph similar to the one above, and a table that lists God classes in the recommended order that they should be refactored. The classes in the top left of the graph are the easiest to refactor while also having the biggest positive impact to team productivity.
102108
If highly coupled classes are detected, a graph and table listing Highly Coupled Classes in will be generated.
103109

change-proneness-ranker/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.hjug.refactorfirst</groupId>
77
<artifactId>refactor-first</artifactId>
8-
<version>0.4.1-SNAPSHOT</version>
8+
<version>0.5.0-M1-SNAPSHOT</version>
99
</parent>
1010

1111
<groupId>org.hjug.refactorfirst.changepronenessranker</groupId>

cli/.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/modules.xml
8+
.idea/jarRepositories.xml
9+
.idea/compiler.xml
10+
.idea/libraries/
11+
*.iws
12+
*.iml
13+
*.ipr
14+
15+
### Eclipse ###
16+
.apt_generated
17+
.classpath
18+
.factorypath
19+
.project
20+
.settings
21+
.springBeans
22+
.sts4-cache
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
build/
31+
!**/src/main/**/build/
32+
!**/src/test/**/build/
33+
34+
### VS Code ###
35+
.vscode/
36+
37+
### Mac OS ###
38+
.DS_Store

cli/pom.xml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.hjug.refactorfirst</groupId>
6+
<artifactId>refactor-first</artifactId>
7+
<version>0.5.0-M1-SNAPSHOT</version>
8+
</parent>
9+
10+
<packaging>jar</packaging>
11+
12+
<groupId>org.hjug.refactorfirst.report</groupId>
13+
<artifactId>cli</artifactId>
14+
15+
<properties>
16+
<maven.compiler.source>11</maven.compiler.source>
17+
<maven.compiler.target>11</maven.compiler.target>
18+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>info.picocli</groupId>
24+
<artifactId>picocli</artifactId>
25+
<version>4.7.4</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.hjug.refactorfirst.report</groupId>
29+
<artifactId>report</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.slf4j</groupId>
33+
<artifactId>slf4j-simple</artifactId>
34+
<version>2.0.7</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.apache.maven</groupId>
38+
<artifactId>maven-core</artifactId>
39+
<version>3.9.2</version>
40+
<scope>compile</scope>
41+
</dependency>
42+
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.apache.maven.plugins</groupId>
49+
<artifactId>maven-shade-plugin</artifactId>
50+
<version>3.5.0</version>
51+
<executions>
52+
<execution>
53+
<phase>package</phase>
54+
<goals>
55+
<goal>shade</goal>
56+
</goals>
57+
<configuration>
58+
<transformers>
59+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
60+
<mainClass>org.hjug.refactorfirst.Main</mainClass>
61+
</transformer>
62+
</transformers>
63+
</configuration>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
68+
<plugin>
69+
<groupId>org.skife.maven</groupId>
70+
<artifactId>really-executable-jar-maven-plugin</artifactId>
71+
<version>2.1.1</version>
72+
<configuration>
73+
<programFile>rf</programFile>
74+
</configuration>
75+
<executions>
76+
<execution>
77+
<phase>package</phase>
78+
<goals>
79+
<goal>really-executable-jar</goal>
80+
</goals>
81+
</execution>
82+
</executions>
83+
</plugin>
84+
</plugins>
85+
</build>
86+
87+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.hjug.refactorfirst;
2+
3+
import picocli.CommandLine;
4+
5+
public class Main {
6+
public static void main(String[] args) {
7+
int exitCode = new CommandLine(new ReportCommand())
8+
.setCaseInsensitiveEnumValuesAllowed(true)
9+
.execute(args);
10+
System.exit(exitCode);
11+
}
12+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.hjug.refactorfirst;
2+
3+
public enum ReportType {
4+
HTML,
5+
JSON,
6+
CSV;
7+
}

cost-benefit-calculator/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.hjug.refactorfirst</groupId>
77
<artifactId>refactor-first</artifactId>
8-
<version>0.4.1-SNAPSHOT</version>
8+
<version>0.5.0-M1-SNAPSHOT</version>
99
</parent>
1010

1111
<groupId>org.hjug.refactorfirst.costbenefitcalculator</groupId>

coverage/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.hjug.refactorfirst</groupId>
99
<artifactId>refactor-first</artifactId>
10-
<version>0.4.1-SNAPSHOT</version>
10+
<version>0.5.0-M1-SNAPSHOT</version>
1111
</parent>
1212

1313
<artifactId>coverage</artifactId>

0 commit comments

Comments
 (0)