org.sonar.api.batch.fs.FilePredicates Java Examples

The following examples show how to use org.sonar.api.batch.fs.FilePredicates. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClojureSensor.java    From sonar-clojure with MIT License 6 votes vote down vote up
@Override
public void execute(SensorContext context) {
    LOG.info("Running ClojureSensor");
    FilePredicates predicates = context.fileSystem().predicates();
    FilePredicate clojure = predicates.hasLanguage(Clojure.KEY);
    FilePredicate main = predicates.hasType(InputFile.Type.MAIN);

    //TODO This is inaccurate. We need to properly count the lines of code, excluding spaces, comments, etc.
    //TODO This is here to make sure analysis data will show up in the Sonar UI.
    Iterable<InputFile> sources = context.fileSystem().inputFiles(predicates.and(clojure, main));

    for (InputFile source : sources) {
        LOG.info(source.toString());
        context.<Integer>newMeasure().withValue(source.lines()).forMetric(NCLOC).on(source).save();
    }
}
 
Example #2
Source File: LuaSquidSensor.java    From sonar-lua with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void execute(SensorContext context) {
  FileSystem fileSystem = context.fileSystem();
  FilePredicates predicates = fileSystem.predicates();
  List<SquidAstVisitor<LexerlessGrammar>> visitors = new ArrayList<>(checks.all());
  visitors.add(new FileLinesVisitor(fileLinesContextFactory, fileSystem));
  LuaConfiguration configuration = new LuaConfiguration(fileSystem.encoding());

  scanner = LuaAstScanner.create(configuration, visitors);

  Iterable<java.io.File> files = fileSystem.files(
    predicates.and(
      predicates.hasType(InputFile.Type.MAIN),
      predicates.hasLanguage(Lua.KEY),
      inputFile -> !inputFile.absolutePath().endsWith("mxml")
    ));
  scanner.scanFiles(ImmutableList.copyOf(files));

  Collection<SourceCode> squidSourceFiles = scanner.getIndex().search(new QueryByType(SourceFile.class));
  save(context, squidSourceFiles);
}
 
Example #3
Source File: PmdSensor.java    From sonar-p3c-pmd with MIT License 5 votes vote down vote up
private boolean hasFilesToCheck(Type type, String repositoryKey) {
  FilePredicates predicates = fs.predicates();
  Iterable<File> files = fs.files(predicates.and(
    predicates.hasLanguage(Java.KEY),
    predicates.hasType(type)));
  return !Iterables.isEmpty(files) && !profile.getActiveRulesByRepository(repositoryKey).isEmpty();
}
 
Example #4
Source File: CheckstyleConfiguration.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
public List<InputFile> getSourceFiles() {
    final FilePredicates predicates = fileSystem.predicates();
    final Iterable<InputFile> files = fileSystem.inputFiles(predicates.and(
            predicates.hasLanguage(CheckstyleConstants.JAVA_KEY),
            predicates.hasType(InputFile.Type.MAIN)));
    final List<InputFile> fileList = new ArrayList<>();
    for (InputFile file : files) {
        fileList.add(file);
    }
    return fileList;
}
 
Example #5
Source File: CheckstyleAuditListenerTest.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Before
public void before() {
    fileSystem = mock(FileSystem.class);
    context = mock(SensorContext.class);
    inputFile = mock(InputFile.class);
    ruleFinder = mock(ActiveRules.class);

    final FilePredicates predicates = mock(FilePredicates.class);
    final FilePredicate filePredicate = mock(FilePredicate.class);
    when(fileSystem.inputFile(any(FilePredicate.class))).thenReturn(inputFile);
    when(fileSystem.predicates()).thenReturn(predicates);
    when(predicates.hasAbsolutePath(anyString())).thenReturn(filePredicate);
}
 
Example #6
Source File: ApexSquidSensor.java    From enforce-sonarqube-plugin with MIT License 5 votes vote down vote up
/**
 * Default construct to initialize the variables.
 *
 * @param fileSystem source files.
 * @param perspectives perspective from resources.
 * @param checkFactory factory to create a check.
 */
