createGraphImage method

void createGraphImage(
  1. String outputPath
)

Implementation

void createGraphImage(String outputPath) {
  final graph = StringBuffer();

  // Create the DOT representation of the graph
  graph.writeln('strict graph {');

  _graph.forEach((package, dependencies) {
    for (var dependency in dependencies) {
      graph.writeln('  "$package" -- "$dependency";');
    }
  });

  graph.writeln('}');

  // Write the DOT representation to a temporary file
  final dotFile = File('temp_graph.dot');
  dotFile.writeAsStringSync(graph.toString());

  // Use the dot command to generate a PNG image
  final result = Process.runSync('dot', ['-Tpng', 'temp_graph.dot', '-o', outputPath]);

  if (result.exitCode != 0) {
    print('Error generating PNG: ${result.stderr}');
  } else {
    print('Graph image created at: $outputPath');
  }

  // Optionally, remove the temporary DOT file
  dotFile.deleteSync();
}