/*
* Sonar Scala Plugin
* Copyright (C) 2011 Felix Müller
* felix.mueller.berlin@googlemail.com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.plugins.scala.sensor;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.Phase;
import org.sonar.api.batch.Phase.Name;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.resources.InputFile;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.plugins.scala.language.Scala;
import org.sonar.plugins.scala.language.ScalaFile;
/**
* This Sensor imports all Scala files into Sonar.
*
* @author Felix Müller
* @since 0.1
*/
@Phase(name = Name.PRE)
public class ScalaSourceImporterSensor extends AbstractScalaSensor {
private static final Logger LOGGER = LoggerFactory.getLogger(ScalaSourceImporterSensor.class);
public ScalaSourceImporterSensor(Scala scala) {
super(scala);
}
public void analyse(Project project, SensorContext sensorContext) {
try {
ProjectFileSystem fileSystem = project.getFileSystem();
String charset = fileSystem.getSourceCharset().toString();
//dedup the files. This is important if the pom.xml has <testSourceDirectory>src/test/scala</testSourceDirectory>
Set<File> canonicalFiles = new HashSet<File>();
for (InputFile sourceFile : fileSystem.mainFiles(getScala().getKey())) {
if (canonicalFiles.add(sourceFile.getFile().getCanonicalFile())) {
addFileToSonar(sensorContext, sourceFile, false, charset);
}
}
for (InputFile testFile : fileSystem.testFiles(getScala().getKey())) {
if (canonicalFiles.add(testFile.getFile().getCanonicalFile())) {
addFileToSonar(sensorContext, testFile, true, charset);
}
}
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
private void addFileToSonar(SensorContext sensorContext, InputFile inputFile,
boolean isUnitTest, String charset) {
try {
String source = FileUtils.readFileToString(inputFile.getFile(), charset);
ScalaFile resource = ScalaFile.fromInputFile(inputFile, isUnitTest);
sensorContext.index(resource);
sensorContext.saveSource(resource, source);
if (LOGGER.isDebugEnabled()) {
if (isUnitTest) {
LOGGER.debug("Added Scala test file to Sonar: " + inputFile.getFile().getAbsolutePath());
} else {
LOGGER.debug("Added Scala source file to Sonar: " + inputFile.getFile().getAbsolutePath());
}
}
} catch (IOException ioe) {
LOGGER.error("Could not read the file: " + inputFile.getFile().getAbsolutePath(), ioe);
}
}
@Override
public String toString() {
return getClass().getSimpleName();
}
}