Skip to content

Commit

Permalink
Added cloc with low memory usage and a separate command called count
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRivis committed Jan 11, 2024
1 parent 52f4b14 commit 2b339cc
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 1 deletion.
4 changes: 4 additions & 0 deletions instrument.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ commands:
# win: insider.bat indent
# unix: ./insider.sh indent

- name: insider-cloc
win: java -Xmx${max-heap} -jar insider.jar count
unix: java -Xmx${max-heap} -jar insider.jar count

parameters:
findConfig: 'config/fingerprints/code_smells.json config/fingerprints/libraries.json config/fingerprints/generated_code.json'
inspectConfig: 'config/rules'
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/dxworks/insider/Insider.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ private static InsiderCommand getInsiderCommand(List<String> commandArgs) {
return new IndentationCount();
case InsiderCommand.MEASURE:
return new MeasureCommand();
case InsiderCommand.COUNT:
return new ClocCommand();
default:
return null;
}
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/org/dxworks/insider/commands/ClocCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.dxworks.insider.commands;

import org.dxworks.ignorerLibrary.Ignorer;
import org.dxworks.ignorerLibrary.IgnorerBuilder;
import org.dxworks.insider.InsiderFile;
import org.dxworks.insider.configuration.InsiderConfiguration;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;

import static org.dxworks.insider.constants.InsiderConstants.CONFIGURATION_FOLDER;
import static org.dxworks.insider.constants.InsiderConstants.RESULTS_FOLDER;

public class ClocCommand implements NoFilesCommand {
private static final int BUFFER_SIZE = 8192;

@Override
public boolean parse(List<String> args) {
return args.size() == 1;
}

@Override
public void execute(List<InsiderFile> insiderFiles, List<String> args) {
processFolder(InsiderConfiguration.getInstance().getRootFolder());
}

@Override
public String usage() {
return "insider count";
}

@Override
public String getName() {
return COUNT;
}


public void processFolder(String folderPath) {
Path startPath = Paths.get(folderPath);
Path csvPath = Paths.get(RESULTS_FOLDER, InsiderConfiguration.getInstance().getProjectID() + "-cloc.csv");

Ignorer ignorer = new IgnorerBuilder(Paths.get(CONFIGURATION_FOLDER, ".ignore")).compile();

try (BufferedWriter writer = Files.newBufferedWriter(csvPath, StandardCharsets.UTF_8)) {
writer.write("file,lines,size\n");
Files.walkFileTree(startPath, new SimpleFileVisitor<>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (ignorer.accepts(file)) {
long lines = countLines(file);
long size = Files.size(file);
String relativePath = startPath.relativize(file).toString();

writer.write("\"" + relativePath + "\"" + "," + lines + "," + size);
writer.newLine();
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}

private long countLines(Path path) throws IOException {
long lines = 0;
try (InputStream in = Files.newInputStream(path);
BufferedInputStream bis = new BufferedInputStream(in)) {
byte[] buffer = new byte[BUFFER_SIZE];
int n;
while ((n = bis.read(buffer)) != -1) {
for (int i = 0; i < n; i++) {
if (buffer[i] == '\n' || buffer[i] == '\r') {
lines++;
// Handle CRLF endings
if (i < n - 1 && buffer[i] == '\r' && buffer[i + 1] == '\n') {
i++;
}
}
}
}
}
return lines + 1;
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/dxworks/insider/commands/HelpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public void execute(List<InsiderFile> insiderFiles, List<String> args) {
new InspectCommand(),
new ExtractCommand(),
new MeasureCommand(),
new IndentationCount())
new IndentationCount(),
new ClocCommand())
.map(InsiderCommand::usage)
.map(s -> "\t" + s)
.collect(Collectors.joining("\n"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface InsiderCommand {
String EXTRACT = "extract";
String INDENT = "indent";
String MEASURE = "measure";
String COUNT = "count";
List<String> VERSION = Arrays.asList("version", "-version", "--version", "-v");
List<String> HELP = Arrays.asList("help", "-help", "--help", "-h");

Expand Down

0 comments on commit 2b339cc

Please sign in to comment.