import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
+import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import java.io.File;
import java.nio.charset.Charset;
logger.info("Index files");
// TODO log configuration too (replace FileSystemLogger)
- IndexStatus status = new IndexStatus(cache.filePathsOfModule(fileSystem.moduleKey()));
+ IndexStatus status = new IndexStatus(cache.fileRelativePaths(fileSystem.moduleKey()));
for (File sourceDir : fileSystem.sourceDirs()) {
indexDirectory(fileSystem, status, sourceDir, InputFile.TYPE_SOURCE);
for (File file : files) {
String relativePath = pathResolver.relativePath(fileSystem.baseDir(), file);
if (!cache.containsFile(fileSystem.moduleKey(), relativePath)) {
- InputFile input = newInputFile(fileSystem, sourceDir, type, file);
- if (accept(input)) {
+ 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
+ private InputFile newInputFile(ModuleFileSystem fileSystem, File sourceDir, String type, File file, String baseRelativePath) {
+ // File extension must be kept case-sensitive
+ String extension = FilenameUtils.getExtension(file.getName());
+ String lang = languageRecognizer.ofExtension(extension);
+ if (lang == null) {
+ return null;
+ }
- private InputFile newInputFile(ModuleFileSystem fileSystem, File sourceDir, String type, File file) {
try {
Map<String, String> attributes = Maps.newHashMap();
+ set(attributes, InputFile.ATTRIBUTE_EXTENSION, extension);
set(attributes, InputFile.ATTRIBUTE_TYPE, type);
+ set(attributes, InputFile.ATTRIBUTE_LANGUAGE, lang);
// paths
- String baseRelativePath = pathResolver.relativePath(fileSystem.baseDir(), file);
set(attributes, InputFile.ATTRIBUTE_SOURCEDIR_PATH, FilenameUtils.normalize(sourceDir.getCanonicalPath(), true));
set(attributes, InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, pathResolver.relativePath(sourceDir, file));
- // File extension must be kept case-sensitive
- String extension = FilenameUtils.getExtension(file.getName());
- set(attributes, InputFile.ATTRIBUTE_EXTENSION, extension);
- set(attributes, InputFile.ATTRIBUTE_LANGUAGE, languageRecognizer.ofExtension(extension));
-
// hash + status
initStatus(file, fileSystem.sourceCharset(), baseRelativePath, attributes);
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
+import org.sonar.api.scan.filesystem.InputFile;
import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
import org.sonar.batch.index.Caches;
assertThat(cache.byModule("struts")).hasSize(1);
assertThat(cache.byModule("struts-core")).hasSize(1);
assertThat(cache.all()).hasSize(2);
+ for (InputFile inputFile : cache.all()) {
+ assertThat(inputFile.relativePath()).startsWith("src/main/java");
+ }
+ assertThat(cache.fileRelativePaths("struts-core")).containsOnly("src/main/java/Foo.java");
cache.removeModule("struts");
assertThat(cache.byModule("struts")).hasSize(0);