Module: SetupHelper

Defined in:
lib/setup_helper.rb

Instance Method Summary collapse

Instance Method Details

#config_gems(config_file, gems) ⇒ Object

Add gem configuration to a specified Rails environment file



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/setup_helper.rb', line 4

def config_gems(config_file, gems)
  sentinel = 'Rails::Initializer.run do |config|'
  config_line = ''
  
  gems.each do |gem|
    gem_info = gem.to_s.split('-')
    if gem_info.size > 1
      gem_owner = gem_info[0]
      gem_lib = gem_info[1]
      config_line = "config.gem '#{gem_owner}-#{gem_lib}', :lib => '#{gem_lib}'"
    else
      gem_lib = gem_info[0]
      config_line = "config.gem '#{gem_lib}'"
    end
    
    gsub_file_if_missing config_file, /(#{Regexp.escape(sentinel)})/mi, config_line do |match|
      "#{match}\n  #{config_line}"
    end
  end
end

#gsub_file_if_missing(path, regexp, new_exp, *args, &block) ⇒ Object

Add info to specified file and beneath specified regex if the expression don’t exist in the file.



26
27
28
29
30
31
32
33
34
# File 'lib/setup_helper.rb', line 26

def gsub_file_if_missing(path, regexp, new_exp, *args, &block)
  existing_content = File.read(path)
  unless existing_content =~ /(#{new_exp.strip}|#{new_exp.strip.tr('\'', '\"')})/
    content = File.read(path).gsub(regexp, *args, &block)
  else
    content = existing_content
  end
  File.open(path, 'wb') { |file| file.write(content) }
end