Module: Savvy::RootFinder

Defined in:
lib/savvy/root_finder.rb

Constant Summary collapse

ROOT_FILES =

Something that, given a directory possessing such a file, would make it look like a potential root directory.

[
  *Savvy::FILES,
  'Gemfile'
].freeze

Class Method Summary collapse

Class Method Details

.call(origin = Dir.pwd) ⇒ Pathname

Parameters:

  • origin (String, Pathname) (defaults to: Dir.pwd)

Returns:

  • (Pathname)


33
34
35
36
37
38
39
40
41
# File 'lib/savvy/root_finder.rb', line 33

def call(origin = Dir.pwd)
  if defined?(Rails) && Rails.root
    Rails.root
  elsif defined?(Bundler) && Bundler.root
    Bundler.root
  else
    find_potential_root_from(origin)
  end
end

.find_potential_root_from(origin = Dir.pwd) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/savvy/root_finder.rb', line 12

def find_potential_root_from(origin = Dir.pwd)
  start_root = Pathname.new(origin)

  start_root.ascend do |path|
    if looks_like_a_root_directory?(path)
      return path
    end
  end

  raise ArgumentError, "Could not find root from #{origin}"
end

.looks_like_a_root_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/savvy/root_finder.rb', line 24

def looks_like_a_root_directory?(path)
  ROOT_FILES.any? do |filename|
    path.join(filename).exist?
  end
end