Added output support for graphviz - select by passing 'text'/'dot' before opam-switch file

This commit is contained in:
rand00 2022-01-06 14:53:36 +01:00
parent 3a738d9299
commit 8440053608
3 changed files with 47 additions and 5 deletions

View file

@ -13,10 +13,17 @@ let read_file file =
let () =
match Sys.argv with
| [| _ ; file |] ->
| [| _ ; output_format; file |] -> (
let switch = read_file file in
let data = OpamFile.SwitchExport.read_from_string switch in
Opam_graph.dependencies data
let graph = Opam_graph.dependencies data in
match output_format with
| "text" -> Format.printf "%a" Opam_graph.pp_graph graph
| "dot" ->
let dot = Opam_graph.Dot.of_graph graph in
Format.printf "%a" Opam_graph.Dot.pp dot
| _ -> failwith "Unsupported output format"
)
| _ ->
print_endline "expecting exactly one argument";
exit 1

View file

@ -1,4 +1,6 @@
(library
(name opam_graph)
(public_name opam-graph)
(libraries opam-core opam-format))
(libraries opam-core opam-format dot)
(flags (:standard (-w -27-26)))
)

View file

@ -74,5 +74,38 @@ let dependencies (switch : OpamFile.SwitchExport.t) =
in
find_deps graph work
in
let graph = find_deps graph (Name_set.singleton top) in
Format.printf "%a" pp_graph graph
find_deps graph (Name_set.singleton top)
module Dot = struct
type t = Odot.graph
let of_graph (graph:graph) : t =
let open Odot in
let stmt_list =
Name_map.fold (fun pkg deps acc ->
let stmt =
let pkg_id = Double_quoted_id (OpamPackage.Name.to_string pkg) in
let pkg_point = Edge_node_id (pkg_id, None) in
let deps_points =
Name_set.elements deps
|> List.map (fun p ->
let id = Double_quoted_id (OpamPackage.Name.to_string p) in
Edge_node_id (id, None)
)
in
let edge = pkg_point, deps_points, [] in
Stmt_edge edge
in
stmt :: acc
) graph.nodes []
in
{ strict = false; (*todo test params*)
kind = Digraph;
id = None;
stmt_list }
let pp ppf dot =
Format.fprintf ppf "%s" (Odot.string_of_graph dot)
end