Module: LockNessMonster

Defined in:
lib/lock_ness_monster.rb,
lib/lock_ness_monster/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.lockObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lock_ness_monster.rb', line 4

def self.lock
  unless File.exists?("Gemfile")
    puts "Gemfile not present. Nothing to lock."
    return
  end

  gems = IO.readlines(File.join(Dir.pwd, "Gemfile"))

  puts "* Looking for gem versions"

  installed_gems = %x{bundle exec gem list}.split("\n").reduce({}) do |hash, gem|
    match = gem.match(/(\S+)\s\((\S+)\)/)
    gem_name, version = match[1], match[2]
    hash[gem_name] = version
    hash
  end

  gems.map! do |line|
    match = line.match /^\s*gem\s['"](\S+)['"]\s*$/
    if match
      gem_name = match[1].strip
      version_number = installed_gems[gem_name]
      line.rstrip + ", '~> #{version_number}'\n"
    else
      line
    end
  end

  puts "* Updating your Gemfile"

  File.open(File.join(Dir.pwd, "Gemfile"), 'w') do |f|
    f.puts gems
  end

  puts "* RAAAWR!! Your Gemfile is locked (and loaded)."
end