commands 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Commands
2
2
  ========
3
3
 
4
- 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.
4
+ 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.
5
5
 
6
6
 
7
7
  Installation
@@ -18,15 +18,8 @@ And then execute:
18
18
  Usage
19
19
  -----
20
20
 
21
- When your console boots, it'll automatically have a `commands` object instantiated (aliased to `c`). The following methods are delegated to this object: rake, test, generate, destroy, update. It's used like this:
21
+ When your console boots, it'll automatically have a `commander` object instantiated. The following methods are delegated to this object: rake, test, generate, destroy, update. It's used like this:
22
22
 
23
23
  > generate "scaffold post title:string"
24
24
  > rake "db:migrate"
25
25
  > test "models/person"
26
-
27
-
28
- Work needed
29
- -----------
30
-
31
- 1. 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.
32
- 1. Generating models seem to be bust for some reason.
Binary file
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'commands'
3
- s.version = '0.0.4'
3
+ s.version = '0.1.0'
4
4
  s.author = 'David Heinemeier Hansson'
5
5
  s.email = 'david@37signals.com'
6
6
  s.summary = 'Run Rake/Rails commands through the console'
@@ -1,133 +1,4 @@
1
- require 'rake'
2
- require 'rails/generators'
3
-
4
- class Commands
5
- module ConsoleMethods
6
- def commands
7
- @commands ||= Commands.new
8
- end
9
-
10
- delegate :rake, :test, :generate, :destroy, :update, to: :commands
11
- end
12
-
13
- include Rake::DSL
14
-
15
- def initialize
16
- load_rake_tasks
17
- load_rails_generators
18
- end
19
-
20
- def rake(task = nil, *args)
21
- task.nil? ? print_rake_tasks : invoke_rake_task(task, *args)
22
- "Completed"
23
- rescue SystemExit, RuntimeError
24
- "Failed"
25
- end
26
-
27
- # FIXME: Turn this into calls directly to the test classes, so we don't have to load environment again.
28
- # Also need to toggle the environment to test and back to dev after running.
29
- def test(what = nil)
30
- forking do
31
- case what
32
- when NilClass
33
- print_test_usage
34
- when "all"
35
- rake "test"
36
- when /^[^\/]+$/ # models
37
- rake "test:#{what}"
38
- when /[\/]+/ # models/person
39
- ENV['TEST'] = "test/#{what}_test.rb"
40
- rake "test:single"
41
- ENV['TEST'] = nil
42
- end
43
- end
44
-
45
- "Completed"
46
- end
47
-
48
- def generate(argv = nil)
49
- generator :generate, argv
50
- end
51
-
52
- def update(argv = nil)
53
- generator :update, argv
54
- end
55
-
56
- def destroy(argv = nil)
57
- generator :destroy, argv
58
- end
59
-
60
-
61
- private
62
- def load_rake_tasks
63
- Rake::TaskManager.record_task_metadata = true # needed to capture comments from define_task
64
- load Rails.root.join('Rakefile')
65
- end
66
-
67
- def load_rails_generators
68
- Rails.application.load_generators
69
- end
70
-
71
-
72
- def print_rake_tasks
73
- Rake.application.options.show_tasks = :tasks
74
- Rake.application.options.show_task_pattern = Regexp.new('')
75
- Rake.application.display_tasks_and_comments
76
- end
77
-
78
- def invoke_rake_task(task, *args)
79
- silence_active_record_logger { Rake::Task[task].invoke(*args) }
80
- Rake.application.tasks.each(&:reenable) # Rake by default only allows tasks to be run once per session
81
- end
82
-
83
-
84
- def print_test_usage
85
- puts <<-EOT
86
- Usage:
87
- test "WHAT"
88
-
89
- Description:
90
- Runs either a full set of test suites or single suite.
91
-
92
- If you supply WHAT with either models, controllers, helpers, integration, or performance,
93
- those whole sets will be run.
94
-
95
- If you supply WHAT with models/person, just test/models/person_test.rb will be run.
96
- EOT
97
- end
98
-
99
- def forking
100
- pid = Kernel.fork do
101
- yield
102
- Kernel.exit
103
- end
104
- Process.wait pid
105
- end
106
-
107
-
108
- def generator(name, argv = nil)
109
- if argv.nil?
110
- # FIXME: I don't know why we can't just catch SystemExit here, then we wouldn't need this if block
111
- Rails::Generators.help name
112
- else
113
- ARGV.replace argv.nil? ? [nil] : argv.split(" ")
114
- load "rails/commands/#{name}.rb"
115
- ARGV.replace [nil]
116
- end
117
-
118
- "Completed"
119
- end
120
-
121
- # Only just ensured that this method is available in rails/master, so need a guard for a bit.
122
- def silence_active_record_logger
123
- begin
124
- old_logger_level, ActiveRecord::Base.logger.level = ActiveRecord::Base.logger.level, Logger::ERROR
125
- yield
126
- ensure
127
- ActiveRecord::Base.logger.level = old_logger_level
128
- end
129
- end
130
- end
131
-
132
1
  require 'rails/console/app'
