|
| 1 | +package com.pica.sonarplugins.ruby.rcov; |
| 2 | + |
| 3 | +import com.pica.sonarplugins.ruby.core.RubyFile; |
| 4 | +import hudson.plugins.rubyMetrics.rcov.RcovParser; |
| 5 | +import hudson.plugins.rubyMetrics.rcov.model.RcovResult; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | +import org.sonar.api.batch.Sensor; |
| 9 | +import org.sonar.api.batch.SensorContext; |
| 10 | +import org.sonar.api.measures.CoreMetrics; |
| 11 | +import org.sonar.api.resources.Project; |
| 12 | +import org.sonar.api.resources.ProjectFileSystem; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | +import java.io.IOException; |
| 16 | + |
| 17 | +public class RcovSensor implements Sensor { |
| 18 | + |
| 19 | + private static final Logger LOG = LoggerFactory.getLogger(RcovSensor.class); |
| 20 | + |
| 21 | + public boolean shouldExecuteOnProject(Project project) { |
| 22 | + return true; |
| 23 | + } |
| 24 | + |
| 25 | + public void analyse(Project project, SensorContext context) { |
| 26 | + //TODO: Configure rcov location |
| 27 | + File reportFile = new File("coverage/rcov/"); |
| 28 | + try { |
| 29 | + parseReport(project.getFileSystem(), reportFile, context); |
| 30 | + } catch (IOException e) { |
| 31 | + LOG.error("Error analysing project.", e); |
| 32 | + } |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + protected void parseReport(ProjectFileSystem fileSystem, File rootFile, final SensorContext context) throws IOException {; |
| 37 | + //Coverage will be assigned to the "root" ruby file |
| 38 | + RubyFile rootRubyFile = RubyFile.fromIOFile(fileSystem.getBasedir(), fileSystem.getSourceDirs()); |
| 39 | + |
| 40 | + RcovParser parser = new RcovParser(rootFile); |
| 41 | + //TODO: Dynamic coverage file locations |
| 42 | + File coverageFile = new File("coverage/rcov/index.html"); |
| 43 | + RcovResult result = parser.parse(coverageFile); |
| 44 | + |
| 45 | + context.saveMeasure(rootRubyFile, CoreMetrics.COVERAGE, result.getCodeCoverageFloat().doubleValue()); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String toString() { |
| 50 | + return "Ruby RcovSensor"; |
| 51 | + } |
| 52 | +} |
0 commit comments