|
20 | 20 | package org.sonarqube.gradle; |
21 | 21 |
|
22 | 22 | import java.io.BufferedReader; |
| 23 | +import java.io.File; |
23 | 24 | import java.io.IOException; |
24 | 25 | import java.io.InputStream; |
25 | 26 | import java.io.InputStreamReader; |
26 | 27 | import java.nio.charset.StandardCharsets; |
| 28 | +import java.util.ArrayList; |
27 | 29 | import java.util.Collections; |
28 | 30 | import java.util.HashMap; |
| 31 | +import java.util.List; |
29 | 32 | import java.util.Map; |
30 | 33 | import org.gradle.api.internal.ConventionTask; |
31 | 34 | import org.gradle.api.logging.LogLevel; |
32 | 35 | import org.gradle.api.logging.Logger; |
33 | 36 | import org.gradle.api.logging.Logging; |
34 | 37 | import org.gradle.api.provider.Provider; |
35 | | -import org.gradle.api.tasks.Input; |
| 38 | +import org.gradle.api.tasks.InputFiles; |
36 | 39 | import org.gradle.api.tasks.Internal; |
37 | 40 | import org.gradle.api.tasks.TaskAction; |
38 | 41 | import org.gradle.util.GradleVersion; |
@@ -189,13 +192,75 @@ private static boolean isSkippedWithProperty(Map<String, String> properties) { |
189 | 192 | * @return The String key/value pairs to be passed to the SonarQube Scanner. |
190 | 193 | * {@code null} values are not permitted. |
191 | 194 | */ |
192 | | - @Input |
| 195 | + @Internal |
193 | 196 | public Provider<Map<String, String>> getProperties() { |
194 | 197 | return properties; |
195 | 198 | } |
196 | 199 |
|
197 | 200 | void setProperties(Provider<Map<String, String>> properties) { |
198 | 201 | this.properties = properties; |
| 202 | + |
| 203 | + getOutputs().cacheIf(t -> properties.map(this::isCacheEnabled).get()); |
| 204 | + getOutputs().file("build/sonar/report-task.txt"); |
| 205 | + |
| 206 | + // Remove properties that should be excluded from the Gradle build cache |
| 207 | + getInputs().property("properties", properties.map(props -> { |
| 208 | + Map<String, String> filtered = new HashMap<>(); |
| 209 | + for (Map.Entry<String, String> entry : props.entrySet()) { |
| 210 | + if (!ScanProperties.excludePropertyFromCache(entry.getKey())) { |
| 211 | + filtered.put(entry.getKey(), entry.getValue()); |
| 212 | + } |
| 213 | + } |
| 214 | + return filtered; |
| 215 | + })); |
| 216 | + } |
| 217 | + |
| 218 | + |
| 219 | + @InputFiles |
| 220 | + public Provider<List<File>> getInputFiles () { |
| 221 | + return properties.map(props -> { |
| 222 | + ArrayList<File> files = new ArrayList<>(); |
| 223 | + files.addAll(parseInputs(props, ScanProperties.PROJECT_SOURCE_DIRS)); |
| 224 | + files.addAll(parseInputs(props, ScanProperties.LIBRARIES)); |
| 225 | + for (String key : props.keySet()) { |
| 226 | + if (key.endsWith(".reportPaths")) { |
| 227 | + files.addAll(parseInputs(props, key)); |
| 228 | + } |
| 229 | + } |
| 230 | + return files; |
| 231 | + }); |
| 232 | + } |
| 233 | + |
| 234 | + private List<File> parseInputs(Map<String, String> props, String key) { |
| 235 | + String sources = props.get(key); |
| 236 | + if (sources == null) { |
| 237 | + return List.of(); |
| 238 | + } |
| 239 | + ArrayList<File> files = new ArrayList<>(); |
| 240 | + for (String source : sources.split(",")) { |
| 241 | + if (source.isEmpty()) { |
| 242 | + continue; |
| 243 | + } |
| 244 | + |
| 245 | + File file = new File(source); |
| 246 | + if (!file.exists()) { |
| 247 | + getLogger().warn("Input file '{}' does not exist", file); |
| 248 | + continue; |
| 249 | + } |
| 250 | + |
| 251 | + if(file.isDirectory()) { |
| 252 | + files.addAll(getProject().fileTree(file).getFiles()); |
| 253 | + } else { |
| 254 | + files.add(file); |
| 255 | + } |
| 256 | + } |
| 257 | + return files; |
| 258 | + } |
| 259 | + |
| 260 | + private boolean isCacheEnabled(Map<String, String> properties) { |
| 261 | + return properties |
| 262 | + .getOrDefault(ScanProperties.GRADLE_CACHE, "true") |
| 263 | + .equals("true"); |
199 | 264 | } |
200 | 265 |
|
201 | 266 | /** |
|
0 commit comments