Class: LeaveIt::CLI
- Inherits:
-
Object
- Object
- LeaveIt::CLI
- Defined in:
- lib/leave_it/cli.rb
Class Method Summary collapse
- .gem_declaration_line?(line) ⇒ Boolean
- .gem_line?(line) ⇒ Boolean
- .gem_section_start?(line) ⇒ Boolean
- .parse_gem_declaration_line(line) ⇒ Object
- .parse_gem_line(line) ⇒ Object
- .parse_lockfile_versions(lockfile) ⇒ Object
- .pessimistic_version(version) ⇒ Object
- .process_gemfile_line(line, lock_versions) ⇒ Object
- .process_lockfile_line(line, versions) ⇒ Object
- .run(path) ⇒ Object
- .update_gemfile_versions(gemfile, lock_versions) ⇒ Object
Class Method Details
.gem_declaration_line?(line) ⇒ Boolean
72 73 74 |
# File 'lib/leave_it/cli.rb', line 72 def self.gem_declaration_line?(line) line =~ /^(\s*)gem ['"]([^'"]+)['"](,\s*['"]([^'"]+)['"])?(.*)$/ end |
.gem_line?(line) ⇒ Boolean
63 64 65 |
# File 'lib/leave_it/cli.rb', line 63 def self.gem_line?(line) line =~ /^\s{4}(\S+)\s\(([^)]+)\)/ end |
.gem_section_start?(line) ⇒ Boolean
59 60 61 |
# File 'lib/leave_it/cli.rb', line 59 def self.gem_section_start?(line) line.strip == 'GEM' end |
.parse_gem_declaration_line(line) ⇒ Object
76 77 78 79 |
# File 'lib/leave_it/cli.rb', line 76 def self.parse_gem_declaration_line(line) match = line.match(/^(\s*)gem ['"]([^'"]+)['"](,\s*['"]([^'"]+)['"])?(.*)$/) [match[1], match[2], match[3], match[4], match[5]] end |
.parse_gem_line(line) ⇒ Object
67 68 69 70 |
# File 'lib/leave_it/cli.rb', line 67 def self.parse_gem_line(line) match = line.match(/^\s{4}(\S+)\s\(([^)]+)\)/) [match[1], match[2]] end |
.parse_lockfile_versions(lockfile) ⇒ Object
21 22 23 24 25 |
# File 'lib/leave_it/cli.rb', line 21 def self.parse_lockfile_versions(lockfile) versions = {} File.foreach(lockfile) { |line| process_lockfile_line(line, versions) } versions end |
.pessimistic_version(version) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/leave_it/cli.rb', line 81 def self.pessimistic_version(version) parts = version.split('.') if parts.length >= 3 "~> #{parts[0]}.#{parts[1]}.#{parts[2]}" else "~> #{version}" end end |
.process_gemfile_line(line, lock_versions) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/leave_it/cli.rb', line 46 def self.process_gemfile_line(line, lock_versions) if gem_declaration_line?(line) indent, gem_name, _, version, rest = parse_gem_declaration_line(line) if version.nil? && lock_versions[gem_name] version_str = pessimistic_version(lock_versions[gem_name]) return "#{indent}gem '#{gem_name}', '#{version_str}'#{rest}\n" end end line end |
.process_lockfile_line(line, versions) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/leave_it/cli.rb', line 33 def self.process_lockfile_line(line, versions) @in_gem_section ||= false @in_gem_section = true if gem_section_start?(line) return unless @in_gem_section if gem_line?(line) gem_name, version = parse_gem_line(line) versions[gem_name] = version elsif line.strip.empty? @in_gem_section = false end end |
.run(path) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/leave_it/cli.rb', line 5 def self.run(path) gemfile = File.join(path, 'Gemfile') lockfile = File.join(path, 'Gemfile.lock') unless File.exist?(gemfile) && File.exist?(lockfile) puts "❌ Gemfile or Gemfile.lock not found in #{path}" exit(1) end versions = parse_lockfile_versions(lockfile) updated = update_gemfile_versions(gemfile, versions) File.write(gemfile, updated) puts "✅ Gemfile in #{path} updated successfully." end |
.update_gemfile_versions(gemfile, lock_versions) ⇒ Object
27 28 29 30 31 |
# File 'lib/leave_it/cli.rb', line 27 def self.update_gemfile_versions(gemfile, lock_versions) new_lines = [] File.foreach(gemfile) { |line| new_lines << process_gemfile_line(line, lock_versions) } new_lines.join end |