Class: Rootee

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

Constant Summary collapse

VERSION =
"1.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(end_path = [], marker = []) ⇒ Rootee

Returns a new instance of Rootee.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rootee.rb', line 24

def initialize(end_path=[], marker=[] )
  @marker = [
    {
      :dir => ['.git/', '.svn/', '_darcs/', '.hg/', '.bzr/', 'nbproject/']
    },
    {
      :file => ['./Makefile', './configure', './Rakefile',
                './NAnt.build', './build.xml', './prj.el',
                './pom.xml', './Gemfile', './Thorfile'],
    },
    {
      :file => ['../project', './tags', './gtags'],
      :dir  => ['bin/', 'src/', 'include/', 'lib/', 'doc/', ],
    },
  ] if marker.empty?


  home = Pathname.new(ENV['HOME'])
  @end_path = end_path.empty? ? [ "/", home.realpath, ] : end_path
end

Instance Attribute Details

#end_pathObject

Returns the value of attribute end_path.



22
23
24
# File 'lib/rootee.rb', line 22

def end_path
  @end_path
end

#markerObject

Returns the value of attribute marker.



21
22
23
# File 'lib/rootee.rb', line 21

def marker
  @marker
end

Instance Method Details

#find_root(start_path = Dir.getwd) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rootee.rb', line 46

def find_root(start_path=Dir.getwd)
  root = start_path
  is_find = false

  @marker.each { |m|
    m.each { |type, filter|

      Dir.chdir(start_path)
      # upwards traverse
      while ! end_path.include?(Dir.getwd)
        glob_ret = Dir.glob(filter).select { |fn|
          if type == :file
            File.file?(fn)
          elsif type == :dir
            File.directory?(fn)
          end
        }

        unless glob_ret.empty?
          is_find = true
          root = Dir.getwd
          break
        end
        Dir.chdir('..')
      end
    }

    break if is_find
  }

  Dir.chdir(start_path)
  return is_find, root
end