Class: Rails::TemplateRunner
- Defined in:
- lib/rails_generator/generators/applications/app/template_runner.rb
Instance Attribute Summary collapse
-
#logger ⇒ Object
writeonly
Sets the attribute logger.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#capify! ⇒ Object
Just run the capify command in root.
-
#environment(data = nil, options = {}, &block) ⇒ Object
Adds a line inside the Initializer block for config/environment.rb.
-
#file(filename, data = nil, log_action = true, &block) ⇒ Object
Create a new file in the Rails project folder.
-
#freeze!(args = {}) ⇒ Object
Add Rails to /vendor/rails.
-
#gem(name, options = {}) ⇒ Object
Adds an entry into config/environment.rb for the supplied gem :.
-
#generate(what, *args) ⇒ Object
Generate something using a generator from Rails or a plugin.
-
#git(command = {}) ⇒ Object
Run a command in git.
-
#initialize(template, root = '') ⇒ TemplateRunner
constructor
:nodoc:.
-
#initializer(filename, data = nil, &block) ⇒ Object
Create a new initializer with the provided code (either in a block or a string).
-
#lib(filename, data = nil, &block) ⇒ Object
Create a new file in the lib/ directory.
- #load_template(template) ⇒ Object
-
#plugin(name, options) ⇒ Object
Install a plugin.
-
#rake(command, options = {}) ⇒ Object
Runs the supplied rake task.
-
#rakefile(filename, data = nil, &block) ⇒ Object
Create a new Rakefile with the provided code (either in a block or a string).
-
#route(routing_code) ⇒ Object
Make an entry in Rails routing file conifg/routes.rb.
-
#run(command, log_action = true) ⇒ Object
Executes a command.
-
#run_ruby_script(command, log_action = true) ⇒ Object
Executes a ruby script (taking into account WIN32 platform quirks).
-
#vendor(filename, data = nil, &block) ⇒ Object
Create a new file in the vendor/ directory.
Constructor Details
#initialize(template, root = '') ⇒ TemplateRunner
:nodoc:
13 14 15 16 17 18 19 20 21 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 13 def initialize(template, root = '') # :nodoc: @root = File.(File.directory?(root) ? root : File.join(Dir.pwd, root)) log 'applying', "template: #{template}" load_template(template) log 'applied', "#{template}" end |
Instance Attribute Details
#logger=(value) ⇒ Object
Sets the attribute logger
11 12 13 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 11 def logger=(value) @logger = value end |
Instance Method Details
#capify! ⇒ Object
Just run the capify command in root
Example
capify!
275 276 277 278 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 275 def capify! log 'capifying' in_root { run('capify .', false) } end |
#environment(data = nil, options = {}, &block) ⇒ Object
Adds a line inside the Initializer block for config/environment.rb. Used by #gem If options :env is specified, the line is appended to the corresponding file in config/environments/#Rails.env.rb
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 103 def environment(data = nil, = {}, &block) sentinel = 'Rails::Initializer.run do |config|' data = block.call if !data && block_given? in_root do if [:env].nil? gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match| "#{match}\n " << data end else Array.wrap([:env]).each do|env| append_file "config/environments/#{env}.rb", "\n#{data}" end end end end |
#file(filename, data = nil, log_action = true, &block) ⇒ Object
Create a new file in the Rails project folder. Specify the relative path from RAILS_ROOT. Data is the return value of a block or a data string.
Examples
file("lib/fun_party.rb") do
hostname = ask("What is the virtual hostname I should use?")
"vhost.name = #{hostname}"
end
file("config/apach.conf", "your apache config")
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 45 def file(filename, data = nil, log_action = true, &block) log 'file', filename if log_action dir, file = [File.dirname(filename), File.basename(filename)] inside(dir) do File.open(file, "w") do |f| if block_given? f.write(block.call) else f.write(data) end end end end |
#freeze!(args = {}) ⇒ Object
Add Rails to /vendor/rails
Example
freeze!
286 287 288 289 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 286 def freeze!(args = {}) log 'vendor', 'rails edge' in_root { run('rake rails:freeze:edge', false) } end |
#gem(name, options = {}) ⇒ Object
Adds an entry into config/environment.rb for the supplied gem :
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 86 def gem(name, = {}) log 'gem', name env = .delete(:env) gems_code = "config.gem '#{name}'" if .any? opts = .inject([]) {|result, h| result << [":#{h[0]} => #{h[1].inspect.gsub('"',"'")}"] }.sort.join(", ") gems_code << ", #{opts}" end environment gems_code, :env => env end |
#generate(what, *args) ⇒ Object
Generate something using a generator from Rails or a plugin. The second parameter is the argument string that is passed to the generator or an Array that is joined.
Example
generate(:authenticated, "user session")
228 229 230 231 232 233 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 228 def generate(what, *args) log 'generating', what argument = args.map(&:to_s).flatten.join(" ") in_root { run_ruby_script("script/generate #{what} #{argument}", false) } end |
#git(command = {}) ⇒ Object
Run a command in git.
Examples
git :init
git :add => "this.file that.rb"
git :add => "onefile.rb", :rm => "badfile.cxx"
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 129 def git(command = {}) in_root do if command.is_a?(Symbol) log 'running', "git #{command}" Git.run(command.to_s) else command.each do |command, | log 'running', "git #{command} #{}" Git.run("#{command} #{}") end end end end |
#initializer(filename, data = nil, &block) ⇒ Object
Create a new initializer with the provided code (either in a block or a string).
Examples
initializer("globals.rb") do
data = ""
['MY_WORK', 'ADMINS', 'BEST_COMPANY_EVAR'].each do
data << "#{const} = :entp"
end
data
end
initializer("api.rb", "API_KEY = '123456'")
215 216 217 218 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 215 def initializer(filename, data = nil, &block) log 'initializer', filename file("config/initializers/#{filename}", data, false, &block) end |
#lib(filename, data = nil, &block) ⇒ Object
Create a new file in the lib/ directory. Code can be specified in a block or a data string can be given.
Examples
lib("crypto.rb") do
"crypted_special_value = '#{rand}--#{Time.now}--#{rand(1337)}--'"
end
lib("foreign.rb", "# Foreign code is fun")
171 172 173 174 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 171 def lib(filename, data = nil, &block) log 'lib', filename file("lib/#{filename}", data, false, &block) end |
#load_template(template) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 23 def load_template(template) begin code = open(template).read in_root { self.instance_eval(code) } rescue LoadError, Errno::ENOENT => e raise "The template [#{template}] could not be loaded. Error: #{e}" end end |
#plugin(name, options) ⇒ Object
Install a plugin. You must provide either a Subversion url or Git url. For a Git-hosted plugin, you can specify if it should be added as a submodule instead of cloned.
Examples
plugin 'restful-authentication', :git => 'git://github.com/technoweenie/restful-authentication.git'
plugin 'restful-authentication', :git => 'git://github.com/technoweenie/restful-authentication.git', :submodule => true
plugin 'restful-authentication', :svn => 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 69 def plugin(name, ) log 'plugin', name if [:git] && [:submodule] in_root do Git.run("submodule add #{[:git]} vendor/plugins/#{name}") end elsif [:git] || [:svn] in_root do run_ruby_script("script/plugin install #{[:svn] || [:git]}", false) end else log "! no git or svn provided for #{name}. skipping..." end end |
#rake(command, options = {}) ⇒ Object
Runs the supplied rake task
Example
rake("db:migrate")
rake("db:migrate", :env => "production")
rake("gems:install", :sudo => true)
262 263 264 265 266 267 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 262 def rake(command, = {}) log 'rake', command env = [:env] || 'development' sudo = [:sudo] ? 'sudo ' : '' in_root { run("#{sudo}rake #{command} RAILS_ENV=#{env}", false) } end |
#rakefile(filename, data = nil, &block) ⇒ Object
Create a new Rakefile with the provided code (either in a block or a string).
Examples
rakefile("bootstrap.rake") do
project = ask("What is the UNIX name of your project?")
<<-TASK
namespace :#{project} do
task :bootstrap do
puts "i like boots!"
end
end
TASK
end
rakefile("seed.rake", "puts 'im plantin ur seedz'")
194 195 196 197 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 194 def rakefile(filename, data = nil, &block) log 'rakefile', filename file("lib/tasks/#{filename}", data, false, &block) end |
#route(routing_code) ⇒ Object
Make an entry in Rails routing file conifg/routes.rb
Example
route "map.root :controller => :welcome"
297 298 299 300 301 302 303 304 305 306 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 297 def route(routing_code) log 'route', routing_code sentinel = 'ActionController::Routing::Routes.draw do |map|' in_root do gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match| "#{match}\n #{routing_code}\n" end end end |
#run(command, log_action = true) ⇒ Object
Executes a command
Example
inside('vendor') do
run('ln -s ~/edge rails')
end
243 244 245 246 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 243 def run(command, log_action = true) log 'executing', "#{command} from #{Dir.pwd}" if log_action `#{command}` end |
#run_ruby_script(command, log_action = true) ⇒ Object
Executes a ruby script (taking into account WIN32 platform quirks)
249 250 251 252 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 249 def run_ruby_script(command, log_action = true) ruby_command = RUBY_PLATFORM=~ /win32/ ? 'ruby ' : '' run("#{ruby_command}#{command}", log_action) end |
#vendor(filename, data = nil, &block) ⇒ Object
Create a new file in the vendor/ directory. Code can be specified in a block or a data string can be given.
Examples
vendor("sekrit.rb") do
sekrit_salt = "#{Time.now}--#{3.years.ago}--#{rand}--"
"salt = '#{sekrit_salt}'"
end
vendor("foreign.rb", "# Foreign code is fun")
155 156 157 158 |
# File 'lib/rails_generator/generators/applications/app/template_runner.rb', line 155 def vendor(filename, data = nil, &block) log 'vendoring', filename file("vendor/#{filename}", data, false, &block) end |