class Breezer::Freezer

Will update (freeze) the gems in the Gemfile

Constants

GEM_REGEX

Public Class Methods

check(line, deps, **options) click to toggle source

Parse a gemfile line, and return true or false wether the line is valid

# File lib/breezer/freezer.rb, line 65
def check(line, deps, **options)
  return { valid: true } unless valid_line?(line)

  matches = line.match(GEM_REGEX)

  # return the line if we didn't matched a name
  return { valid: true } unless matches[:name]

  proposed_version = deps[matches[:name]]
  get_version_string(proposed_version, options)

  # Do we have a version ?
  {
    name: matches[:name],
    proposed_version: proposed_version,
    valid: !matches[:version].nil?
  }
end
check_gemfile!(gemfile, deps, **options) click to toggle source
# File lib/breezer/freezer.rb, line 20
def check_gemfile!(gemfile, deps, **options)
  gemfile.split("\n").each_with_index.map do |line, no|
    [no + 1, check(line, deps, options)]
  end.to_h
end
get_version_string(version, options) click to toggle source

Will return the Gemfile.lock version of a deps, with the version converted according to the given level (default 'patch')

# File lib/breezer/freezer.rb, line 99
def get_version_string(version, options)
  options = { level: 'patch' }.merge(options)

  gv = Gem::Version.create(version)
  return unless gv

  segments = [*gv.canonical_segments, 0, 0, 0].first(3)
  case options[:level].to_s
  when 'major'
    "~> #{segments.first}"
  when 'minor'
    "~> #{segments.first(2).join('.')}"
  when 'patch'
    "~> #{segments.first(3).join('.')}"
  when 'exact'
    "= #{version}"
  else
    raise("Unsupported option: #{options[:level]}")
  end
end
parse(line, deps, **options) click to toggle source

Parse a gemfile line, and return the line updated with dependencies

# File lib/breezer/freezer.rb, line 27
def parse(line, deps, **options)
  return line unless valid_line?(line)

  matches = line.match(GEM_REGEX)

  # return the line if we didn't matched a name
  return line unless matches[:name]

  proposed_version = deps[matches[:name]]
  version_string = get_version_string(proposed_version, options)

  # return the line if we didn't find a version
  return line unless proposed_version && version_string

  # if we already have a version and we don't want to override
  return line if matches[:version] && options[:preserve]

  transform_line_for_version(line, matches, version_string)
end
transform_line_for_version(line, matches, version_string) click to toggle source

Will rewrite the old deps line with the good version

# File lib/breezer/freezer.rb, line 85
def transform_line_for_version(line, matches, version_string)
  # We remove the other version
  line = line.gsub(matches[:fullsecversion], '') if matches[:fullsecversion]

  # If we had a version
  if matches[:version]
    line.gsub(matches[:version], version_string)
  else
    line.gsub(matches[:fullname], "#{matches[:fullname]}, '#{version_string}'")
  end
end
update_gemfile!(gemfile, deps, **options) click to toggle source
# File lib/breezer/freezer.rb, line 14
def update_gemfile!(gemfile, deps, **options)
  new_gemfile = []
  gemfile.split("\n").each { |line| new_gemfile << parse(line, deps, options) }
  [*new_gemfile, ''].join("\n")
end
valid_line?(line) click to toggle source

Return false if there is no deps declaration in the given line

# File lib/breezer/freezer.rb, line 48
def valid_line?(line)
  # Drop lines if no gem declared
  return false if (line =~ /gem[\s]+/).nil?

  # Skip git and github direct references
  return false if line =~ %r{(git://|(github(:|\s)))}

  # Drop line if it's a comment
  return false unless (line =~ /^[\s]?#/).nil?

  # Drop line if it contains a skip comment
  return false unless (line =~ /breezer-disable/).nil?

  true
end