1 namespace eval Commands {
11 variable alias_index {}
13 variable name_guards {}
17 variable _current_node {}
18 variable _current_path {}
21 variable _current_node
26 variable _current_path
38 set _a [Launcher::GetOr args {}]
39 if {$_a eq {}} {
return {}}
44 proc Arg {index {default ""}} {
45 set _v [
lindex [
Args] $index]
46 if {$_v eq ""} {
return $default}
66 { description str "" }
74 { requires_proj bool 0 }
76 { passthrough bool 0 }
80 proc New {key raw_dict} {
82 set norm [dict create]
83 dict for {k v} $raw_dict { dict set norm [string tolower $k] $v }
86 set node [tdict create]
87 foreach field $_fields {
88 lassign $field fname ftype fdefault
93 [dict exists $raw_dict name] && [dict get $raw_dict name] ne ""
94 ? [string toupper [string trim [dict get $raw_dict name]]] : [string toupper $key]
96 if {$name eq ""} { error "Command has no name"}
97 if {[
string first "." $name] >= 0} { error "Command '$name': name may not contain '.'"}
98 tdict set node name [
tstr $name]
104 foreach a [
expr {[dict exists $raw_dict aliases] ? [dict get $raw_dict aliases] : {}}] {
105 set au [
string toupper $a]
106 if {$au ne "" && [
string first "." $au] < 0 && $au ne [tdict getval $node name] && $au ni $aliases} {
lappend aliases $au}
108 tdict set node aliases [tlist create {*}$aliases]
113 if {[dict exists $raw_dict $fname] && [dict get $raw_dict $fname] ne ""} {
114 set v [dict get $raw_dict $fname]
119 str { tdict set node $fname [
tstr [
string trim $v]]}
120 bool { tdict set node $fname [
tbool $v]}
121 list { tdict set node $fname [tlist create {*}$v]}
128 proc HasStages {node} {
return [
expr {[tlist length [tdict getobjor $node stages [tlist create]]] > 0}]}
129 proc IsRunnable {node} {
return [
expr {[tdict getval $node script] ne "" || [HasStages $node]}]}
130 proc RequiresProj {node} {
return [tdict getval $node requires_proj]}
132 proc IsTool {node} {
return [regexp {^TOOL\.[^.]+$} [tdict getor $node canon ""]]}
133 proc IsFlow {node} {
return [regexp {^TOOL\.[^.]+\.FLOW\.[^.]+$} [tdict getor $node canon ""]]}
134 proc IsStage {node} {
return [regexp {^TOOL\.[^.]+\.STAGE\.[^.]+$} [tdict getor $node canon ""]]}
139 proc _sibling {node ref} {
140 set canon [tdict getval $node canon]
141 set idx [
string last "." $canon]
142 if {$idx < 0} {
return {}}
151 proc FlattenStages {node} {
return [dict get [
_walk_stages $node] stages]}
160 proc _walk_stages {node {_seen {}}} {
161 set canon [tdict getval $node canon]
162 if {$canon in $_seen} {
163 return [dict create stages {} badrefs {} cycles [list [list $canon $_seen]]]
169 tlist foreachval s [tdict getobjor $node stages [tlist create]] {
170 if {![string match "@*" $s]} { lappend stages $s; continue }
171 set rnode [_sibling $node [string range $s 1 end]]
172 if {$rnode eq {}} { lappend badrefs [list $canon $s]; continue }
173 set sub [_walk_stages $rnode $_seen]
174 lappend stages {*}[dict get $sub stages]
175 lappend badrefs {*}[dict get $sub badrefs]
176 lappend cycles {*}[dict get $sub cycles]
178 return [dict create stages $stages badrefs $badrefs cycles $cycles]
182 proc FlattenOptions {node {_seen {}}} {
183 set canon [tdict getval $node canon]
184 if {$canon in $_seen} {
return {}}
189 tlist foreachval o [tdict getobjor $node options [tlist create]] { lappend specs $o }
190 tlist foreachval s [tdict getobjor $node stages [tlist create]] {
191 if {[string match "@*" $s]} {
192 set rnode [_sibling $node [string range $s 1 end]]
193 if {$rnode ne {}} { lappend specs {*}[FlattenOptions $rnode $_seen] }
195 set snode [::Commands::GetCommand "TOOL.[::Commands::_tool_of $canon].STAGE.$s"]
197 tlist foreachval o [tdict getobjor $snode options [tlist create]] { lappend specs $o }
205 foreach spec $specs {
206 set n [
lindex $spec 0]
207 if {$n ni $names} {
lappend result $spec
lappend names $n}
212 proc Run {node {path {}}} {
214 error "Cannot run '[tdict getval $node name]' - no script or stages defined"
216 set _prev_node ${::Commands::_current_node}
217 set _prev_path ${::Commands::_current_path}
218 set ::Commands::_current_node $node
219 set ::Commands::_current_path $path
225 set _name [tdict getval $node name]
228 set _code [
catch {uplevel #0 [tdict getval $node script]} _res _opts]
230 set ::Commands::_current_node $_prev_node
231 set ::Commands::_current_path $_prev_path
232 return -options $_opts $_res
237 proc AddNameGuard {name reason} {
239 dict set name_guards [
string toupper $name] $reason
244 set key [
string toupper $name]
245 if {[dict exists $name_guards $key]} {
return [dict get $name_guards $key]}
257 proc _canon {alias} {
259 if {$alias eq ""} {
return ""}
260 set key [
string toupper $alias]
261 if {[dict exists $alias_index $key]} {
return [dict get $alias_index $key]}
262 set parts [
split $key "."]
263 if {[
llength $parts] < 2} {
return ""}
264 set canon [
_canon [
lindex $parts 0]]
265 if {$canon eq ""} {
return ""}
266 foreach part [
lrange $parts 1 end] {
268 if {$canon eq ""} {
return ""}
273 proc _canon_child {parent token} {
275 set key "$parent.[
string toupper $token]"
276 if {[dict exists $alias_index $key]} {
return [dict get $alias_index $key]}
287 proc _register_at {node parent is_custom source} {
291 set bare_name [tdict getval $node name]
292 set canon [
expr {$parent eq "" ? $bare_name : "$parent.$bare_name"}]
293 tdict set node custom [
tbool $is_custom]
294 tdict set node canon [
tstr $canon]
295 tdict set node source_path [
tstr $source]
299 tlist foreachval a [tdict getval $node aliases] { lappend aliases $a }
301 dict set nodes $canon $node
303 dict set alias_index $canon $canon
304 foreach a [list $name {*}$aliases] {
305 set key [
expr {$parent eq "" ? $a : "$parent.$a"}]
306 if {$key ne $canon && ![dict exists $alias_index $key]} {
307 dict set alias_index $key $canon
313 proc GetChildren {canon} {
316 set plen [
string length $prefix]
318 dict for {k v} $nodes {
319 if {[string equal -length $plen $prefix $k] && [string first "." $k $plen] < 0} {
326 proc _deregister {canon} {
330 if {![dict exists $nodes $canon]} {
return}
334 foreach k [dict keys [dict filter $alias_index value $canon]] {
335 dict unset alias_index $k
338 dict unset nodes $canon
344 proc RegisterCommand {key raw_dict args} {
347 set is_custom [
expr {"-custom" in $args}]
349 if {[
set _si [lsearch -exact $args -source]] >= 0} {
set source [
lindex $args [
expr {$_si+1}]]}
351 set idx [
string last "." $key]
352 set bare_key [
expr {$idx < 0 ? $key : [string range $key [expr {$idx+1}] end]}]
355 set parent [
_canon [
string toupper [
string range $key 0 [
expr {$idx-1}]]]]
356 if {$parent eq "" || ![dict exists $nodes $parent]} {
357 Msg Warning "RegisterCommand: parent for '$key' not found"
360 set parent_node [dict get $nodes $parent]
361 if {"-custom" ni $args} {
set is_custom [tobj value [tdict get $parent_node custom]]}
362 if {$source eq ""} {
set source [tdict getval $parent_node source_path]}
365 if {"-custom" ni $args} {
set is_custom [
set ::Hog::_loading_custom]}
366 if {$source eq ""} {
set source [
set ::Hog::_loading_source]}
369 if {[
catch {
Node::New $bare_key $raw_dict} node]} {
370 Msg Warning "Skipping command '$key': $node"
373 set name [tdict getval $node name]
376 if {$parent eq "" && [
set why [
_veto $name]] ne ""} {
377 Msg Warning "Command '$name' rejected: $why"
380 set existing [
expr {$parent eq "" ? [_canon $name] : [_canon_child $parent $name]}]
381 if {$existing ne ""} {
382 Msg Warning "Command '$name' already exists, skipping"
388 tlist foreachval a [tdict getval $node aliases] {
389 set a_existing [expr {$parent eq "" ? [_canon $a] : [_canon_child $parent $a]}]
390 if {$parent eq "" && [set why [_veto $a]] ne ""} {
391 Msg Warning "Alias '$a' for '$name' rejected: $why"
392 } elseif {$a_existing ne ""} {
393 Msg Warning "Alias '$a' for '$name' already exists, skipping alias"
398 tdict set node aliases [tlist create {*}$clean]
405 proc AddAlias {alias canon} {
408 if {![dict exists $nodes $canon]} {
409 Msg Warning "AddAlias: '$canon' does not exist, skipping alias '$alias'"
412 set key [
string toupper $alias]
413 if {[
set why [
_veto $key]] ne ""} {
414 Msg Warning "Alias '$alias' for '$canon' rejected: $why"
417 if {[dict exists $alias_index $key]} {
418 if {[dict get $alias_index $key] ne $canon} {
419 Msg Warning "Alias '$alias' already exists, skipping (wanted for '$canon')"
423 dict set alias_index $key $canon
428 proc RegisterCommandsFile {f args} {
432 proc RegisterCommandsDir {dir args} {
433 if {![
file isdirectory $dir]} {
return}
434 foreach f [lsort [glob -nocomplain -directory $dir *.tcl]] {
437 foreach sub [lsort [glob -nocomplain -type d -directory $dir *]] {
438 set name [
file tail $sub]
439 set entry [
file join $sub "$name.tcl"]
440 if {![
file exists $entry]} {
set entry [
file join $sub "main.tcl"]}
441 if {![
file exists $entry]} {
442 Msg Warning "Command directory '$name' has no '$name.tcl' or 'main.tcl', skipping"
451 proc UpdateCommand {key new_dict} {
454 set canon [
string toupper $key]
455 if {![dict exists $nodes $canon]} {
set canon [
_canon $canon]}
456 if {$canon eq "" || ![dict exists $nodes $canon]} {
457 Msg Warning "UpdateCommand: '$key' not found - use RegisterCommand to add new commands"
460 set idx [
string last "." $canon]
461 set bare [
expr {$idx < 0 ? $canon : [string range $canon [expr {$idx+1}] end]}]
462 set parent [
expr {$idx < 0 ? "" : [string range $canon 0 [expr {$idx-1}]]}]
463 if {[
catch {
Node::New $bare $new_dict} node]} {
464 Msg Warning "UpdateCommand: skipping '$canon': $node"
467 set existing [dict get $nodes $canon]
468 tdict set node custom [tdict get $existing custom]
469 tdict set node canon [tdict get $existing canon]
470 tdict set node source_path [tdict get $existing source_path]
473 foreach k [dict keys $alias_index] {
474 if {$k ne $canon && [dict get $alias_index $k] eq $canon} { dict unset alias_index $k}
477 tlist foreachval a [tdict getval $node aliases] {
478 set a_existing [expr {$parent eq "" ? [_canon $a] : [_canon_child $parent $a]}]
479 if {[set why [_veto $a]] ne ""} {
480 Msg Warning "Alias '$a' for '$bare' rejected: $why"
481 } elseif {$a_existing ne "" && $a_existing ne $canon} {
482 Msg Warning "Alias '$a' for '$bare' already exists, skipping alias"
487 tdict set node aliases [tlist create {*}$clean]
489 set a_key [
expr {$parent eq "" ? $a : "$parent.$a"}]
490 if {$a_key ne $canon} { dict set alias_index $a_key $canon}
493 dict set nodes $canon $node
497 proc RemoveCommand {key} {
499 set canon [
string toupper $key]
500 if {![dict exists $nodes $canon]} {
set canon [
_canon $canon]}
501 if {$canon eq "" || [
string first "." $canon] >= 0 || ![dict exists $nodes $canon]} {
502 Msg Warning "RemoveCommand: '$key' not found"
509 proc AliasExists {alias} {
511 return [dict exists $alias_index [
string toupper $alias]]
515 set canon [
_canon [
string toupper $cmd]]
516 if {$canon eq ""} {
Msg Error "Unknown command '$cmd'"
return}
521 proc GetCommand {cmd} {
523 set canon [
_canon [
string toupper $cmd]]
524 if {$canon eq "" || ![dict exists $nodes $canon]} {
return {}}
525 return [dict get $nodes $canon]
529 proc _tool_of {canon} {
530 if {[regexp {^TOOL\.([^.]+)\.} $canon -> bare]} {
return $bare}
535 proc _flows_naming_stage {node} {
536 set canon [tdict getval $node canon]
538 set stage_name [
string toupper [tdict getval $node name]]
542 if {$fnode eq {}} continue
543 tlist foreachval s [tdict getobjor $fnode stages [tlist create]] {
544 if {[string match "@*" $s]} continue
545 if {[string toupper $s] eq $stage_name} { lappend result $fnode; break }
556 proc GetCommandOptions {cmd_or_node} {
557 if {[tobj isobj $cmd_or_node] && [tobj type $cmd_or_node] eq "Dict"} {
558 set node $cmd_or_node
562 if {$node eq {}} {
return {}}
567 tlist foreachval o [tdict get $node options] {
569 if {$n ni $names} { lappend result $o; lappend names $n }
573 set n [
lindex $spec 0]
574 if {$n ni $names} {
lappend result $spec
lappend names $n}
582 proc GetCommands {{depth 1}} {
585 dict for {canon node} $nodes {
586 if {[llength [split $canon "."]] <= $depth} { dict set result $canon $node }
592 proc _owning_stage_proc {canon stage} {
593 if {[
string match "::*" $stage]} {
return $stage}
595 set snode [
GetCommand "TOOL.$tool_bare.STAGE.$stage"]
596 if {$snode ne {}} {
return [tdict getval $snode script]}
598 if {$tool_ns eq ""} {
return ""}
599 return ${tool_ns}::${stage}
617 dict for {canon node} $nodes {
618 set children [GetChildren $canon]
619 set has_script [expr {[tdict getval $node script] ne ""}]
620 set has_stages [Node::HasStages $node]
621 set is_flow [Node::IsFlow $node]
622 set is_stage [Node::IsStage $node]
624 if {$has_script && $has_stages} {
625 lappend problems "$canon declares both 'script' and 'stages'.\
626 Node::Run branches on 'stages' first, so the script is dead code."
629 if {[tdict getval $node passthrough] && [llength $children] == 0} {
630 lappend problems "$canon is 'passthrough true' but has no children.\
631 The flag only changes how a following token is resolved against\
632 children, so it does nothing here."
635 if {[Node::RequiresProj $node] && [llength $children] > 0 && \
636 ![tdict getval $node passthrough]} {
637 lappend problems "$canon requires a project and has children, but is not\
638 'passthrough true'. Its project argument will be read as a subcommand\
642 if {$is_flow && ![Node::RequiresProj $node]} {
643 lappend problems "$canon is a flow with 'requires_proj false'. Flows are\
644 project-scoped by definition; this one cannot be reached as a flow."
647 # A declared stage's script is a proc name, so it is checkable. This is how
648 # a typo in 2-arg 'RegisterStage DoNothing' gets caught - the node registers
649 # happily and names a proc that was never defined.
651 set p [tdict getval $node script]
652 if {$p ne "" && [info commands $p] eq ""} {
653 lappend problems "$canon declares a stage backed by '$p', which is not\
654 a defined proc. Two-arg RegisterStage names a proc the tool must\
655 already define; three-arg RegisterStage defines it for you."
659 if {$is_flow && !$has_stages} {
660 lappend problems "$canon is a flow that declares no stages.\
661 Running it would do nothing."
663 if {!$has_stages} continue
665 set walk [Node::_walk_stages $node]
667 # Only problems this node itself declared - every node is visited, so
668 # filtering here reports each one once, against its author, instead of once
669 # per flow that @references it.
670 foreach bad [dict get $walk badrefs] {
671 lassign $bad owner ref
672 if {$owner ne $canon} continue
673 lappend problems "$canon references unknown flow '$ref'. It will be\
674 skipped, silently shortening the stage list."
677 foreach cyc [dict get $walk cycles] {
678 lassign $cyc owner chain
679 if {$owner ne $canon} continue
680 # A cycle is discovered once from each participant (A->B->A is found
681 # walking A *and* walking B), so key on the unordered set to name it once.
682 set sig [lsort -unique [concat $chain $owner]]
683 if {$sig in $seen_cycles} continue
684 lappend seen_cycles $sig
685 lappend problems "circular @reference: [join $chain { -> }] -> $owner.\
686 Flattening survives it, but the stage list is not what was intended."
689 if {[llength [dict get $walk stages]] == 0} {
690 lappend problems "$canon flattens to no stages - every stage it\
691 @references is unresolvable. Running it will fail."
694 # Bare names only, checked where they are written rather than where they are
695 # spliced in. A stage with its own STAGE node is checked above, at that node.
696 tlist foreachval st [tdict getobjor $node stages [tlist create]] {
697 if {[string match "@*" $st]} continue
698 set tool_bare [_tool_of $canon]
699 if {[GetCommand "TOOL.$tool_bare.STAGE.$st"] ne {}} continue
700 set p [_owning_stage_proc $canon $st]
701 if {$p eq "" || [info commands $p] eq ""} {
702 lappend problems "$canon names stage '$st', which resolves to no proc\
703 ([expr {$p eq "" ? "no tool owns '$tool_bare'" : "no proc $p"}])."
708 if {[
llength $problems] == 0} {
return 0}
710 set strict [
expr {[info exists ::env(HOG_STRICT)] &&
711 $::env(HOG_STRICT) ni {"" 0 false no}}]
712 foreach p $problems {
713 Msg [
expr {$strict ? "CriticalWarning" : "Warning"}] "Lint: $p"
716 error "Commands::Lint found [
llength $problems] problem(s) with HOG_STRICT set" \
719 return [
llength $problems]
725 proc ToolsWithFlow {name} {
727 set needle [
string toupper $name]
729 dict for {canon _n} $nodes {
730 if {![regexp {^TOOL\.([^.]+)\.FLOW$} $canon -> tool_name]} { continue }
731 if {[_canon_child $canon $needle] ne ""} { lappend result $tool_name }
741 proc ProducersOf {name} {
744 dict for {canon node} $nodes {
745 if {[tlist elemExists [tdict getobjor $node produces [tlist create]] $name]} {
746 lappend result $canon
756 proc ResolvePath {argv_list} {
759 if {[
llength $argv_list] == 0} {
return {}}
761 set first [
string toupper [
lindex $argv_list 0]]
763 if {$canon eq "" || ![dict exists $nodes $canon]} {
return {}}
765 set node [dict get $nodes $canon]
766 set path [list [
lindex $argv_list 0]]
767 set remaining [
lrange $argv_list 1 end]
769 while {[
llength [
GetChildren $canon]] > 0 && [
llength $remaining] > 0} {
770 set token [
lindex $remaining 0]
771 set next_canon [
_canon_child $canon [
string toupper $token]]
772 if {$next_canon eq "" || ![dict exists $nodes $next_canon]} break
773 set canon $next_canon
774 set node [dict get $nodes $next_canon]
776 set remaining [
lrange $remaining 1 end]
779 return [dict create node $node path $path remaining $remaining]
782 proc _strip_top {token} {
783 return [
string trimright [regsub {^(\./)?Top/} $token ""] "/ "]
786 proc ParseArgv {argv top_path} {
787 if {[
llength $argv] >= 2 && ![
string match "-*" [
lindex $argv 1]]} {
788 set argv [
lreplace $argv 1 1 [
_strip_top [
lindex $argv 1]]]
793 set directive [
string toupper [
lindex $argv 0]]
794 if {$directive ne "" && ![
AliasExists $directive] && [
llength $argv] >= 2} {
795 set proj [
lindex $argv 1]
796 if {[
file exists [
file join $top_path $proj hog.conf]]} {
799 set tool_key [
string toupper [
namespace tail $tool]]
800 if {[
_canon_child "TOOL.$tool_key.FLOW" $directive] ne ""} {
801 set argv [
concat [list $tool_key FLOW $directive] [
lrange $argv 1 end]]
807 set directive [
string toupper [
lindex $argv 0]]
809 if {[
llength $resolved] > 0} {
810 set node [dict get $resolved node]
811 set path [dict get $resolved path]
812 set rest [dict get $resolved remaining]
815 set path [list $directive]
816 set rest [
lrange $argv 1 end]
824 while {[
llength $rest] > 0 && ![
string match "-*" [
lindex $rest 0]]} {
825 lappend pargs [
lindex $rest 0]
826 set rest [
lrange $rest 1 end]
834 if {[
llength $pargs] > 0} {
835 set project_name [
_strip_top [
lindex $pargs 0]]
836 set pargs [
lreplace $pargs 0 0 $project_name]
839 return [dict create \
843 project [
file tail $project_name] \
844 project_name $project_name \
851 proc _InContext {ide} {
853 if {$ide eq "tclsh"} {
return [
expr {$here eq "tclsh"}]}
854 return [
expr {[Tools::ResolveAlias $ide] eq $here}]
858 proc _RunInContext {node full_path} {
859 cd [Repo::Get repo_path]
860 set ide [tdict getval $node ide]
861 if {$ide ne "tclsh"} {
862 set ::DataStore::inIDE 1
864 Launcher::Set ide $tool
865 CurrentProject::Set ide $tool
866 if {[
info commands ${tool}::Initialize] ne ""} { ${tool}::Initialize}
873 proc Dispatch {node typed_path project} {
874 set label [
join $typed_path { }]
875 set full_path [
expr {$project ne "" ? [concat $typed_path [list $project]] : $typed_path}]
878 set directive [
string toupper [
lindex $typed_path 0]]
880 if {[
llength $tools] > 0} {
881 return -code error "Flow '$directive' requires a project\
882 (provided by [
join [
string tolower $tools] {, }]).\
883 Usage: ./Hog/Do $directive <project>"
885 return -code error "Unknown directive '$directive'. Run './Hog/Do HELP' for usage."
888 set node_canon [tdict getval $node canon]
889 set has_children [
expr {[llength [GetChildren $node_canon]] > 0}]
892 if {$has_children && $project ne "" && ![tdict getval $node passthrough] && \
893 [
_canon_child $node_canon [
string toupper $project]] eq ""} {
894 return [list usage $typed_path "'$label' has no subcommand '[
string toupper $project]'"]
898 if {$has_children} {
return [list help $typed_path]}
899 return -code error "Command '$label' is not executable"
902 if {[tdict getval $node requires_proj] && $project eq ""} {
903 return -code error "Command '$label' requires a project. Usage: ./Hog/Do [
join $typed_path { }] <project>"
906 set ide [tdict getval $node ide]
911 return [list boot $ide]
916 proc Resolve {argv} {
917 set directive [
string toupper [
lindex $argv 0]]
918 set request [
ParseArgv $argv [Repo::Get top_path]]
920 Launcher::Set directive $directive
921 Launcher::Set cmd [
expr {[dict get $request node] ne {} ? [tdict getval [dict get $request node] canon] : ""}]
922 Launcher::Set full_cmd [dict get $request typed_path]
923 Launcher::Set args [tlist createstr {*}[dict get $request args]]
924 Launcher::Set project [dict get $request project]
925 Launcher::Set project_name [dict get $request project_name]
928 Launcher::Set options [tlist createstr {*}[dict get $request options]]
934 proc RunRequest {request} {
935 set node [dict get $request node]
936 set typed_path [dict get $request typed_path]
937 set project [dict get $request project]
938 set raw_opts [dict get $request options]
940 if {"--help" in $raw_opts || "-help" in $raw_opts || "-h" in $raw_opts || "-?" in $raw_opts} {
946 set _conf [
file join [Repo::Get top_path] $project hog.conf]
947 if {![
file exists $_conf]} {
948 return -code error "Project '$project' not found (no hog.conf at $_conf)."
953 set options $raw_opts
956 if {[
llength $opt_specs] > 0} {
958 return -code error "Option error: $err"
961 set _t [tdict create]
962 foreach k [
array names _parsed] { tdict set _t $k [
tinf $_parsed($k)]}
963 Launcher::Set options $_t
965 set verdict [
Dispatch $node $typed_path $project]
966 switch -- [
lindex $verdict 0] {
967 ran {
return $verdict}
973 lassign $verdict _ path message
979 lassign $verdict _ ide
980 Launcher::Set ide $ide
990 default {
return -code error "Unexpected dispatch verdict: $verdict"}
996 proc ToJson {{flag {}}} {
999 set ai [tdict create]
1000 dict for {k v} $alias_index { tdict set ai $k [tstr $v] }
1001 set nd [tdict create]
1002 dict for {canon node} $nodes {
1003 # children isn't stored on the node - compute it here for export only.
1004 tdict set node children [tlist create {*}[GetChildren $canon]]
1005 tdict set nd $canon $node
1007 set root [tdict create alias_index $ai nodes $nd]
1008 if {$flag eq "-pretty"} {
return [tobj tojson $root -pretty]}
1009 return [tobj tojson $root]