34
34
import org .gradle .api .DefaultTask ;
35
35
import org .gradle .api .GradleException ;
36
36
import org .gradle .api .InvalidUserDataException ;
37
+ import org .gradle .api .Project ;
37
38
import org .gradle .api .file .FileCollection ;
38
39
import org .gradle .api .file .FileTree ;
39
40
import org .gradle .api .plugins .JavaPluginExtension ;
48
49
import org .gradle .api .tasks .util .PatternFilterable ;
49
50
import org .gradle .api .tasks .util .PatternSet ;
50
51
52
+ import javax .inject .Inject ;
53
+
51
54
import java .io .File ;
52
55
import java .io .IOException ;
53
56
import java .io .UncheckedIOException ;
@@ -89,8 +92,10 @@ public class ForbiddenPatternsTask extends DefaultTask {
89
92
* The rules: a map from the rule name, to a rule regex pattern.
90
93
*/
91
94
private final Map <String , String > patterns = new HashMap <>();
95
+ private final Project project ;
92
96
93
- public ForbiddenPatternsTask () {
97
+ @ Inject
98
+ public ForbiddenPatternsTask (Project project ) {
94
99
setDescription ("Checks source files for invalid patterns like nocommits or tabs" );
95
100
getInputs ().property ("excludes" , filesFilter .getExcludes ());
96
101
getInputs ().property ("rules" , patterns );
@@ -99,20 +104,22 @@ public ForbiddenPatternsTask() {
99
104
patterns .put ("nocommit" , "nocommit|NOCOMMIT" );
100
105
patterns .put ("nocommit should be all lowercase or all uppercase" , "((?i)nocommit)(?<!(nocommit|NOCOMMIT))" );
101
106
patterns .put ("tab" , "\t " );
107
+
108
+ this .project = project ;
102
109
}
103
110
104
111
@ InputFiles
105
112
@ SkipWhenEmpty
106
113
@ IgnoreEmptyDirectories
107
114
@ PathSensitive (PathSensitivity .RELATIVE )
108
115
public FileCollection getFiles () {
109
- return getProject () .getExtensions ()
116
+ return project .getExtensions ()
110
117
.getByType (JavaPluginExtension .class )
111
118
.getSourceSets ()
112
119
.stream ()
113
120
.map (sourceSet -> sourceSet .getAllSource ().matching (filesFilter ))
114
121
.reduce (FileTree ::plus )
115
- .orElse (getProject () .files ().getAsFileTree ());
122
+ .orElse (project .files ().getAsFileTree ());
116
123
}
117
124
118
125
@ TaskAction
@@ -131,7 +138,7 @@ public void checkInvalidPatterns() throws IOException {
131
138
.boxed ()
132
139
.collect (Collectors .toList ());
133
140
134
- String path = getProject () .getRootProject ().getProjectDir ().toURI ().relativize (f .toURI ()).toString ();
141
+ String path = project .getRootProject ().getProjectDir ().toURI ().relativize (f .toURI ()).toString ();
135
142
failures .addAll (
136
143
invalidLines .stream ()
137
144
.map (l -> new AbstractMap .SimpleEntry <>(l + 1 , lines .get (l )))
@@ -155,7 +162,7 @@ public void checkInvalidPatterns() throws IOException {
155
162
156
163
@ OutputFile
157
164
public File getOutputMarker () {
158
- return new File (getProject () .getBuildDir (), "markers/" + getName ());
165
+ return new File (project .getBuildDir (), "markers/" + getName ());
159
166
}
160
167
161
168
@ Input
0 commit comments