133
- Rails::ConsoleMethods.send :include, Commands::ConsoleMethods
2
+ require 'commands/console_delegation'
3
+
4
+ Rails::ConsoleMethods.send :include, Commands::ConsoleDelegation
@@ -0,0 +1,32 @@
1
+ require 'commands/raker'
2
+ require 'commands/tester'
3
+ require 'commands/generator'
4
+
5
+ module Commands
6
+ class Commander
7
+ delegate :rake, to: :raker
8
+ delegate :test, to: :tester
9
+ delegate :generate, :destroy, :update, to: :generator
10
+
11
+ # Only just ensured that this method is available in rails/master, so need a guard for a bit.
12
+ def self.silence_active_record_logger
13
+ return yield unless defined? ActiveRecord::Base.logger
14
+
15
+ begin
16
+ old_logger_level, ActiveRecord::Base.logger.level = ActiveRecord::Base.logger.level, Logger::ERROR
17
+ yield
18
+ ensure
19
+ ActiveRecord::Base.logger.level = old_logger_level
20
+ end
21
+ end
22
+
23
+ attr_reader :raker, :tester, :generator
24
+
25
+ def initialize
26
+ @raker = Raker.new
27
+ @tester = Tester.new
28
+ @generator = Generator.new
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,11 @@
1
+ require 'commands/commander'
2
+
3
+ module Commands
4
+ module ConsoleDelegation
5
+ def commander
6
+ @commander ||= Commander.new
7
+ end
8
+
9
+ delegate :rake, :test, :generate, :destroy, :update, to: :commander
10
+ end
11
+ end
@@ -0,0 +1,40 @@
1
+ require 'rails/generators'
2
+
3
+ module Commands
4
+ class Generator
5
+ def initialize
6
+ load_rails_generators
7
+ end
8
+
9
+ def generate(argv = nil)
10
+ generator :generate, argv
11
+ end
12
+
13
+ def update(argv = nil)
14
+ generator :update, argv
15
+ end
16
+
17
+ def destroy(argv = nil)
18
+ generator :destroy, argv
19
+ end
20
+
21
+
22
+ private
23
+ def load_rails_generators
24
+ Rails.application.load_generators
25
+ end
26
+
27
+ def generator(name, argv = nil)
28
+ if argv.nil?
29
+ # FIXME: I don't know why we can't just catch SystemExit here, then we wouldn't need this if block
30
+ Rails::Generators.help name
31
+ else
32
+ ARGV.replace argv.nil? ? [nil] : argv.split(" ")
33
+ load "rails/commands/#{name}.rb"
34
+ ARGV.replace [nil]
35
+ end
36
+
37
+ "Completed"
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ require 'rake'
2
+
3
+ module Commands
4
+ class Raker
5
+ include Rake::DSL
6
+
7
+ def initialize
8
+ load_rake_tasks
9
+ end
10
+
11
+ def rake(task = nil, *args)
12
+ task.nil? ? print_rake_tasks : invoke_rake_task(task, *args)
13
+ "Completed"
14
+ rescue SystemExit, RuntimeError
15
+ "Failed"
16
+ end
17
+
18
+
19
+ private
20
+ def load_rake_tasks
21
+ Rake::TaskManager.record_task_metadata = true # needed to capture comments from define_task
22
+ load Rails.root.join('Rakefile')
23
+ end
24
+
25
+ def print_rake_tasks
26
+ Rake.application.options.show_tasks = :tasks
27
+ Rake.application.options.show_task_pattern = Regexp.new('')
28
+ Rake.application.display_tasks_and_comments
29
+ end
30
+
31
+ def invoke_rake_task(task, *args)
32
+ Commander.silence_active_record_logger { Rake::Task[task].invoke(*args) }
33
+ Rake.application.tasks.each(&:reenable) # Rake by default only allows tasks to be run once per session
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,81 @@
1
+ module Commands
2
+ class Tester
3
+ # FIXME: Turn this into calls directly to the test classes, so we don't have to load environment again.
4
+ # Also need to toggle the environment to test and back to dev after running.
5
+ def test(what = nil)
6
+ case what
7
+ when NilClass
8
+ print_test_usage
9
+ when "all"
10
+ # test/**/*_test.rb doesn't work because of performance tests
11
+ run "test/models/**/*_test.rb", "test/controllers/**/*_test.rb", "test/integration/**/*_test.rb"
12
+ when /^[^\/]+$/ # models
13
+ run "test/#{what}/**/*_test.rb"
14
+ when /[\/]+/ # models/person
15
+ run "test/#{what}_test.rb"
16
+ end
17
+
18
+ "Completed"
19
+ end
20
+
21
+
22
+ private
23
+ # Executes the tests matching the passed filename globs
24
+ def run(*test_patterns)
25
+ forking do
26
+ switch_env_to :test
27
+ redirect_active_record_logger
28
+ require_test_files(test_patterns)
29
+ end
30
+ end
31
+
32
+ def switch_env_to(new_env)
33
+ Rails.env = new_env.to_s
34
+ Rails.application
35
+
36
+ $:.unshift("./#{new_env}")
37
+
38
+ reset_active_record
39
+ end
40
+
41
+ def reset_active_record
42
+ if defined? ::ActiveRecord
43
+ ::ActiveRecord::Base.clear_active_connections!
44
+ ::ActiveRecord::Base.establish_connection
45
+ end
46
+ end
47
+
48
+ def require_test_files(test_patterns)
49
+ # load the test files
50
+ test_patterns.each do |test_pattern|
51
+ Dir[test_pattern].each do |path|
52
+ require File.expand_path(path)
53
+ end
54
+ end
55
+ end
56
+
57
+ def forking
58
+ Kernel.fork do
59
+ yield
60
+ Kernel.exit
61
+ end
62
+
63
+ Process.waitall
64
+ end
65
+
66
+ def print_test_usage
67
+ puts <<-EOT
68
+ Usage:
69
+ test "WHAT"
70
+
71
+ Description:
72
+ Runs either a full set of test suites or single suite.
73
+
74
+ If you supply WHAT with either models, controllers, helpers, integration, or performance,
75
+ those whole sets will be run.
76
+
77
+ If you supply WHAT with models/person, just test/models/person_test.rb will be run.
78
+ EOT
79
+ end
80
+ end
81
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commands
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-21 00:00:00.000000000 Z
12
+ date: 2012-12-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -33,11 +33,15 @@ executables: []
33
33
  extensions: []
34
34
  extra_rdoc_files: []
35
35
  files:
36
- - ./commands-0.0.2.gem
37
- - ./commands-0.0.3.gem
36
+ - ./commands-0.0.1.gem
38
37
  - ./commands.gemspec
39
38
  - ./Gemfile
40
39
  - ./Gemfile.lock
40
+ - ./lib/commands/commander.rb
41
+ - ./lib/commands/console_delegation.rb
42
+ - ./lib/commands/generator.rb
43
+ - ./lib/commands/raker.rb
44
+ - ./lib/commands/tester.rb
41
45
  - ./lib/commands.rb
42
46
  - ./MIT-LICENSE
43
47
  - ./Rakefile
Binary file
Binary file