Class: GemCheckUpdates::Gemfile

Inherits:
Object
  • Object
show all
Defined in:
lib/gem_check_updates/gemfile.rb

Constant Summary collapse

RUBYGEMS_API =
'https://rubygems.org/api/v1/versions'
CONCURRENCY =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option = GemCheckUpdates::Option.new) ⇒ Gemfile

Returns a new instance of Gemfile.



12
13
14
15
16
17
18
# File 'lib/gem_check_updates/gemfile.rb', line 12

def initialize(option = GemCheckUpdates::Option.new)
  @option = option
  @file_backup = "#{option.file}.backup"
  @gems = parse(option.update_scope)

  check_updates!
end

Instance Attribute Details

#file_backupObject (readonly)

Returns the value of attribute file_backup.



9
10
11
# File 'lib/gem_check_updates/gemfile.rb', line 9

def file_backup
  @file_backup
end

#gemsObject (readonly)

Returns the value of attribute gems.



10
11
12
# File 'lib/gem_check_updates/gemfile.rb', line 10

def gems
  @gems
end

#optionObject (readonly)

Returns the value of attribute option.



8
9
10
# File 'lib/gem_check_updates/gemfile.rb', line 8

def option
  @option
end

Instance Method Details

#backupObject



20
21
22
# File 'lib/gem_check_updates/gemfile.rb', line 20

def backup
  FileUtils.cp(@option.file, @file_backup)
end

#check_updates!Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gem_check_updates/gemfile.rb', line 45

def check_updates!
  EventMachine.synchrony do
    EventMachine::Synchrony::FiberIterator.new(@gems, CONCURRENCY).each do |gem|
      http = EventMachine::HttpRequest.new("#{RUBYGEMS_API}/#{gem.name}.json").get
      response = JSON.parse(http.response)
      versions = response.map do |v|
        number = v['number']
        pre_release = v['prerelease']

        GemCheckUpdates::GemVersion.new(number: number, pre_release: pre_release)
      end

      gem.versions = versions
      GemCheckUpdates::Message.out('.')
    end

    GemCheckUpdates::Message.out("\n\n")
    EventMachine.stop
  end

  @gems
end

#parse(update_scope) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/gem_check_updates/gemfile.rb', line 32

def parse(update_scope)
  Bundler::Definition.build(@option.file, nil, nil).dependencies.map do |gem|
    name = gem.name
    version_range, version_number = gem.requirements_list.first.split(' ')
    version = GemCheckUpdates::GemVersion.new(number: version_number)

    Gem.new(name: name,
            current_version: version,
            version_range: version_range,
            update_scope: update_scope)
  end
end

#remove_backupObject



28
29
30
# File 'lib/gem_check_updates/gemfile.rb', line 28

def remove_backup
  FileUtils.rm(@file_backup)
end

#restoreObject



24
25
26
# File 'lib/gem_check_updates/gemfile.rb', line 24

def restore
  FileUtils.mv(@file_backup, @option.file)
end

#updateObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gem_check_updates/gemfile.rb', line 68

def update
  gemfile_lines = []

  File.open(@option.file) do |current|
    current.each_line do |line|
      gemfile_lines << line
    end
  end

  File.open(@option.file, 'w') do |updated|
    gemfile_lines.each do |line|
      matcher = line.match(/gem ['"](.+?)['"]\s*,\s*['"][>=|~>]*\s+(.+?)['"]/)

      if matcher
        _, name, old_version = *matcher
        target = @gems.find { |gem| gem.name == name }

        line.gsub!(old_version, target.latest_version.to_s) unless target.nil?
      end

      updated.puts(line)
    end
  end
end