You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractExclusionFilters.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.scan.filesystem;
  21. import java.nio.file.Path;
  22. import java.util.Arrays;
  23. import java.util.function.Function;
  24. import java.util.stream.Stream;
  25. import org.apache.commons.lang.ArrayUtils;
  26. import org.apache.commons.lang.StringUtils;
  27. import org.sonar.api.CoreProperties;
  28. import org.sonar.api.batch.fs.InputFile;
  29. import org.sonar.api.batch.fs.internal.PathPattern;
  30. import org.sonar.api.utils.log.Logger;
  31. import org.sonar.api.utils.log.Loggers;
  32. public abstract class AbstractExclusionFilters {
  33. private static final Logger LOG = Loggers.get(AbstractExclusionFilters.class);
  34. private final String[] sourceInclusions;
  35. private final String[] testInclusions;
  36. private final String[] sourceExclusions;
  37. private final String[] testExclusions;
  38. private PathPattern[] mainInclusionsPattern;
  39. private PathPattern[] mainExclusionsPattern;
  40. private PathPattern[] testInclusionsPattern;
  41. private PathPattern[] testExclusionsPattern;
  42. public AbstractExclusionFilters(Function<String, String[]> configProvider) {
  43. this.sourceInclusions = inclusions(configProvider, CoreProperties.PROJECT_INCLUSIONS_PROPERTY);
  44. this.testInclusions = inclusions(configProvider, CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY);
  45. this.sourceExclusions = exclusions(configProvider, CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_EXCLUSIONS_PROPERTY);
  46. this.testExclusions = exclusions(configProvider, CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY, CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY);
  47. this.mainInclusionsPattern = prepareMainInclusions(sourceInclusions);
  48. this.mainExclusionsPattern = prepareMainExclusions(sourceExclusions, testInclusions);
  49. this.testInclusionsPattern = prepareTestInclusions(testInclusions);
  50. this.testExclusionsPattern = prepareTestExclusions(testExclusions);
  51. }
  52. protected void log() {
  53. log("Included sources: ", mainInclusionsPattern);
  54. log("Excluded sources: ", mainExclusionsPattern);
  55. log("Included tests: ", testInclusionsPattern);
  56. log("Excluded tests: ", testExclusionsPattern);
  57. }
  58. private String[] inclusions(Function<String, String[]> configProvider, String propertyKey) {
  59. return Arrays.stream(configProvider.apply(propertyKey))
  60. .map(StringUtils::trim)
  61. .filter(s -> !"**/*".equals(s))
  62. .filter(s -> !"file:**/*".equals(s))
  63. .toArray(String[]::new);
  64. }
  65. private String[] exclusions(Function<String, String[]> configProvider, String globalExclusionsProperty, String exclusionsProperty) {
  66. String[] globalExclusions = configProvider.apply(globalExclusionsProperty);
  67. String[] exclusions = configProvider.apply(exclusionsProperty);
  68. return Stream.concat(Arrays.stream(globalExclusions), Arrays.stream(exclusions))
  69. .map(StringUtils::trim)
  70. .toArray(String[]::new);
  71. }
  72. public boolean hasPattern() {
  73. return mainInclusionsPattern.length > 0 || mainExclusionsPattern.length > 0 || testInclusionsPattern.length > 0 || testExclusionsPattern.length > 0;
  74. }
  75. private static void log(String title, PathPattern[] patterns) {
  76. if (patterns.length > 0) {
  77. LOG.info(title);
  78. for (PathPattern pattern : patterns) {
  79. LOG.info(" {}", pattern);
  80. }
  81. }
  82. }
  83. public boolean accept(Path absolutePath, Path relativePath, InputFile.Type type) {
  84. PathPattern[] inclusionPatterns;
  85. PathPattern[] exclusionPatterns;
  86. if (InputFile.Type.MAIN == type) {
  87. inclusionPatterns = mainInclusionsPattern;
  88. exclusionPatterns = mainExclusionsPattern;
  89. } else if (InputFile.Type.TEST == type) {
  90. inclusionPatterns = testInclusionsPattern;
  91. exclusionPatterns = testExclusionsPattern;
  92. } else {
  93. throw new IllegalArgumentException("Unknown file type: " + type);
  94. }
  95. if (inclusionPatterns.length > 0) {
  96. boolean matchInclusion = false;
  97. for (PathPattern pattern : inclusionPatterns) {
  98. matchInclusion |= pattern.match(absolutePath, relativePath);
  99. }
  100. if (!matchInclusion) {
  101. return false;
  102. }
  103. }
  104. if (exclusionPatterns.length > 0) {
  105. for (PathPattern pattern : exclusionPatterns) {
  106. if (pattern.match(absolutePath, relativePath)) {
  107. return false;
  108. }
  109. }
  110. }
  111. return true;
  112. }
  113. private static PathPattern[] prepareMainInclusions(String[] sourceInclusions) {
  114. if (sourceInclusions.length > 0) {
  115. // User defined params
  116. return PathPattern.create(sourceInclusions);
  117. }
  118. return new PathPattern[0];
  119. }
  120. private static PathPattern[] prepareTestInclusions(String[] testInclusions) {
  121. return PathPattern.create(testInclusions);
  122. }
  123. static PathPattern[] prepareMainExclusions(String[] sourceExclusions, String[] testInclusions) {
  124. String[] patterns = (String[]) ArrayUtils.addAll(
  125. sourceExclusions, testInclusions);
  126. return PathPattern.create(patterns);
  127. }
  128. private static PathPattern[] prepareTestExclusions(String[] testExclusions) {
  129. return PathPattern.create(testExclusions);
  130. }
  131. @Override
  132. public boolean equals(Object o) {
  133. if (this == o) {
  134. return true;
  135. }
  136. if (!(o instanceof AbstractExclusionFilters)) {
  137. return false;
  138. }
  139. AbstractExclusionFilters that = (AbstractExclusionFilters) o;
  140. return Arrays.equals(sourceInclusions, that.sourceInclusions) &&
  141. Arrays.equals(testInclusions, that.testInclusions) &&
  142. Arrays.equals(sourceExclusions, that.sourceExclusions) &&
  143. Arrays.equals(testExclusions, that.testExclusions);
  144. }
  145. @Override
  146. public int hashCode() {
  147. int result = Arrays.hashCode(sourceInclusions);
  148. result = 31 * result + Arrays.hashCode(testInclusions);
  149. result = 31 * result + Arrays.hashCode(sourceExclusions);
  150. result = 31 * result + Arrays.hashCode(testExclusions);
  151. return result;
  152. }
  153. }