From ce253b2cd74fb9b0796fd8627fb87c66ca2dca7c Mon Sep 17 00:00:00 2001 From: Rahul Krishna Date: Mon, 21 Oct 2024 10:59:41 -0400 Subject: [PATCH] Add version number to analysis.json Signed-off-by: Rahul Krishna --- src/main/java/com/ibm/cldk/CodeAnalyzer.java | 26 ++++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/ibm/cldk/CodeAnalyzer.java b/src/main/java/com/ibm/cldk/CodeAnalyzer.java index 81badcc..0b9dd71 100644 --- a/src/main/java/com/ibm/cldk/CodeAnalyzer.java +++ b/src/main/java/com/ibm/cldk/CodeAnalyzer.java @@ -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" }; } } /** @@ -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 symbolTable; @@ -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>> symbolTableExtractionResult = @@ -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 @@ -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); }