Skip to content

Commit c1f0a84

Browse files
committed
Gradle: Implement base plugin
- Working implementation using `shadowJar`
1 parent 6692cc5 commit c1f0a84

File tree

7 files changed

+309
-13
lines changed

7 files changed

+309
-13
lines changed

refactor-first-gradle-plugin/build.gradle

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
11
plugins {
2+
id 'java'
23
id 'java-gradle-plugin'
34
id 'maven-publish'
4-
id 'com.gradle.plugin-publish' version '0.12.0'
5+
id 'com.gradleup.shadow' version '9.1.0'
6+
}
7+
8+
group = 'org.hjug.refactorfirst.plugin'
9+
version = '0.7.2-SNAPSHOT'
10+
11+
java {
12+
toolchain {
13+
languageVersion = JavaLanguageVersion.of(11)
14+
}
515
}
616

717
repositories {
18+
mavenLocal()
819
mavenCentral()
920
maven { url 'target/dependencies' }
10-
mavenLocal()
1121
}
1222

1323
dependencies {
1424
compileOnly gradleApi()
15-
// api "org.hjug.refactorfirst.graphdatagenerator:graph-data-generator:${version}"
25+
implementation 'org.hjug.refactorfirst.report:report:0.7.2-SNAPSHOT'
26+
}
27+
28+
jar {
29+
manifest {
30+
attributes 'Main-Class': 'org.hjug.gradlereport.RefactorFirstPlugin',
31+
'Implementation-Title': 'RefactorFirstPlugin',
32+
'Implementation-Version': version
33+
}
1634
}
1735

