require "rubygems" require "tree" # Contains the file tree as well as some methods for working with it # @note this should really subclass the Tree::TreeNode class class Dir_Tree attr_accessor :root, :depots def initialize @root = Tree::TreeNode.new("//") @depots = [] end # Add a path to the file tree # Splits the input path, and finds which elements (directories/files) need to be # added to the tree # @param [String] path A path e.g '//depot/a/b/c.txt def add_path(path) npath = path.split("//") split_path = npath[1].split("/") parent = @root split_path.each { |pathticle| unless parent[pathticle] parent << Tree::TreeNode.new(pathticle) end parent = parent[pathticle] } end # Add a depot name to the epot list @depots # @param [String] depot A depot name def add_depot(depot) @depots << depot end # @note This method is no longer used, due to the new parser # It is kept just in case it is shown to be useful again # Removes client paths from the tree e.g removes //brett_ws def remove_client_trees @root.children { |child| unless @depots.include?(child.name) @root.remove!(child) end } end # Convert a file node into a path # @param [Tree::TreeNode] child The file node # @return [String] The path of that file node def tree_to_path(child) ancestors = child.parentage path_out = "" if ancestors name_array = [] ancestors.reverse! ancestors.each { |ancestor| unless ancestor.name == "//" name_array << ancestor.name else name_array << "/" end } path_out = name_array.join("/") path_out = path_out +"/#{child.name}" else path_out = "//" end return path_out end # Apply a protection to the file tree # The protection is added to the node that corresponds to path # @param [String] path The protections path # @param [Protection] protection The protection object to be addedd def apply_protection_to_tree(path, protection) split_path = path.split("/") split_path.shift(2) current_root = @root split_path.each { |pathticle| current_root = current_root[pathticle] } if current_root.content == nil current_root.content = [protection] else current_root.content << protection end end end