class Dependabot::Bundler::FileFetcher::PathGemspecFinder

Finds the paths of any gemspecs declared using `path: ` in the passed Gemfile.

Attributes

gemfile[R]

Public Class Methods

new(gemfile:) click to toggle source
# File lib/dependabot/bundler/file_fetcher/path_gemspec_finder.rb, line 14
def initialize(gemfile:)
  @gemfile = gemfile
end

Public Instance Methods

path_gemspec_paths() click to toggle source
# File lib/dependabot/bundler/file_fetcher/path_gemspec_finder.rb, line 18
def path_gemspec_paths
  ast = Parser::CurrentRuby.parse(gemfile.content)
  find_path_gemspec_paths(ast)
rescue Parser::SyntaxError
  raise Dependabot::DependencyFileNotParseable, gemfile.path
end

Private Instance Methods

clean_path(path) click to toggle source
# File lib/dependabot/bundler/file_fetcher/path_gemspec_finder.rb, line 65
def clean_path(path)
  if Pathname.new(path).absolute?
    base_path = Pathname.new(File.expand_path(Dir.pwd))
    path = Pathname.new(path).relative_path_from(base_path).to_s
  end
  path = File.join(current_dir, path) unless current_dir.nil?
  Pathname.new(path).cleanpath
end
current_dir() click to toggle source
# File lib/dependabot/bundler/file_fetcher/path_gemspec_finder.rb, line 52
def current_dir
  @current_dir ||= gemfile.name.rpartition("/").first
  @current_dir = nil if @current_dir == ""
  @current_dir
end
declares_path_dependency?(node) click to toggle source
# File lib/dependabot/bundler/file_fetcher/path_gemspec_finder.rb, line 58
def declares_path_dependency?(node)
  return false unless node.is_a?(Parser::AST::Node)
  return false unless node.children[1] == :gem

  !path_node_for_gem_declaration(node).nil?
end
find_path_gemspec_paths(node) click to toggle source
# File lib/dependabot/bundler/file_fetcher/path_gemspec_finder.rb, line 29
def find_path_gemspec_paths(node)
  return [] unless node.is_a?(Parser::AST::Node)

  if declares_path_dependency?(node)
    path_node = path_node_for_gem_declaration(node)

    unless path_node.type == :str
      path = gemfile.path
      msg = "Dependabot only supports uninterpolated string arguments "\
            "for path dependencies. Got "\
            "`#{path_node.loc.expression.source}`"
      raise Dependabot::DependencyFileNotParseable.new(path, msg)
    end

    path = path_node.loc.expression.source.gsub(/['"]/, "")
    return [clean_path(path)]
  end

  node.children.flat_map do |child_node|
    find_path_gemspec_paths(child_node)
  end
end
key_from_hash_pair(node) click to toggle source
# File lib/dependabot/bundler/file_fetcher/path_gemspec_finder.rb, line 88
def key_from_hash_pair(node)
  node.children.first.children.first.to_sym
end
path_node_for_gem_declaration(node) click to toggle source
# File lib/dependabot/bundler/file_fetcher/path_gemspec_finder.rb, line 74
def path_node_for_gem_declaration(node)
  return unless node.children.last.type == :hash

  kwargs_node = node.children.last

  path_hash_pair =
    kwargs_node.children.
    find { |hash_pair| key_from_hash_pair(hash_pair) == :path }

  return unless path_hash_pair

  path_hash_pair.children.last
end