Skip to content

Commit

Permalink
Small changes before release
Browse files Browse the repository at this point in the history
  • Loading branch information
gretard committed Aug 8, 2018
1 parent ba2981a commit 7561ad9
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 82 deletions.
72 changes: 36 additions & 36 deletions sonar-ps-plugin/.classpath
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.8.0_60">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.sonar.plugins.powershell;

public final class Constants {
public static final String PROFILE_NAME = "Sonar Way";
public static final String SKIP_TOKENIZER = "sonar.ps.tokenizer.skip";
public static final String FILE_SUFFIXES = "sonar.ps.file.suffixes";

}
package org.sonar.plugins.powershell;

public final class Constants {
public static final String PROFILE_NAME = "Sonar Way";
public static final String SKIP_TOKENIZER = "sonar.ps.tokenizer.skip";
public static final String FILE_SUFFIXES = "sonar.ps.file.suffixes";
public static final String PS_EXECUTABLE = "sonar.ps.executable";
public static final String SKIP_PLUGIN = "sonar.ps.plugin.skip";

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public void define(final Context context) {
context.addExtension(PropertyDefinition.builder(Constants.SKIP_TOKENIZER).name("Skip tokenizer")
.description("Flag whether to skip tokenizer").defaultValue("false").type(PropertyType.BOOLEAN)
.build());
context.addExtension(PropertyDefinition.builder(Constants.SKIP_PLUGIN).name("Skip plugin")
.description("Flag whether to skip plugin").defaultValue("false").type(PropertyType.BOOLEAN)
.build());
context.addExtension(PropertyDefinition.builder(Constants.PS_EXECUTABLE).name("Path to powershell executable")
.description("Path to powershell executable").defaultValue("powershell.exe").type(PropertyType.STRING)
.build());

context.addExtension(PropertyDefinition.builder(Constants.FILE_SUFFIXES).name("Suffixes to analyze")
.description("Suffixes supported by the plugin").defaultValue(".ps1,.psm1,.psd1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

import javax.xml.bind.JAXBContext;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.SystemUtils;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.TempFolder;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
Expand All @@ -16,10 +18,9 @@

public class ScriptAnalyzerSensor implements org.sonar.api.batch.sensor.Sensor {


private final TempFolder folder;

private static final String psCommand = "(Invoke-ScriptAnalyzer -Path '%s' -Recurse | Select-Object RuleName, Message, Line, Column, Severity, @{Name='File';Expression={$_.Extent.File }} | ConvertTo-Xml).Save('%s')";
private static final String psCommand = "%s -inputDir %s -output %s";

private static final Logger LOGGER = Loggers.get(ScriptAnalyzerSensor.class);

Expand All @@ -37,30 +38,44 @@ public void describe(final SensorDescriptor descriptor) {

public void execute(final SensorContext context) {

if (!SystemUtils.IS_OS_WINDOWS) {
LOGGER.info("Skipping sensor as OS is not windows");
final Settings settings = context.settings();
final boolean skipPlugin = settings.getBoolean(Constants.SKIP_PLUGIN);

if (skipPlugin) {
LOGGER.debug("Skipping sensor as skip plugin flag is set");
return;
}

final String powershellExecutable = settings.getString(Constants.PS_EXECUTABLE);

try {
final File parserFile = folder.newFile("ps", "scriptAnalyzer.ps1");

try {
FileUtils.copyURLToFile(getClass().getResource("/scriptAnalyzer.ps1"), parserFile);
} catch (final Throwable e1) {
LOGGER.warn("Exception while copying tokenizer script", e1);
return;
}
final String scriptFile = parserFile.getAbsolutePath();
final File resultsFile = folder.newFile();
final FileSystem fileSystem = context.fileSystem();
final File sourceDir = fileSystem.baseDir().toPath().toFile();

final String command = String.format(psCommand, sourceDir.getAbsolutePath(),
final String command = String.format(psCommand, scriptFile, sourceDir.getAbsolutePath(),
resultsFile.toPath().toFile().getAbsolutePath());

try {
LOGGER.info(String.format("Starting running powershell analysis: %s", command));
final Process process = new ProcessBuilder("powershell.exe", command).start();
final Process process = new ProcessBuilder(powershellExecutable, command).start();
process.waitFor();
LOGGER.info("Finished running powershell analysis");

} catch (final Throwable e) {
LOGGER.warn("Error executing Powershell script analyzer. Maybe Script-Analyzer is not installed?", e);
return;
}

final JAXBContext jaxbContext = JAXBContext.newInstance(Objects.class);
final Objects issues = (Objects) jaxbContext.createUnmarshaller().unmarshal(resultsFile);
this.issuesFiller.fill(context, sourceDir, issues);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.sonar.plugins.powershell;

import static java.lang.String.format;

import java.io.File;

import javax.xml.bind.JAXBContext;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.SystemUtils;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
Expand All @@ -25,14 +22,14 @@
public class TokenizerSensor implements org.sonar.api.batch.sensor.Sensor {

private static final Logger LOGGER = Loggers.get(TokenizerSensor.class);

private static final boolean isDebugEnabled = LOGGER.isDebugEnabled();

private static final String psCommand = "%s -inputFile %s -output %s";

private final IFiller[] fillers = new IFiller[] { new CpdFiller(), new HighlightingFiller(),
new HalsteadComplexityFiller() , new CComplexityFiller()};
new HalsteadComplexityFiller(), new CComplexityFiller() };

private final TempFolder folder;

public TokenizerSensor(final TempFolder folder) {
Expand All @@ -53,17 +50,23 @@ private Tokens readTokens(final File file) throws Exception {

@Override
public void execute(final SensorContext context) {
if (!SystemUtils.IS_OS_WINDOWS) {
LOGGER.info("Skipping sensor as OS is not Wwindows.");

final Settings settings = context.settings();
final boolean skipAnalysis = settings.getBoolean(Constants.SKIP_TOKENIZER);
final boolean skipPlugin = settings.getBoolean(Constants.SKIP_PLUGIN);

if (skipPlugin) {
LOGGER.debug("Skipping sensor as skip plugin flag is set");
return;
}
Settings settings = context.settings();
final boolean skipAnalysis = settings.getBoolean(Constants.SKIP_TOKENIZER);

final String powershellExecutable = settings.getString(Constants.PS_EXECUTABLE);

if (skipAnalysis) {
LOGGER.debug(format("Skipping tokenizer as skip flag is set"));
LOGGER.debug("Skipping tokenizer as skip flag is set");
return;
}

final File parserFile = folder.newFile("ps", "parser.ps1");

try {
Expand All @@ -80,11 +83,11 @@ public void execute(final SensorContext context) {

final String analysisFile = inputFile.file().getAbsolutePath();
final String resultsFile = folder.newFile().toPath().toFile().getAbsolutePath();
final String command = String.format(this.psCommand, scriptFile, analysisFile, resultsFile);
final String command = String.format(psCommand, scriptFile, analysisFile, resultsFile);
if (isDebugEnabled) {
LOGGER.debug(String.format("Running %s command", command));
}
final Process process = new ProcessBuilder("powershell.exe", command).start();
final Process process = new ProcessBuilder(powershellExecutable, command).start();
process.waitFor();
final File tokensFile = new File(resultsFile);
if (!tokensFile.exists() || tokensFile.length() <= 0) {
Expand All @@ -94,7 +97,6 @@ public void execute(final SensorContext context) {
}

final Tokens tokens = readTokens(tokensFile);
System.out.println(tokens.getComplexity());
for (final IFiller filler : this.fillers) {
filler.fill(context, inputFile, tokens);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public void fill(SensorContext context, InputFile f, Tokens tokens) {
try {
context.<Integer>newMeasure().on(f).forMetric(CoreMetrics.COMPLEXITY).withValue(tokens.getComplexity())
.save();

} catch (final Throwable e) {
LOGGER.warn("Exception while saving tokens", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,7 @@ public void fill(SensorContext context, InputFile f, Tokens tokens) {
if (StringUtils.isBlank(token.getText())) {
continue;
}
try {
cpdTokens.addToken(token.getStartLineNumber(), token.getStartColumnNumber(),
token.getEndLineNumber(), token.getEndColumnNumber(), token.getText());
} catch (final Throwable e) {
if (isDebugEnabled) {
LOGGER.debug("Exception while adding token", e);
}
}
tryAddToken(cpdTokens, token);
}

cpdTokens.save();
Expand All @@ -40,4 +33,15 @@ public void fill(SensorContext context, InputFile f, Tokens tokens) {

}

private static void tryAddToken(final NewCpdTokens cpdTokens, final Token token) {
try {
cpdTokens.addToken(token.getStartLineNumber(), token.getStartColumnNumber(),
token.getEndLineNumber(), token.getEndColumnNumber(), token.getText());
} catch (final Throwable e) {
if (isDebugEnabled) {
LOGGER.debug("Exception while adding token", e);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class HalsteadComplexityFiller implements IFiller {
@Override
public void fill(final SensorContext context, final InputFile f, final Tokens tokens) {
try {
final List<String> uniqueOperands = new LinkedList<String>();
final List<String> uniqueOperators = new LinkedList<String>();
final List<String> uniqueOperands = new LinkedList<>();
final List<String> uniqueOperators = new LinkedList<>();
int totalOperands = 0;
int totalOperators = 0;

Expand All @@ -36,7 +36,6 @@ public void fill(final SensorContext context, final InputFile f, final Tokens to
final String text = token.getText().toLowerCase();
if (operandTypes.contains(token.getKind())) {
totalOperands++;

if (!uniqueOperands.contains(text)) {
uniqueOperands.add(text);
}
Expand All @@ -45,7 +44,6 @@ public void fill(final SensorContext context, final InputFile f, final Tokens to
totalOperators++;
if (!uniqueOperators.contains(text)) {
uniqueOperators.add(text);

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void fill(final SensorContext context, final InputFile f, final Tokens to
}
}

@SuppressWarnings("deprecation")
private static void highlightToken(final NewHighlighting highlighting, final Token token) {
try {
final List<String> kinds = Arrays.asList(token.getTokenFlags().toLowerCase().split(","));
Expand All @@ -48,7 +49,6 @@ private static void highlightToken(final NewHighlighting highlighting, final Tok
}
if (check("Variable", token, kinds)) {
highlighting.highlight(token.getStartOffset(), token.getEndOffset(), TypeOfText.KEYWORD_LIGHT);
return;
}

} catch (Throwable e) {
Expand Down
7 changes: 4 additions & 3 deletions sonar-ps-plugin/src/main/resources/parser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ $ast = [Management.Automation.Language.Parser]::ParseInput($text , [ref]$tokens,
$complexity = 1;
$switches = $ast.FindAll({$args[0] -is [System.Management.Automation.Language.SwitchStatementAst]}, $true)

Foreach ( $item in $switches ) {
Foreach ( $item in $switches ) {
$complexity += $item.Clauses.Count
}
}

$tryCatches = $ast.FindAll({$args[0] -is [System.Management.Automation.Language.TryStatementAst]}, $true)

Expand All @@ -38,12 +38,13 @@ $whileStatements = $ast.FindAll({$args[0] -is [System.Management.Automation.Lang
Foreach ( $item in $whileStatements ) {
$complexity += 1
}

$xmlWriter = New-Object System.XMl.XmlTextWriter($output , $Null);
$xmlWriter.WriteStartDocument();
$xmlWriter.WriteStartElement("Tokens");
$xmlWriter.WriteAttributeString("complexity", $complexity);

foreach ($item in $tokens) {
Foreach ($item in $tokens) {
$xmlWriter.WriteStartElement("Token");
$xmlWriter.WriteElementString("Text", $item.Text);
$xmlWriter.WriteElementString("Value", $item.Value);
Expand Down
6 changes: 6 additions & 0 deletions sonar-ps-plugin/src/main/resources/scriptAnalyzer.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
param(
[string]$inputDir,
[string]$output
)
Import-Module PSScriptAnalyzer;
(Invoke-ScriptAnalyzer -Path "$inputDir" -Recurse | Select-Object RuleName, Message, Line, Column, Severity, @{Name='File';Expression={$_.Extent.File }} | ConvertTo-Xml).Save("$output")
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void testExecute() throws IOException {


SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot());
ctxTester.settings().setProperty(Constants.PS_EXECUTABLE, "powershell.exe");
File baseFile = folder.newFile("test.ps1");
FileUtils.copyURLToFile(getClass().getResource("/testFiles/test.ps1"), baseFile);
DefaultInputFile ti = new TestInputFileBuilder("test", "test.ps1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class TokenizerSensorTest {
@Test
public void testExecute() throws IOException {
SensorContextTester ctxTester = SensorContextTester.create(folder.getRoot().getAbsoluteFile().toPath());
ctxTester.settings().setProperty(Constants.PS_EXECUTABLE, "powershell.exe");
File baseFile = folder.newFile("test.ps1");

FileUtils.copyURLToFile(getClass().getResource("/testFiles/test.ps1"), baseFile);
Expand Down

0 comments on commit 7561ad9

Please sign in to comment.