18-
pluginBundle {
19-
website = 'https://github.com/jimbethancourt/RefactorFirst'
20-
vcsUrl = 'https://github.com/jimbethancourt/RefactorFirst.git'
21-
tags = ['refactor', 'report']
36+
shadowJar {
37+
minimizeJar = true
2238
}
2339

2440
gradlePlugin {
25-
plugins {
26-
refactorFirstPlugin {
27-
id = 'org.hjug.refactor-first'
28-
displayName = 'RefactorFirst'
29-
description = 'Plugin that identifies God classes in a codebase and suggests which classes should be refactored first.'
41+
plugins {
42+
refactorFirstPlugin {
43+
id = 'org.hjug.refactor-first'
44+
displayName = 'RefactorFirst'
45+
description = 'Plugin that identifies God classes in a codebase and suggests which classes should be refactored first.'
3046
implementationClass = 'org.hjug.gradlereport.RefactorFirstPlugin'
3147
}
3248
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.hjug.gradlereport;
2+
3+
import java.io.File;
4+
import org.gradle.api.DefaultTask;
5+
import org.gradle.api.tasks.TaskAction;
6+
import org.hjug.refactorfirst.report.CsvReport;
7+
8+
public class CsvReportTask extends DefaultTask {
9+
10+
@TaskAction
11+
public void generate() {
12+
RefactorFirstExtension ext = getProject().getExtensions().findByType(RefactorFirstExtension.class);
13+
if (ext == null) {
14+
ext = new RefactorFirstExtension();
15+
}
16+
17+
final String projectName = ext.getProjectName() != null ? ext.getProjectName() : getProject().getName();
18+
final String projectVersion = ext.getProjectVersion() != null ? ext.getProjectVersion() : String.valueOf(getProject().getVersion());
19+
final File baseDir = getProject().getProjectDir();
20+
final File outputDir = ext.resolveOutputDir(baseDir);
21+
22+
CsvReport csvReport = new CsvReport();
23+
csvReport.execute(
24+
ext.isShowDetails(),
25+
projectName,
26+
projectVersion,
27+
RefactorFirstPlugin.relativizeToProject(baseDir, outputDir),
28+
baseDir
29+
);
30+
}
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.hjug.gradlereport;
2+
3+
import java.io.File;
4+
import org.gradle.api.DefaultTask;
5+
import org.gradle.api.tasks.TaskAction;
6+
import org.hjug.refactorfirst.report.HtmlReport;
7+
8+
public class HtmlReportTask extends DefaultTask {
9+
10+
@TaskAction
11+
public void generate() {
12+
RefactorFirstExtension ext = getProject().getExtensions().findByType(RefactorFirstExtension.class);
13+
if (ext == null) {
14+
ext = new RefactorFirstExtension();
15+
}
16+
17+
final String projectName = ext.getProjectName() != null ? ext.getProjectName() : getProject().getName();
18+
final String projectVersion = ext.getProjectVersion() != null ? ext.getProjectVersion() : String.valueOf(getProject().getVersion());
19+
final File baseDir = getProject().getProjectDir();
20+
final File outputDir = ext.resolveOutputDir(baseDir);
21+
22+
HtmlReport htmlReport = new HtmlReport();
23+
htmlReport.execute(
24+
ext.getBackEdgeAnalysisCount(),
25+
ext.isAnalyzeCycles(),
26+
ext.isShowDetails(),
27+
ext.isMinifyHtml(),
28+
ext.isExcludeTests(),
29+
ext.getTestSourceDirectory(),
30+
projectName,
31+
projectVersion,
32+
baseDir,
33+
RefactorFirstPlugin.relativizeToProject(baseDir, outputDir)
34+
);
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.hjug.gradlereport;
2+
3+
import java.io.File;
4+
import org.gradle.api.DefaultTask;
5+
import org.gradle.api.tasks.TaskAction;
6+
import org.hjug.refactorfirst.report.json.JsonReportExecutor;
7+
8+
public class JsonReportTask extends DefaultTask {
9+
10+
@TaskAction
11+
public void generate() {
12+
RefactorFirstExtension ext = getProject().getExtensions().findByType(RefactorFirstExtension.class);
13+
if (ext == null) {
14+
ext = new RefactorFirstExtension();
15+
}
16+
17+
final File baseDir = getProject().getProjectDir();
18+
final File outputDir = ext.resolveOutputDir(baseDir);
19+
20+
JsonReportExecutor jsonReportExecutor = new JsonReportExecutor();
21+
jsonReportExecutor.execute(baseDir, RefactorFirstPlugin.relativizeToProject(baseDir, outputDir));
22+
}
23+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package org.hjug.gradlereport;
2+
3+
import java.io.File;
4+
5+
/**
6+
* Gradle extension to configure RefactorFirst plugin.<br>
7+
* Mirrors the Maven plugin parameters where possible.
8+
*
9+
* @author FX
10+
*/
11+
public class RefactorFirstExtension {
12+
private boolean showDetails = false;
13+
private int backEdgeAnalysisCount = 50;
14+
private boolean analyzeCycles = true;
15+
private boolean minifyHtml = false;
16+
private boolean excludeTests = true;
17+
/**
18+
* The test source directory containing test class sources (pattern).
19+
*/
20+
private String testSourceDirectory; // e.g. "src/test"
21+
22+
private String projectName; // default to Gradle project name if null
23+
private String projectVersion; // default to Gradle project version if null
24+
25+
/**
26+
* Output directory relative to project root for generated site files.
27+
* Defaults to target/site (to match Maven plugin output paths).
28+
*/
29+
private String outputDirectory; // e.g. "target/site"
30+
31+
public boolean isShowDetails() {
32+
return showDetails;
33+
}
34+
35+
public void setShowDetails(boolean showDetails) {
36+
this.showDetails = showDetails;
37+
}
38+
39+
public int getBackEdgeAnalysisCount() {
40+
return backEdgeAnalysisCount;
41+
}
42+
43+
public void setBackEdgeAnalysisCount(int backEdgeAnalysisCount) {
44+
this.backEdgeAnalysisCount = backEdgeAnalysisCount;
45+
}
46+
47+
public boolean isAnalyzeCycles() {
48+
return analyzeCycles;
49+
}
50+
51+
public void setAnalyzeCycles(boolean analyzeCycles) {
52+
this.analyzeCycles = analyzeCycles;
53+
}
54+
55+
public boolean isMinifyHtml() {
56+
return minifyHtml;
57+
}
58+
59+
public void setMinifyHtml(boolean minifyHtml) {
60+
this.minifyHtml = minifyHtml;
61+
}
62+
63+
public boolean isExcludeTests() {
64+
return excludeTests;
65+
}
66+
67+
public void setExcludeTests(boolean excludeTests) {
68+
this.excludeTests = excludeTests;
69+
}
70+
71+
public String getTestSourceDirectory() {
72+
return testSourceDirectory;
73+
}
74+
75+
public void setTestSourceDirectory(String testSourceDirectory) {
76+
this.testSourceDirectory = testSourceDirectory;
77+
}
78+
79+
public String getProjectName() {
80+
return projectName;
81+
}
82+
83+
public void setProjectName(String projectName) {
84+
this.projectName = projectName;
85+
}
86+
87+
public String getProjectVersion() {
88+
return projectVersion;
89+
}
90+
91+
public void setProjectVersion(String projectVersion) {
92+
this.projectVersion = projectVersion;
93+
}
94+
95+
public String getOutputDirectory() {
96+
return outputDirectory;
97+
}
98+
99+
public void setOutputDirectory(String outputDirectory) {
100+
this.outputDirectory = outputDirectory;
101+
}
102+
103+
// Convenience to resolve output directory to an absolute File
104+
File resolveOutputDir(File projectDir) {
105+
String out = outputDirectory;
106+
if (out == null || out.trim().isEmpty()) {
107+
out = "target/site"; // match Maven default location
108+
}
109+
return new File(projectDir, out);
110+
}
111+
}
Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,53 @@
11
package org.hjug.gradlereport;
22

3+
import java.io.File;
34
import org.gradle.api.Plugin;
45
import org.gradle.api.Project;
56

7+
/**
8+
* The gradle refactor first plugin
9+
*/
610
public class RefactorFirstPlugin implements Plugin<Project> {
11+
12+
@Override
713
public void apply(Project project) {
8-
// impl
14+
// Create extension to configure the plugin
15+
project.getExtensions().create("refactorFirst", RefactorFirstExtension.class);
16+
17+
// Register tasks
18+
project.getTasks().register("refactorFirstHtmlReport", HtmlReportTask.class, task -> {
19+
task.setGroup("RefactorFirst");
20+
task.setDescription("Generates the RefactorFirst full HTML report (with graphs)");
21+
});
22+
23+
project.getTasks().register("refactorFirstSimpleHtmlReport", SimpleHtmlReportTask.class, task -> {
24+
task.setGroup("RefactorFirst");
25+
task.setDescription("Generates the RefactorFirst simplified HTML report (no heavy graphs)");
26+
});
27+
28+
project.getTasks().register("refactorFirstJsonReport", JsonReportTask.class, task -> {
29+
task.setGroup("RefactorFirst");
30+
task.setDescription("Generates the RefactorFirst JSON data report");
31+
});
32+
33+
project.getTasks().register("refactorFirstCsvReport", CsvReportTask.class, task -> {
34+
task.setGroup("RefactorFirst");
35+
task.setDescription("Generates the RefactorFirst CSV report");
36+
});
37+
}
38+
39+
public static String relativizeToProject(File baseDir, File outputDir) {
40+
// Maven plugin passes a path relative to project base dir
41+
String basePath = baseDir.getAbsolutePath();
42+
String outPath = outputDir.getAbsolutePath();
43+
if (outPath.startsWith(basePath)) {
44+
String rel = outPath.substring(basePath.length());
45+
if (rel.startsWith(File.separator)) {
46+
rel = rel.substring(1);
47+
}
48+
return rel;
49+
}
50+
// Fallback to default
51+
return "target/site";
952
}
1053
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.hjug.gradlereport;
2+
3+
import java.io.File;
4+
import org.gradle.api.DefaultTask;
5+
import org.gradle.api.tasks.TaskAction;
6+
import org.hjug.refactorfirst.report.SimpleHtmlReport;
7+
8+
public class SimpleHtmlReportTask extends DefaultTask {
9+
10+
@TaskAction
11+
public void generate() {
12+
RefactorFirstExtension ext = getProject().getExtensions().findByType(RefactorFirstExtension.class);
13+
if (ext == null) {
14+
ext = new RefactorFirstExtension();
15+
}
16+
17+
final String projectName = ext.getProjectName() != null ? ext.getProjectName() : getProject().getName();
18+
final String projectVersion = ext.getProjectVersion() != null ? ext.getProjectVersion() : String.valueOf(getProject().getVersion());
19+
final File baseDir = getProject().getProjectDir();
20+
final File outputDir = ext.resolveOutputDir(baseDir);
21+
22+
SimpleHtmlReport htmlReport = new SimpleHtmlReport();
23+
htmlReport.execute(
24+
ext.getBackEdgeAnalysisCount(),
25+
ext.isAnalyzeCycles(),
26+
ext.isShowDetails(),
27+
ext.isMinifyHtml(),
28+
ext.isExcludeTests(),
29+
ext.getTestSourceDirectory(),
30+
projectName,
31+
projectVersion,
32+
baseDir,
33+
RefactorFirstPlugin.relativizeToProject(baseDir, outputDir)
34+
);
35+
}
36+
}

0 commit comments

Comments
 (0)