Skip to content
This repository has been archived by the owner on Jul 8, 2019. It is now read-only.

Commit

Permalink
Merge be38a66 into 1eaae62
Browse files Browse the repository at this point in the history
  • Loading branch information
chintans committed Jun 21, 2016
2 parents 1eaae62 + be38a66 commit 5f9869c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ Desktop.ini
hs_err_pid*

#Custom stuff
sample.tslint.json
sample.tslint.json
.idea
40 changes: 20 additions & 20 deletions src/main/java/com/pablissimo/sonar/TsLintExecutorImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,67 +12,67 @@
public class TsLintExecutorImpl implements TsLintExecutor {
public static final int MAX_COMMAND_LENGTH = 4096;
private static final Logger LOG = LoggerFactory.getLogger(TsLintExecutorImpl.class);

private StringBuilder stdOut;
private StringBuilder stdErr;

private static Command getBaseCommand(String pathToTsLint, String configFile, String rulesDir) {
Command command =
Command command =
Command
.create("node")
.addArgument(pathToTsLint)
.addArgument("--format")
.addArgument("json");

if (rulesDir != null && rulesDir.length() > 0) {
command
.addArgument("--rules-dir")
.addArgument(rulesDir);
}

command
.addArgument("--config")
.addArgument(configFile)
.setNewShell(false);

return command;
}

public String execute(String pathToTsLint, String configFile, String rulesDir, List<String> files, Integer timeoutMs) {
// New up a command that's everything we need except the files to process
// We'll use this as our reference for chunking up files
int baseCommandLength = getBaseCommand(pathToTsLint, configFile, rulesDir).toCommandLine().length();
int availableForBatching = MAX_COMMAND_LENGTH - baseCommandLength;

List<List<String>> batches = new ArrayList<List<String>>();
List<String> currentBatch = new ArrayList<String>();
batches.add(currentBatch);

int currentBatchLength = 0;
for (int i = 0; i < files.size(); i++) {
String nextPath = files.get(i).trim();

// +1 for the space we'll be adding between filenames
if (currentBatchLength + nextPath.length() + 1 > availableForBatching) {
if (currentBatchLength + nextPath.length() + 1 > availableForBatching) {
// Too long to add to this batch, create new
currentBatch = new ArrayList<String>();
currentBatchLength = 0;
batches.add(currentBatch);
}

currentBatch.add(nextPath);
currentBatchLength += nextPath.length() + 1;
}

LOG.debug("Split " + files.size() + " files into " + batches.size() + " batches for processing");

this.stdOut = new StringBuilder();
this.stdErr = new StringBuilder();

StreamConsumer stdOutConsumer = new StreamConsumer() {
public void consumeLine(String line) {
LOG.trace("TsLint Out: " + line);
stdOut.append(line + "\n");
stdOut.append(line);
}
};

Expand All @@ -86,25 +86,25 @@ public void consumeLine(String line) {
for (int i = 0; i < batches.size(); i++) {
List<String> thisBatch = batches.get(i);
Command thisCommand = getBaseCommand(pathToTsLint, configFile, rulesDir);

for (int fileIndex = 0; fileIndex < thisBatch.size(); fileIndex++) {
thisCommand.addArgument(thisBatch.get(fileIndex));
}

LOG.debug("Executing TsLint with command: " + thisCommand.toCommandLine());

// Timeout is specified per file, not per batch (which can vary a lot)
// so multiply it up
this.createExecutor().execute(thisCommand, stdOutConsumer, stdErrConsumer, timeoutMs * thisBatch.size());
}

String rawOutput = stdOut.toString();

// TsLint returns nonsense for its JSON output when faced with multiple files
// so we need to fix it up before we do anything else
return "[" + rawOutput.replaceAll("\\]\\[", "],[") + "]";
}

protected CommandExecutor createExecutor() {
return CommandExecutor.create();
}
Expand Down

0 comments on commit 5f9869c

Please sign in to comment.