diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-09-29 12:18:51 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-09-29 12:18:51 +0000 |
commit | 0a0ac868453e9d8d9ee1dac6a7eb274772235b05 (patch) | |
tree | 1db7e1fce4d2c62f97c204ef72e7898c5263c82a /railties/lib | |
parent | 070d218c479c8cbb41e78f599110f0ea1cf0343a (diff) | |
download | rails-0a0ac868453e9d8d9ee1dac6a7eb274772235b05.tar.gz rails-0a0ac868453e9d8d9ee1dac6a7eb274772235b05.tar.bz2 rails-0a0ac868453e9d8d9ee1dac6a7eb274772235b05.zip |
Removed all the scripts in script/* and replaced it with one generic portal: script/run
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2411 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'railties/lib')
-rw-r--r-- | railties/lib/commands.rb | 5 | ||||
-rw-r--r-- | railties/lib/commands/breakpointer | 3 | ||||
-rw-r--r-- | railties/lib/commands/console.rb | 22 | ||||
-rw-r--r-- | railties/lib/commands/destroy | 7 | ||||
-rwxr-xr-x | railties/lib/commands/generate | 7 | ||||
-rw-r--r-- | railties/lib/commands/ncgi/listener | 86 | ||||
-rw-r--r-- | railties/lib/commands/ncgi/tracker | 69 | ||||
-rw-r--r-- | railties/lib/commands/perfom/benchmarker | 19 | ||||
-rw-r--r-- | railties/lib/commands/perfom/profiler | 35 | ||||
-rw-r--r-- | railties/lib/commands/perform.rb | 5 | ||||
-rw-r--r-- | railties/lib/commands/process.rb | 5 | ||||
-rw-r--r-- | railties/lib/commands/runner.rb | 26 | ||||
-rw-r--r-- | railties/lib/commands/server.rb | 57 | ||||
-rw-r--r-- | railties/lib/commands/update | 5 | ||||
-rw-r--r-- | railties/lib/rails_generator/generators/applications/app/app_generator.rb | 9 |
15 files changed, 352 insertions, 8 deletions
diff --git a/railties/lib/commands.rb b/railties/lib/commands.rb new file mode 100644 index 0000000000..2ca8741e4c --- /dev/null +++ b/railties/lib/commands.rb @@ -0,0 +1,5 @@ +if %w( console perform process runner server ).include?(ARGV.first) + require "#{File.dirname(__FILE__)}/process/#{ARGV.shift}" +else + puts "Choose: console perform process runner server" +end
\ No newline at end of file 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) diff --git a/railties/lib/rails_generator/generators/applications/app/app_generator.rb b/railties/lib/rails_generator/generators/applications/app/app_generator.rb index aef333c13a..1d5a864de4 100644 --- a/railties/lib/rails_generator/generators/applications/app/app_generator.rb +++ b/railties/lib/rails_generator/generators/applications/app/app_generator.rb @@ -44,14 +44,7 @@ class AppGenerator < Rails::Generator::Base m.file "environments/test.rb", "config/environments/test.rb" # Scripts - %w(console destroy generate server runner benchmarker profiler process/reaper process/spinner process/spawner ).each do |file| - m.file "bin/#{file}", "script/#{file}", script_options - end - if options[:gem] - m.file "bin/breakpointer_for_gem", "script/breakpointer", script_options - else - m.file "bin/breakpointer", "script/breakpointer", script_options - end + m.file "bin/run", "script/run", script_options # Dispatches m.file "dispatches/dispatch.rb", "public/dispatch.rb", script_options |