Skip to content
This repository
Browse code

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...
commit d236827881d119fb9ad25c81ce8e7756f1966823 1 parent 144f41e
David Heinemeier Hansson authored
6  railties/CHANGELOG
... ...
@@ -1,5 +1,11 @@
1 1
 *Edge*
2 2
 
  3
+* 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:
  4
+
  5
+  ./script/generate scaffold post title:string  can now be called as  rails g scaffold post title:string
  6
+  
  7
+  Run rails --help inside an app for more help.
  8
+
3 9
 * Removed config/initializers/new_rails_defaults.rb as all frameworks now follow the settings from it [DHH]
4 10
 
5 11
 * Set config.time_zone to UTC by default [DHH]
47  railties/bin/rails
... ...
@@ -1,27 +1,30 @@
1  
-begin
2  
-  require 'rails/ruby_version_check'
3  
-rescue LoadError
4  
-  # If people are not using gems, the load path must still
5  
-  # be correct.
6  
-  # TODO: Remove the begin / rescue block somehow
7  
-  $:.unshift File.expand_path('../../lib', __FILE__)
8  
-  $:.unshift File.expand_path('../../../activesupport/lib', __FILE__)
9  
-  $:.unshift File.expand_path('../../../actionpack/lib', __FILE__)
10  
-  require 'rails/ruby_version_check'
11  
-end
  1
+if File.exists?(Dir.getwd + '/script/rails')
  2
+  exec(Dir.getwd + '/script/rails', *ARGV)
  3
+else
  4
+  begin
  5
+    require 'rails/ruby_version_check'
  6
+  rescue LoadError
  7
+    # If people are not using gems, the load path must still
  8
+    # be correct.
  9
+    # TODO: Remove the begin / rescue block somehow
  10
+    $:.unshift File.expand_path('../../lib', __FILE__)
  11
+    $:.unshift File.expand_path('../../../activesupport/lib', __FILE__)
  12
+    $:.unshift File.expand_path('../../../actionpack/lib', __FILE__)
  13
+    require 'rails/ruby_version_check'
  14
+  end
12 15
 
13  
-Signal.trap("INT") { puts; exit }
  16
+  Signal.trap("INT") { puts; exit }
14 17
 
15  
-require 'rails/version'
16  
-if %w(--version -v).include? ARGV.first
17  
-  puts "Rails #{Rails::VERSION::STRING}"
18  
-  exit(0)
19  
-end
  18
+  require 'rails/version'
  19
+  if %w(--version -v).include? ARGV.first
  20
+    puts "Rails #{Rails::VERSION::STRING}"
  21
+    exit(0)
  22
+  end
20 23
 
21  
-ARGV << "--help" if ARGV.empty?
  24
+  ARGV << "--help" if ARGV.empty?
22 25
 
  26
+  require 'rails/generators'
  27
+  require 'generators/rails/app/app_generator'
23 28
 
24  
-require 'rails/generators'
25  
-require 'generators/rails/app/app_generator'
26  
-
27  
-Rails::Generators::AppGenerator.start
  29
+  Rails::Generators::AppGenerator.start
  30
+end
3  railties/lib/generators/rails/app/templates/script/about
... ...
@@ -1,3 +0,0 @@
1  
-require File.expand_path('../../config/environment',  __FILE__)
2  
-$LOAD_PATH.unshift "#{RAILTIES_PATH}/builtin/rails_info"
3  
-require 'rails/commands/about'
5  railties/lib/generators/rails/app/templates/script/console.tt
... ...
@@ -1,5 +0,0 @@
1  
-require File.expand_path('../../config/boot',  __FILE__)
2  
-require 'rails/commands/console'
3  
-require File.expand_path('../../config/application',  __FILE__)
4  
-
5  
-Rails::Console.start(Rails::Application)
5  railties/lib/generators/rails/app/templates/script/dbconsole.tt
... ...
@@ -1,5 +0,0 @@
1  
-require File.expand_path('../../config/boot',  __FILE__)
2  
-require 'rails/commands/dbconsole'
3  
-require File.expand_path('../../config/application',  __FILE__)
4  
-
5  
-Rails::DBConsole.start(Rails::Application)
2  railties/lib/generators/rails/app/templates/script/destroy
... ...
@@ -1,2 +0,0 @@
1  
-require File.expand_path('../../config/environment',  __FILE__)
2  
-require 'rails/commands/destroy'
2  railties/lib/generators/rails/app/templates/script/generate
... ...
@@ -1,2 +0,0 @@
1  
-require File.expand_path('../../config/environment',  __FILE__)
2  
-require 'rails/commands/generate'
2  railties/lib/generators/rails/app/templates/script/performance/benchmarker
... ...
@@ -1,2 +0,0 @@
1  
-require File.expand_path('../../../config/environment',  __FILE__)
2  
-require 'rails/commands/performance/benchmarker'
2  railties/lib/generators/rails/app/templates/script/performance/profiler
... ...
@@ -1,2 +0,0 @@
1  
-require File.expand_path('../../../config/environment',  __FILE__)
2  
-require 'rails/commands/performance/profiler'
2  railties/lib/generators/rails/app/templates/script/plugin
... ...
@@ -1,2 +0,0 @@
1  
-require File.expand_path('../../config/application',  __FILE__)
2  
-require 'rails/commands/plugin'
6  railties/lib/generators/rails/app/templates/script/rails
... ...
@@ -0,0 +1,6 @@
  1
