tomo/examples/tomodeps/tomodeps.tm

116 lines
3.9 KiB
Plaintext
Raw Normal View History

2024-09-06 21:03:17 -07:00
# Show a Tomo dependency graph
2024-09-16 10:54:47 -07:00
_USAGE := "Usage: tomodeps <files...>"
2024-09-06 21:03:17 -07:00
_HELP := "
2024-09-16 10:54:47 -07:00
tomodeps: Show a file dependency graph for Tomo source files.
$_USAGE
2024-09-06 21:03:17 -07:00
"
2024-09-09 13:54:20 -07:00
enum Dependency(File(path:Path), Module(name:Text))
func _get_file_dependencies(file:Path -> {Dependency}):
2024-09-09 13:54:20 -07:00
if not file:is_file():
!! Could not read file: $file
return {:Dependency}
2024-09-06 21:03:17 -07:00
deps := @{:Dependency}
2024-09-15 13:53:42 -07:00
if lines := file:by_line():
for line in lines:
if line:matches($/use {..}.tm/):
file_import := Path.from_text(line:replace($/use {..}/, "\1")):resolved(relative_to=file)
2024-09-15 13:53:42 -07:00
deps:add(Dependency.File(file_import))
else if line:matches($/use {id}/):
module_name := line:replace($/use {..}/, "\1")
deps:add(Dependency.Module(module_name))
return deps[]
2024-09-09 13:54:20 -07:00
2025-01-12 13:54:37 -08:00
func _build_dependency_graph(dep:Dependency, dependencies:@{Dependency,{Dependency}}):
2024-09-09 13:54:20 -07:00
return if dependencies:has(dep)
2024-11-30 12:50:54 -08:00
dependencies[dep] = {:Dependency} # Placeholder
2024-09-09 13:54:20 -07:00
dep_deps := when dep is File(path):
_get_file_dependencies(path)
is Module(module):
2024-09-16 10:54:47 -07:00
dir := (~/.local/share/tomo/installed/$module)
module_deps := @{:Dependency}
visited := @{:Path}
unvisited := @{f:resolved() for f in dir:files() if f:extension() == ".tm"}
2024-09-09 13:54:20 -07:00
while unvisited.length > 0:
file := unvisited.items[-1]
unvisited:remove(file)
visited:add(file)
for file_dep in _get_file_dependencies(file):
when file_dep is File(f):
if not visited:has(f):
unvisited:add(f)
is Module(m):
module_deps:add(file_dep)
module_deps[]
2024-09-09 13:54:20 -07:00
2024-11-30 12:50:54 -08:00
dependencies[dep] = dep_deps
2024-09-09 13:54:20 -07:00
for dep2 in dep_deps:
_build_dependency_graph(dep2, dependencies)
2025-01-12 13:54:37 -08:00
func get_dependency_graph(dep:Dependency -> {Dependency,{Dependency}}):
graph := @{:Dependency,{Dependency}}
2024-10-27 17:49:03 -07:00
_build_dependency_graph(dep, graph)
2024-09-09 13:54:20 -07:00
return graph
func _printable_name(dep:Dependency -> Text):
2024-09-09 13:54:20 -07:00
when dep is Module(module):
return "$(\x1b)[34;1m$module$(\x1b)[m"
is File(f):
2025-03-17 15:36:39 -07:00
f = f:relative_to((.))
2024-09-09 13:54:20 -07:00
if f:exists():
return Text(f)
2024-09-09 13:54:20 -07:00
else:
return "$(\x1b)[31;1m$(f) (not found)$(\x1b)[m"
2024-09-09 13:54:20 -07:00
2025-01-12 13:54:37 -08:00
func _draw_tree(dep:Dependency, dependencies:{Dependency,{Dependency}}, already_printed:@{Dependency}, prefix="", is_last=yes):
2024-09-09 13:54:20 -07:00
if already_printed:has(dep):
say(prefix ++ (if is_last: "└── " else: "├── ") ++ _printable_name(dep) ++ " $\x1b[2m(recursive)$\x1b[m")
return
2024-09-09 13:54:20 -07:00
say(prefix ++ (if is_last: "└── " else: "├── ") ++ _printable_name(dep))
already_printed:add(dep)
child_prefix := prefix ++ (if is_last: " " else: "│ ")
2024-11-30 12:50:54 -08:00
children := dependencies[dep] or {:Dependency}
for i,child in children.items:
is_child_last := (i == children.length)
2024-09-06 21:33:07 -07:00
_draw_tree(child, dependencies, already_printed, child_prefix, is_child_last)
2025-01-12 13:54:37 -08:00
func draw_tree(dep:Dependency, dependencies:{Dependency,{Dependency}}):
2024-10-27 17:49:03 -07:00
printed := @{:Dependency}
2024-09-09 13:54:20 -07:00
say(_printable_name(dep))
printed:add(dep)
2024-11-30 12:50:54 -08:00
deps := dependencies[dep] or {:Dependency}
2024-09-09 13:54:20 -07:00
for i,child in deps.items:
is_child_last := (i == deps.length)
2024-10-27 17:49:03 -07:00
_draw_tree(child, dependencies, already_printed=printed, is_last=is_child_last)
2024-09-09 14:04:46 -07:00
func main(files:[Text]):
2024-09-06 21:35:57 -07:00
if files.length == 0:
2024-09-15 13:42:42 -07:00
exit("
2024-09-06 21:35:57 -07:00
Please provide at least one file!
$_USAGE
")
2024-09-09 14:04:46 -07:00
for arg in files:
if arg:matches($/{..}.tm/):
path := Path.from_text(arg):resolved()
2024-09-09 14:04:46 -07:00
dependencies := get_dependency_graph(File(path))
draw_tree(File(path), dependencies)
else if arg:matches($/{id}/):
dependencies := get_dependency_graph(Module(arg))
draw_tree(Module(arg), dependencies)
else:
say("$\x1b[2mSkipping $arg$\x1b[m")
skip