Class: CodeServer::Tree

Inherits:
Object
  • Object
show all
Defined in:
lib/tree.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_path) ⇒ Tree

Returns a new instance of Tree.



7
8
9
# File 'lib/tree.rb', line 7

def initialize(root_path)
  self.root_path = root_path
end

Instance Attribute Details

#root_pathObject

Returns the value of attribute root_path.



5
6
7
# File 'lib/tree.rb', line 5

def root_path
  @root_path
end

Instance Method Details

#directory_structureObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tree.rb', line 11

def directory_structure
  if %x{git rev-parse 2>&1}.empty?
    data = %x{cd #{root_path}; git ls-files 2>/dev/null; git ls-files --exclude-standard --others 2>/dev/null}.split("\n")
  else
    data = %x{cd #{root_path}; find . | cut -c3-}
  end

  tree = { files: [], directories: [] }
  data.each do |line|
    current_tree = tree

    *directories, file = line.split("/")

    path = "."
    while directory = directories.shift do
      path << "/#{directory}"

      subdirectory = current_tree[:directories].find { |entry| entry[:name] == directory }
      if subdirectory.nil?
        subdirectory = { name: directory, path: path, directories: [], files: [] }
        current_tree[:directories] << subdirectory
      end

      current_tree = subdirectory
    end

    current_tree[:files] << { name: file, path: "./#{line}" }
  end

  tree
end