Search in sources :

Example 6 with DefaultInputFile

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

the class DefaultSensorStorage method store.

@Override
public void store(DefaultSymbolTable symbolTable) {
    ScannerReportWriter writer = reportPublisher.getWriter();
    DefaultInputFile inputFile = (DefaultInputFile) symbolTable.inputFile();
    inputFile.setPublish(true);
    int componentRef = inputFile.batchId();
    if (writer.hasComponentData(FileStructure.Domain.SYMBOLS, componentRef)) {
        throw new UnsupportedOperationException("Trying to save symbol table twice for the same file is not supported: " + symbolTable.inputFile().absolutePath());
    }
    final ScannerReport.Symbol.Builder builder = ScannerReport.Symbol.newBuilder();
    final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder();
    writer.writeComponentSymbols(componentRef, symbolTable.getReferencesBySymbol().entrySet().stream().map(input -> {
        builder.clear();
        rangeBuilder.clear();
        TextRange declaration = input.getKey();
        builder.setDeclaration(rangeBuilder.setStartLine(declaration.start().line()).setStartOffset(declaration.start().lineOffset()).setEndLine(declaration.end().line()).setEndOffset(declaration.end().lineOffset()).build());
        for (TextRange reference : input.getValue()) {
            builder.addReference(rangeBuilder.setStartLine(reference.start().line()).setStartOffset(reference.start().lineOffset()).setEndLine(reference.end().line()).setEndOffset(reference.end().lineOffset()).build());
        }
        return builder.build();
    }).collect(Collectors.toList()));
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TextRange(org.sonar.api.batch.fs.TextRange) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter)

Example 7 with DefaultInputFile

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

the class DefaultSensorStorage method store.

@Override
public void store(DefaultHighlighting highlighting) {
    ScannerReportWriter writer = reportPublisher.getWriter();
    DefaultInputFile inputFile = (DefaultInputFile) highlighting.inputFile();
    inputFile.setPublish(true);
    int componentRef = inputFile.batchId();
    if (writer.hasComponentData(FileStructure.Domain.SYNTAX_HIGHLIGHTINGS, componentRef)) {
        throw new UnsupportedOperationException("Trying to save highlighting twice for the same file is not supported: " + inputFile.absolutePath());
    }
    final ScannerReport.SyntaxHighlightingRule.Builder builder = ScannerReport.SyntaxHighlightingRule.newBuilder();
    final ScannerReport.TextRange.Builder rangeBuilder = ScannerReport.TextRange.newBuilder();
    writer.writeComponentSyntaxHighlighting(componentRef, highlighting.getSyntaxHighlightingRuleSet().stream().map(input -> {
        builder.setRange(rangeBuilder.setStartLine(input.range().start().line()).setStartOffset(input.range().start().lineOffset()).setEndLine(input.range().end().line()).setEndOffset(input.range().end().lineOffset()).build());
        builder.setType(ScannerReportUtils.toProtocolType(input.getTextType()));
        return builder.build();
    }).collect(toList()));
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TextRange(org.sonar.api.batch.fs.TextRange) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter)

Example 8 with DefaultInputFile

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

the class InputComponentStore method put.

public InputComponentStore put(InputFile inputFile) {
    DefaultInputFile file = (DefaultInputFile) inputFile;
    addToLanguageCache(file);
    inputFileCache.put(file.moduleKey(), inputFile.relativePath(), inputFile);
    globalInputFileCache.put(getProjectRelativePath(file), inputFile);
    inputComponents.put(inputFile.key(), inputFile);
    filesByNameCache.put(FilenamePredicate.getFilename(inputFile), inputFile);
    filesByExtensionCache.put(FileExtensionPredicate.getExtension(inputFile), inputFile);
    return this;
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 9 with DefaultInputFile

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

the class InputFileBuilder method create.

@CheckForNull
DefaultInputFile create(Path file, InputFile.Type type, Charset defaultEncoding) {
    String relativePath = pathResolver.relativePath(moduleBaseDir, file);
    if (relativePath == null) {
        LOG.warn("File '{}' is ignored. It is not located in module basedir '{}'.", file.toAbsolutePath(), moduleBaseDir);
        return null;
    }
    DefaultIndexedFile indexedFile = new DefaultIndexedFile(moduleKey, moduleBaseDir, relativePath, type, idGenerator.get());
    String language = langDetection.language(indexedFile);
    if (language == null && langDetection.forcedLanguage() != null) {
        LOG.warn("File '{}' is ignored because it doens't belong to the forced langauge '{}'", file.toAbsolutePath(), langDetection.forcedLanguage());
        return null;
    }
    indexedFile.setLanguage(language);
    DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(f, defaultEncoding));
    if (language != null) {
        inputFile.setPublish(true);
    }
    return inputFile;
}
Also used : DefaultIndexedFile(org.sonar.api.batch.fs.internal.DefaultIndexedFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) CheckForNull(javax.annotation.CheckForNull)

Example 10 with DefaultInputFile

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

the class SameInputFilePredicate method test.

@Override
public boolean test(InputFile inputFile) {
    FileData fileDataPerPath = projectRepositories.fileData(moduleKey, inputFile.relativePath());
    if (fileDataPerPath == null) {
        // ADDED
        return true;
    }
    String previousHash = fileDataPerPath.hash();
    if (StringUtils.isEmpty(previousHash)) {
        // ADDED
        return true;
    }
    // this will trigger computation of metadata
    String hash = ((DefaultInputFile) inputFile).hash();
    if (StringUtils.equals(hash, previousHash)) {
        // SAME
        LOG.debug("'{}' filtering unmodified file", inputFile.relativePath());
        return false;
    }
    // CHANGED
    return true;
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) FileData(org.sonar.scanner.repository.FileData)

Aggregations

DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)173 Test (org.junit.Test)117 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)75 File (java.io.File)63 InputFile (org.sonar.api.batch.fs.InputFile)46 Path (java.nio.file.Path)25 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)18 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)17 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)16 FileMetadata (org.sonar.api.batch.fs.internal.FileMetadata)14 IOException (java.io.IOException)13 DefaultIndexedFile (org.sonar.api.batch.fs.internal.DefaultIndexedFile)11 Metadata (org.sonar.api.batch.fs.internal.Metadata)11 Before (org.junit.Before)10 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)10 BlameLine (org.sonar.api.batch.scm.BlameLine)9 List (java.util.List)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)8 ProjectDefinition (org.sonar.api.batch.bootstrap.ProjectDefinition)7 TextRange (org.sonar.api.batch.fs.TextRange)7