GraphJsonExporter.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.IO;
  2. using System.Linq;
  3. using System.Text;
  4. // using Newtonsoft.Json;
  5. using IntelligentProjectAnalyzer.Editor.DependencyViewer;
  6. using IntelligentProjectAnalyzer.Helper;
  7. namespace IntelligentProjectAnalyzer.Editor.Graphing
  8. {
  9. public static class GraphJsonExporter
  10. {
  11. /// <summary>
  12. /// Exports the dependency and reference graph for a given asset as a compact JSON string.
  13. /// </summary>
  14. /// <param name="rootAssetGuid">The GUID of the asset to start the graph traversal from.</param>
  15. /// <param name="jsonOutput">The resulting JSON string if the export is successful.</param>
  16. /// <returns>True if the root asset exists and the graph was exported, otherwise false.</returns>
  17. public static bool TryExportAssetGraph(string rootAssetGuid, out string jsonOutput)
  18. {
  19. if (TryBuildGraph(rootAssetGuid, out var serializableGraph))
  20. {
  21. jsonOutput = JsonFileSystem.GetJson(serializableGraph);
  22. return true;
  23. }
  24. jsonOutput = null;
  25. return false;
  26. }
  27. /// <summary>
  28. /// Appends a human-readable representation of the asset's dependency graph to a StringBuilder.
  29. /// </summary>
  30. /// <param name="rootAssetGuid">The GUID of the asset to analyze.</param>
  31. /// <param name="builder">The StringBuilder to append the report to.</param>
  32. /// <returns>True if the root asset exists and the report was generated, otherwise false.</returns>
  33. public static bool TryAppendHumanReadableGraph(string rootAssetGuid, StringBuilder builder)
  34. {
  35. if (!TryBuildGraph(rootAssetGuid, out var graph))
  36. {
  37. return false;
  38. }
  39. if (graph.Nodes == null || graph.Links == null || !graph.Nodes.Any()) return true;
  40. var nodeMap = graph.Nodes
  41. .Select((node, index) => new { node, index })
  42. .ToDictionary(item => item.index, item => item.node);
  43. builder.AppendLine($"- Dependency Report for: {Path.GetFileName(nodeMap[0].Path)}");
  44. foreach (var link in graph.Links)
  45. {
  46. if (nodeMap.TryGetValue(link.Source, out var sourceNode) &&
  47. nodeMap.TryGetValue(link.Target, out var targetNode))
  48. {
  49. builder.AppendLine($" - [{sourceNode.Type}] '{Path.GetFileName(sourceNode.Path)}' (Guid: {sourceNode.Guid}) -> USES -> [{targetNode.Type}] '{Path.GetFileName(targetNode.Path)}' (Guid: {targetNode.Guid})");
  50. }
  51. }
  52. builder.AppendLine("--- End of Report ---");
  53. return true;
  54. }
  55. /// <summary>
  56. /// Private helper to build the graph, shared by both export methods.
  57. /// </summary>
  58. private static bool TryBuildGraph(string rootAssetGuid, out SerializableGraph graph)
  59. {
  60. var dependencyGraph = new DependencyGraph();
  61. if (!dependencyGraph.AssetExists(rootAssetGuid))
  62. {
  63. graph = null;
  64. return false;
  65. }
  66. var graphBuilder = new GraphBuilder(dependencyGraph);
  67. graph = graphBuilder.BuildFromRoot(rootAssetGuid);
  68. return graph != null;
  69. }
  70. }
  71. }