aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/commands
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib/commands')
-rw-r--r--railties/lib/commands/breakpointer3
-rw-r--r--railties/lib/commands/console.rb22
-rw-r--r--railties/lib/commands/destroy7
-rwxr-xr-xrailties/lib/commands/generate7
-rw-r--r--railties/lib/commands/ncgi/listener86
-rw-r--r--railties/lib/commands/ncgi/tracker69
-rw-r--r--railties/lib/commands/perfom/benchmarker19
-rw-r--r--railties/lib/commands/perfom/profiler35
-rw-r--r--railties/lib/commands/perform.rb5
-rw-r--r--railties/lib/commands/process.rb5
-rw-r--r--railties/lib/commands/runner.rb26
-rw-r--r--railties/lib/commands/server.rb57
-rw-r--r--railties/lib/commands/update5
13 files changed, 346 insertions, 0 deletions
diff --git a/railties/lib/commands/breakpointer b/railties/lib/commands/breakpointer
new file mode 100644
index 0000000000..4ac3e7c3c9
--- /dev/null
+++ b/railties/lib/commands/breakpointer
@@ -0,0 +1,3 @@
+#!/usr/local/bin/ruby
+$LOAD_PATH << File.dirname(__FILE__) + '/../vendor/railties/lib'
+require 'breakpoint_client'
diff --git a/railties/lib/commands/console.rb b/railties/lib/commands/console.rb
new file mode 100644
index 0000000000..b8756ba3a1
--- /dev/null
+++ b/railties/lib/commands/console.rb
@@ -0,0 +1,22 @@
+irb = RUBY_PLATFORM =~ /mswin32/ ? 'irb.bat' : 'irb'
+
+require 'optparse'
+options = { :sandbox => false, :irb => irb }
+OptionParser.new do |opt|
+ opt.on('-s', '--sandbox', 'Rollback database modifications on exit.') { |options[:sandbox]| }
+ opt.on("--irb=[#{irb}]", 'Invoke a different irb.') { |options[:irb]| }
+ opt.parse!(ARGV)
+end
+
+libs = " -r irb/completion"
+libs << " -r #{File.dirname(__FILE__)}/../config/environment"
+libs << " -r console_sandbox" if options[:sandbox]
+
+ENV['RAILS_ENV'] = ARGV.first || 'development'
+if options[:sandbox]
+ puts "Loading #{ENV['RAILS_ENV']} environment in sandbox."
+ puts "Any modifications you make will be rolled back on exit."
+else
+ puts "Loading #{ENV['RAILS_ENV']} environment."
+end
+exec "#{options[:irb]} #{libs} --prompt-mode simple"
diff --git a/railties/lib/commands/destroy b/railties/lib/commands/destroy
new file mode 100644
index 0000000000..f9d9d181a4
--- /dev/null
+++ b/railties/lib/commands/destroy
@@ -0,0 +1,7 @@
+#!/usr/local/bin/ruby
+require File.dirname(__FILE__) + '/../config/environment'
+require 'rails_generator'
+require 'rails_generator/scripts/destroy'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+Rails::Generator::Scripts::Destroy.new.run(ARGV)
diff --git a/railties/lib/commands/generate b/railties/lib/commands/generate
new file mode 100755
index 0000000000..43bcfd0cb1
--- /dev/null
+++ b/railties/lib/commands/generate
@@ -0,0 +1,7 @@
+#!/usr/local/bin/ruby
+require File.dirname(__FILE__) + '/../config/environment'
+require 'rails_generator'
+require 'rails_generator/scripts/generate'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+Rails::Generator::Scripts::Generate.new.run(ARGV)
diff --git a/railties/lib/commands/ncgi/listener b/railties/lib/commands/ncgi/listener
new file mode 100644
index 0000000000..421c453f23
--- /dev/null
+++ b/railties/lib/commands/ncgi/listener
@@ -0,0 +1,86 @@
+#!/usr/local/bin/ruby
+
+require 'stringio'
+require 'fileutils'
+require 'fcgi_handler'
+
+def message(s)
+ $stderr.puts "listener: #{s}" if ENV && ENV["DEBUG_GATEWAY"]
+end
+
+class RemoteCGI < CGI
+ attr_accessor :stdinput, :stdoutput, :env_table
+ def initialize(env_table, input = nil, output = nil)
+ self.env_table = env_table
+ self.stdinput = input || StringIO.new
+ self.stdoutput = output || StringIO.new
+ super()
+ end
+
+ def out(stream) # Ignore the requested output stream
+ super(stdoutput)
+ end
+end
+
+class Listener
+ include DRbUndumped
+
+ def initialize(timeout, socket_path)
+ @socket = File.expand_path(socket_path)
+ @mutex = Mutex.new
+ @active = false
+ @timeout = timeout
+
+ @handler = RailsFCGIHandler.new
+ @handler.extend DRbUndumped
+
+ message 'opening socket'
+ DRb.start_service("drbunix:#{@socket}", self)
+
+ message 'entering process loop'
+ @handler.process! self
+ end
+
+ def each_cgi(&cgi_block)
+ @cgi_block = cgi_block
+ message 'entering idle loop'
+ loop do
+ sleep @timeout rescue nil
+ die! unless @active
+ @active = false
+ end
+ end
+
+ def process(env, input)
+ message 'received request'
+ @mutex.synchronize do
+ @active = true
+
+ message 'creating input stream'
+ input_stream = StringIO.new(input)
+ message 'building CGI instance'
+ cgi = RemoteCGI.new(eval(env), input_stream)
+
+ message 'yielding to fcgi handler'
+ @cgi_block.call cgi
+ message 'yield finished -- sending output'
+
+ cgi.stdoutput.seek(0)
+ output = cgi.stdoutput.read
+
+ return output
+ end
+ end
+
+ def die!
+ message 'shutting down'
+ DRb.stop_service
+ FileUtils.rm_f @socket
+ Kernel.exit 0
+ end
+end
+
+socket_path = ARGV.shift
+timeout = (ARGV.shift || 90).to_i
+
+Listener.new(timeout, socket_path) \ No newline at end of file
diff --git a/railties/lib/commands/ncgi/tracker b/railties/lib/commands/ncgi/tracker
new file mode 100644
index 0000000000..859c9fa0e0
--- /dev/null
+++ b/railties/lib/commands/ncgi/tracker
@@ -0,0 +1,69 @@
+#!/usr/local/bin/ruby
+
+require 'drb'
+require 'thread'
+
+def message(s)
+ $stderr.puts "tracker: #{s}" if ENV && ENV["DEBUG_GATEWAY"]
+end
+
+class Tracker
+ include DRbUndumped
+
+ def initialize(instances, socket_path)
+ @instances = instances
+ @socket = File.expand_path(socket_path)
+ @active = false
+
+ @listeners = []
+ @instances.times { @listeners << Mutex.new }
+
+ message "using #{@listeners.length} listeners"
+ message "opening socket at #{@socket}"
+
+ @service = DRb.start_service("drbunix://#{@socket}", self)
+ end
+
+ def with_listener
+ message "listener requested"
+
+ mutex = has_lock = index = nil
+ 3.times do
+ @listeners.each_with_index do |mutex, index|
+ has_lock = mutex.try_lock
+ break if has_lock
+ end
+ break if has_lock
+ sleep 0.05
+ end
+
+ if has_lock
+ message "obtained listener #{index}"
+ @active = true
+ begin yield index
+ ensure
+ mutex.unlock
+ message "released listener #{index}"
+ end
+ else
+ message "dropping request because no listeners are available!"
+ end
+ end
+
+ def background(check_interval = nil)
+ if check_interval
+ loop do
+ sleep check_interval
+ message "Idle for #{check_interval}, shutting down" unless @active
+ @active = false
+ Kernel.exit 0
+ end
+ else DRb.thread.join
+ end
+ end
+end
+
+socket_path = ARGV.shift
+instances = ARGV.shift.to_i
+t = Tracker.new(instances, socket_path)
+t.background(ARGV.first ? ARGV.shift.to_i : 90) \ No newline at end of file
diff --git a/railties/lib/commands/perfom/benchmarker b/railties/lib/commands/perfom/benchmarker
new file mode 100644
index 0000000000..b07ddcfcb2
--- /dev/null
+++ b/railties/lib/commands/perfom/benchmarker
@@ -0,0 +1,19 @@
+#!/usr/local/bin/ruby
+
+if ARGV.empty?
+ puts "Usage: benchmarker times 'Person.expensive_way' 'Person.another_expensive_way' ..."
+ exit
+end
+
+require File.dirname(__FILE__) + '/../config/environment'
+require 'benchmark'
+include Benchmark
+
+# Don't include compilation in the benchmark
+ARGV[1..-1].each { |expression| eval(expression) }
+
+bm(6) do |x|
+ ARGV[1..-1].each_with_index do |expression, idx|
+ x.report("##{idx + 1}") { ARGV[0].to_i.times { eval(expression) } }
+ end
+end \ No newline at end of file
diff --git a/railties/lib/commands/perfom/profiler b/railties/lib/commands/perfom/profiler
new file mode 100644
index 0000000000..d84c8b739f
--- /dev/null
+++ b/railties/lib/commands/perfom/profiler
@@ -0,0 +1,35 @@
+#!/usr/local/bin/ruby
+if ARGV.empty?
+ $stderr.puts "Usage: profiler 'Person.expensive_method(10)' [times]"
+ exit(1)
+end
+
+# Keep the expensive require out of the profile.
+$stderr.puts 'Loading Rails...'
+require File.dirname(__FILE__) + '/../config/environment'
+
+# Define a method to profile.
+if ARGV[1] and ARGV[1].to_i > 1
+ eval "def profile_me() #{ARGV[1]}.times { #{ARGV[0]} } end"
+else
+ eval "def profile_me() #{ARGV[0]} end"
+end
+
+# Use the ruby-prof extension if available. Fall back to stdlib profiler.
+begin
+ require 'prof'
+ $stderr.puts 'Using the ruby-prof extension.'
+ Prof.clock_mode = Prof::GETTIMEOFDAY
+ Prof.start
+ profile_me
+ results = Prof.stop
+ require 'rubyprof_ext'
+ Prof.print_profile(results, $stderr)
+rescue LoadError
+ require 'profiler'
+ $stderr.puts 'Using the standard Ruby profiler.'
+ Profiler__.start_profile
+ profile_me
+ Profiler__.stop_profile
+ Profiler__.print_profile($stderr)
+end
diff --git a/railties/lib/commands/perform.rb b/railties/lib/commands/perform.rb
new file mode 100644
index 0000000000..ae45e7e633
--- /dev/null
+++ b/railties/lib/commands/perform.rb
@@ -0,0 +1,5 @@
+if %w( benchmarker profiler ).include?(ARGV.first)
+ require "#{File.dirname(__FILE__)}/process/#{ARGV.shift}"
+else
+ puts "Choose either reaper, spawner, or spinner"
+end \ No newline at end of file
diff --git a/railties/lib/commands/process.rb b/railties/lib/commands/process.rb
new file mode 100644
index 0000000000..87b4b158cd
--- /dev/null
+++ b/railties/lib/commands/process.rb
@@ -0,0 +1,5 @@
+if %w( reaper spawner spinner ).include?(ARGV.first)
+ require "#{File.dirname(__FILE__)}/process/#{ARGV.shift}"
+else
+ puts "Choose either reaper, spawner, or spinner"
+end \ No newline at end of file
diff --git a/railties/lib/commands/runner.rb b/railties/lib/commands/runner.rb
new file mode 100644
index 0000000000..7d1b56e3f9
--- /dev/null
+++ b/railties/lib/commands/runner.rb
@@ -0,0 +1,26 @@
+require 'optparse'
+
+options = { :environment => "development" }
+
+ARGV.options do |opts|
+ script_name = File.basename($0)
+ opts.banner = "Usage: runner 'puts Person.find(1).name' [options]"
+
+ opts.separator ""
+
+ opts.on("-e", "--environment=name", String,
+ "Specifies the environment for the runner to operate under (test/development/production).",
+ "Default: development") { |options[:environment]| }
+
+ opts.separator ""
+
+ opts.on("-h", "--help",
+ "Show this help message.") { puts opts; exit }
+
+ opts.parse!
+end
+
+ENV["RAILS_ENV"] = options[:environment]
+
+require RAILS_ROOT + '/config/environment'
+eval(ARGV.first) \ No newline at end of file
diff --git a/railties/lib/commands/server.rb b/railties/lib/commands/server.rb
new file mode 100644
index 0000000000..5330c050b9
--- /dev/null
+++ b/railties/lib/commands/server.rb
@@ -0,0 +1,57 @@
+require 'webrick'
+require 'optparse'
+
+OPTIONS = {
+ :port => 3000,
+ :ip => "0.0.0.0",
+ :environment => "development",
+ :server_root => File.expand_path(File.dirname(__FILE__) + "/../public/"),
+ :server_type => WEBrick::SimpleServer,
+ :charset => "UTF-8",
+ :mime_types => WEBrick::HTTPUtils::DefaultMimeTypes
+}
+
+ARGV.options do |opts|
+ script_name = File.basename($0)
+ opts.banner = "Usage: ruby #{script_name} [options]"
+
+ opts.separator ""
+
+ opts.on("-p", "--port=port", Integer,
+ "Runs Rails on the specified port.",
+ "Default: 3000") { |OPTIONS[:port]| }
+ opts.on("-b", "--binding=ip", String,
+ "Binds Rails to the specified ip.",
+ "Default: 0.0.0.0") { |OPTIONS[:ip]| }
+ opts.on("-e", "--environment=name", String,
+ "Specifies the environment to run this server under (test/development/production).",
+ "Default: development") { |OPTIONS[:environment]| }
+ opts.on("-m", "--mime-types=filename", String,
+ "Specifies an Apache style mime.types configuration file to be used for mime types",
+ "Default: none") { |mime_types_file| OPTIONS[:mime_types] = WEBrick::HTTPUtils::load_mime_types(mime_types_file) }
+
+ opts.on("-d", "--daemon",
+ "Make Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
+ ) { OPTIONS[:server_type] = WEBrick::Daemon }
+
+ opts.on("-c", "--charset=charset", String,
+ "Set default charset for output.",
+ "Default: UTF-8") { |OPTIONS[:charset]| }
+
+ opts.separator ""
+
+ opts.on("-h", "--help",
+ "Show this help message.") { puts opts; exit }
+
+ opts.parse!
+end
+
+ENV["RAILS_ENV"] = OPTIONS[:environment]
+require File.dirname(__FILE__) + "/../config/environment"
+require 'webrick_server'
+
+OPTIONS['working_directory'] = File.expand_path(RAILS_ROOT)
+
+puts "=> Rails application started on http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}"
+puts "=> Ctrl-C to shutdown server; call with --help for options" if OPTIONS[:server_type] == WEBrick::SimpleServer
+DispatchServlet.dispatch(OPTIONS)
diff --git a/railties/lib/commands/update b/railties/lib/commands/update
new file mode 100644
index 0000000000..430d325b07
--- /dev/null
+++ b/railties/lib/commands/update
@@ -0,0 +1,5 @@
+#!/usr/local/bin/ruby
+require File.dirname(__FILE__) + '/../config/environment'
+require 'rails_generator'
+require 'rails_generator/scripts/update'
+Rails::Generator::Scripts::Update.new.run(ARGV)