Skip to content

Commit c6bce92

Browse files
committed
Add --no-dependents option to output modules without dependents
- Introduced a new command-line flag `--no-dependents` and its shorthand `-nd` to filter output for modules that have no dependents. - Implemented functionality in the formatter to handle output in both DOT and JSON formats for modules without dependents. - Added a helper function to identify modules with no dependents in the dependency graph.
1 parent 4bb70ab commit c6bce92

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

bin/main.ml

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ let verbose = ref false
88
let benchmark = ref false
99
let skip_cache = ref false
1010
let clear_cache = ref false
11+
let no_dependents = ref false
1112

1213
let spec_list =
1314
[
@@ -51,6 +52,12 @@ let spec_list =
5152
( "--clear-cache",
5253
Arg.Set clear_cache,
5354
"Clear the in-memory cache before analyzing" );
55+
( "--no-dependents",
56+
Arg.Set no_dependents,
57+
"Output modules with no dependents" );
58+
( "-nd",
59+
Arg.Set no_dependents,
60+
"Output modules with no dependents (short for --no-dependents)" );
5461
]
5562

5663
let anon_fun file = input_files := file :: !input_files
@@ -142,12 +149,19 @@ let main () =
142149
(* Output to file only *)
143150
if !verbose then Printf.eprintf "Writing output to file: %s\n" file;
144151
let out_channel = open_out file in
145-
Rescriptdep.Formatter.output_graph !format focused_graph out_channel;
152+
if !no_dependents then
153+
Rescriptdep.Formatter.output_no_dependents !format focused_graph
154+
out_channel
155+
else
156+
Rescriptdep.Formatter.output_graph !format focused_graph out_channel;
146157
close_out out_channel
147158
| None ->
148159
(* Output to stdout when no file output option is provided *)
149160
if !verbose then Printf.eprintf "Writing output to stdout\n";
150-
Rescriptdep.Formatter.output_graph !format focused_graph stdout);
161+
if !no_dependents then
162+
Rescriptdep.Formatter.output_no_dependents !format focused_graph
163+
stdout
164+
else Rescriptdep.Formatter.output_graph !format focused_graph stdout);
151165

152166
let output_end_time = Unix.gettimeofday () in
153167
let output_time = output_end_time -. output_start_time in

lib/dependency_graph.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,8 @@ let create_focused_graph graph center_module =
330330
in
331331

332332
result
333+
334+
(* Find modules with no dependents *)
335+
let find_modules_with_no_dependents graph =
336+
let modules = get_modules graph in
337+
List.filter (fun m -> find_dependents graph m = []) modules

lib/formatter.ml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,26 @@ and output_json graph out_channel =
421421
(* JSON closing *)
422422
output_string out_channel "}\n"
423423

424+
(* Output modules with no dependents *)
425+
let output_no_dependents_dot modules out_channel =
426+
output_string out_channel "digraph G {\n";
427+
output_string out_channel " rankdir=LR;\n";
428+
output_string out_channel " node [shape=box];\n";
429+
List.iter
430+
(fun m -> output_string out_channel (Printf.sprintf " \"%s\";\n" m))
431+
modules;
432+
output_string out_channel "}\n"
433+
434+
let output_no_dependents_json modules out_channel =
435+
let json = `List (List.map (fun m -> `String m) modules) in
436+
Yojson.Basic.pretty_to_channel out_channel json
437+
438+
let output_no_dependents format graph out_channel =
439+
let modules = Dependency_graph.find_modules_with_no_dependents graph in
440+
match format with
441+
| Dot -> output_no_dependents_dot modules out_channel
442+
| Json -> output_no_dependents_json modules out_channel
443+
424444
(* String representation of a format *)
425445
let format_to_string = function Dot -> "dot" | Json -> "json"
426446

0 commit comments

Comments
 (0)