|
7 | 7 | import java.nio.charset.Charset; |
8 | 8 | import java.util.List; |
9 | 9 |
|
| 10 | +import org.apache.commons.io.FileUtils; |
10 | 11 | import org.apache.commons.lang.builder.ToStringBuilder; |
11 | 12 | import org.apache.commons.lang.builder.ToStringStyle; |
12 | 13 | import org.slf4j.Logger; |
|
16 | 17 | import org.sonar.api.batch.SensorContext; |
17 | 18 | import org.sonar.api.resources.Project; |
18 | 19 | import org.sonar.api.resources.ProjectFileSystem; |
| 20 | +import org.sonar.api.resources.Resource; |
19 | 21 | import org.sonar.api.utils.SonarException; |
20 | 22 |
|
21 | 23 | @Phase(name = Phase.Name.PRE) |
22 | | - |
23 | 24 | public class RubySourceImporter extends AbstractSourceImporter { |
24 | 25 |
|
25 | 26 | private static final Logger LOG = LoggerFactory.getLogger(RubySourceImporter.class); |
@@ -51,22 +52,47 @@ protected void doAnalyse(Project project, SensorContext context) throws IOExcept |
51 | 52 | Charset sourceCharset = fileSystem.getSourceCharset(); |
52 | 53 |
|
53 | 54 | List<File> sourceDirs = fileSystem.getSourceDirs(); |
| 55 | + LOG.info("Got {} source dirs", sourceDirs.size()); |
54 | 56 | List<File> sourceFiles = fileSystem.getSourceFiles(INSTANCE); |
| 57 | + LOG.info("Got {} source files", sourceFiles.size()); |
55 | 58 | parseDirs(context, sourceFiles, sourceDirs, false, sourceCharset); |
56 | 59 | for (File directory : sourceDirs) { |
57 | 60 | LOG.info(directory.getName()); |
58 | 61 | } |
59 | 62 |
|
60 | 63 | // Importing tests files |
61 | 64 | List<File> testDirs = fileSystem.getTestDirs(); |
| 65 | + LOG.info("Got {} test dirs", testDirs.size()); |
62 | 66 | List<File> testFiles = fileSystem.getTestFiles(INSTANCE); |
| 67 | + LOG.info("Got {} test files", testFiles.size()); |
63 | 68 | parseDirs(context, testFiles, testDirs, true, sourceCharset); |
64 | 69 | // Display source dirs and tests directories if info level is enabled. |
65 | 70 | for (File directory : testDirs) { |
66 | 71 | LOG.info(directory.getName()); |
67 | 72 | } |
68 | 73 | } |
69 | 74 |
|
| 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 | + |
70 | 96 | @Override |
71 | 97 | public String toString() { |
72 | 98 | ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE); |
|
0 commit comments