project = true,
global = false,
category = CoreProperties.CATEGORY_SCM
+ ),
+ @Property(
+ key = CoreProperties.INDEX_ALL_FILES_KEY,
+ defaultValue = "false",
+ name = "Index all files",
+ description = "Should all files be indexed even if there is no matching language plugin installed.",
+ module = false,
+ project = true,
+ global = true,
+ category = CoreProperties.CATEGORY_GENERAL
)
})
public final class CorePlugin extends SonarPlugin {
String newData = getSourceData(inputFile);
String newDataHash = newData != null ? DigestUtils.md5Hex(newData) : "0";
Date now = system2.newDate();
- if (previous == null) {
- FileSourceDto newFileSource = new FileSourceDto()
- .setProjectUuid(projectTree.getRootProject().getUuid())
- .setFileUuid(fileUuid)
- .setData(newData)
- .setDataHash(newDataHash)
- .setSrcHash(inputFile.hash())
- .setLineHashes(lineHashesAsMd5Hex(inputFile))
- .setCreatedAt(now.getTime())
- .setUpdatedAt(now.getTime());
- mapper.insert(newFileSource);
- session.commit();
- } else {
- if (!newDataHash.equals(previous.getDataHash())) {
- previous
+ try {
+ if (previous == null) {
+ FileSourceDto newFileSource = new FileSourceDto()
+ .setProjectUuid(projectTree.getRootProject().getUuid())
+ .setFileUuid(fileUuid)
.setData(newData)
- .setLineHashes(lineHashesAsMd5Hex(inputFile))
.setDataHash(newDataHash)
.setSrcHash(inputFile.hash())
+ .setLineHashes(lineHashesAsMd5Hex(inputFile))
+ .setCreatedAt(now.getTime())
.setUpdatedAt(now.getTime());
- mapper.update(previous);
+ mapper.insert(newFileSource);
session.commit();
+ } else {
+ if (!newDataHash.equals(previous.getDataHash())) {
+ previous
+ .setData(newData)
+ .setLineHashes(lineHashesAsMd5Hex(inputFile))
+ .setDataHash(newDataHash)
+ .setSrcHash(inputFile.hash())
+ .setUpdatedAt(now.getTime());
+ mapper.update(previous);
+ session.commit();
+ }
}
+ } catch (Exception e) {
+ throw new IllegalStateException("Unable to save file sources for " + inputPath.absolutePath(), e);
}
}
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.sonar.api.CoreProperties;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile;
+import org.sonar.api.config.Settings;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.batch.bootstrap.AnalysisMode;
import org.sonar.batch.util.DeprecatedKeyUtils;
private final StatusDetection statusDetection;
private final DefaultModuleFileSystem fs;
private final AnalysisMode analysisMode;
+ private final Settings settings;
InputFileBuilder(String moduleKey, PathResolver pathResolver, LanguageDetection langDetection,
- StatusDetection statusDetection, DefaultModuleFileSystem fs, AnalysisMode analysisMode) {
+ StatusDetection statusDetection, DefaultModuleFileSystem fs, AnalysisMode analysisMode, Settings settings) {
this.moduleKey = moduleKey;
this.pathResolver = pathResolver;
this.langDetection = langDetection;
this.statusDetection = statusDetection;
this.fs = fs;
this.analysisMode = analysisMode;
+ this.settings = settings;
}
String moduleKey() {
inputFile.setEncoding(fs.encoding().name());
String lang = langDetection.language(inputFile);
- if (lang == null) {
+ if (lang == null && !settings.getBoolean(CoreProperties.INDEX_ALL_FILES_KEY)) {
return null;
}
inputFile.setLanguage(lang);
import org.sonar.api.BatchComponent;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.config.Settings;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.batch.bootstrap.AnalysisMode;
private final LanguageDetectionFactory langDetectionFactory;
private final StatusDetectionFactory statusDetectionFactory;
private final AnalysisMode analysisMode;
+ private final Settings settings;
public InputFileBuilderFactory(ProjectDefinition def, PathResolver pathResolver, LanguageDetectionFactory langDetectionFactory,
- StatusDetectionFactory statusDetectionFactory, AnalysisMode analysisMode) {
- this(def.getKeyWithBranch(), pathResolver, langDetectionFactory, statusDetectionFactory, analysisMode);
+ StatusDetectionFactory statusDetectionFactory, AnalysisMode analysisMode, Settings settings) {
+ this(def.getKeyWithBranch(), pathResolver, langDetectionFactory, statusDetectionFactory, analysisMode, settings);
}
private InputFileBuilderFactory(String effectiveKey, PathResolver pathResolver, LanguageDetectionFactory langDetectionFactory,
- StatusDetectionFactory statusDetectionFactory, AnalysisMode analysisMode) {
+ StatusDetectionFactory statusDetectionFactory, AnalysisMode analysisMode, Settings settings) {
this.moduleKey = effectiveKey;
this.pathResolver = pathResolver;
this.langDetectionFactory = langDetectionFactory;
this.statusDetectionFactory = statusDetectionFactory;
this.analysisMode = analysisMode;
+ this.settings = settings;
}
InputFileBuilder create(DefaultModuleFileSystem fs) {
- return new InputFileBuilder(moduleKey, pathResolver, langDetectionFactory.create(), statusDetectionFactory.create(), fs, analysisMode);
+ return new InputFileBuilder(moduleKey, pathResolver, langDetectionFactory.create(), statusDetectionFactory.create(), fs, analysisMode, settings);
}
}
*/
package org.sonar.batch.mediumtest.fs;
-import org.sonar.batch.mediumtest.TaskResult;
-
import com.google.common.collect.ImmutableMap;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.MessageException;
import org.sonar.api.utils.System2;
import org.sonar.batch.mediumtest.BatchMediumTester;
+import org.sonar.batch.mediumtest.TaskResult;
import org.sonar.xoo.XooPlugin;
import java.io.File;
}
}
+ @Test
+ public void indexAnyFile() throws IOException {
+ File srcDir = new File(baseDir, "src");
+ srcDir.mkdir();
+
+ File xooFile = new File(srcDir, "sample.xoo");
+ FileUtils.write(xooFile, "Sample xoo\ncontent");
+
+ File otherFile = new File(srcDir, "sample.other");
+ FileUtils.write(otherFile, "Sample other\ncontent");
+
+ TaskResult result = tester.newTask()
+ .properties(builder
+ .put("sonar.sources", "src")
+ .put("sonar.index_all_files", "true")
+ .build())
+ .start();
+
+ assertThat(result.inputFiles()).hasSize(2);
+ assertThat(result.inputFile("src/sample.other").type()).isEqualTo(InputFile.Type.MAIN);
+ assertThat(result.inputFile("src/sample.other").relativePath()).isEqualTo("src/sample.other");
+ assertThat(result.inputFile("src/sample.other").language()).isNull();
+ }
+
}
import org.junit.Test;
import org.mockito.Mockito;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.config.Settings;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.batch.bootstrap.AnalysisMode;
AnalysisMode analysisMode = mock(AnalysisMode.class);
InputFileBuilderFactory factory = new InputFileBuilderFactory(ProjectDefinition.create().setKey("struts"), pathResolver, langDetectionFactory,
- statusDetectionFactory, analysisMode);
+ statusDetectionFactory, analysisMode, new Settings());
InputFileBuilder builder = factory.create(fs);
assertThat(builder.langDetection()).isNotNull();
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile;
+import org.sonar.api.config.Settings;
import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.utils.PathUtils;
import org.sonar.batch.bootstrap.AnalysisMode;
.thenReturn(InputFile.Status.ADDED);
InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
- langDetection, statusDetection, fs, analysisMode);
+ langDetection, statusDetection, fs, analysisMode, new Settings());
DeprecatedDefaultInputFile inputFile = builder.create(srcFile);
inputFile = builder.complete(inputFile, InputFile.Type.MAIN);
when(fs.baseDir()).thenReturn(basedir);
InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
- langDetection, statusDetection, fs, analysisMode);
+ langDetection, statusDetection, fs, analysisMode, new Settings());
DeprecatedDefaultInputFile inputFile = builder.create(srcFile);
assertThat(inputFile).isNull();
when(langDetection.language(any(InputFile.class))).thenReturn(null);
InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
- langDetection, statusDetection, fs, analysisMode);
+ langDetection, statusDetection, fs, analysisMode, new Settings());
DeprecatedDefaultInputFile inputFile = builder.create(srcFile);
inputFile = builder.complete(inputFile, InputFile.Type.MAIN);
.thenReturn(InputFile.Status.ADDED);
InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
- langDetection, statusDetection, fs, analysisMode);
+ langDetection, statusDetection, fs, analysisMode, new Settings());
DeprecatedDefaultInputFile inputFile = builder.create(srcFile);
inputFile = builder.complete(inputFile, InputFile.Type.MAIN);
.thenReturn(InputFile.Status.ADDED);
InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(),
- langDetection, statusDetection, fs, analysisMode);
+ langDetection, statusDetection, fs, analysisMode, new Settings());
DeprecatedDefaultInputFile inputFile = builder.create(srcFile);
inputFile = builder.complete(inputFile, InputFile.Type.MAIN);
* @since 5.0
*/
String SCM_PROVIDER_KEY = "sonar.scm.provider";
+
+ /**
+ * @since 5.1
+ */
+ String INDEX_ALL_FILES_KEY = "sonar.index_all_files";
}
*/
package org.sonar.api.batch.fs;
+import javax.annotation.CheckForNull;
+
import java.io.File;
/**
File file();
/**
- * Language, for example "java" or "php". It's automatically guessed if it is not
- * set in project settings.
+ * Language, for example "java" or "php". Can be null if indexation of all files is enabled and no language claims to support the file.
*/
+ @CheckForNull
String language();
/**