class Konacha::Chai::Matchers::Library

Attributes

data[R]
name[R]

Public Class Methods

new(data) click to toggle source
# File lib/konacha-chai-matchers/library.rb, line 9
def initialize(data)
  @data = data
  @name = data[:name]
  @commit = data[:commit]
end

Public Instance Methods

latest_version() click to toggle source
# File lib/konacha-chai-matchers/library.rb, line 19
def latest_version
  `cd ./#@name && git fetch`
  @data[:tag]
end
main_file() click to toggle source
# File lib/konacha-chai-matchers/library.rb, line 45
def main_file
  return @main_file unless @main_file.nil?
  r = determine_file_from_suggestion @name
  r ||= get_main_file_from_package
  @main_file = r
end
to_s() click to toggle source
# File lib/konacha-chai-matchers/library.rb, line 15
def to_s
  "#{@data[:name]} => #{@data[:tag]}"
end
update() click to toggle source
# File lib/konacha-chai-matchers/library.rb, line 24
def update
  puts "Updating #@name to #{latest_version}"
  `cd ./#@name && git checkout #@commit`
  if File.exist? "./#@name/build"
    system("cd ./#@name && bundle exec ./build") or raise "Building #@name failed"
  end
end
vendor() click to toggle source
# File lib/konacha-chai-matchers/library.rb, line 32
def vendor
  file = main_file
  unless file
    puts "Cannot determine library file for #@name"
    return false
  end

  path = "./vendor/assets/javascripts/"
  Pathname.new(path).mkpath()
  FileUtils.cp(file, "#{path}#{@name}.js")
  return true
end
version() click to toggle source
# File lib/konacha-chai-matchers/library.rb, line 52
def version
  get_value_from_package 'version'
end

Private Instance Methods

determine_file_from_suggestion(filename) click to toggle source
# File lib/konacha-chai-matchers/library.rb, line 58
def determine_file_from_suggestion filename
  unless filename
    puts "unable to determine main filename for #@name"
    return
  end
  file_search = filename.downcase
  file_search << '.js' unless file_search =~ /\.js$/

  path_order = [
    "./#@name/pkg/#{file_search}",
    "./#@name/#{file_search}",
    "./#@name/lib/#{file_search}"
  ]
  path_order.map do |p|
    p if File.exist? p
  end.flatten.compact.first
end
get_main_file_from_package() click to toggle source
# File lib/konacha-chai-matchers/library.rb, line 85
def get_main_file_from_package
  main_file = get_value_from_package 'main'
  determine_file_from_suggestion main_file
end
get_value_from_package(key) click to toggle source
# File lib/konacha-chai-matchers/library.rb, line 76
def get_value_from_package key
  filename =  "./#@name/package.json"
  return unless File.exist? filename
  json_string = File.open(filename).read
  json = JSON.parse json_string
  return unless json.has_key? "main"
  json[key]
end