Class: Breezer::Freezer

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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/breezer/freezer.rb', line 56

def self.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]]
  version_for_name(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



14
15
16
17
18
# File 'lib/breezer/freezer.rb', line 14

def self.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 convert the version according to the given level (default ‘patch’)



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/breezer/freezer.rb', line 94

def self.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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/breezer/freezer.rb', line 21

def self.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 = version_for_name(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



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/breezer/freezer.rb', line 76

def self.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



8
9
10
11
12
# File 'lib/breezer/freezer.rb', line 8

def self.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)


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/breezer/freezer.rb', line 42

def self.valid_line?(line)
  # Drop lines if no gem declared
  return false if (line =~ /gem[\s]+/).nil?

  # 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

.version_for_name(proposed_version, options) ⇒ Object

Will return the Gemfile.lock version of a deps



89
90
91
# File 'lib/breezer/freezer.rb', line 89

def self.version_for_name(proposed_version, options)
  get_version_string(proposed_version, options)
end