commands 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -23,3 +23,5 @@ When your console boots, it'll automatically have a `commander` object instantia
23
23
  > generate "scaffold post title:string"
24
24
  > rake "db:migrate"
25
25
  > test "models/person"
26
+
27
+ You can see the options available for all the commands by running them with no parameters.
Binary file
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'commands'
3
- s.version = '0.1.0'
3
+ s.version = '0.2.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,4 +1,4 @@
1
1
  require 'rails/console/app'
2
- require 'commands/console_delegation'
2
+ require 'rails/commands/console_delegation'
3
3
 
4
- Rails::ConsoleMethods.send :include, Commands::ConsoleDelegation
4
+ Rails::ConsoleMethods.send :include, Rails::Commands::ConsoleDelegation
@@ -0,0 +1,21 @@
1
+ require 'rails/commands/raker'
2
+ require 'rails/commands/tester'
3
+ require 'rails/commands/generator'
4
+
5
+ module Rails
6
+ module Commands
7
+ class Commander
8
+ delegate :rake, to: :raker
9
+ delegate :test, to: :tester
10
+ delegate :generate, :destroy, :update, to: :generator
11
+
12
+ attr_reader :raker, :tester, :generator
13
+
14
+ def initialize
15
+ @raker = Raker.new
16
+ @tester = Tester.new
17
+ @generator = Generator.new
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails/commands/commander'
2
+
3
+ module Rails
4
+ module Commands
5
+ module ConsoleDelegation
6
+ def commander
7
+ @commander ||= Commander.new
8
+ end
9
+
10
+ delegate :rake, :test, :generate, :destroy, :update, to: :commander
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module Rails
2
+ module Commands
3
+ module Environment
4
+ extend self
5
+
6
+ def fork
7
+ Kernel.fork do
8
+ yield
9
+ Kernel.exit
10
+ end
11
+
12
+ Process.waitall
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,42 @@
1
+ require 'rails/generators'
2
+
3
+ module Rails
4
+ module Commands
5
+ class Generator
6
+ def initialize
7
+ load_rails_generators
8
+ end
9
+
10
+ def generate(argv = nil)
11
+ generator :generate, argv
12
+ end
13
+
14
+ def update(argv = nil)
15
+ generator :update, argv
16
+ end
17
+
18
+ def destroy(argv = nil)
19
+ generator :destroy, argv
20
+ end
21
+
22
+
23
+ private
24
+ def load_rails_generators
25
+ Rails.application.load_generators
26
+ end
27
+
28
+ def generator(name, argv = nil)
29
+ if argv.nil?
30
+ # FIXME: I don't know why we can't just catch SystemExit here, then we wouldn't need this if block
31
+ Rails::Generators.help name
32
+ else
33
+ ARGV.replace argv.nil? ? [nil] : argv.split(" ")
34
+ load "rails/commands/#{name}.rb"
35
+ ARGV.replace [nil]
36
+ end
37
+
38
+ "Completed"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,62 @@
1
+ require 'rake'
2
+
3
+ module Rails
4
+ module Commands
5
+ class Raker
6
+ include Rake::DSL
7
+
8
+ def initialize
9
+ load_rake_tasks
10
+ end
11
+
12
+ def rake(task = nil)
13
+ task.nil? ? print_rake_tasks : invoke_rake_task(task)
14
+ "Completed"
15
+ rescue SystemExit, RuntimeError => e
16
+ "Failed: #{e.message}"
17
+ end
18
+
19
+
20
+ private
21
+ def load_rake_tasks
22
+ Rake::TaskManager.record_task_metadata = true # needed to capture comments from define_task
23
+ load Rails.root.join('Rakefile')
24
+ end
25
+
26
+ def print_rake_tasks
27
+ Rake.application.options.show_tasks = :tasks
28
+ Rake.application.options.show_task_pattern = Regexp.new('')
29
+ Rake.application.display_tasks_and_comments
30
+ end
31
+
32
+ def invoke_rake_task(task)
33
+ task, *options = task.split(" ")
34
+
35
+ ARGV.replace options
36
+
37
+ # FIXME: Before we can use this, we need a way to reset the options again
38
+ # Rake.application.handle_options
39
+
40
+ expose_argv_arguments_via_env { Rake::Task[task].invoke }
41
+ Rake.application.tasks.each(&:reenable) # Rake by default only allows tasks to be run once per session
42
+ ensure
43
+ ARGV.replace([])
44
+ end
45
+
46
+ def expose_argv_arguments_via_env
47
+ argv_arguments.each { |key, value| ENV[key] = value }
48
+ yield
49
+ ensure
50
+ argv_arguments.keys.each { |key| ENV.delete(key) }
51
+ end
52
+
53
+ def argv_arguments
54
+ ARGV.each_with_object({}) do |arg, arguments|
55
+ if arg =~ /^(\w+)=(.*)$/
56
+ arguments[$1] = $2
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,59 @@
1
+ require 'rails/commands/environment'
2
+
3
+ module Rails
4
+ module Commands
5
+ module TestEnvironment
6
+ extend self
7
+
8
+ def fork
9
+ Environment.fork do
10
+ switch_to_test
11
+ yield
12
+ end
13
+ end
14
+
15
+
16
+ private
17
+ def switch_to_test
18
+ switch_rails
19
+ switch_bundler
20
+
21
+ reset_active_record
22
+ reload_classes
23
+
24
+ add_test_dir_to_load_path
25
+ end
26
+
27
+ def switch_rails
28
+ ENV['RAILS_ENV'] = Rails.env = "test"
29
+
30
+ Kernel.silence_warnings do
31
+ Dir[Rails.root.join('config', 'initializers', '*.rb')].map { |file| load file }
32
+ load Rails.root.join('config', 'environments', "test.rb")
33
+ end
34
+ end
35
+
36
+ def switch_bundler
37
+ Bundler.require "test"
38
+ end
39
+
40
+
41
+ def reload_classes
42
+ ActionDispatch::Reloader.cleanup!
43
+ ActionDispatch::Reloader.prepare!
44
+ end
45
+
46
+ def reset_active_record
47
+ if defined? ActiveRecord
48
+ ActiveRecord::Base.clear_active_connections!
49
+ ActiveRecord::Base.establish_connection
50
+ end
51
+ end
52
+
53
+
54
+ def add_test_dir_to_load_path
55
+ $:.unshift("./test")
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,49 @@
1
+ require 'rails/commands/test_environment'
2
+
3
+ module Rails
4
+ module Commands
5
+ class Tester
6
+ def test(what = nil)
7
+ case what
8
+ when NilClass
9
+ print_test_usage
10
+ when "all"
11
+ run "test/**/**/*_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
+ def run(*test_patterns)
24
+ TestEnvironment.fork do
25
+ test_patterns.each do |test_pattern|
26
+ Dir[test_pattern].each do |path|
27
+ require File.expand_path(path)
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def print_test_usage
34
+ puts <<-EOT
35
+ Usage:
36
+ test "WHAT"
37
+
38
+ Description:
39
+ Runs either a full set of test suites or single suite.
40
+
41
+ If you supply WHAT with either models, controllers, helpers, integration, or performance,
42
+ those whole sets will be run.
43
+
44
+ If you supply WHAT with models/person, just test/models/person_test.rb will be run.
45
+ EOT
46
+ end
47
+ end
48
+ end
49
+ 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.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -34,15 +34,18 @@ extensions: []
34
34
  extra_rdoc_files: []
35
35
  files:
36
36
  - ./commands-0.0.1.gem
37
+ - ./commands-0.1.0.gem
37
38
  - ./commands.gemspec
38
39
  - ./Gemfile
39
40
  - ./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
45
41
  - ./lib/commands.rb
42
+ - ./lib/rails/commands/commander.rb
43
+ - ./lib/rails/commands/console_delegation.rb
44
+ - ./lib/rails/commands/environment.rb
45
+ - ./lib/rails/commands/generator.rb
46
+ - ./lib/rails/commands/raker.rb
47
+ - ./lib/rails/commands/test_environment.rb
48
+ - ./lib/rails/commands/tester.rb
46
49
  - ./MIT-LICENSE
47
50
  - ./Rakefile
48
51
  - ./README.md
@@ -1,32 +0,0 @@
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
-
@@ -1,11 +0,0 @@
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
@@ -1,40 +0,0 @@
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
@@ -1,36 +0,0 @@
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
@@ -1,81 +0,0 @@
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