+ENV_PATH  = File.expand_path('../../config/environment',  __FILE__)
  2
+BOOT_PATH = File.expand_path('../../config/boot',  __FILE__)
  3
+APP_PATH  = File.expand_path('../../config/application',  __FILE__)
  4
+
  5
+require BOOT_PATH
  6
+require 'rails/commands/rails'
3  railties/lib/generators/rails/app/templates/script/runner
... ...
@@ -1,3 +0,0 @@
1  
-require File.expand_path('../../config/boot',  __FILE__)
2  
-require 'rails/commands/runner'
3  
-require File.expand_path('../../config/environment',  __FILE__)
5  railties/lib/generators/rails/app/templates/script/server.tt
... ...
@@ -1,5 +0,0 @@
1  
-require File.expand_path('../../config/boot',  __FILE__)
2  
-require 'rails/commands/server'
3  
-
4  
-Dir.chdir(File.expand_path('../..',  __FILE__))
5  
-Rails::Server.start
12  railties/lib/rails/commands/application.rb
... ...
@@ -0,0 +1,12 @@
  1
+require 'rails/version'
  2
+if %w(--version -v).include? ARGV.first
  3
+  puts "Rails #{Rails::VERSION::STRING}"
  4
+  exit(0)
  5
+end
  6
+
  7
+ARGV << "--help" if ARGV.empty?
  8
+
  9
+require 'rails/generators'
  10
+require 'generators/rails/app/app_generator'
  11
+
  12
+Rails::Generators::AppGenerator.start
69  railties/lib/rails/commands/rails.rb
... ...
@@ -0,0 +1,69 @@
  1
+if ARGV.empty?
  2
+  ARGV << '--help'
  3
+end
  4
+
  5
+HELP_TEXT = <<-EOT
  6
+usage: rails COMMAND [ARGS]
  7
+
  8
+The most common rails commands are:
  9
+ generate  Generate new code (short-cut alias: "g")
  10
+ console   Start the Rails console (short-cut alias: "c")
  11
+ server    Start the Rails server (short-cut alias: "s")
  12
+
  13
+In addition to those, there are:
  14
+ application  Generate the Rails application code
  15
+ dbconsole    Start a console for the database specified in config/database.yml
  16
+ destroy      Undo code generated with "generate"
  17
+ benchmarker  See how fast a piece of code runs
  18
+ profiler     Get profile information from a piece of code
  19
+ plugin       Install a plugin
  20
+ runner       Run a piece of code in the application environment
  21
+
  22
+All commands can be run with -h for more information.
  23
+EOT
  24
+
  25
+
  26
+case ARGV.shift
  27
+when 'g', 'generate'
  28
+  require ENV_PATH
  29
+  require 'rails/commands/generate'
  30
+when 'c', 'console'
  31
+  require BOOT_PATH
  32
+  require 'rails/commands/console'
  33
+  require APP_PATH
  34
+  Rails::Console.start(Rails::Application)
  35
+when 's', 'server'
  36
+  require File.expand_path('../../config/boot',  __FILE__)
  37
+  require 'rails/commands/server'
  38
+  Dir.chdir(File.expand_path('../..',  __FILE__))
  39
+  Rails::Server.start
  40
+  
  41
+
  42
+when 'dbconsole'
  43
+  require BOOT_PATH
  44
+  require 'rails/commands/dbconsole'
  45
+  require APP_PATH
  46
+  Rails::DBConsole.start(Rails::Application)
  47
+when 'destroy'
  48
+  require ENV_PATH
  49
+  require 'rails/commands/destroy'
  50
+when 'benchmarker'
  51
+  require ENV_PATH
  52
+  require 'rails/commands/performance/benchmarker'
  53
+when 'profiler'
  54
+  require ENV_PATH
  55
+  require 'rails/commands/performance/profiler'
  56
+when 'plugin'
  57
+  require APP_PATH
  58
+  require 'rails/commands/plugin'
  59
+when 'runner'
  60
+  require BOOT_PATH
  61
+  require 'rails/commands/runner'
  62
+  require ENV_PATH
  63
+
  64
+when '--help', '-h'
  65
+  puts HELP_TEXT
  66
+else
  67
+  puts "Error: Command not recognized"
  68
+  puts HELP_TEXT
  69
+end

27 notes on commit d236827

Marcelo Silveira

Awesome!

Yuri Tomanek

Very nice.

Travis Dunn

wow, this is cool.

Carlos Antonio da Silva

No aliases anymore =).

floering

sweetness. no more fat fingering the slashes.

Antonio Rosado

This is so nice.

Alexander Uvarov

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

Alexey Trofimenko

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

Ruby on Rails
Owner

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.

Pete Nicholls

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

+1 Nice commit.

James Miller

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')'

James Miller

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

Alexey Trofimenko

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

+1

changwang

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

Mikel Lindsaar
Collaborator

@changwang thanks, will be fixed shortly

Johannes Jörg Schmidt

+1 for what I always have been waited for

Damien Mathieu
Collaborator

Yay ! :)

Matthew Riley MacPherson

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.

Jônatas Davi Paganini

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

  • alias db='script/dbconsole '
  • alias db='rails dbconsole'
Ruby on Rails
Owner

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

Jean-Denis Vauguet

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: #' 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.

Luigi Maselli

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)

Luigi Maselli

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'

Alexey Trofimenko

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.

Grant Hutchins

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

Please sign in to comment.
Something went wrong with that request. Please try again.