rails-sh 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 jugyo
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,41 @@
1
+ = rails-sh
2
+
3
+ The Rails Shell to execute sub commands of rails quickly.
4
+
5
+ == Usage
6
+
7
+ $ cd your-rails3-app
8
+ $ rails-sh
9
+
10
+ == Example
11
+
12
+ rails> g controller foo bar
13
+ $ rails g controller foo bar
14
+ create app/controllers/foo_controller.rb
15
+ route get "foo/bar"
16
+ invoke erb
17
+ create app/views/foo
18
+ create app/views/foo/bar.html.erb
19
+ invoke test_unit
20
+ create test/functional/foo_controller_test.rb
21
+ invoke helper
22
+ create app/helpers/foo_helper.rb
23
+ invoke test_unit
24
+ create test/unit/helpers/foo_helper_test.rb
25
+
26
+ rails> routes
27
+ foo_bar GET /foo/bar(.:format) {:controller=>"foo", :action=>"bar"}
28
+
29
+ == Contributing to rails-sh
30
+
31
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
32
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
33
+ * Fork the project
34
+ * Start a feature/bugfix branch
35
+ * Commit and push until you are happy with your contribution
36
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
37
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
38
+
39
+ == Copyright
40
+
41
+ Copyright (c) 2011 jugyo. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "rails-sh"
16
+ gem.homepage = "http://github.com/jugyo/rails-sh"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{The Rails Shell}
19
+ gem.description = %Q{The Rails Shell to execute sub commands of rails quickly.}
20
+ gem.email = "jugyo.org@gmail.com"
21
+ gem.authors = ["jugyo"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "rails-sh #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/bin/rails-sh ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ ENV["RAILS_ENV"] ||= ARGV.shift
4
+
5
+ APP_PATH = File.expand_path('./config/application')
6
+ require File.expand_path('./config/boot')
7
+ require APP_PATH
8
+ Rails.application.require_environment!
9
+
10
+ puts "Rails.env: #{Rails.env}"
11
+
12
+ $:.unshift File.expand_path('../../lib', __FILE__)
13
+ require 'rails/sh'
14
+ Rails::Sh.start
data/lib/rails/sh.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'rails/sh/command'
2
+ require 'rails/sh/patch_for_kernel'
3
+ require 'readline'
4
+
5
+ module Rails
6
+ module Sh
7
+ RAILS_SUB_COMMANDS = ['generate', 'destroy', 'plugin', 'benchmarker', 'profiler',
8
+ 'console', 'server', 'dbconsole', 'application', 'runner']
9
+
10
+ class << self
11
+ def start
12
+ setup_readline
13
+ while buf = Readline.readline("\e[42mrails>\e[0m ", true)
14
+ execute(buf)
15
+ setup_readline
16
+ end
17
+ end
18
+
19
+ def setup_readline
20
+ Readline.basic_word_break_characters = ""
21
+ Readline.completion_proc = lambda do |word|
22
+ (Command.command_names + RAILS_SUB_COMMANDS).grep(/^#{Regexp.quote(word)}/)
23
+ end
24
+ end
25
+
26
+ def execute(line)
27
+ line = line.strip
28
+ if command = Command.find(line)
29
+ arg = line[/\s+\w+/].strip rescue nil
30
+ command.call(arg)
31
+ else
32
+ execute_rails_command(line)
33
+ end
34
+ end
35
+
36
+ def execute_rails_command(line)
37
+ ENV.delete("RAILS_ENV")
38
+ ARGV.clear
39
+ ARGV.concat line.split(/\s+/)
40
+ puts "\e[42m$ rails #{ARGV.join(" ")}\e[0m"
41
+ load 'rails/commands.rb'
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,60 @@
1
+ module Rails
2
+ module Sh
3
+ class Command
4
+ class << self
5
+ def commands
6
+ @commands ||= {}
7
+ end
8
+
9
+ def define(name, &block)
10
+ commands[name.to_sym] = block
11
+ end
12
+
13
+ def find(line)
14
+ commands[line[/\w+/].to_sym]
15
+ end
16
+
17
+ def command_names
18
+ commands.keys
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ module Kernel
26
+ alias_method :_exit, :exit
27
+ def exit(*args); end
28
+ end
29
+
30
+ Rails::Sh::Command.define 'exit' do
31
+ _exit
32
+ end
33
+
34
+ Rails::Sh::Command.define 'routes' do |controller|
35
+ Rails.application.reload_routes!
36
+ all_routes = Rails.application.routes.routes
37
+
38
+ if controller.present?
39
+ all_routes = all_routes.select{ |route| route.defaults[:controller] == controller }
40
+ end
41
+
42
+ routes = all_routes.collect do |route|
43
+
44
+ reqs = route.requirements.dup
45
+ reqs[:to] = route.app unless route.app.class.name.to_s =~ /^ActionDispatch::Routing/
46
+ reqs = reqs.empty? ? "" : reqs.inspect
47
+
48
+ {:name => route.name.to_s, :verb => route.verb.to_s, :path => route.path, :reqs => reqs}
49
+ end
50
+
51
+ routes.reject! { |r| r[:path] =~ %r{/rails/info/properties} } # Skip the route if it's internal info route
52
+
53
+ name_width = routes.map{ |r| r[:name].length }.max
54
+ verb_width = routes.map{ |r| r[:verb].length }.max
55
+ path_width = routes.map{ |r| r[:path].length }.max
56
+
57
+ routes.each do |r|
58
+ puts "#{r[:name].rjust(name_width)} #{r[:verb].ljust(verb_width)} #{r[:path].ljust(path_width)} #{r[:reqs]}"
59
+ end
60
+ end
@@ -0,0 +1,15 @@
1
+ module Kernel
2
+ alias_method :_require, :require
3
+ def require(name)
4
+ if name =~ /^rails\/commands\//
5
+ load "#{name}.rb"
6
+ else
7
+ _require(name)
8
+ end
9
+ end
10
+
11
+ alias_method :_exec, :exec
12
+ def exec(*args)
13
+ system(*args)
14
+ end
15
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rails::Sh::Command do
4
+ context 'when define a command' do
5
+ before do
6
+ @block = lambda {}
7
+ Rails::Sh::Command.define('foo', &@block)
8
+ end
9
+
10
+ it 'We can find it' do
11
+ Rails::Sh::Command.find('foo').should eq(@block)
12
+ end
13
+
14
+ it 'We can get command names' do
15
+ Rails::Sh::Command.command_names.should =~ [:exit, :foo, :routes]
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rails::Sh do
4
+ context 'when define a command' do
5
+ before do
6
+ @block = lambda {}
7
+ Rails::Sh::Command.define('foo', &@block)
8
+ end
9
+
10
+ ['foo'].each do |line|
11
+ it "the command should be executed if the line is '#{line}'" do
12
+ @block.should_receive(:call).with(nil).once
13
+ Rails::Sh.should_receive(:execute_rails_command).with(line).exactly(0).times
14
+ Rails::Sh.execute(line)
15
+ end
16
+ end
17
+
18
+ ['fo', 'bar'].each do |line|
19
+ it "the command should not be executed if the line is '#{line}'" do
20
+ @block.should_receive(:call).with(nil).exactly(0).times
21
+ Rails::Sh.should_receive(:execute_rails_command).with(line).once
22
+ Rails::Sh.execute(line)
23
+ end
24
+ end
25
+ end
26
+
27
+ ['console', 'g --help'].each do |line|
28
+ it "a rails's command should be executed if the line is #{line}" do
29
+ Rails::Sh.should_receive(:load).with("rails/commands.rb") do
30
+ ARGV.should eq(line.split(/\s+/))
31
+ end
32
+ Rails::Sh.execute(line)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'rails/sh'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-sh
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - jugyo
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-24 00:00:00 +09:00
18
+ default_executable: rails-sh
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 3
30
+ - 0
31
+ version: 2.3.0
32
+ type: :development
33
+ prerelease: false
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 0
45
+ - 0
46
+ version: 1.0.0
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: jeweler
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ segments:
58
+ - 1
59
+ - 5
60
+ - 2
61
+ version: 1.5.2
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: rcov
67
+ requirement: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: *id004
78
+ description: The Rails Shell to execute sub commands of rails quickly.
79
+ email: jugyo.org@gmail.com
80
+ executables:
81
+ - rails-sh
82
+ extensions: []
83
+
84
+ extra_rdoc_files:
85
+ - LICENSE.txt
86
+ - README.rdoc
87
+ files:
88
+ - .document
89
+ - .rspec
90
+ - Gemfile
91
+ - LICENSE.txt
92
+ - README.rdoc
93
+ - Rakefile
94
+ - VERSION
95
+ - bin/rails-sh
96
+ - lib/rails/sh.rb
97
+ - lib/rails/sh/command.rb
98
+ - lib/rails/sh/patch_for_kernel.rb
99
+ - spec/rails/sh/command_spec.rb
100
+ - spec/rails/sh_spec.rb
101
+ - spec/spec_helper.rb
102
+ has_rdoc: true
103
+ homepage: http://github.com/jugyo/rails-sh
104
+ licenses:
105
+ - MIT
106
+ post_install_message:
107
+ rdoc_options: []
108
+
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: -4152638312712520890
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ requirements: []
129
+
130
+ rubyforge_project:
131
+ rubygems_version: 1.3.7
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: The Rails Shell
135
+ test_files:
136
+ - spec/rails/sh/command_spec.rb
137
+ - spec/rails/sh_spec.rb
138
+ - spec/spec_helper.rb