Search in sources :

Example 21 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-openedge by Riverside-Software.

the class OpenEdgeWarningsSensor method execute.

@Override
public void execute(SensorContext context) {
    if (context.runtime().getProduct() == SonarProduct.SONARLINT)
        return;
    settings.init();
    int warningsImportNum = 0;
    final RuleKey defaultWarningRuleKey = RuleKey.of(Constants.STD_REPOSITORY_KEY, OpenEdgeRulesDefinition.COMPILER_WARNING_RULEKEY);
    if (context.activeRules().find(defaultWarningRuleKey) == null) {
        LOG.info("'Compiler warning' rule is not activated in your profile - Warning files analysis skipped");
        return;
    }
    FilePredicates predicates = context.fileSystem().predicates();
    for (InputFile file : context.fileSystem().inputFiles(predicates.and(predicates.hasLanguage(Constants.LANGUAGE_KEY), predicates.hasType(Type.MAIN)))) {
        LOG.debug("Looking for warnings of {}", file);
        processFile(context, file);
        warningsImportNum++;
        if (context.isCancelled()) {
            LOG.info("Analysis cancelled...");
            return;
        }
    }
    LOG.info("{} warning files imported", warningsImportNum);
}
Also used : RuleKey(org.sonar.api.rule.RuleKey) FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile)

Example 22 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-web by SonarSource.

the class WebSensor method execute.

@Override
public void execute(SensorContext sensorContext) {
    // configure the lexer
    final PageLexer lexer = new PageLexer();
    FileSystem fileSystem = sensorContext.fileSystem();
    // configure page scanner and the visitors
    final HtmlAstScanner scanner = setupScanner(sensorContext);
    FilePredicates predicates = fileSystem.predicates();
    Iterable<InputFile> inputFiles = fileSystem.inputFiles(predicates.and(predicates.hasType(InputFile.Type.MAIN), predicates.hasLanguage(WebConstants.LANGUAGE_KEY)));
    for (InputFile inputFile : inputFiles) {
        WebSourceCode sourceCode = new WebSourceCode(inputFile);
        try (FileReader reader = new FileReader(inputFile.file())) {
            scanner.scan(lexer.parse(reader), sourceCode, fileSystem.encoding());
            saveMetrics(sensorContext, sourceCode);
            saveLineLevelMeasures(inputFile, sourceCode);
        } catch (Exception e) {
            LOG.error("Cannot analyze file " + inputFile.file().getAbsolutePath(), e);
        }
    }
}
Also used : PageLexer(org.sonar.plugins.web.lex.PageLexer) FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) WebSourceCode(org.sonar.plugins.web.visitor.WebSourceCode) FileReader(java.io.FileReader) HtmlAstScanner(org.sonar.plugins.web.visitor.HtmlAstScanner) InputFile(org.sonar.api.batch.fs.InputFile)

Example 23 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-go by SonarSource.

the class AbstractReportSensor method getInputFile.

InputFile getInputFile(SensorContext context, String filePath) {
    FilePredicates predicates = context.fileSystem().predicates();
    InputFile inputFile = context.fileSystem().inputFile(predicates.or(predicates.hasRelativePath(filePath), predicates.hasAbsolutePath(filePath)));
    if (inputFile == null) {
        LOG.warn(logPrefix() + "No input file found for {}. No {} issues will be imported on this file.", filePath, linterName());
        return null;
    }
    return inputFile;
}
Also used : FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile)

Example 24 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-go by SonarSource.

the class GoCoverageReport method findInputFile.

/**
 *  It is possible that absolutePath references a file that does not exist in the file system.
 *  It happens when go tests where executed on a different computer.
 *  Even when absolute path does not match a file of the project, this method try to find a valid
 *  mach using a shorter relative path.
 *  @see <a href="https://github.com/SonarSource/sonar-go/issues/218">sonar-go/issues/218</a>
 */
private static InputFile findInputFile(String absolutePath, FileSystem fileSystem) {
    FilePredicates predicates = fileSystem.predicates();
    InputFile inputFile = fileSystem.inputFile(predicates.hasAbsolutePath(absolutePath));
    if (inputFile != null) {
        return inputFile;
    }
    LOG.debug("Resolving file {} using relative path", absolutePath);
    Path path = Paths.get(absolutePath);
    inputFile = fileSystem.inputFile(predicates.hasRelativePath(path.toString()));
    while (inputFile == null && path.getNameCount() > 1) {
        path = path.subpath(1, path.getNameCount());
        inputFile = fileSystem.inputFile(predicates.hasRelativePath(path.toString()));
    }
    return inputFile;
}
Also used : Path(java.nio.file.Path) FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile)

Example 25 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-java by SonarSource.

the class Jasper method findWebInfParentDirectory.

private static Optional<Path> findWebInfParentDirectory(FileSystem fs) {
    FilePredicates predicates = fs.predicates();
    List<InputFile> inputFiles = new ArrayList<>();
    fs.inputFiles(predicates.matchesPathPattern("**/WEB-INF/**")).forEach(inputFiles::add);
    if (!inputFiles.isEmpty()) {
        Path path = Paths.get(inputFiles.get(0).absolutePath());
        Path parent = path.getParent();
        while (parent != null) {
            if (parent.endsWith("WEB-INF")) {
                return Optional.ofNullable(parent.getParent());
            }
            parent = parent.getParent();
        }
    }
    LOG.debug("WEB-INF directory not found, will use basedir as context root");
    return Optional.empty();
}
Also used : Path(java.nio.file.Path) FilePredicates(org.sonar.api.batch.fs.FilePredicates) ArrayList(java.util.ArrayList) InputFile(org.sonar.api.batch.fs.InputFile)

Aggregations

FilePredicates (org.sonar.api.batch.fs.FilePredicates)43 InputFile (org.sonar.api.batch.fs.InputFile)38 FileSystem (org.sonar.api.batch.fs.FileSystem)22 File (java.io.File)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Path (java.nio.file.Path)4 FilePredicate (org.sonar.api.batch.fs.FilePredicate)4 SensorContext (org.sonar.api.batch.sensor.SensorContext)4 RuleKey (org.sonar.api.rule.RuleKey)4 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)3 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)3 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 StreamSupport (java.util.stream.StreamSupport)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ActiveRule (org.sonar.api.batch.rule.ActiveRule)2