Skip to content

Commit

Permalink
Add version number to analysis.json
Browse files Browse the repository at this point in the history
Signed-off-by: Rahul Krishna <[email protected]>
  • Loading branch information
rahlk committed Oct 21, 2024
1 parent 84b2533 commit ce253b2
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/main/java/com/ibm/cldk/CodeAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
class VersionProvider implements CommandLine.IVersionProvider {
public String[] getVersion() throws Exception {
String version = getClass().getPackage().getImplementationVersion();
return new String[]{ "codeanalyzer " + (version != null ? version : "unknown") };
return new String[]{ version != null ? version : "unknown" };
}
}
/**
Expand Down Expand Up @@ -99,12 +99,12 @@ public void run() {
Log.setVerbosity(verbose);
try {
analyze();
} catch (IOException | CallGraphBuilderCancelException | ClassHierarchyException e) {
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private static void analyze() throws IOException, ClassHierarchyException, CallGraphBuilderCancelException {
private static void analyze() throws Exception {

JsonObject combinedJsonObject = new JsonObject();
Map<String, JavaCompilationUnit> symbolTable;
Expand Down Expand Up @@ -159,6 +159,7 @@ private static void analyze() throws IOException, ClassHierarchyException, CallG
symbolTable = existingSymbolTable;
}
}

else {
// construct symbol table for project, write parse problems to file in output directory if specified
Pair<Map<String, JavaCompilationUnit>, Map<String, List<Problem>>> symbolTableExtractionResult =
Expand All @@ -176,16 +177,14 @@ private static void analyze() throws IOException, ClassHierarchyException, CallG
String sdgAsJSONString = SystemDependencyGraph.construct(input, dependencies, build);
JsonElement sdgAsJSONElement = gson.fromJson(sdgAsJSONString, JsonElement.class);
JsonObject sdgAsJSONObject = sdgAsJSONElement.getAsJsonObject();
JsonElement edges = sdgAsJSONObject.get("edges");

// We don't really need these fields, so we'll remove it.
sdgAsJSONObject.remove("nodes");
sdgAsJSONObject.remove("creator");
sdgAsJSONObject.remove("version");

// Remove the 'edges' element and move the list of edges up one level
JsonElement edges = sdgAsJSONObject.get("edges");
combinedJsonObject.add("system_dependency_graph", edges);

// Remove the 'edges' element and move the list of edges up one level
combinedJsonObject.add("system_dependency_graph", edges);
}
}
// Cleanup library dependencies directory
Expand All @@ -196,6 +195,17 @@ private static void analyze() throws IOException, ClassHierarchyException, CallG
JsonElement symbolTableJSON = gson.fromJson(symbolTableJSONString, JsonElement.class);
combinedJsonObject.add("symbol_table", symbolTableJSON);

// Add version number to the output JSON
try {
String[] versions = new VersionProvider().getVersion();
if (versions.length > 0) {
combinedJsonObject.addProperty("version", versions[0]);
} else {
combinedJsonObject.addProperty("version", "unknown");
}
} catch (Exception e) {
combinedJsonObject.addProperty("version", "error retrieving version");
}
String consolidatedJSONString = gson.toJson(combinedJsonObject);
emit(consolidatedJSONString);
}
Expand Down

0 comments on commit ce253b2

Please sign in to comment.