diff --git a/examples/dependencies.tm b/examples/tomodeps.tm similarity index 68% rename from examples/dependencies.tm rename to examples/tomodeps.tm index 3719e07..e7066a5 100644 --- a/examples/dependencies.tm +++ b/examples/tomodeps.tm @@ -8,7 +8,7 @@ _HELP := " $_USAGE " -func get_module_imports_from_file(file:Text, imports:&{Text}, visited_files:&{Text}): +func _get_module_imports_from_file(file:Text, imports:&{Text}, visited_files:&{Text}): return if visited_files:has(file) reader := when LineReader.from_file(file) is Failure(msg): @@ -24,12 +24,12 @@ func get_module_imports_from_file(file:Text, imports:&{Text}, visited_files:&{Te resolved := relative_path(resolve_path(local_import, file)) if resolved != "": local_import = resolved - get_module_imports_from_file(local_import, imports, visited_files) + _get_module_imports_from_file(local_import, imports, visited_files) else if line:matches($/use {id}/): other_module := line:replace($/use {..}/, "\1") imports:add(other_module) -func get_module_imports_from_module(module:Text)->{Text}: +func _get_module_imports_from_module(module:Text)->{Text}: files_path := resolve_path("~/.local/src/tomo/$module/lib$(module).files") if files_path == "": !! couldn't resolve: $files_path @@ -44,21 +44,21 @@ func get_module_imports_from_module(module:Text)->{Text}: for line in files_content:lines(): line_resolved := resolve_path(line, relative_to="~/.local/src/tomo/$module/") skip if line_resolved == "" - get_module_imports_from_file(line_resolved, &imports, &visited) + _get_module_imports_from_file(line_resolved, &imports, &visited) return imports -func build_module_dependency_graph(module:Text, dependencies:&{Text:@{Text}}): +func _build_module_dependency_graph(module:Text, dependencies:&{Text:@{Text}}): return if dependencies:has(module) module_deps := @{:Text} dependencies:set(module, module_deps) - for dep in get_module_imports_from_module(module): + for dep in _get_module_imports_from_module(module): module_deps:add(dep) - build_module_dependency_graph(dep, dependencies) + _build_module_dependency_graph(dep, dependencies) -func build_file_dependency_graph(filename:Text, dependencies:&{Text:@{Text}}): +func _build_file_dependency_graph(filename:Text, dependencies:&{Text:@{Text}}): return if dependencies:has(filename) reader := when LineReader.from_file(filename) is Failure(msg): @@ -77,20 +77,20 @@ func build_file_dependency_graph(filename:Text, dependencies:&{Text:@{Text}}): used_file = resolved file_deps:add(used_file) - build_file_dependency_graph(used_file, dependencies) + _build_file_dependency_graph(used_file, dependencies) else if line:matches($/use {id}/): module := line:replace($/use {..}/, "\1") file_deps:add(module) - build_module_dependency_graph(module, dependencies) + _build_module_dependency_graph(module, dependencies) func get_dependency_graph(file:Text)->{Text:{Text}}: graph := {:Text:@{Text}} resolved := relative_path(file) - build_file_dependency_graph(resolved, &graph) + _build_file_dependency_graph(resolved, &graph) return {f:deps[] for f,deps in graph} -func draw_tree(file:Text, dependencies:{Text:{Text}}, already_printed:&{Text}, prefix="", is_last=yes): +func _draw_tree(file:Text, dependencies:{Text:{Text}}, already_printed:&{Text}, prefix="", is_last=yes): color_file := if file:matches($/{id}/): "$\x1b[34;1m$file$\x1b[m" else if resolve_path(file) != "": @@ -110,7 +110,20 @@ func draw_tree(file:Text, dependencies:{Text:{Text}}, already_printed:&{Text}, p children := dependencies:get(file, {:Text}) for i,child in children.items: is_child_last := (i == children.length) - draw_tree(child, dependencies, already_printed, child_prefix, is_child_last) + _draw_tree(child, dependencies, already_printed, child_prefix, is_child_last) + +func draw_tree(file:Text, dependencies:{Text:{Text}}): + printed := {:Text} + resolved := relative_path(file) + if resolved != "": + file = resolved + + say(file) + printed:add(file) + children := dependencies:get(file, {:Text}) + for i,child in children.items: + is_child_last := (i == children.length) + _draw_tree(child, dependencies, already_printed=&printed, is_last=is_child_last) func main(files:[Text]): for f,file in files: @@ -118,16 +131,6 @@ func main(files:[Text]): say("$\x1b[2mSkipping $file$\x1b[m") skip - printed := {:Text} - resolved := relative_path(file) - if resolved != "": - file = resolved + dependencies := get_dependency_graph(file) + draw_tree(file, dependencies) - deps := get_dependency_graph(file) - - say(file) - printed:add(file) - children := deps:get(file, {:Text}) - for i,child in children.items: - is_child_last := (i == children.length) - draw_tree(child, deps, already_printed=&printed, is_last=is_child_last)