Skip to content

Commit

Permalink
feat: failure-on-cycles option
Browse files Browse the repository at this point in the history
  • Loading branch information
heowc committed Jun 3, 2024
1 parent 80b60d4 commit 623c27f
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 14 deletions.
7 changes: 6 additions & 1 deletion heo-cli/src/main/java/dev/heowc/heo/cli/HeoCliService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import dev.heowc.heo.core.HeoException;

import org.springframework.stereotype.Service;

import dev.heowc.heo.core.Module;
Expand Down Expand Up @@ -29,11 +31,14 @@ public HeoCliService(ModuleLoaderService moduleLoaderService,
this.reportVisualizationService = reportVisualizationService;
}

public void command(String directory, String rootPackage, String destination) {
public void command(String directory, String rootPackage, String destination, HeoConfig heoConfig) {
final List<Module> modules = moduleLoaderService.loads(directory, rootPackage);
final DependencyAnalysisResult result =
dependencyAnalysisService.analyzeProjectDependencies(modules, rootPackage);
final String report = analysisReportService.createReport(result);
reportVisualizationService.createFile(report, destination);
if (heoConfig.failureOnCycles() && result.hasCycle()) {
throw new HeoException("Cycles occurred");
}
}
}
5 changes: 3 additions & 2 deletions heo-cli/src/main/java/dev/heowc/heo/cli/HeoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ public HeoCommand(HeoCliService service) {
@Command(command = "heo")
public void doHeo(@Option(shortNames = 'd', required = true) String directory,
@Option(shortNames = 'p', required = true) String prefixPackage,
@Option(shortNames = 'o') String destination) {
@Option(shortNames = 'o') String destination,
@Option(longNames = "failure-on-cycles", defaultValue = "false") boolean failureOnCycles) {
if (StringUtils.isBlank(destination)) {
destination = String.format("result-%s.png", System.currentTimeMillis());
}
service.command(directory, prefixPackage, destination);
service.command(directory, prefixPackage, destination, new HeoConfig(failureOnCycles));
}
}
5 changes: 5 additions & 0 deletions heo-cli/src/main/java/dev/heowc/heo/cli/HeoConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.heowc.heo.cli;

public record HeoConfig(boolean failureOnCycles) {

}
8 changes: 8 additions & 0 deletions heo-core/src/main/java/dev/heowc/heo/core/HeoException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.heowc.heo.core;

public class HeoException extends RuntimeException {

public HeoException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ public DomainGraph getDomainGraph() {
public CycleDetector<String, DomainEdge> getCycleDetector() {
return cycleDetector;
}

public boolean hasCycle() {
return cycleDetector.detectCycles();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@
import java.nio.file.StandardCopyOption;
import java.util.List;

import javax.annotation.Nullable;

import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.tasks.JavaExec;

import javax.annotation.Nullable;

public class HeoPlugin implements Plugin<Project> {

private static final String REPORT_PATH = "build/reports/heo";

@Override
public void apply(Project project) {
final HeoConfig config = project.getExtensions().create("heo", HeoConfig.class);
final HeoPluginConfig config = project.getExtensions().create("heo", HeoPluginConfig.class);
project.getTasks().register("heoReport", JavaExec.class, task -> {
task.setGroup("heo");
task.setDescription("Execute heo-cli");
Expand All @@ -37,24 +37,33 @@ public void apply(Project project) {

task.setMain("-jar");
task.args(tempJar.getAbsolutePath());
task.args(List.of("-d", determineDirectory(project, config.getDirectoryPath()),
"-p", determinePrefixPackage(project, config.getPrefixPackage()),
"-o", determineDestination(project, config.getDestination())));
task.args(List.of(
"-d", determineDirectory(project, config.getDirectoryPath()),
"-p", determinePrefixPackage(project, config.getPrefixPackage()),
"-o", determineDestination(project, config.getDestination()),
"--failure-on-cycles", config.isFailureOnCycles()
));

tempJar.deleteOnExit();
});
}

private static String determineDirectory(Project project, @Nullable String directoryPath) {
return StringUtils.isBlank(directoryPath) ? project.getProjectDir().getAbsolutePath() : directoryPath;
return StringUtils.isBlank(directoryPath)
? project.getProjectDir().getAbsolutePath()
: directoryPath;
}

private static String determinePrefixPackage(Project project, @Nullable String prefixPackage) {
return StringUtils.isBlank(prefixPackage) ? project.getGroup().toString() : prefixPackage;
return StringUtils.isBlank(prefixPackage)
? project.getGroup().toString()
: prefixPackage;
}

private static String determineDestination(Project project, @Nullable String destination) {
return StringUtils.isBlank(destination) ? Path.of(project.getProjectDir().getAbsolutePath(), REPORT_PATH, "index.png").toString() : destination;
return StringUtils.isBlank(destination)
? Path.of(project.getProjectDir().getAbsolutePath(), REPORT_PATH, "index.png").toString()
: destination;
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package dev.heowc.heo.gradle;

public class HeoConfig {
public class HeoPluginConfig {

private String directoryPath;
private String prefixPackage;
private String destination;
private boolean failureOnCycles;

public String getDirectoryPath() {
return directoryPath;
Expand All @@ -29,4 +30,12 @@ public String getDestination() {
public void setDestination(String destination) {
this.destination = destination;
}

public boolean isFailureOnCycles() {
return failureOnCycles;
}

public void setFailureOnCycles(boolean failureOnCycles) {
this.failureOnCycles = failureOnCycles;
}
}
40 changes: 40 additions & 0 deletions it/cycled-gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
buildscript {
repositories {
mavenLocal()
}
dependencies {
if (project.hasProperty('localPlugin')) {
classpath "dev.heowc.heo:heo-gradle-plugin:${version}"
}
}
}

if (project.hasProperty('localPlugin')) {
apply plugin: 'dev.heowc.heo'
apply plugin: 'java'

heo {
directoryPath = "${rootDir}/it/cycled-gradle-plugin"
prefixPackage = "dev.heowc.heo.it.cycled"
failureOnCycles = true
}

test {
dependsOn 'pluginTest'
}

tasks.register('pluginTest') {
doLast {
var failure = false
try {
tasks.heoReport.exec()
} catch (Exception e) {
failure = true
}
if (!failure) {
throw new IllegalStateException("The task did not fail")
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.heowc.heo.it.cycled;

public class CycledMain {

public static void main(String[] args) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.heowc.heo.it.cycled.a;

import dev.heowc.heo.it.cycled.b.B;

public class A {

private B b;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.heowc.heo.it.cycled.b;

import dev.heowc.heo.it.cycled.c.C;

public class B {

private C c;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dev.heowc.heo.it.cycled.c;

import dev.heowc.heo.it.cycled.a.A;

public class C {

private A a;
}
19 changes: 19 additions & 0 deletions it/gradle-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,29 @@ buildscript {

if (project.hasProperty('localPlugin')) {
apply plugin: 'dev.heowc.heo'
apply plugin: 'java'

heo {
directoryPath = "${rootDir}/heo-core"
prefixPackage = "dev.heowc.heo.core"
}

test {
dependsOn 'pluginTest'
}

tasks.register('pluginTest') {
doLast {
var failure = false
try {
tasks.heoReport.exec()
} catch (Exception e) {
failure = true
}
if (failure) {
throw new IllegalStateException("The task did not succeed")
}
}
}
}

2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ include 'heo-cli'
include 'heo-gradle-plugin'

include 'it:gradle-plugin'

include 'it:cycled-gradle-plugin'

0 comments on commit 623c27f

Please sign in to comment.