Skip to content

Commit a8033b0

Browse files
committed
Override AbstractSourceImporter#parseDirs and add logging to get source to display in sonar 3.1
1 parent 144eafc commit a8033b0

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

src/main/java/com/pica/sonarplugins/ruby/core/RubySourceImporter.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.nio.charset.Charset;
88
import java.util.List;
99

10+
import org.apache.commons.io.FileUtils;
1011
import org.apache.commons.lang.builder.ToStringBuilder;
1112
import org.apache.commons.lang.builder.ToStringStyle;
1213
import org.slf4j.Logger;
@@ -16,10 +17,10 @@
1617
import org.sonar.api.batch.SensorContext;
1718
import org.sonar.api.resources.Project;
1819
import org.sonar.api.resources.ProjectFileSystem;
20+
import org.sonar.api.resources.Resource;
1921
import org.sonar.api.utils.SonarException;
2022

2123
@Phase(name = Phase.Name.PRE)
22-
2324
public class RubySourceImporter extends AbstractSourceImporter {
2425

2526
private static final Logger LOG = LoggerFactory.getLogger(RubySourceImporter.class);
@@ -51,22 +52,47 @@ protected void doAnalyse(Project project, SensorContext context) throws IOExcept
5152
Charset sourceCharset = fileSystem.getSourceCharset();
5253

5354
List<File> sourceDirs = fileSystem.getSourceDirs();
55+
LOG.info("Got {} source dirs", sourceDirs.size());
5456
List<File> sourceFiles = fileSystem.getSourceFiles(INSTANCE);
57+
LOG.info("Got {} source files", sourceFiles.size());
5558
parseDirs(context, sourceFiles, sourceDirs, false, sourceCharset);
5659
for (File directory : sourceDirs) {
5760
LOG.info(directory.getName());
5861
}
5962

6063
// Importing tests files
6164
List<File> testDirs = fileSystem.getTestDirs();
65+
LOG.info("Got {} test dirs", testDirs.size());
6266
List<File> testFiles = fileSystem.getTestFiles(INSTANCE);
67+
LOG.info("Got {} test files", testFiles.size());
6368
parseDirs(context, testFiles, testDirs, true, sourceCharset);
6469
// Display source dirs and tests directories if info level is enabled.
6570
for (File directory : testDirs) {
6671
LOG.info(directory.getName());
6772
}
6873
}
6974

75+
@Override
76+
protected void parseDirs(SensorContext context, List<File> files, List<File> sourceDirs, boolean unitTest, Charset sourcesEncoding) {
77+
for (File file : files) {
78+
Resource resource = createResource(file, sourceDirs, unitTest);
79+
if (resource != null) {
80+
try {
81+
LOG.debug("Indexing resource: " + resource.getName());
82+
context.index(resource);
83+
if (isEnabled(project)) {
84+
String source = FileUtils.readFileToString(file, sourcesEncoding.name());
85+
LOG.debug("Saving source from: " + file.getPath());
86+
context.saveSource(resource, source);
87+
}
88+
} catch (Exception e) {
89+
throw new SonarException("Unable to read and import the source file : '" + file.getAbsolutePath() + "' with the charset : '"
90+
+ sourcesEncoding.name() + "'.", e);
91+
}
92+
}
93+
}
94+
}
95+
7096
@Override
7197
public String toString() {
7298
ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);

0 commit comments

Comments
 (0)