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 / runner.rb
100644 171 lines (148 sloc) 4.21 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#
# 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/>.
#
 
require File.dirname(__FILE__) + '/db'
require File.dirname(__FILE__) + '/gems'
require File.dirname(__FILE__) + '/facet'
 
class Runner
  
  class << self
    attr_accessor :app_name
  end
  
  def initialize(app_name, runfile = "~/.suprails/config")
    Runner.app_name = app_name
    @runfile = File.expand_path(runfile)
    @sources = File.expand_path('~/.suprails/sources/')
    @facets_source = File.expand_path('~/.suprails/facets/')
    Dir["#{@facets_source}/*.rb"].each{|x| load x }
    
    Facet.registered_facets.each do |name, facet|
      self.class.send(:define_method, name) {}
      instance_eval do
        self.class.send(:define_method, name) do
          facet.go Runner.app_name
        end
      end
    end
  end
  
  def methods
    super.each{|x| puts x}
  end
 
  def run
    gems = Gems.new Runner.app_name
    db = DB.new Runner.app_name
    @base = File.expand_path "./#{Runner.app_name}"
    Dir.mkdir(@base)
    text = File.read(@runfile).split('\n')
    text.each {|l| instance_eval(l)}
  end
 
  def sources sourcefolder
    @sources = File.expand_path "#{sourcefolder}/"
  end
 
  def rails
    `rails #{Runner.app_name}`
  end
  
  def frozen_rails
    `rails #{Runner.app_name} --freeze`
  end
 
  def debug p = ''
    puts "debug: #{p}"
  end
 
  def plugin plugin_location
    `cd #{Runner.app_name}; script/plugin install #{plugin_location}`
  end
 
  def generate generator, *opts
    if opts.length
      args = ''
      opts.each {|x| args += " #{x}"}
      `cd #{Runner.app_name}; script/generate #{generator} #{args}`
    else
      `cd #{Runner.app_name}; script/generate #{generator}`
    end
  end
 
  def folder folder_name
    path = "#{@base}/"
    paths = folder_name.split('/')
    paths.each do |p|
      path += "#{p}/"
      Dir.mkdir path if !File.exists? path
    end
  end
 
  def file source_file, destination
    require 'ftools'
    source = File.expand_path "#{@sources}/#{source_file}"
    dest = File.expand_path "./#{Runner.app_name}/#{destination}"
    File.copy(source, dest, false) if File.exists? source
  end
 
  def delete file_name
    file_name = "#{@base}/#{file_name}"
    File.delete file_name if File.exists?(file_name)
  end
 
  def gpl
    puts 'Installing the GPL into COPYING'
    require 'net/http'
    http = Net::HTTP.new('www.gnu.org')
    path = '/licenses/gpl-3.0.txt'
    begin
      resp = http.get(path)
      if resp.code == '200'
        File.open("#{@base}/COPYING", 'w') do |f|
          f.puts(resp.body)
        end
      else
        puts "Error #{resp.code} while retrieving GPL text."
      end
    rescue SocketError
      puts 'SocketError: You might not be connected to the internet. GPL retrieval failed.'
    end
  end
 
  def rake *opts
    if opts.length
      args = ''
      opts.each {|x| args += " #{x}"}
      `cd #{Runner.app_name}; rake #{args}`
    else
      `cd #{Runner.app_name}; rake`
    end
  end
 
  def git
    gem = false
    begin
      gem = require 'git'
    rescue LoadError => e
      nil
    end
    if gem
      g = Git.init(@base)
    else
      `cd #{Runner.app_name}; git init`
    end
  end
 
  def svn
    `cd #{Runner.app_name}; svnadmin create`
  end
  
  def runcommand *opts
    cmd = opts.shift
    if opts.length
      args = ''
      opts.each {|x| args += " #{x}"}
      `cd #{Runner.app_name}; #{cmd} #{args}`
    else
      `cd #{Runner.app_name}; #{cmd}`
    end
  end
  
end