public ApexSquidSensor(FileSystem fileSystem, ResourcePerspectives perspectives, CheckFactory checkFactory) {
    this.checks = checkFactory
            .<SquidAstVisitor<Grammar>>create(CheckList.REPOSITORY_KEY)
            .addAnnotatedChecks(CheckList.getChecks());
    this.fileSystem = fileSystem;
    this.resourcePerspectives = perspectives;

    FilePredicates predicates = fileSystem.predicates();
    filePredicate = predicates.and(
            predicates.hasType(InputFile.Type.MAIN),
            predicates.hasLanguage(Apex.KEY));
}
 
Example #7
Source File: HtlFilePredicateProvider.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
public static FilePredicate createFilePredicate(Configuration configuration, SensorContext sensorContext) {
    FilePredicates predicates = sensorContext.fileSystem().predicates();

    List<FilePredicate> fileExtensions = getFileExtensionsPredicates(predicates, configuration);
    List<FilePredicate> relativePaths = getPathsPredicate(predicates, configuration);

    return predicates.and(
            predicates.hasType(InputFile.Type.MAIN),
            predicates.or(
                    predicates.hasLanguages(Htl.KEY),
                    predicates.or(fileExtensions)
            ),
            predicates.or(relativePaths)
    );
}
 
Example #8
Source File: HtlFilePredicateProvider.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private static List<FilePredicate> getFileExtensionsPredicates(FilePredicates predicates, Configuration configuration) {
    return Stream.of(configuration.getStringArray(Constants.FILE_EXTENSIONS_PROP_KEY))
            .filter(Objects::nonNull)
            .map(extension -> StringUtils.removeStart(extension, "."))
            .map(predicates::hasExtension)
            .collect(Collectors.toList());
}
 
Example #9
Source File: HtlFilePredicateProvider.java    From AEM-Rules-for-SonarQube with Apache License 2.0 5 votes vote down vote up
private static List<FilePredicate> getPathsPredicate(FilePredicates predicates, Configuration configuration) {
    return Stream.of(configuration.getStringArray(Constants.HTL_FILES_RELATIVE_PATHS_KEY))
            .filter(StringUtils::isNotEmpty)
            .map(path -> StringUtils.wrapIfMissing(path, "**"))
            .map(predicates::matchesPathPattern)
            .collect(Collectors.toList());
}
 
Example #10
Source File: CoberturaReportParser.java    From sonar-lua with GNU Lesser General Public License v3.0 5 votes vote down vote up
private static void collectFileMeasures(SensorContext context, SMInputCursor clazz) throws XMLStreamException {
  FileSystem fileSystem = context.fileSystem();
  FilePredicates predicates = fileSystem.predicates();
  Map<String, InputFile> inputFileByFilename = new HashMap<>();
  while (clazz.getNext() != null) {
    String fileName = clazz.getAttrValue("filename");

    InputFile inputFile;
    // mxml files are not supported by the plugin
    if (inputFileByFilename.containsKey(fileName)) {
      inputFile = inputFileByFilename.get(fileName);
    } else {
      String key = fileName.startsWith(File.separator) ? fileName : (File.separator + fileName);
      inputFile = fileSystem.inputFile(predicates.and(
        predicates.matchesPathPattern("file:**" + key.replace(File.separator, "/")),
        predicates.hasType(InputFile.Type.MAIN),
        predicates.hasLanguage(Lua.KEY)));
      inputFileByFilename.put(fileName, inputFile);
      if (inputFile == null && !fileName.endsWith(".mxml")) {
        LOG.warn("Cannot save coverage result for file: {}, because resource not found.", fileName);
      }
    }
    if (inputFile != null) {
      collectFileData(
        clazz,
        context.newCoverage()
          .onFile(inputFile)
          .ofType(CoverageType.UNIT)
      );
    } else {
      SMInputCursor line = clazz.childElementCursor("lines").advance().childElementCursor("line");
      while (line.getNext() != null) {
        // advance
      }
    }
  }
}
 
Example #11
Source File: PmdExecutor.java    From sonar-p3c-pmd with MIT License 4 votes vote down vote up
public Iterable<File> javaFiles(Type fileType) {
  FilePredicates predicates = fs.predicates();
  return fs.files(predicates.and(
    predicates.hasLanguage(Java.KEY),
    predicates.hasType(fileType)));
}