class Ivar::ProjectRoot

Handles project root detection and caching

def clear_cache

Clear the cache (mainly for testing)
def clear_cache
  @mutex.synchronize { @cache.clear }
end

def find(caller_location = nil)

Returns:
  • (String) - The absolute path to the project root directory

Parameters:
  • caller_location (String, nil) -- Optional file path to start from (defaults to caller's location)
def find(caller_location = nil)
  file_path = caller_location || caller_locations(2, 1).first&.path
  return Dir.pwd unless file_path
  @mutex.synchronize do
    return @cache[file_path] if @cache.key?(file_path)
  end
  dir = File.dirname(File.expand_path(file_path))
  root = find_project_root(dir)
  @mutex.synchronize do
    @cache[file_path] = root
  end
  root
end

def find_project_root(start_dir)

Returns:
  • (String) - The project root directory

Parameters:
  • start_dir (String) -- Directory to start the search from
def find_project_root(start_dir)
  path = Pathname.new(start_dir)
  path.ascend do |dir|
    INDICATORS.each do |indicator|
      return dir.to_s if dir.join(indicator).exist?
    end
  end
  start_dir
end

def initialize

def initialize
  @cache = {}
  @mutex = Mutex.new
end