Class: EY::Serverside::LockfileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/engineyard-serverside/lockfile_parser.rb

Constant Summary collapse

DEFAULT =
"1.1.5"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lockfile_contents) ⇒ LockfileParser

Returns a new instance of LockfileParser.



13
14
15
16
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 13

def initialize(lockfile_contents)
  @contents = lockfile_contents
  parse
end

Instance Attribute Details

#bundler_versionObject (readonly)

Returns the value of attribute bundler_version.



11
12
13
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 11

def bundler_version
  @bundler_version
end

#lockfile_versionObject (readonly)

Returns the value of attribute lockfile_version.



11
12
13
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 11

def lockfile_version
  @lockfile_version
end

Class Method Details

.default_versionObject



7
8
9
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 7

def self.default_version
  DEFAULT
end

Instance Method Details

#any_database_adapter?Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 22

def any_database_adapter?
  any_ruby_adapter = %w[mysql2 mysql do_mysql pg do_postgres].any? do |type|
    @contents.index(/^\s+#{type}\s\([^\)]+\)$/)
  end

  any_jruby_adapter = %w[mysql postgresql postgres].any? do |type|
    @contents.index(/^\s+jdbc-#{type}\s\([^\)]+\)$/) || @contents.index(/^\s+activerecord-jdbc#{type}-adapter\s\([^\)]+\)$/)
  end

  any_ruby_adapter || any_jruby_adapter
end

#fetch_version(bundler_version, version_qualifier) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 79

def fetch_version(bundler_version, version_qualifier)
  return bundler_version || DEFAULT unless version_qualifier

  case version_qualifier
  when '='
    bundler_version
  when '>='
    Gem::Version.new(bundler_version) > Gem::Version.new(DEFAULT) ? bundler_version : DEFAULT
  when '~>'
    bundler_gem_version = Gem::Version.new(bundler_version)
    recommendation = bundler_gem_version.spermy_recommendation.gsub(/~>\s*(.+)$/, '\1.')
    recommends_default = DEFAULT.index(recommendation) == 0
    default_newer_than_requested = Gem::Version.new(DEFAULT) > bundler_gem_version
    (recommends_default && default_newer_than_requested) ? DEFAULT : bundler_version
  end
end

#has_ey_config?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 18

def has_ey_config?
  @contents.index(/^\s+ey_config\s\([^\)]+\)$/)
end

#parseObject



38
39
40
41
42
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 38

def parse
   ||
    parse_from_dependencies ||
    raise("Malformed or pre bundler-1.0.0 Gemfile.lock: #{@contents[0,50]}...")
end

#parse_from_dependenciesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 65

def parse_from_dependencies
  section = slice_section('DEPENDENCIES')

  if section.empty?
    return nil
  end

  result = scan_bundler(section)
  bundler_version = result ? result.last : nil
  version_qualifier = result ? result.first : nil
  @lockfile_version = :bundler10
  @bundler_version = fetch_version(bundler_version, version_qualifier)
end

#parse_from_metadataObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 53

def 
  section = slice_section('METADATA')

  if section.empty?
    return nil
  end

  result = section.scan(/^\s*version:\s*(.*)$/).first
  @lockfile_version = :bundler10
  @bundler_version = result ? result.first : DEFAULT
end

#scan_bundler(dep_section) ⇒ Object



96
97
98
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 96

def scan_bundler(dep_section)
  dep_section.scan(/^\s*bundler\s*\((>?=|~>)\s*([^,\)]+)/).first
end

#slice_section(header) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 44

def slice_section(header)
  if start = @contents.index(/^#{header}/)
    finish = @contents.index(/(^\S|\Z)/, start + header.length)
    @contents.slice(start..finish)
  else
    ""
  end
end

#uses_sqlite3?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/engineyard-serverside/lockfile_parser.rb', line 34

def uses_sqlite3?
  !any_database_adapter? && @contents.index(/^\s+sqlite3\s\([^\)]+\)$/)
end