@@ -6,6 +6,7 @@ import org.gradle.api.Project
6
6
import org.gradle.api.Task
7
7
import org.gradle.api.artifacts.Configuration
8
8
import org.gradle.api.plugins.JavaBasePlugin
9
+
9
10
/**
10
11
* @author Oliver Becker
11
12
*/
@@ -16,9 +17,10 @@ class DecyclePlugin implements Plugin<Project> {
16
17
@Override
17
18
void apply (final Project project ) {
18
19
Properties props = new Properties ()
19
- getClass(). classLoader. getResource(" META-INF/gradle-plugins/de.obqo.decycle.properties" ). withInputStream { stream ->
20
- props. load(stream)
21
- }
20
+ getClass(). classLoader. getResource(" META-INF/gradle-plugins/de.obqo.decycle.properties" )
21
+ .withInputStream { stream ->
22
+ props. load(stream)
23
+ }
22
24
23
25
// create a runner task that runs all single decycle tasks
24
26
final Task decycleRunnerTask = project. tasks. create(TASK_NAME )
@@ -39,35 +41,64 @@ class DecyclePlugin implements Plugin<Project> {
39
41
decycle " de.obqo.decycle:decycle-lib:${ toolVersion} "
40
42
}
41
43
42
- def sources = configuration. sourceSets
44
+ def isAndroidProject = project. hasProperty(" android" )
45
+
46
+ def sources = isAndroidProject ? configuration. androidSourceSets : configuration. sourceSets
43
47
if (sources. empty) {
44
- def projectSources = project. findProperty(" sourceSets" )
45
- if (projectSources == null ) {
46
- throw new GradleException (" No source sets found. Did you forget to apply the 'java' plugin?" )
48
+ def projectSources
49
+ if (isAndroidProject) {
50
+ projectSources = project. android. sourceSets
51
+ } else {
52
+ projectSources = project. findProperty(" sourceSets" )
53
+ if (projectSources == null ) {
54
+ throw new GradleException (" No source sets found. Did you forget to apply the 'java' plugin?" )
55
+ }
47
56
}
48
57
sources = projectSources. asMap. values()
49
58
}
50
59
51
60
// create decycle work tasks, one for each source set
52
61
sources. forEach { source ->
53
- def name = source. name
54
-
55
- DecycleTask decycleWorkTask = project. tasks. create(TASK_NAME + name. capitalize(), DecycleTask )
56
- decycleWorkTask.
57
- description = " Checks the ${ name} sources for package cycles and other custom constraints"
58
- decycleWorkTask. group = JavaBasePlugin . VERIFICATION_GROUP
59
- decycleWorkTask. configuration. set(configuration)
60
- decycleWorkTask. classpath. set(source. output)
61
- decycleWorkTask. reportFile. set(new File (project. buildDir, " reports/decycle/${ name} .html" ))
62
- decycleWorkTask. reportTitle. set(project. name + " | " + name);
63
- decycleWorkTask. workerClasspath. set(workerClasspath)
62
+ def name = isAndroidProject ? adjustSourceSetName(source. name) : source. name
63
+ def output = isAndroidProject
64
+ // TODO is there a better way to resolve the output (classes) directory in android projects?
65
+ ? project. files(project. layout. buildDirectory. dir(" tmp/kotlin-classes/$name " ),
66
+ project. layout. buildDirectory. dir(" intermediates/javac/$name " ))
67
+ : source. output
68
+ def compileTaskName = isAndroidProject
69
+ // TODO is there a better way to determine the compile task for each source set in android projects?
70
+ ? " compile" + name. capitalize() + " JavaWithJavac"
71
+ : source. classesTaskName
72
+ def compileTask = project. tasks. findByPath(compileTaskName)
73
+ if (compileTask != null ) { // ignore android source sets without compile task
74
+ DecycleTask decycleWorkTask = project. tasks. create(TASK_NAME + name. capitalize(), DecycleTask )
75
+ decycleWorkTask.
76
+ description = " Checks the ${ name} sources for package cycles and other custom constraints"
77
+ decycleWorkTask. group = JavaBasePlugin . VERIFICATION_GROUP
78
+ decycleWorkTask. configuration. set(configuration)
79
+ decycleWorkTask. classpath. set(output)
80
+ decycleWorkTask. reportFile. set(new File (project. buildDir, " reports/decycle/${ name} .html" ))
81
+ decycleWorkTask. reportTitle. set(project. name + " | " + name)
82
+ decycleWorkTask. workerClasspath. set(workerClasspath)
64
83
65
- // set task dependencies, e.g. decycle -> decycleTest -> testClasses
66
- decycleRunnerTask. dependsOn(decycleWorkTask. dependsOn(project. tasks[source. classesTaskName]))
84
+ // set task dependencies, e.g. decycle -> decycleTest -> testClasses
85
+ decycleRunnerTask. dependsOn(decycleWorkTask. dependsOn(compileTask))
86
+ } else {
87
+ project. getLogger(). info(
88
+ " Decycle : Ignore sourceSet $name – cannot determine the corresponding compile task" )
89
+ }
67
90
}
68
91
69
92
// finally make task 'check' depend on the runner task
70
93
project. tasks[JavaBasePlugin . CHECK_TASK_NAME ]. dependsOn decycleRunnerTask
71
94
}
72
95
}
96
+
97
+ private static String adjustSourceSetName (String sourceSetName ) {
98
+ switch (sourceSetName) {
99
+ case " testDebug" : return " debugUnitTest"
100
+ case " testRelease" : return " releaseUnitTest"
101
+ default : return sourceSetName
102
+ }
103
+ }
73
104
}
0 commit comments