ffmike / suprails forked from listrophy/suprails

Substitute for the "rails" command that can pre-install any gems, init a git db, and/or freeze rails, etc on top of a rails application

This URL has Read+Write access

suprails / lib / gems.rb
100644 82 lines (71 sloc) 2.156 kb
1
2
3
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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
#
# Suprails: The customizable wrapper to the rails command
#
# Copyright 2008 Bradley Grzesiak
# This file is part of Suprails.
#
# Suprails is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Suprails is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Suprails. If not, see <http://www.gnu.org/licenses/>.
#
 
class Gems
  def initialize app_name
    @app_name = app_name
  end
  def update *gems
    if gems.length
      args = ''
      gems.each {|x| args += " #{x}"}
      result = `gem update #{args}`
      if result
        puts 'Gem update failed. Please enter your admin password for sudo gem update:'
        `sudo gem update #{args}`
      end
    else
      result = `gem update`
      if result
        puts 'Gem update failed. Please enter your admin password for sudo gem update:'
        `sudo gem update`
      end
    end
  end
  
  def config *gems
    # need to do some file editing here
    to_insert = []
    @gems = []
 
    gems.each do |g|
      to_insert << " config.gem '#{g}'\n"
      @gems << g
    end
    
    file_contents = []
 
    File.open("#{@app_name}/config/environment.rb", 'r') do |f|
      f.each {|l| file_contents << l }
    end
    
    insertion_point = 0
    file_contents.reverse.each_index do |i|
      if file_contents[i] =~ /config\.gem/
        insertion_point = i
        break
      end
    end
    
    to_insert.each {|x| file_contents.insert(insertion_point, x) }
 
    File.open("#{@app_name}/config/environment.rb", 'w') do |f|
      file_contents.each do |l|
        f.puts l
      end
    end
    # `cd #{@app_name}; rake gems:install`
  end
  
  def unpack
    `cd #{@app_name}; rake gems:unpack`
  end
end