Class: SmarterBundler::Gemfile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGemfile

Returns a new instance of Gemfile.



7
8
9
10
11
12
13
14
15
# File 'lib/smarter_bundler/gemfile.rb', line 7

def initialize
  @filename = ENV['BUNDLE_GEMFILE'] || 'Gemfile'
  @contents = [ ]
  File.open(@filename, 'r').each do |line|
    line.chomp
    @contents << line
  end
  @changed = false
end

Instance Attribute Details

#changedObject (readonly)

Returns the value of attribute changed.



5
6
7
# File 'lib/smarter_bundler/gemfile.rb', line 5

def changed
  @changed
end

#contentsObject (readonly)

Returns the value of attribute contents.



5
6
7
# File 'lib/smarter_bundler/gemfile.rb', line 5

def contents
  @contents
end

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/smarter_bundler/gemfile.rb', line 5

def filename
  @filename
end

Instance Method Details

#restrict_gem_version(gem, version_limit) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/smarter_bundler/gemfile.rb', line 17

def restrict_gem_version gem, version_limit
  return false unless version_limit.to_s =~ /\d\.\d/
  if @contents.select{|line| line =~ /^\s*gem\s+['"]#{gem}['"]/}.empty?
    @contents << "gem '#{gem}', '>=0'"
  end
  adjusted = false
  @contents.map! do |line|
    if line =~ /^(\s*gem\s+['"]#{gem}['"])(\s*,\*['"]([^'"]*)['"])?(.*?)$/
      gem_and_name = $1
      rest_of_line = $4
      version = $3.to_s
      puts "Found #{gem_name_name} with version spec: #{version} and other args: #{rest_of_line}"
      new_version = version.sub(/<=?\s*[^,\s]+/, '').sub(/^\s*,\s*/, '').sub(/\s*,\s*$/, '') + (version == '' ? '' : ', ') + "< #{version_limit}"
      puts "  Calculated new_version spec: #{new_version}"
      if new_version != version
        @changed = true
        rest_of_line.sub!(/  # REQUIRED - Added by SmarterBundler.*/, '')
        rest_of_line << '  # REQUIRED - Added by SmarterBundler'
        line = "#{gem_and_name}, '#{new_version}'#{rest_of_line}"
        puts "Changed Gemfile line to: #{line}"
        line
      else
        puts "Unable to change version for #{gem}"
        line
      end
    else
      line
    end
  end
  @changed
end

#saveObject



49
50
51
52
53
54
55
56
57
# File 'lib/smarter_bundler/gemfile.rb', line 49

def save
  if @changed
    File.open("#{@filename}.new", 'w') do |file|
      file.puts *@contents
    end
    FileUtils.move "#{@filename}.new", @filename, :force => true
    @changed = false
  end
end