Class: Breezer::Freezer

Inherits:
Object
  • Object
show all
Defined in:
lib/breezer/freezer.rb

Overview

Will update (freeze) the gems in the Gemfile

Constant Summary collapse

GEM_REGEX =
/gem[\s]+(?<fullname>['"](?<name>[\w\-_]+)['"])(?<fullversion>,?[\s]?['"](?<version>[~><=]+[\s]?[\d\.]+)['"])?(?<fullsecversion>,?[\s]?['"](?<secversion>[~><=]+[\s]?[\d\.]+)['"])?/.freeze

Class Method Summary collapse

Class Method Details

.check(line, deps, **options) ⇒ Object

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



20
21
22
23
24
# 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) ⇒ Object

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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) ⇒ Object

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object

Will rewrite the old deps line with the good version



85
86
87
88
89
90
91
92
93
94
95
# 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) ⇒ Object



14
15
16
17
18
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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