Search in sources :

Example 31 with FilePredicates

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

the class PythonSensor method getInputFiles.

private static List<InputFile> getInputFiles(InputFile.Type type, SensorContext context) {
    FilePredicates p = context.fileSystem().predicates();
    Iterable<InputFile> it = context.fileSystem().inputFiles(p.and(p.hasType(type), p.hasLanguage(Python.KEY)));
    List<InputFile> list = new ArrayList<>();
    it.forEach(list::add);
    return Collections.unmodifiableList(list);
}
Also used : FilePredicates(org.sonar.api.batch.fs.FilePredicates) ArrayList(java.util.ArrayList) InputFile(org.sonar.api.batch.fs.InputFile)

Example 32 with FilePredicates

use of org.sonar.api.batch.fs.FilePredicates in project sonar-bsl-plugin-community by 1c-syntax.

the class BSLCoreSensor method execute.

@Override
public void execute(SensorContext context) {
    LOGGER.info("Parsing files...");
    FileSystem fileSystem = context.fileSystem();
    File baseDir = fileSystem.baseDir();
    var absoluteSourceDirs = sourcesList.stream().map((String sourceDir) -> {
        Path sourcePath = Path.of(sourceDir.trim());
        if (sourcePath.isAbsolute()) {
            return sourcePath;
        } else {
            return Path.of(baseDir.toString(), sourceDir);
        }
    }).map(Absolute::path).collect(Collectors.toList());
    FilePredicates predicates = fileSystem.predicates();
    Iterable<InputFile> inputFiles = fileSystem.inputFiles(predicates.hasLanguage(BSLLanguage.KEY));
    Map<Path, List<InputFile>> inputFilesByPath = StreamSupport.stream(inputFiles.spliterator(), true).collect(Collectors.groupingBy((InputFile inputFile) -> {
        var filePath = Absolute.path(inputFile.uri());
        return absoluteSourceDirs.stream().filter(filePath::startsWith).findAny().orElse(baseDir.toPath());
    }));
    LanguageServerConfiguration languageServerConfiguration = getLanguageServerConfiguration();
    inputFilesByPath.forEach((Path sourceDir, List<InputFile> inputFilesList) -> {
        LOGGER.info("Source dir: {}", sourceDir);
        Path configurationRoot = LanguageServerConfiguration.getCustomConfigurationRoot(languageServerConfiguration, sourceDir);
        var bslServerContext = BSLLSBinding.getServerContext();
        bslServerContext.setConfigurationRoot(configurationRoot);
        bslServerContext.populateContext();
        try (ProgressBar pb = new ProgressBarBuilder().setTaskName("").setInitialMax(inputFilesList.size()).setStyle(ProgressBarStyle.ASCII).build()) {
            inputFilesList.parallelStream().forEach((InputFile inputFile) -> {
                URI uri = inputFile.uri();
                LOGGER.debug(uri.toString());
                pb.step();
                processFile(inputFile, bslServerContext);
            });
        }
        bslServerContext.clear();
    });
    BSLLSBinding.getApplicationContext().close();
}
Also used : Path(java.nio.file.Path) LanguageServerConfiguration(com.github._1c_syntax.bsl.languageserver.configuration.LanguageServerConfiguration) URI(java.net.URI) InputFile(org.sonar.api.batch.fs.InputFile) ProgressBarBuilder(me.tongfei.progressbar.ProgressBarBuilder) FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) List(java.util.List) ArrayList(java.util.ArrayList) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) ProgressBar(me.tongfei.progressbar.ProgressBar)

Example 33 with FilePredicates

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

the class JavaCpdBlockIndexerSensor method execute.

@Override
public void execute(SensorContext context) {
    FilePredicates p = context.fileSystem().predicates();
    List<InputFile> sourceFiles = StreamSupport.stream(context.fileSystem().inputFiles(p.and(p.hasType(InputFile.Type.MAIN), p.hasLanguage("java"))).spliterator(), false).filter(f -> !((DefaultInputFile) f).isExcludedForDuplication()).collect(Collectors.toList());
    if (sourceFiles.isEmpty()) {
        return;
    }
    createIndex(sourceFiles);
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) SonarCpdBlockIndex(org.sonar.scanner.cpd.index.SonarCpdBlockIndex) Block(org.sonar.duplications.block.Block) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) LoggerFactory(org.slf4j.LoggerFactory) FilePredicates(org.sonar.api.batch.fs.FilePredicates) BlockChunker(org.sonar.duplications.block.BlockChunker) JavaTokenProducer(org.sonar.duplications.java.JavaTokenProducer) JavaStatementBuilder(org.sonar.duplications.java.JavaStatementBuilder) StreamSupport(java.util.stream.StreamSupport) Phase(org.sonar.api.batch.Phase) Logger(org.slf4j.Logger) StatementChunker(org.sonar.duplications.statement.StatementChunker) IOException(java.io.IOException) Reader(java.io.Reader) ProjectSensor(org.sonar.api.scanner.sensor.ProjectSensor) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) SensorContext(org.sonar.api.batch.sensor.SensorContext) TokenChunker(org.sonar.duplications.token.TokenChunker) FileNotFoundException(java.io.FileNotFoundException) Statement(org.sonar.duplications.statement.Statement) SensorDescriptor(org.sonar.api.batch.sensor.SensorDescriptor) List(java.util.List) InputStream(java.io.InputStream) FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 34 with FilePredicates

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

the class CreateIssueByInternalKeySensor method execute.

@Override
public void execute(SensorContext context) {
    FileSystem fs = context.fileSystem();
    FilePredicates p = fs.predicates();
    for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(Type.MAIN)))) {
        createIssues(file, context);
    }
}
Also used : FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) InputFile(org.sonar.api.batch.fs.InputFile)

Example 35 with FilePredicates

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

the class OneBugIssuePerLineSensor method analyse.

private void analyse(SensorContext context, String language, String repo) {
    FileSystem fs = context.fileSystem();
    FilePredicates p = fs.predicates();
    for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(language), p.hasType(Type.MAIN)))) {
        createIssues(file, context, repo);
    }
}
Also used : FileSystem(org.sonar.api.batch.fs.FileSystem) FilePredicates(org.sonar.api.batch.fs.FilePredicates) 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