class Spoom::FileTree
Build a file hierarchy from a set of file paths.
def add_path(path)
def add_path(path) prefix = @strip_prefix path = path.delete_prefix("#{prefix}/") if prefix parts = path.split("/") if path.empty? || parts.size == 1 return @roots[path] ||= Node.new(parent: nil, name: path) end parent_path = T.must(parts[0...-1]).join("/") parent = add_path(parent_path) name = T.must(parts.last) parent.children[name] ||= Node.new(parent: parent, name: name) end
def add_paths(paths)
def add_paths(paths) paths.each { |path| add_path(path) } end
def collect_nodes(node, collected_nodes = [])
def collect_nodes(node, collected_nodes = []) collected_nodes << node node.children.values.each { |child| collect_nodes(child, collected_nodes) } collected_nodes end
def initialize(paths = [], strip_prefix: nil)
def initialize(paths = [], strip_prefix: nil) @roots = T.let({}, T::Hash[String, Node]) @strip_prefix = strip_prefix add_paths(paths) end
def nodes
def nodes all_nodes = [] @roots.values.each { |root| collect_nodes(root, all_nodes) } all_nodes end
def paths
def paths nodes.collect(&:path) end
def print(out: $stdout, show_strictness: true, colors: true, indent_level: 0)
def print(out: $stdout, show_strictness: true, colors: true, indent_level: 0) printer = TreePrinter.new( tree: self, out: out, show_strictness: show_strictness, colors: colors, indent_level: indent_level ) printer.print_tree end
def roots
def roots @roots.values end