Skip to content

Commit

Permalink
#28 Running analysis and adding properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Omar WALDMANN authored and begarco committed Sep 13, 2018
1 parent 7b59c14 commit cd8b0a1
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
41 changes: 37 additions & 4 deletions src/main/java/fr/cnes/sonar/plugins/icode/check/ICodeSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package fr.cnes.sonar.plugins.icode.check;

import fr.cnes.sonar.plugins.icode.ICodePlugin;
import fr.cnes.sonar.plugins.icode.languages.Fortran77Language;
import fr.cnes.sonar.plugins.icode.languages.Fortran90Language;
import fr.cnes.sonar.plugins.icode.languages.ShellLanguage;
Expand All @@ -26,6 +27,7 @@
import fr.cnes.sonar.plugins.icode.model.XmlHandler;
import fr.cnes.sonar.plugins.icode.rules.ICodeRulesDefinition;
import fr.cnes.sonar.plugins.icode.settings.ICodePluginProperties;
import org.apache.commons.lang.SystemUtils;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
Expand All @@ -37,15 +39,18 @@
import org.sonar.api.batch.sensor.issue.NewIssueLocation;
import org.sonar.api.config.Configuration;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.PathUtils;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.command.Command;
import org.sonar.api.utils.command.CommandExecutor;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

import javax.xml.bind.JAXBException;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.*;

/**
* Executed during sonar-scanner call.
Expand Down Expand Up @@ -97,6 +102,34 @@ public void execute(SensorContext sensorContext) {
final Configuration config = sensorContext.config();
// Represent the active rules used for the analysis.
final ActiveRules activeRules = sensorContext.activeRules();
// run i-Code CNES execution
if(config.getBoolean(ICodePluginProperties.AUTOLAUNCH_PROP_KEY).orElse(Boolean.getBoolean(ICodePluginProperties.AUTOLAUNCH_PROP_DEFAULT))) {
LOGGER.info("i-Code CNES auto-launch enabled.");
final String executable = config.get(ICodePluginProperties.ICODE_PATH_KEY).orElse(ICodePluginProperties.ICODE_PATH_DEFAULT);
final String[] files = sensorContext.fileSystem().baseDir().list();
final String outputFile = config.get(ICodePluginProperties.REPORT_PATH_KEY).orElse(ICodePluginProperties.REPORT_PATH_DEFAULT);
final String outputPath = Paths.get(sensorContext.fileSystem().baseDir().toString(),outputFile).toString();
final String outputOption = "-o";
final String command = String.join(" ", executable, String.join(" ",files), outputOption, outputPath);

LOGGER.info("running i-Code CNES and generating results to "+ outputPath);
LOGGER.debug("command : " + command);
try {
final Process icode = Runtime.getRuntime().exec(command);
int success = icode.waitFor();
if(success==0){
LOGGER.info("Auto-launch successfully executed i-Code CNES");
}else{
LOGGER.error("i-Code CNES auto-launch analysis failed with exit code "+success);
}
} catch (InterruptedException | IOException e) {
LOGGER.error(e.getMessage(), e);
sensorContext.newAnalysisError().message(e.getMessage()).save();
}


}

// Report files found in file system and corresponding to SQ property.
final List<String> reportFiles = getReportFiles(config, fileSystem);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package fr.cnes.sonar.plugins.icode.settings;

import org.sonar.api.PropertyType;
import org.sonar.api.config.PropertyDefinition;

import java.util.Arrays;
Expand All @@ -26,6 +27,12 @@
*/
public class ICodePluginProperties {


/**
* i-Code CNES default location's path value.
*/
public static final String ICODE_PATH_DEFAULT = "${HOME}/icode-cnes/icode.exe";

/**
* Prefix used by all properties of this plugin.
**/
Expand Down Expand Up @@ -107,6 +114,34 @@ public class ICodePluginProperties {
* Default value for the report path property
**/
public static final String REPORT_PATH_DEFAULT = "result.res";
/**
* i-Code CNES launching mode key
*/
public static final String AUTOLAUNCH_PROP_KEY = PROPERTIES_PREFIX + "launch";
/**
* i-Code CNES launching mode default value
*/
public static final String AUTOLAUNCH_PROP_DEFAULT = "false";
/**
* Launching mode name
*/
public static final String AUTOLAUNCH_PROP_NAME = "i-Code CNES auto-launch";
/**
* Launching mode description
*/
public static final String AUTOLAUNCH_PROP_DESC = "Auto-launch i-Code CNES on analysis using indicated location.";
/**
* i-Code CNES location's path key
*/
public static final String ICODE_PATH_KEY = PROPERTIES_PREFIX + "path";
/**
* i-Code CNES location's path key
*/
public static final String ICODE_PATH_NAME = "i-Code CNES location";
/**
* i-Code CNES location's path key
*/
public static final String ICODE_PATH_DESC = "Define i-Code CNES executable path to auto-launch it on analysis.";

private ICodePluginProperties() {
super();
Expand All @@ -119,6 +154,21 @@ private ICodePluginProperties() {
*/
public static List<PropertyDefinition> getProperties() {
return Arrays.asList(
PropertyDefinition.builder(AUTOLAUNCH_PROP_KEY)
.defaultValue(AUTOLAUNCH_PROP_DEFAULT)
.category(ICODE_NAME)
.name(AUTOLAUNCH_PROP_NAME)
.description(AUTOLAUNCH_PROP_DESC)
.type(PropertyType.BOOLEAN)
.build()
,
PropertyDefinition.builder(ICODE_PATH_KEY)
.defaultValue(ICODE_PATH_DEFAULT)
.category(ICODE_NAME)
.name(ICODE_PATH_NAME)
.description(ICODE_PATH_DESC)
.build()
,
PropertyDefinition.builder(SHELL_SUFFIX_KEY).multiValues(true)
.defaultValue(SHELL_SUFFIX_DEFAULT).category(ICODE_NAME)
.name(SHELL_SUFFIX_NAME).description(SHELL_SUFFIX_DESC)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void test_extensions_are_all_set() {
SonarRuntime runtime = SonarRuntimeImpl.forSonarQube(VERSION_6_7, SonarQubeSide.SERVER);
Plugin.Context context = new Plugin.Context(runtime);
iCodePlugin.define(context);
Assert.assertEquals(11, context.getExtensions().size());
Assert.assertEquals(13, context.getExtensions().size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public class ICodePluginPropertiesTest {
@Test
public void test_plugin_properties_definition() {
List<PropertyDefinition> actual = ICodePluginProperties.getProperties();
assertEquals(4, actual.size());
PropertyDefinition codeSuffix = actual.get(0);
assertEquals(6, actual.size());
PropertyDefinition codeSuffix = actual.get(2);
Assert.assertEquals(ICodePluginProperties.ICODE_NAME, codeSuffix.category());
assertEquals(ICodePluginProperties.SHELL_SUFFIX_KEY, codeSuffix.key());
assertEquals(ICodePluginProperties.SHELL_SUFFIX_DEFAULT, codeSuffix.defaultValue());
PropertyDefinition reportPath = actual.get(3);
PropertyDefinition reportPath = actual.get(5);
assertEquals(ICodePluginProperties.ICODE_NAME, reportPath.category());
assertEquals(ICodePluginProperties.REPORT_PATH_KEY, reportPath.key());
assertEquals(ICodePluginProperties.REPORT_PATH_DEFAULT, reportPath.defaultValue());
Expand Down

0 comments on commit cd8b0a1

Please sign in to comment.