Use the flag module in tools/imports-graph.elv.

This commit is contained in:
Qi Xiao 2021-12-31 18:49:59 +00:00
parent 5bf688cf4d
commit 32a2c4c73d

View File

@ -1,19 +1,22 @@
use flag
use re
use str
var prefix = src.elv.sh/
var merge-clusters = $false
var imports-of = [&]
var q = [$prefix''cmd/elvish]
var seen = [&q[0]=$true]
var clusters = [&]
fn keep-if {|p| each {|x| if ($p $x) { put $x }} }
fn get {|x k def| if (has-key $x $k) { put $x[$k] } else { put $def } }
fn get-cluster {|x| put (re:find '^'(re:quote $prefix)'[^/]+/[^/]+' $x)[text] }
fn node {|x| put '"'(str:trim-prefix $x $prefix)'"' }
while (not-eq $q []) {
fn main {|&merge-clusters=$false|
var imports-of = [&]
var q = [$prefix''cmd/elvish]
var seen = [&q[0]=$true]
var clusters = [&]
while (not-eq $q []) {
var next-q = []
for pkg $q {
var c = (get-cluster $pkg)
@ -31,16 +34,14 @@ while (not-eq $q []) {
set @next-q = (all $next-q) (all $new-pkgs)
}
set q = $next-q
}
}
fn node {|x| put '"'(str:trim-prefix $x $prefix)'"' }
echo 'strict digraph imports {'
echo ' rankdir = LR;'
echo ' node [shape = box, width = 1.5];'
echo ' splines = ortho;'
echo ' nodesep = 0.1;'
if $merge-clusters {
echo 'strict digraph imports {'
echo ' rankdir = LR;'
echo ' node [shape = box, width = 1.5];'
echo ' splines = ortho;'
echo ' nodesep = 0.1;'
if $merge-clusters {
for pkg [(keys $imports-of)] {
for import $imports-of[$pkg] {
var src = (get-cluster $pkg)
@ -50,7 +51,7 @@ if $merge-clusters {
}
}
}
} else {
} else {
var cluster-seq = 0
for c [(keys $clusters)] {
var pkgs = $clusters[$c]
@ -69,5 +70,8 @@ if $merge-clusters {
echo ' '(node $pkg)' -> '(node $import)';'
}
}
}
echo '}'
}
echo '}'
flag:call $main~ $args