Skip to content

Commit

Permalink
spoon: updated analyser to take specific files
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisTimperley committed Jun 30, 2024
1 parent b868093 commit d3bbeae
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
19 changes: 17 additions & 2 deletions src/kaskara/spoon/analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import typing as t
from collections.abc import Mapping, Sequence
from dataclasses import dataclass
from pathlib import Path

import dockerblade
from loguru import logger
Expand Down Expand Up @@ -43,13 +44,27 @@ def for_project(
@overrides
def run(self) -> Analysis:
container = self._container
dir_source = self._project.directory
dir_source = Path(self._project.directory)
assert dir_source.is_absolute()

container_output_dir = container.files.mktemp()
container.files.remove(container_output_dir)
container.files.makedirs(container_output_dir)

paths_to_index: list[Path]
if self._project.files:
paths_to_index = [Path(filename) for filename in self._project.files]
paths_to_index = [
path if path.is_absolute() else dir_source / path
for path in paths_to_index
]
else:
paths_to_index = [dir_source]

paths_to_index_arg = " ".join(str(path) for path in paths_to_index)
command_args = [
BINARY_PATH,
dir_source,
paths_to_index_arg,
"-o",
container_output_dir,
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,23 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.concurrent.Callable;
import picocli.CommandLine;


@CommandLine.Command(name = "kaskara", mixinStandardHelpOptions = true)
@CommandLine.Command(name = "kaskara-spoon", mixinStandardHelpOptions = true)
public class Main implements Callable<Integer> {
@CommandLine.Parameters(
index = "0",
description = "The root source code directory for the project."
index = "0..*",
description = "Paths to the source files or directories that should be indexed."
)
private String directory;
private List<String> paths;

@CommandLine.Option(
names = "-o",
defaultValue = ".",
description = "The directory to which results should be written."
description = "Directory to which results should be written."
)
private String outputDirectory;

Expand Down Expand Up @@ -110,7 +111,7 @@ public Integer call() throws IOException {
this.mapper = new ObjectMapper();
this.mapper.enable(SerializationFeature.INDENT_OUTPUT);

var project = Project.build(this.directory);
var project = Project.build(this.paths);
this.findStatements(project);
this.findFunctions(project);
this.findLoops(project);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package christimperley.kaskara;

import java.util.List;
import spoon.Launcher;
import spoon.reflect.CtModel;

Expand All @@ -10,14 +11,16 @@ public class Project {
private final CtModel model;

/**
* Constructs a description of a project whose source code is in a given directory.
* @param sourceDirectory The absolute path to the source code directory.
* Constructs a description of a Java project.
* @param paths A list of paths to the source files or directories that should be indexed.
* @return A description of the project.
*/
public static Project build(String sourceDirectory) {
public static Project build(List<String> paths) {
var launcher = new Launcher();
launcher.getEnvironment().setAutoImports(true);
launcher.addInputResource(sourceDirectory);
for (var path : paths) {
launcher.addInputResource(path);
}
var model = launcher.buildModel();
return new Project(model);
}
Expand Down

0 comments on commit d3bbeae

Please sign in to comment.