Permalink
Browse files

Removed all the default commands in script/* and replaced them with s…

…cript/rails and a rails command that'll act the same when run from within the app [DHH]
  • Loading branch information...
1 parent 144f41e commit d236827881d119fb9ad25c81ce8e7756f1966823 @dhh dhh committed Feb 2, 2010
View
@@ -1,5 +1,11 @@
*Edge*
+* Removed all the default commands in script/* and replaced them with script/rails and a rails command that'll act the same when run from within the app [DHH]. Example:
+
+ ./script/generate scaffold post title:string can now be called as rails g scaffold post title:string
+
+ Run rails --help inside an app for more help.
+
* Removed config/initializers/new_rails_defaults.rb as all frameworks now follow the settings from it [DHH]
* Set config.time_zone to UTC by default [DHH]
View
@@ -1,27 +1,30 @@
-begin
- require 'rails/ruby_version_check'
-rescue LoadError
- # If people are not using gems, the load path must still
- # be correct.
- # TODO: Remove the begin / rescue block somehow
- $:.unshift File.expand_path('../../lib', __FILE__)
- $:.unshift File.expand_path('../../../activesupport/lib', __FILE__)
- $:.unshift File.expand_path('../../../actionpack/lib', __FILE__)
- require 'rails/ruby_version_check'
-end
+if File.exists?(Dir.getwd + '/script/rails')
+ exec(Dir.getwd + '/script/rails', *ARGV)
+else
+ begin
+ require 'rails/ruby_version_check'
+ rescue LoadError
+ # If people are not using gems, the load path must still
+ # be correct.
+ # TODO: Remove the begin / rescue block somehow
+ $:.unshift File.expand_path('../../lib', __FILE__)
+ $:.unshift File.expand_path('../../../activesupport/lib', __FILE__)
+ $:.unshift File.expand_path('../../../actionpack/lib', __FILE__)
+ require 'rails/ruby_version_check'
+ end
-Signal.trap("INT") { puts; exit }
+ Signal.trap("INT") { puts; exit }
-require 'rails/version'
-if %w(--version -v).include? ARGV.first
- puts "Rails #{Rails::VERSION::STRING}"
- exit(0)
-end
+ require 'rails/version'
+ if %w(--version -v).include? ARGV.first
+ puts "Rails #{Rails::VERSION::STRING}"
+ exit(0)
+ end
-ARGV << "--help" if ARGV.empty?
+ ARGV << "--help" if ARGV.empty?
+ require 'rails/generators'
+ require 'generators/rails/app/app_generator'
-require 'rails/generators'
-require 'generators/rails/app/app_generator'
-
-Rails::Generators::AppGenerator.start
+ Rails::Generators::AppGenerator.start
+end
@@ -1,3 +0,0 @@
-require File.expand_path('../../config/environment', __FILE__)
-$LOAD_PATH.unshift "#{RAILTIES_PATH}/builtin/rails_info"
-require 'rails/commands/about'
@@ -1,5 +0,0 @@
-require File.expand_path('../../config/boot', __FILE__)
-require 'rails/commands/console'
-require File.expand_path('../../config/application', __FILE__)
-
-Rails::Console.start(Rails::Application)
@@ -1,5 +0,0 @@
-require File.expand_path('../../config/boot', __FILE__)
-require 'rails/commands/dbconsole'
-require File.expand_path('../../config/application', __FILE__)
-
-Rails::DBConsole.start(Rails::Application)
@@ -1,2 +0,0 @@
-require File.expand_path('../../config/environment', __FILE__)
-require 'rails/commands/destroy'
@@ -1,2 +0,0 @@
-require File.expand_path('../../config/environment', __FILE__)
-require 'rails/commands/generate'
@@ -1,2 +0,0 @@
-require File.expand_path('../../../config/environment', __FILE__)
-require 'rails/commands/performance/benchmarker'
@@ -1,2 +0,0 @@
-require File.expand_path('../../../config/environment', __FILE__)
-require 'rails/commands/performance/profiler'
@@ -1,2 +0,0 @@
-require File.expand_path('../../config/application', __FILE__)
-require 'rails/commands/plugin'
@@ -0,0 +1,6 @@
+ENV_PATH = File.expand_path('../../config/environment', __FILE__)
+BOOT_PATH = File.expand_path('../../config/boot', __FILE__)
+APP_PATH = File.expand_path('../../config/application', __FILE__)
+
+require BOOT_PATH
+require 'rails/commands/rails'
@@ -1,3 +0,0 @@
-require File.expand_path('../../config/boot', __FILE__)
-require 'rails/commands/runner'
-require File.expand_path('../../config/environment', __FILE__)
@@ -1,5 +0,0 @@
-require File.expand_path('../../config/boot', __FILE__)
-require 'rails/commands/server'
-
-Dir.chdir(File.expand_path('../..', __FILE__))
-Rails::Server.start
@@ -0,0 +1,12 @@
+require 'rails/version'
+if %w(--version -v).include? ARGV.first
+ puts "Rails #{Rails::VERSION::STRING}"
+ exit(0)
+end
+
+ARGV << "--help" if ARGV.empty?
+
+require 'rails/generators'
+require 'generators/rails/app/app_generator'
+
+Rails::Generators::AppGenerator.start
@@ -0,0 +1,69 @@
+if ARGV.empty?
+ ARGV << '--help'
+end
+
+HELP_TEXT = <<-EOT
+usage: rails COMMAND [ARGS]
+
+The most common rails commands are:
+ generate Generate new code (short-cut alias: "g")
+ console Start the Rails console (short-cut alias: "c")
+ server Start the Rails server (short-cut alias: "s")
+
+In addition to those, there are:
+ application Generate the Rails application code
+ dbconsole Start a console for the database specified in config/database.yml
+ destroy Undo code generated with "generate"
+ benchmarker See how fast a piece of code runs
+ profiler Get profile information from a piece of code
+ plugin Install a plugin
+ runner Run a piece of code in the application environment
+
+All commands can be run with -h for more information.
+EOT
+
+
+case ARGV.shift
+when 'g', 'generate'
+ require ENV_PATH
+ require 'rails/commands/generate'
+when 'c', 'console'
+ require BOOT_PATH
+ require 'rails/commands/console'
+ require APP_PATH
+ Rails::Console.start(Rails::Application)
+when 's', 'server'
+ require File.expand_path('../../config/boot', __FILE__)
+ require 'rails/commands/server'
+ Dir.chdir(File.expand_path('../..', __FILE__))
+ Rails::Server.start
+
+
+when 'dbconsole'
+ require BOOT_PATH
+ require 'rails/commands/dbconsole'
+ require APP_PATH
+ Rails::DBConsole.start(Rails::Application)
+when 'destroy'
+ require ENV_PATH
+ require 'rails/commands/destroy'
+when 'benchmarker'
+ require ENV_PATH
+ require 'rails/commands/performance/benchmarker'
+when 'profiler'
+ require ENV_PATH
+ require 'rails/commands/performance/profiler'
+when 'plugin'
+ require APP_PATH
+ require 'rails/commands/plugin'
+when 'runner'
+ require BOOT_PATH
+ require 'rails/commands/runner'
+ require ENV_PATH
+
+when '--help', '-h'
+ puts HELP_TEXT
+else
+ puts "Error: Command not recognized"
+ puts HELP_TEXT
+end

27 comments on commit d236827

@mhfs
Contributor
mhfs commented on d236827 Feb 3, 2010

Awesome!

@yuritomanek

Very nice.

@blahed
blahed commented on d236827 Feb 3, 2010

wow, this is cool.

@carlosantoniodasilva

No aliases anymore =).

@floering
Contributor

sweetness. no more fat fingering the slashes.

@antoniorosado

This is so nice.

@wildchild
Contributor

This is cool, but merb's way (merb, merb-gen) is better imho.

@codesnik
Contributor

you type "rails generate",
then shell finds "rails" command somewhere in "usr/local/bin"
then it runs it just to ask ruby to search actual rails executable buried somewhere in your gems
just to find real rails in your script/ directory
and execute it, and once again

nice, two unnecessary steps
gonna buy faster laptop
or write witty alias for rails

P.S. zsh allows me to type
s/g TAB to complete script/generate, so no relaxation for fingers either

@rails
rails commented on d236827 Feb 3, 2010

codesnik, I recommend you do not look at how Ruby code is turned into Assembler. The inefficiencies that the Ruby community at large go through to present a nicer human environment is probably disgusting to most if you look under the covers. Don't look inside inside the chocolate factory!

Also, you can do "rails g" for generate. All the major commands have shortcuts. See "rails --help" for an index of them all.

@Aupajo
Aupajo commented on d236827 Feb 3, 2010

Wait, there's a chocolate factory? Sounds like _why's secret hiding place.

+1 Nice commit.

@bensie
Contributor
bensie commented on d236827 Feb 3, 2010

When trying to create a new app -

rails/railties/bin/rails: line 1: syntax error near unexpected token Dir.getwd' rails/railties/bin/rails: line 1:if File.exists?(Dir.getwd + '/script/rails')'

@bensie
Contributor
bensie commented on d236827 Feb 3, 2010

Really wishing you could delete comments right about now...

@codesnik
Contributor

ok, i'm a little grouchy here. still, i'd like to hear what jruby guys feel about extra "exec".
but of course they can use script/rails

@jinzhu
jinzhu commented on d236827 Feb 3, 2010

+1

@changwang

I think you guys should modify the default index page though, there is no script/generate any more. :)

@mikel
Member
mikel commented on d236827 Feb 3, 2010

@changwang thanks, will be fixed shortly

@jo
jo commented on d236827 Feb 3, 2010

+1 for what I always have been waited for

@dmathieu
Contributor

Yay ! :)

@tofumatt

This is a good idea; script/* was always something that felt clunky. I'm alright with the extra "exec" ;-)

@alan-andrade

Thanks for this fix. Its definitely better than the actual way.

@jonatas
jonatas commented on d236827 Feb 3, 2010

actually, i'm using 'db' shortcut for script/dbconsole :D

  • alias db='script/dbconsole '
  • alias db='rails dbconsole'
@rails
rails commented on d236827 Feb 3, 2010

See http://github.com/rails/rails/commit/f390eade5fd70615ba2ee8e089821bcf2d5f7b17 -- we added rails db as a shortcut for dbconsole.

@chikamichi

I'm a little bit confused by the new way to go. I created a brand new app with ruby rails my_app --dev, rails being an alias to my railties/bin/rails. Went fine, bundle's ok. But now I want to run the server, so I guess I must do ruby rails s, but I only get a load of code being prompted. Ctrl-Cing and checking for the error backtrace, I read:

WARNING: Invalid .gemspec format in '/home/jd/.gem/ruby/1.9.1/specifications/mail-2.1.2.gemspec'
WARNING: #<NoMethodError: undefined method `>' for nil:NilClass>

-*- encoding: u^C/opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/user_interaction.rb:240:in `write': Interrupt

from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/user_interaction.rb:240:in `puts'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/user_interaction.rb:240:in `alert_warning'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/user_interaction.rb:125:in `alert_warning'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/source_index.rb:108:in `rescue in load_specification'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/source_index.rb:94:in `load_specification'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/source_index.rb:153:in `block (2 levels) in load_gems_in'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/source_index.rb:152:in `each'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/source_index.rb:152:in `block in load_gems_in'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/source_index.rb:149:in `reverse_each'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/source_index.rb:149:in `load_gems_in'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/source_index.rb:345:in `refresh!'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/source_index.rb:78:in `from_gems_in'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/source_index.rb:58:in `from_installed_gems'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems.rb:944:in `source_index'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/gem_path_searcher.rb:84:in `init_gemspecs'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/gem_path_searcher.rb:19:in `initialize'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems.rb:889:in `new'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems.rb:889:in `block in searcher'
from <internal:prelude>:8:in `synchronize'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems.rb:888:in `searcher'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems.rb:528:in `find_files'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems.rb:1132:in `<top (required)>'
from <internal:gem_prelude>:235:in `require'
from <internal:gem_prelude>:235:in `load_full_rubygems_library'
from <internal:gem_prelude>:334:in `const_missing'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/specification.rb:1038:in `<class:Specification>'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/rubygems-update-1.3.5/lib/rubygems/specification.rb:28:in `<top (required)>'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/bundler-0.9.0.pre4/lib/bundler/rubygems.rb:2:in `require'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/bundler-0.9.0.pre4/lib/bundler/rubygems.rb:2:in `<top (required)>'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/bundler-0.9.0.pre4/lib/bundler.rb:4:in `require'
from /opt/ruby1.9/lib/ruby1.9/gems/1.9.1/gems/bundler-0.9.0.pre4/lib/bundler.rb:4:in `<top (required)>'
from /var/www/oggo/config/boot.rb:6:in `require'
from /var/www/oggo/config/boot.rb:6:in `rescue in <top (required)>'
from /var/www/oggo/config/boot.rb:2:in `<top (required)>'
from /var/www/oggo/script/rails:9:in `require'
from /var/www/oggo/script/rails:9:in `<main>'

What did I do wrong? Brand new install of Ruby1.9 from source, Rails edge fetched from git following RailsBlog wycats instructions.
Thx.

@grigio
grigio commented on d236827 Feb 4, 2010

I get this:
WARNING: Invalid .gemspec format in '/var/lib/gems/1.9.0/specifications/memcache-client-1.7.7.gemspec'
/home/grigio/Documenti/rails/railties/lib/rails.rb:25:in <top (required)>': undefined methoddefault_external=' for Encoding:Class (NoMethodError)

@grigio
grigio commented on d236827 Feb 5, 2010

OMG! internal:gem_prelude:114:in push_gem_version_on_load_path': undefined method<=>' for nil:NilClass (NoMethodError)
from internal:gem_prelude:8:in `gem'

@codesnik
Contributor

meanwhile, for zsh (bash too?) users out there

http://gist.github.com/298942

so it would work even while rails3 is in beta. and maybe afterwards.

@nertzy
Contributor
nertzy commented on d236827 Feb 16, 2010

codesnik, why not just run script/rails directly if you are worried about the additional exec?

Please sign in to comment.