class Cbradeps::GemfileScraper

Public Class Methods

new(root_path) click to toggle source
# File lib/cobradeps/gemfile_scraper.rb, line 3
def initialize(root_path)
  @root_path = root_path
end

Public Instance Methods

cobra_dependencies() click to toggle source
# File lib/cobradeps/gemfile_scraper.rb, line 15
def cobra_dependencies
  dirdep = direct_dependencies
  transitive_cobra_dependencies.select do |dep|
    dirdep.include?(dep[:name]) || dep[:options][:direct]
  end
end
name() click to toggle source
# File lib/cobradeps/gemfile_scraper.rb, line 7
def name
  Pathname.new(@root_path).basename.to_s
end
to_s() click to toggle source
# File lib/cobradeps/gemfile_scraper.rb, line 11
def to_s
  cobra_dependencies
end

Private Instance Methods

direct_dependencies() click to toggle source
# File lib/cobradeps/gemfile_scraper.rb, line 40
def direct_dependencies
  raw_gemspec.split("\n").inject([]) do |memo, line|
    match = line.match(/add_(?:development_)?dependency\s+["']([^'"]+)["']/)
    memo << match[1] if match
    memo
  end
end
gem_dependencies() click to toggle source
# File lib/cobradeps/gemfile_scraper.rb, line 53
def gem_dependencies
  in_path_block_dir = nil
  raw_gemfile.split("\n").inject([]) do |memo, line|
    if in_path_block_dir
      if match = line.match(/gem\s+["']([^'"]+)["'](.*)/)
        memo << {name: match[1], options: {path: "#{in_path_block_dir}/#{match[1]}", direct: true}}
      elsif line.match(/end/)
        in_path_block_dir = nil
      end
    else
      if match = line.match(/gem\s+["']([^'"]+)["'](.*)/)
        memo << {name: match[1], options: NonBlockOptionParser.new(match[2]).parse}
      elsif match = line.match(/path\s+[\"\'\:]?([^\"\']*)[\"\']?\s+do/)
        in_path_block_dir = match[1]
      end
    end
    memo
  end
end
raw_gemfile() click to toggle source
# File lib/cobradeps/gemfile_scraper.rb, line 48
def raw_gemfile
  path = File.expand_path(File.join(@root_path, "Gemfile"))
  File.read(path)
end
raw_gemspec() click to toggle source
# File lib/cobradeps/gemfile_scraper.rb, line 35
def raw_gemspec
  path = File.expand_path(File.join(@root_path, "#{underscore(name)}.gemspec"))
  File.exist?(path) ? File.read(path) : ""
end
transitive_cobra_dependencies() click to toggle source
# File lib/cobradeps/gemfile_scraper.rb, line 24
def transitive_cobra_dependencies
  gem_dependencies.inject([]) do |memo, dep|
    if !!dep[:options][:path]
      absolute_dep = dep.clone
      absolute_dep[:options][:path] = File.expand_path(File.join(@root_path, dep[:options][:path]))
      memo << dep
    end
    memo
  end
end
underscore(string) click to toggle source
# File lib/cobradeps/gemfile_scraper.rb, line 73
def underscore(string)
  string.gsub(/::/, '/').
      gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').
      gsub(/([a-z\d])([A-Z])/, '\1_\2').
      tr("-", "_").
      downcase
end