private static void analyse(SensorContext context) { FileSystem fs = context.fileSystem(); FilePredicates p = fs.predicates(); RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY); StreamSupport.stream(fs.inputFiles(p.hasType(Type.MAIN)).spliterator(), false) .map(file -> fs.inputDir(file.file().getParentFile())) .filter(Objects::nonNull) .distinct() .forEach(inputDir -> { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(inputDir) .message("This issue is generated for any non-empty directory")) .save(); }); } }
private void processFileHighlighting(InputFile inputFile, SensorContext context) { File ioFile = inputFile.file(); File highlightingFile = new File(ioFile.getParentFile(), ioFile.getName() + HIGHLIGHTING_EXTENSION); if (highlightingFile.exists()) { LOG.debug("Processing " + highlightingFile.getAbsolutePath()); try { List<String> lines = FileUtils.readLines(highlightingFile, context.fileSystem().encoding().name()); int lineNumber = 0; NewHighlighting highlighting = context.newHighlighting() .onFile(inputFile); for (String line : lines) { lineNumber++; if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } processLine(highlightingFile, lineNumber, highlighting, line); } highlighting.save(); } catch (IOException e) { throw new IllegalStateException(e); } } }
@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(InputFile.Type.MAIN)))) { createIssues(file, context); } if (context.config().getBoolean(REGISTER_AD_HOC_RULE).orElse(false)) { context.newAdHocRule() .engineId(ENGINE_ID) .ruleId(RULE_ID) .name("An ad hoc rule") .description("blah blah") .severity(Severity.BLOCKER) .type(RuleType.BUG) .save(); } }
@Override public void execute(SensorContext context) { RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY); FilePredicate unknownFilesPredicate = context.fileSystem().predicates().matchesPathPattern("**/*.unknown"); Iterable<InputFile> unknownFiles = context.fileSystem().inputFiles(unknownFilesPredicate); unknownFiles.forEach(inputFile -> { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(inputFile) .message("This issue is generated on each file with extension 'unknown'")) .save(); }); }
@Override public void execute(SensorContext context) { for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY, Xoo2.KEY))) { File ioFile = file.file(); File measureFile = new File(ioFile.getParentFile(), ioFile.getName() + MEASURES_EXTENSION); processFileMeasures(file, measureFile, context); InputDir inputDir = context.fileSystem().inputDir(ioFile.getParentFile()); if (inputDir != null) { processFileMeasures(inputDir, new File(ioFile.getParentFile(), "folder" + MEASURES_EXTENSION), context); } } processFileMeasures(context.module(), new File(context.fileSystem().baseDir(), "module" + MEASURES_EXTENSION), context); } }
private static void createIssues(InputFile file, SensorContext context) { RuleKey ruleKey = RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY); InputDir inputDir = context.fileSystem().inputDir(file.file().getParentFile()); if (inputDir != null) { NewIssue newIssue = context.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(inputDir) .message("This issue is generated for file " + file.relativePath())) .save(); } } }
@Override public void execute(SensorContext sensorContext, InputFile file, RuleKey ruleKey) { NewIssue newIssue = sensorContext.newIssue(); newIssue .forRule(ruleKey) .at(newIssue.newLocation() .on(file) .at(file.selectLine(line))) .save(); }
@Override protected void processFile(InputFile inputFile, SensorContext context, RuleKey ruleKey, String languageKey) { NewIssue newIssue = context.newIssue() .forRule(ruleKey); newIssue.at(newIssue.newLocation() .on(inputFile) .message("This issue is generated on each file with a debt of one day")) .save(); }
@Override public void execute(SensorContext context) { FilePredicates p = context.fileSystem().predicates(); for (InputFile file : context.fileSystem().inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(Type.MAIN)))) { tokenize(file, context); } } }
@Override public void execute(SensorContext context) { context.fileSystem().inputFiles(context.fileSystem().predicates().all()).forEach(inputFile -> LOG.info("Deprecated Global Sensor: {}", inputFile.relativePath())); } }
@Override public void execute(SensorContext context) { Iterator<InputFile> inputFiles = context.fileSystem().inputFiles(context.fileSystem().predicates().all()).iterator(); if (!inputFiles.hasNext()) { throw new IllegalStateException("No files indexed"); } InputFile file = inputFiles.next(); context.newHighlighting() .onFile(file) .highlight(file.selectLine(1), TypeOfText.CONSTANT) .save(); context.newHighlighting() .onFile(file) .highlight(file.selectLine(file.lines()), TypeOfText.COMMENT) .save(); } }
@Override public void execute(SensorContext context) { for (InputFile file : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguages(Xoo.KEY))) { processFileMeasures(file, context); } } }
private void parseFiles(SMInputCursor fileCursor, SensorContext context) throws XMLStreamException { while (fileCursor.getNext() != null) { checkElementName(fileCursor, "file"); String filePath = mandatoryAttribute(fileCursor, "path"); InputFile inputFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(filePath)); if (inputFile == null) { numberOfUnknownFiles++; if (numberOfUnknownFiles <= MAX_STORED_UNKNOWN_FILE_PATHS) { firstUnknownFiles.add(filePath); } continue; } Preconditions.checkState( inputFile.language() != null, "Line %s of report refers to a file with an unknown language: %s", fileCursor.getCursorLocation().getLineNumber(), filePath); matchedFileKeys.add(inputFile.key()); NewCoverage newCoverage = context.newCoverage().onFile(inputFile); SMInputCursor lineToCoverCursor = fileCursor.childElementCursor(); while (lineToCoverCursor.getNext() != null) { parseLineToCover(lineToCoverCursor, newCoverage); } newCoverage.save(); } }
@Override public void execute(SensorContext context) { FileSystem fs = context.fileSystem(); // only "main" files, but not "tests" Iterable<InputFile> files = fs.inputFiles(fs.predicates().hasType(InputFile.Type.MAIN)); for (InputFile file : files) { context.<Integer>newMeasure() .forMetric(FILENAME_SIZE) .on(file) .withValue(file.file().getName().length()) .save(); } } }
@Override public void execute(final SensorContext context) { FileSystem fs = context.fileSystem(); for (InputFile f : fs.inputFiles(fs.predicates().all())) { BatchReportReader reader = new BatchReportReader(reportPublisher.getReportDir()); int batchId = resourceCache.get(f).batchId(); String language = f.language(); if (reader.hasSyntaxHighlighting(batchId) || language == null) { continue; } codeColorizers.toSyntaxHighlighting(f.file(), fs.encoding(), language, context.newHighlighting().onFile(f)); } }
@Override public void execute(final SensorContext context) { FileSystem fs = context.fileSystem(); for (InputFile f : fs.inputFiles(fs.predicates().hasType(Type.MAIN))) { if (((DefaultInputFile) f).isExcludedForCoverage()) { continue; } if (!isCoverageMeasuresAlreadyDefined(f)) { DefaultMeasure<String> execLines = (DefaultMeasure<String>) measureCache.byMetric(f.key(), CoreMetrics.EXECUTABLE_LINES_DATA_KEY); if (execLines != null) { storeZeroCoverageForEachExecutableLine(context, f, execLines); } } } }
@Override public void execute(SensorContext context) { if (configuration.getOriginalProperties().containsKey(OLD_UNIT_TEST_REPORT_PATHS_PROPERTY_KEY)) { LOG.warn("Property '{}' is deprecated. Please use '{}' instead.", OLD_UNIT_TEST_REPORT_PATHS_PROPERTY_KEY, REPORT_PATHS_PROPERTY_KEY); } for (String reportPath : configuration.getStringArray(REPORT_PATHS_PROPERTY_KEY)) { File reportFile = context.fileSystem().resolvePath(reportPath); LOG.info("Parsing {}", reportFile); GenericTestExecutionReportParser parser = new GenericTestExecutionReportParser(testPlanBuilder); parser.parse(reportFile, context); LOG.info("Imported test execution data for {} files", parser.numberOfMatchedFiles()); int numberOfUnknownFiles = parser.numberOfUnknownFiles(); if (numberOfUnknownFiles > 0) { LOG.info("Test execution data ignored for {} unknown files, including:\n{}", numberOfUnknownFiles, parser.firstUnknownFiles().stream().collect(Collectors.joining("\n"))); } } }
@Override public void visitFile(Tree tree) { highlighting = sensorContext.newHighlighting().onFile(fileSystem.inputFile(fileSystem.predicates().is(getContext().getFile()))); }
private static void saveFileCoverage(SensorContext sensorContext, String filePath, List<CoverageStat> coverageStats) throws IOException { FileSystem fileSystem = sensorContext.fileSystem(); InputFile inputFile = findInputFile(filePath, fileSystem); if (inputFile != null) { LOG.debug("Saving coverage measures for file '{}'", filePath); List<String> lines = Arrays.asList(inputFile.contents().split("\\r?\\n")); NewCoverage newCoverage = sensorContext.newCoverage().onFile(inputFile); FileCoverage fileCoverage = new FileCoverage(coverageStats, lines); for (Map.Entry<Integer, LineCoverage> entry : fileCoverage.lineMap.entrySet()) { newCoverage.lineHits(entry.getKey(), entry.getValue().hits); } newCoverage.save(); } else { LOG.warn("File '{}' is not included in the project, ignoring coverage", filePath); } }
@Override public void visitFile(Tree scriptTree) { File file = getContext().getFile(); inputFile = fileSystem.inputFile(fileSystem.predicates().is(file)); cpdTokens = sensorContext.newCpdTokens().onFile(inputFile); }