Skip to content

Commit

Permalink
Merge pull request #10 from IBM/add-a-build-cli-flag
Browse files Browse the repository at this point in the history
Codeanalyzer now has a few extra flags.
  • Loading branch information
rahlk authored Apr 24, 2024
2 parents f918e3a + 3584331 commit 497e5b5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
12 changes: 11 additions & 1 deletion src/main/java/com/ibm/northstar/CodeAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public class CodeAnalyzer implements Runnable {
@Option(names = {"-i", "--input"}, required = true, description = "Path to the project root directory.")
private static String input;

@Option(names = {"-b", "--build-cmd"}, description = "Custom build command. Defaults to auto build.")
private static String build;

@Option(names = {"--no-build"}, description = "Do not build your application. Use this option if you have already built your application.")
private static boolean noBuild = false;

@Option(names = {"-a", "--analysis-level"}, description = "[Optional] Level of analysis to perform. Options: 1 (for just symbol table) or 2 (for full analysis including the system depenedency graph). Default: 1")
private static int analysisLevel = 1;

Expand Down Expand Up @@ -114,7 +120,11 @@ private static void analyze() throws IOException, ClassHierarchyException, CallG
combinedJsonObject.add("symbol_table", symbolTableJSON);
if (analysisLevel > 1) {
// Save SDG, IPCFG, and Call graph as JSON
String sdgAsJSONString = SystemDependencyGraph.construct(input, dependencies, analyzeSource);
// If noBuild is not true, and build is also not provided, we will use "auto" as the build command
build = build == null ? "auto" : build;
// Is noBuild is true, we will not build the project
build = noBuild ? null : build;
String sdgAsJSONString = SystemDependencyGraph.construct(input, dependencies, build);
JsonElement sdgAsJSONElement = gson.fromJson(sdgAsJSONString, JsonElement.class);
JsonObject sdgAsJSONObject = sdgAsJSONElement.getAsJsonObject();

Expand Down
11 changes: 3 additions & 8 deletions src/main/java/com/ibm/northstar/SystemDependencyGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private static org.jgrapht.Graph<Pair<String, Callable>, AbstractGraphEdge> buil
*
* @param input the input
* @param dependencies the dependencies
* @param experimental the experimental
* @param build The build options
* @return A List of triples containing the source, destination, and edge type
* @throws IOException the io exception
* @throws ClassHierarchyException the class hierarchy exception
Expand All @@ -204,16 +204,11 @@ private static org.jgrapht.Graph<Pair<String, Callable>, AbstractGraphEdge> buil
* exception
*/
public static String construct(
String input, String dependencies, boolean experimental)
String input, String dependencies, String build)
throws IOException, ClassHierarchyException, IllegalArgumentException, CallGraphBuilderCancelException {

// Initialize scope
AnalysisScope scope;
if (dependencies == null) {
scope = ScopeUtils.createScope(input, experimental);
} else {
scope = ScopeUtils.createScope(input, dependencies, experimental);
}
AnalysisScope scope = ScopeUtils.createScope(input, dependencies, build);
IClassHierarchy cha = ClassHierarchyFactory.makeWithRoot(scope,
new ECJClassLoaderFactory(scope.getExclusions()));
Log.done("There were a total of " + cha.getNumberOfClasses() + " classes of which "
Expand Down
29 changes: 20 additions & 9 deletions src/main/java/com/ibm/northstar/utils/BuildProject.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,25 @@ public static boolean gradleBuild(String projectPath) {
return buildWithTool(gradleCommand);
}

private static boolean buildProject(String projectPath) {
private static boolean buildProject(String projectPath, String build) {
File pomFile = new File(projectPath, "pom.xml");
if (pomFile.exists()) {
Log.info("Found pom.xml in the project directory. Using Maven to build the project.");
return mavenBuild(projectPath); // Use Maven if pom.xml exists
} else {
Log.info("Did not find a pom.xml in the project directory. Using Gradle to build the project.");
return gradleBuild(projectPath); // Otherwise, use Gradle
if (build ==null) {
return true;
} else if (build.equals("auto")) {
if (pomFile.exists()) {
Log.info("Found pom.xml in the project directory. Using Maven to build the project.");
return mavenBuild(projectPath); // Use Maven if pom.xml exists
} else {
Log.info("Did not find a pom.xml in the project directory. Using Gradle to build the project.");
return gradleBuild(projectPath); // Otherwise, use Gradle
}
}
else {
// Update command with a project path
build = build.replace("mvn", "mvn -f " + projectPath);
Log.info("Using custom build command: " + build);
String[] customBuildCommand = build.split(" ");
return buildWithTool(customBuildCommand);
}
}

Expand All @@ -102,8 +113,8 @@ private static boolean buildProject(String projectPath) {
* @param projectPath is the path to the project to be streamed.
* @return true if the streaming was successful, false otherwise.
*/
public static List<Path> buildProjectAndStreamClassFiles(String projectPath) throws IOException {
return buildProject(projectPath) ? classFilesStream(projectPath) : null;
public static List<Path> buildProjectAndStreamClassFiles(String projectPath, String build) throws IOException {
return buildProject(projectPath, build) ? classFilesStream(projectPath) : null;
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/com/ibm/northstar/utils/ScopeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ public class ScopeUtils {
* @return scope The created analysis scope
* @throws IOException the io exception
*/
public static AnalysisScope createScope(String projectPath, boolean experimental)
throws IOException {
return createScope(projectPath, null, experimental);
}

/**
* Create an analysis scope base on the input
*
Expand All @@ -65,7 +60,7 @@ public static AnalysisScope createScope(String projectPath, boolean experimental
* @return scope The created analysis scope
* @throws IOException the io exception
*/
public static AnalysisScope createScope(String projectPath, String applicationDeps, boolean experimental)
public static AnalysisScope createScope(String projectPath, String applicationDeps, String build)
throws IOException {
Log.info("Create analysis scope.");
AnalysisScope scope = new JavaSourceAnalysisScope();
Expand Down Expand Up @@ -106,7 +101,7 @@ public static AnalysisScope createScope(String projectPath, String applicationDe
Path workDir = Paths.get(tmpDirString);
FileUtils.cleanDirectory(workDir.toFile());

List<Path> applicationClassFiles = BuildProject.buildProjectAndStreamClassFiles(projectPath);
List<Path> applicationClassFiles = BuildProject.buildProjectAndStreamClassFiles(projectPath, build);
Log.debug("Application class files: " + String.valueOf(applicationClassFiles.size()));
if (applicationClassFiles == null) {
Log.error("No application classes found.");
Expand Down

0 comments on commit 497e5b5

Please sign in to comment.