Module: Gemlock

Defined in:
lib/gemlock.rb,
lib/gemlock/config.rb,
lib/gemlock/railtie.rb,
lib/gemlock/version.rb,
lib/generators/gemlock/config_generator.rb

Defined Under Namespace

Modules: Config Classes: ConfigGenerator, Railtie

Constant Summary collapse

VERSION =
"0.2.8"

Class Method Summary collapse

Class Method Details

.check_gems_individually(gems) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gemlock.rb', line 111

def check_gems_individually(gems)
  outdated = {}

  gems.each_pair do |name, version|
    latest_version = lookup_version(name)
    update_type = difference(version, latest_version)
    if Gem::Version.new(latest_version) > Gem::Version.new(version)
      if Config.parsed && Config.parsed['releases'].include?(update_type)
        outdated[name] = latest_version
      end
    end
  end

  outdated.inject({}) do |hash, gem|
    name, version = *gem
    hash[name] = { :latest => version, :current => gems[name] }
    hash
  end
end

.difference(version_a, version_b) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gemlock.rb', line 71

def difference(version_a, version_b)
  major_a, minor_a, patch_a = process_version(version_a)
  major_b, minor_b, patch_b = process_version(version_b)

  if (major_a - major_b).abs > 0
    "major"
  elsif (minor_a - minor_b).abs > 0
    "minor"
  elsif (patch_a - patch_b).abs > 0
    "patch"
  else
    "none"
  end
end

.initializer(automatic = true) ⇒ Object

By default, check for updates every 2 weeks



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gemlock.rb', line 87

def initializer(automatic = true)
  update_interval = Config.update_interval
  Thread.new(update_interval) do |interval|
    loop do
      puts "Checking for gem updates..."
      outdated = Gemlock.outdated(automatic)
      if outdated.empty?
        puts "All gems up to date!"
      else
        outdated.each_pair do |name, versions|
          puts "#{name} is out of date!"
          puts "Installed version: #{versions[:current]}. Latest version: #{versions[:latest]}"
          puts "To update: bundle update #{name}"
        end
        puts ""
        puts "To update all your gems via bundler:"
        puts "bundle update"
      end
      puts "Checking for updates again in #{interval} seconds"
      sleep interval
    end
  end
end

.locked_gemfile_specsObject



21
22
23
24
25
26
27
28
# File 'lib/gemlock.rb', line 21

def locked_gemfile_specs
  locked_content = Bundler.read_file(lockfile)
  locked = Bundler::LockfileParser.new(locked_content)

  gemfile_names = locked.dependencies.map(&:name)
  locked_gemfile_specs = locked.specs.clone
  locked_gemfile_specs.delete_if { |spec| !gemfile_names.include?(spec.name) }
end

.lockfileObject



13
14
15
16
17
18
19
# File 'lib/gemlock.rb', line 13

def lockfile
  @lockfile ||= if defined?(Rails)
                  Rails.root.join('Gemfile.lock')
                else
                  Bundler::SharedHelpers.default_lockfile
                end
end

.lookup_version(name) ⇒ Object



30
31
32
33
34
# File 'lib/gemlock.rb', line 30

def lookup_version(name)
  json_hash = JSON.parse(RestClient.get("http://gemlock.herokuapp.com/ruby_gems/#{name}/latest.json"))

  return json_hash["version"]
end

.outdated(automatic = false) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/gemlock.rb', line 36

def outdated(automatic = false)
  specs = {}
  locked_gemfile_specs.each do |spec|
    specs[spec.name] = spec.version.to_s
  end

  begin
    types = nil
    app_name = nil

    if Config.parsed
      type = Config.parsed['releases']
      app_name = Config.parsed['name']
    end

    params = {:gems => specs.to_json }
    params[:types]     = types        if types
    params[:app_name]  = app_name     if app_name
    params[:automatic] = automatic    if automatic
    params[:interval]  = Config.update_interval
    params[:email]     = Config.email if Config.email

    response = RestClient.get("http://gemlock.herokuapp.com/ruby_gems/updates.json", :params => params)
    gems = JSON.parse(response)

    gems.inject({}) do |hash, gem|
      name, version = *gem
      hash[name] = {:latest => version, :current => specs[name]}
      hash
    end
  rescue RestClient::GatewayTimeout => e
    check_gems_individually(specs)
  end
end