Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First commit

  • Loading branch information...
commit 758661319da8af43ffcf145dfe0e7b6c3096ae37 0 parents
@dhh dhh authored
5 Gemfile
@@ -0,0 +1,5 @@
+source "http://rubygems.org"
+
+gemspec
+
+gem "rails"
50 Gemfile.lock
@@ -0,0 +1,50 @@
+PATH
+ remote: .
+ specs:
+ jbuilder (0.9.1)
+ activesupport (>= 3.0.0)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ actionpack (3.2.8)
+ activemodel (= 3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ erubis (~> 2.7.0)
+ journey (~> 1.0.4)
+ rack (~> 1.4.0)
+ rack-cache (~> 1.2)
+ rack-test (~> 0.6.1)
+ sprockets (~> 2.1.3)
+ activemodel (3.2.8)
+ activesupport (= 3.2.8)
+ builder (~> 3.0.0)
+ activesupport (3.2.8)
+ i18n (~> 0.6)
+ multi_json (~> 1.0)
+ builder (3.0.0)
+ erubis (2.7.0)
+ hike (1.2.1)
+ i18n (0.6.1)
+ journey (1.0.4)
+ multi_json (1.3.6)
+ rack (1.4.1)
+ rack-cache (1.2)
+ rack (>= 0.4)
+ rack-test (0.6.1)
+ rack (>= 1.0)
+ rake (0.9.2.2)
+ sprockets (2.1.3)
+ hike (~> 1.2)
+ rack (~> 1.0)
+ tilt (~> 1.1, != 1.3.0)
+ tilt (1.3.3)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ actionpack
+ jbuilder!
+ rake
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2012 David Heinemeier Hansson, 37signals
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 README.md
@@ -0,0 +1,21 @@
+Commands
+========
+
+Let you run Rake and Rails commands during a console session. This side-steps the need to load the entire environment over and over again when you run these commands from the shell. This constant reloading of the environment is what causes slow boot time on big applications. Think of this like a baby Zeus or the Turbolinks of commands.
+
+
+Usage
+-----
+
+When your console boots, it'll automatically have a commands object instantiated (aliased to c). It can be used like this:
+
+ > c.generate "scaffold post title:string"
+ > c.rake "db:migrate"
+ > c.test "models/person"
+
+
+Work needed
+-----------
+
+# The test runner needs to run in the same process, not use Rake (as it shells out). So the test class needs to be instantiated directly and run under the test environment.
+# Generating models seem to be bust for some reason.
10 Rakefile
@@ -0,0 +1,10 @@
+require 'bundler'
+require 'rake/testtask'
+
+Bundler.require
+
+Rake::TestTask.new do |test|
+ test.test_files = FileList["test/*_test.rb"]
+end
+
+task :default => :test
11 commands.gemspec
@@ -0,0 +1,11 @@
+Gem::Specification.new do |s|
+ s.name = 'commands'
+ s.version = '0.0.1'
+ s.author = 'David Heinemeier Hansson'
+ s.email = 'david@37signals.com'
+ s.summary = 'Run Rake/Rails commands through the console'
+
+ s.add_dependency 'rails', '>= 3.0.0'
+
+ s.files = Dir["#{File.dirname(__FILE__)}/**/*"]
+end
86 lib/commands.rb
@@ -0,0 +1,86 @@
+require 'rake'
+
+class Commands
+ module ConsoleMethods
+ def commands
+ @commands ||= Commands.new
+ end
+
+ alias_method :c, :commands
+ end
+
+ include Rake::DSL
+
+ def initialize
+ load Rails.root.join('Rakefile')
+ end
+
+ def rake(task, *args)
+ ActiveRecord::Base.logger.silence do
+ Rake::Task[task].invoke(*args)
+
+ # Rake by default only allows tasks to be run once per session
+ Rake.application.tasks.each(&:reenable)
+ end
+ nil
+ end
+
+ # FIXME: Turn this into calls directly to the test classes, so we don't have to load environment again.
+ # Also need to toggle the environment to test and back to dev after running.
+ def test(what = nil)
+ ActiveRecord::Base.logger.silence do
+ case what
+ when NilClass, :all
+ rake "test:units"
+ when String
+ ENV['TEST'] = "test/#{what}_test.rb"
+ rake "test:single"
+ ENV['TEST'] = nil
+ end
+ end
+ nil
+ rescue SystemExit
+ nil
+ end
+
+ def generate(argv = nil)
+ generator :generate, argv
+ end
+
+ def update(argv = nil)
+ generator :update, argv
+ end
+
+ def destroy(argv = nil)
+ generator :destroy, argv
+ end
+
+
+ private
+ def generator(name, argv = nil)
+ if argv.nil?
+ # FIXME: I don't know why we can't just catch SystemExit here, then we wouldn't need this if block
+ require 'rails/generators'
+ Rails::Generators.help name
+ else
+ ARGV.replace argv.nil? ? [nil] : argv.split(" ")
+ load "rails/commands/#{name}.rb"
+ ARGV.replace [nil]
+ end
+
+ nil
+ end
+end
+
+require 'rails/console/app'
+Rails::ConsoleMethods.send :include, Commands::ConsoleMethods
+
+# class Rails::Console
+# def start_with_commands
+# console::ExtendCommandBundle.send :include, Commands::ConsoleMethods
+# start_without_commands
+# end
+#
+# alias_method_chain :start, :commands
+# end
+#
7 test/commands_test.rb
@@ -0,0 +1,7 @@
+require 'test/unit'
+require 'active_support/test_case'
+
+require 'commands'
+
+class CommandsTest < ActiveSupport::TestCase
+end
Please sign in to comment.
Something went wrong with that request. Please try again.