Skip to content

Commit 497e5b5

Browse files
authored
Merge pull request #10 from IBM/add-a-build-cli-flag
Codeanalyzer now has a few extra flags.
2 parents f918e3a + 3584331 commit 497e5b5

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

src/main/java/com/ibm/northstar/CodeAnalyzer.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public class CodeAnalyzer implements Runnable {
4646
@Option(names = {"-i", "--input"}, required = true, description = "Path to the project root directory.")
4747
private static String input;
4848

49+
@Option(names = {"-b", "--build-cmd"}, description = "Custom build command. Defaults to auto build.")
50+
private static String build;
51+
52+
@Option(names = {"--no-build"}, description = "Do not build your application. Use this option if you have already built your application.")
53+
private static boolean noBuild = false;
54+
4955
@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")
5056
private static int analysisLevel = 1;
5157

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

src/main/java/com/ibm/northstar/SystemDependencyGraph.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ private static org.jgrapht.Graph<Pair<String, Callable>, AbstractGraphEdge> buil
195195
*
196196
* @param input the input
197197
* @param dependencies the dependencies
198-
* @param experimental the experimental
198+
* @param build The build options
199199
* @return A List of triples containing the source, destination, and edge type
200200
* @throws IOException the io exception
201201
* @throws ClassHierarchyException the class hierarchy exception
@@ -204,16 +204,11 @@ private static org.jgrapht.Graph<Pair<String, Callable>, AbstractGraphEdge> buil
204204
* exception
205205
*/
206206
public static String construct(
207-
String input, String dependencies, boolean experimental)
207+
String input, String dependencies, String build)
208208
throws IOException, ClassHierarchyException, IllegalArgumentException, CallGraphBuilderCancelException {
209209

210210
// Initialize scope
211-
AnalysisScope scope;
212-
if (dependencies == null) {
213-
scope = ScopeUtils.createScope(input, experimental);
214-
} else {
215-
scope = ScopeUtils.createScope(input, dependencies, experimental);
216-
}
211+
AnalysisScope scope = ScopeUtils.createScope(input, dependencies, build);
217212
IClassHierarchy cha = ClassHierarchyFactory.makeWithRoot(scope,
218213
new ECJClassLoaderFactory(scope.getExclusions()));
219214
Log.done("There were a total of " + cha.getNumberOfClasses() + " classes of which "

src/main/java/com/ibm/northstar/utils/BuildProject.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,25 @@ public static boolean gradleBuild(String projectPath) {
8585
return buildWithTool(gradleCommand);
8686
}
8787

88-
private static boolean buildProject(String projectPath) {
88+
private static boolean buildProject(String projectPath, String build) {
8989
File pomFile = new File(projectPath, "pom.xml");
90-
if (pomFile.exists()) {
91-
Log.info("Found pom.xml in the project directory. Using Maven to build the project.");
92-
return mavenBuild(projectPath); // Use Maven if pom.xml exists
93-
} else {
94-
Log.info("Did not find a pom.xml in the project directory. Using Gradle to build the project.");
95-
return gradleBuild(projectPath); // Otherwise, use Gradle
90+
if (build ==null) {
91+
return true;
92+
} else if (build.equals("auto")) {
93+
if (pomFile.exists()) {
94+
Log.info("Found pom.xml in the project directory. Using Maven to build the project.");
95+
return mavenBuild(projectPath); // Use Maven if pom.xml exists
96+
} else {
97+
Log.info("Did not find a pom.xml in the project directory. Using Gradle to build the project.");
98+
return gradleBuild(projectPath); // Otherwise, use Gradle
99+
}
100+
}
101+
else {
102+
// Update command with a project path
103+
build = build.replace("mvn", "mvn -f " + projectPath);
104+
Log.info("Using custom build command: " + build);
105+
String[] customBuildCommand = build.split(" ");
106+
return buildWithTool(customBuildCommand);
96107
}
97108
}
98109

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

109120
/**

src/main/java/com/ibm/northstar/utils/ScopeUtils.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ public class ScopeUtils {
5252
* @return scope The created analysis scope
5353
* @throws IOException the io exception
5454
*/
55-
public static AnalysisScope createScope(String projectPath, boolean experimental)
56-
throws IOException {
57-
return createScope(projectPath, null, experimental);
58-
}
59-
6055
/**
6156
* Create an analysis scope base on the input
6257
*
@@ -65,7 +60,7 @@ public static AnalysisScope createScope(String projectPath, boolean experimental
6560
* @return scope The created analysis scope
6661
* @throws IOException the io exception
6762
*/
68-
public static AnalysisScope createScope(String projectPath, String applicationDeps, boolean experimental)
63+
public static AnalysisScope createScope(String projectPath, String applicationDeps, String build)
6964
throws IOException {
7065
Log.info("Create analysis scope.");
7166
AnalysisScope scope = new JavaSourceAnalysisScope();
@@ -106,7 +101,7 @@ public static AnalysisScope createScope(String projectPath, String applicationDe
106101
Path workDir = Paths.get(tmpDirString);
107102
FileUtils.cleanDirectory(workDir.toFile());
108103

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

0 commit comments

Comments
 (0)