diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-10 15:35:46 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-10 15:35:46 +0200 |
commit | f11d84b4c45711fb168aecc047332051ede8aeea (patch) | |
tree | 9ff9cf416973e6fc3bc309765e6e4b90e8c55fe3 /sonar-batch | |
parent | 71d0fab1ebb6082bb0178da61c6b73e51e651b1f (diff) | |
download | sonarqube-f11d84b4c45711fb168aecc047332051ede8aeea.tar.gz sonarqube-f11d84b4c45711fb168aecc047332051ede8aeea.zip |
SONAR-3677 fix support of list of source files (C#)
Diffstat (limited to 'sonar-batch')
3 files changed, 59 insertions, 32 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java index 35158ab08f4..9045cb7b8f0 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java @@ -53,8 +53,8 @@ public class DefaultModuleFileSystem implements ModuleFileSystem, Startable { private List<File> sourceDirs = Lists.newArrayList(); private List<File> testDirs = Lists.newArrayList(); private List<File> binaryDirs = Lists.newArrayList(); - private List<File> additionalSourceFiles = Lists.newArrayList(); - private List<File> additionalTestFiles = Lists.newArrayList(); + private List<File> sourceFiles = Lists.newArrayList(); + private List<File> testFiles = Lists.newArrayList(); public DefaultModuleFileSystem(ProjectDefinition module, Settings settings, FileIndex index, ModuleFileSystemInitializer initializer) { this(module.getKey(), settings, index, initializer); @@ -71,8 +71,8 @@ public class DefaultModuleFileSystem implements ModuleFileSystem, Startable { this.sourceDirs = initializer.sourceDirs(); this.testDirs = initializer.testDirs(); this.binaryDirs = initializer.binaryDirs(); - this.additionalSourceFiles = initializer.additionalSourceFiles(); - this.additionalTestFiles = initializer.additionalTestFiles(); + this.sourceFiles = initializer.additionalSourceFiles(); + this.testFiles = initializer.additionalTestFiles(); } @Override @@ -111,12 +111,12 @@ public class DefaultModuleFileSystem implements ModuleFileSystem, Startable { return workingDir; } - List<File> additionalSourceFiles() { - return additionalSourceFiles; + List<File> sourceFiles() { + return sourceFiles; } - List<File> additionalTestFiles() { - return additionalTestFiles; + List<File> testFiles() { + return testFiles; } @Override diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndex.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndex.java index 944bf3d973f..0d2a4d415a8 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndex.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndex.java @@ -49,17 +49,17 @@ import java.util.Set; */ public class FileIndex implements BatchComponent { - private static class IndexStatus { + private static class Progress { private int count = 0; - private final Set<String> orphans; + private final Set<String> removedRelativePaths; - IndexStatus(Set<String> orphans) { - this.orphans = orphans; + Progress(Set<String> removedRelativePaths) { + this.removedRelativePaths = removedRelativePaths; } void markAsIndexed(String relativePath) { count++; - orphans.remove(relativePath); + removedRelativePaths.remove(relativePath); } } @@ -85,40 +85,67 @@ public class FileIndex implements BatchComponent { logger.info("Index files"); // TODO log configuration too (replace FileSystemLogger) - IndexStatus status = new IndexStatus(cache.fileRelativePaths(fileSystem.moduleKey())); + Progress progress = new Progress(cache.fileRelativePaths(fileSystem.moduleKey())); - for (File sourceDir : fileSystem.sourceDirs()) { - indexDirectory(fileSystem, status, sourceDir, InputFile.TYPE_SOURCE); - } - for (File testDir : fileSystem.testDirs()) { - indexDirectory(fileSystem, status, testDir, InputFile.TYPE_TEST); + if (fileSystem.sourceFiles().isEmpty()) { + // index directories + for (File sourceDir : fileSystem.sourceDirs()) { + indexDirectory(fileSystem, progress, sourceDir, InputFile.TYPE_SOURCE); + } + } else { + // index only given files + indexFiles(fileSystem, progress, fileSystem.sourceDirs(), fileSystem.sourceFiles(), InputFile.TYPE_SOURCE); } - // TODO index additional sources and test files + if (fileSystem.testFiles().isEmpty()) { + // index directories + for (File testDir : fileSystem.testDirs()) { + indexDirectory(fileSystem, progress, testDir, InputFile.TYPE_TEST); + } + } else { + // index only given files + indexFiles(fileSystem, progress, fileSystem.testDirs(), fileSystem.testFiles(), InputFile.TYPE_TEST); + } - for (String relativePath : status.orphans) { + // Remove files that have been removed since previous indexation + for (String relativePath : progress.removedRelativePaths) { cache.remove(fileSystem.moduleKey(), relativePath); } - logger.info(String.format("%d files indexed", status.count)); + logger.info(String.format("%d files indexed", progress.count)); + } + + private void indexFiles(DefaultModuleFileSystem fileSystem, Progress progress, List<File> sourceDirs, List<File> sourceFiles, String type) { + for (File sourceFile : sourceFiles) { + PathResolver.RelativePath sourceDirPath = pathResolver.relativePath(sourceDirs, sourceFile); + if (sourceDirPath == null) { + LoggerFactory.getLogger(getClass()).warn(String.format("File %s is not declared in give source directories", sourceFile.getAbsoluteFile())); + } else { + indexFile(fileSystem, progress, sourceDirPath.dir(), sourceFile, type); + } + } } Iterable<InputFile> inputFiles(String moduleKey) { return cache.byModule(moduleKey); } - private void indexDirectory(ModuleFileSystem fileSystem, IndexStatus status, File sourceDir, String type) { + private void indexDirectory(ModuleFileSystem fileSystem, Progress status, File sourceDir, String type) { Collection<File> files = FileUtils.listFiles(sourceDir, FILE_FILTER, DIR_FILTER); for (File file : files) { - String relativePath = pathResolver.relativePath(fileSystem.baseDir(), file); - if (!cache.containsFile(fileSystem.moduleKey(), relativePath)) { - InputFile input = newInputFile(fileSystem, sourceDir, type, file, relativePath); - if (input != null && accept(input)) { - cache.put(fileSystem.moduleKey(), input); - } + indexFile(fileSystem, status, sourceDir, file, type); + } + } + + private void indexFile(ModuleFileSystem fileSystem, Progress status, File sourceDir, File file, String type) { + String relativePath = pathResolver.relativePath(fileSystem.baseDir(), file); + if (!cache.containsFile(fileSystem.moduleKey(), relativePath)) { + InputFile input = newInputFile(fileSystem, sourceDir, type, file, relativePath); + if (input != null && accept(input)) { + cache.put(fileSystem.moduleKey(), input); } - status.markAsIndexed(relativePath); } + status.markAsIndexed(relativePath); } @CheckForNull diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java index c0633e901f6..b7948e7f5bd 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java @@ -112,8 +112,8 @@ public class DefaultModuleFileSystemTest { assertThat(fs.sourceDirs()).hasSize(2); assertThat(fs.testDirs()).hasSize(1); assertThat(fs.binaryDirs()).hasSize(1); - assertThat(fs.additionalSourceFiles()).containsOnly(additionalFile); - assertThat(fs.additionalTestFiles()).containsOnly(additionalTest); + assertThat(fs.sourceFiles()).containsOnly(additionalFile); + assertThat(fs.testFiles()).containsOnly(additionalTest); } @Test |