millisami-thor 0.14.6
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +8 -0
- data/.gemtest +0 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/CHANGELOG.rdoc +103 -0
- data/Gemfile +11 -0
- data/LICENSE.md +20 -0
- data/README.md +26 -0
- data/Thorfile +13 -0
- data/bin/rake2thor +86 -0
- data/bin/thor +6 -0
- data/lib/thor/actions/create_file.rb +105 -0
- data/lib/thor/actions/create_link.rb +57 -0
- data/lib/thor/actions/directory.rb +93 -0
- data/lib/thor/actions/empty_directory.rb +134 -0
- data/lib/thor/actions/file_manipulation.rb +270 -0
- data/lib/thor/actions/inject_into_file.rb +109 -0
- data/lib/thor/actions.rb +314 -0
- data/lib/thor/base.rb +598 -0
- data/lib/thor/core_ext/file_binary_read.rb +9 -0
- data/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
- data/lib/thor/core_ext/ordered_hash.rb +100 -0
- data/lib/thor/error.rb +30 -0
- data/lib/thor/group.rb +276 -0
- data/lib/thor/invocation.rb +168 -0
- data/lib/thor/parser/argument.rb +67 -0
- data/lib/thor/parser/arguments.rb +165 -0
- data/lib/thor/parser/option.rb +120 -0
- data/lib/thor/parser/options.rb +181 -0
- data/lib/thor/parser.rb +4 -0
- data/lib/thor/rake_compat.rb +70 -0
- data/lib/thor/runner.rb +309 -0
- data/lib/thor/shell/basic.rb +302 -0
- data/lib/thor/shell/color.rb +108 -0
- data/lib/thor/shell/html.rb +121 -0
- data/lib/thor/shell.rb +88 -0
- data/lib/thor/task.rb +129 -0
- data/lib/thor/util.rb +229 -0
- data/lib/thor/version.rb +3 -0
- data/lib/thor.rb +336 -0
- data/spec/actions/create_file_spec.rb +170 -0
- data/spec/actions/directory_spec.rb +136 -0
- data/spec/actions/empty_directory_spec.rb +98 -0
- data/spec/actions/file_manipulation_spec.rb +317 -0
- data/spec/actions/inject_into_file_spec.rb +135 -0
- data/spec/actions_spec.rb +322 -0
- data/spec/base_spec.rb +274 -0
- data/spec/core_ext/hash_with_indifferent_access_spec.rb +43 -0
- data/spec/core_ext/ordered_hash_spec.rb +115 -0
- data/spec/fixtures/application.rb +2 -0
- data/spec/fixtures/bundle/execute.rb +6 -0
- data/spec/fixtures/bundle/main.thor +1 -0
- data/spec/fixtures/doc/%file_name%.rb.tt +1 -0
- data/spec/fixtures/doc/README +3 -0
- data/spec/fixtures/doc/block_helper.rb +3 -0
- data/spec/fixtures/doc/components/.empty_directory +0 -0
- data/spec/fixtures/doc/config.rb +1 -0
- data/spec/fixtures/doc/config.yaml.tt +1 -0
- data/spec/fixtures/group.thor +114 -0
- data/spec/fixtures/invoke.thor +112 -0
- data/spec/fixtures/path with spaces +0 -0
- data/spec/fixtures/script.thor +184 -0
- data/spec/fixtures/task.thor +10 -0
- data/spec/group_spec.rb +216 -0
- data/spec/invocation_spec.rb +100 -0
- data/spec/parser/argument_spec.rb +47 -0
- data/spec/parser/arguments_spec.rb +65 -0
- data/spec/parser/option_spec.rb +202 -0
- data/spec/parser/options_spec.rb +329 -0
- data/spec/rake_compat_spec.rb +72 -0
- data/spec/register_spec.rb +92 -0
- data/spec/runner_spec.rb +210 -0
- data/spec/shell/basic_spec.rb +223 -0
- data/spec/shell/color_spec.rb +41 -0
- data/spec/shell/html_spec.rb +27 -0
- data/spec/shell_spec.rb +47 -0
- data/spec/spec_helper.rb +54 -0
- data/spec/task_spec.rb +74 -0
- data/spec/thor_spec.rb +362 -0
- data/spec/util_spec.rb +163 -0
- data/thor.gemspec +25 -0
- metadata +241 -0
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'thor/shell/basic'
|
2
|
+
|
3
|
+
class Thor
|
4
|
+
module Shell
|
5
|
+
# Inherit from Thor::Shell::Basic and add set_color behavior. Check
|
6
|
+
# Thor::Shell::Basic to see all available methods.
|
7
|
+
#
|
8
|
+
class Color < Basic
|
9
|
+
# Embed in a String to clear all previous ANSI sequences.
|
10
|
+
CLEAR = "\e[0m"
|
11
|
+
# The start of an ANSI bold sequence.
|
12
|
+
BOLD = "\e[1m"
|
13
|
+
|
14
|
+
# Set the terminal's foreground ANSI color to black.
|
15
|
+
BLACK = "\e[30m"
|
16
|
+
# Set the terminal's foreground ANSI color to red.
|
17
|
+
RED = "\e[31m"
|
18
|
+
# Set the terminal's foreground ANSI color to green.
|
19
|
+
GREEN = "\e[32m"
|
20
|
+
# Set the terminal's foreground ANSI color to yellow.
|
21
|
+
YELLOW = "\e[33m"
|
22
|
+
# Set the terminal's foreground ANSI color to blue.
|
23
|
+
BLUE = "\e[34m"
|
24
|
+
# Set the terminal's foreground ANSI color to magenta.
|
25
|
+
MAGENTA = "\e[35m"
|
26
|
+
# Set the terminal's foreground ANSI color to cyan.
|
27
|
+
CYAN = "\e[36m"
|
28
|
+
# Set the terminal's foreground ANSI color to white.
|
29
|
+
WHITE = "\e[37m"
|
30
|
+
|
31
|
+
# Set the terminal's background ANSI color to black.
|
32
|
+
ON_BLACK = "\e[40m"
|
33
|
+
# Set the terminal's background ANSI color to red.
|
34
|
+
ON_RED = "\e[41m"
|
35
|
+
# Set the terminal's background ANSI color to green.
|
36
|
+
ON_GREEN = "\e[42m"
|
37
|
+
# Set the terminal's background ANSI color to yellow.
|
38
|
+
ON_YELLOW = "\e[43m"
|
39
|
+
# Set the terminal's background ANSI color to blue.
|
40
|
+
ON_BLUE = "\e[44m"
|
41
|
+
# Set the terminal's background ANSI color to magenta.
|
42
|
+
ON_MAGENTA = "\e[45m"
|
43
|
+
# Set the terminal's background ANSI color to cyan.
|
44
|
+
ON_CYAN = "\e[46m"
|
45
|
+
# Set the terminal's background ANSI color to white.
|
46
|
+
ON_WHITE = "\e[47m"
|
47
|
+
|
48
|
+
# Set color by using a string or one of the defined constants. If a third
|
49
|
+
# option is set to true, it also adds bold to the string. This is based
|
50
|
+
# on Highline implementation and it automatically appends CLEAR to the end
|
51
|
+
# of the returned String.
|
52
|
+
#
|
53
|
+
def set_color(string, color, bold=false)
|
54
|
+
color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
|
55
|
+
bold = bold ? BOLD : ""
|
56
|
+
"#{bold}#{color}#{string}#{CLEAR}"
|
57
|
+
end
|
58
|
+
|
59
|
+
protected
|
60
|
+
|
61
|
+
# Overwrite show_diff to show diff with colors if Diff::LCS is
|
62
|
+
# available.
|
63
|
+
#
|
64
|
+
def show_diff(destination, content) #:nodoc:
|
65
|
+
if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
|
66
|
+
actual = File.binread(destination).to_s.split("\n")
|
67
|
+
content = content.to_s.split("\n")
|
68
|
+
|
69
|
+
Diff::LCS.sdiff(actual, content).each do |diff|
|
70
|
+
output_diff_line(diff)
|
71
|
+
end
|
72
|
+
else
|
73
|
+
super
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def output_diff_line(diff) #:nodoc:
|
78
|
+
case diff.action
|
79
|
+
when '-'
|
80
|
+
say "- #{diff.old_element.chomp}", :red, true
|
81
|
+
when '+'
|
82
|
+
say "+ #{diff.new_element.chomp}", :green, true
|
83
|
+
when '!'
|
84
|
+
say "- #{diff.old_element.chomp}", :red, true
|
85
|
+
say "+ #{diff.new_element.chomp}", :green, true
|
86
|
+
else
|
87
|
+
say " #{diff.old_element.chomp}", nil, true
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
# Check if Diff::LCS is loaded. If it is, use it to create pretty output
|
92
|
+
# for diff.
|
93
|
+
#
|
94
|
+
def diff_lcs_loaded? #:nodoc:
|
95
|
+
return true if defined?(Diff::LCS)
|
96
|
+
return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
|
97
|
+
|
98
|
+
@diff_lcs_loaded = begin
|
99
|
+
require 'diff/lcs'
|
100
|
+
true
|
101
|
+
rescue LoadError
|
102
|
+
false
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'thor/shell/basic'
|
2
|
+
|
3
|
+
class Thor
|
4
|
+
module Shell
|
5
|
+
# Inherit from Thor::Shell::Basic and add set_color behavior. Check
|
6
|
+
# Thor::Shell::Basic to see all available methods.
|
7
|
+
#
|
8
|
+
class HTML < Basic
|
9
|
+
# The start of an HTML bold sequence.
|
10
|
+
BOLD = "<strong>"
|
11
|
+
# The end of an HTML bold sequence.
|
12
|
+
END_BOLD = "</strong>"
|
13
|
+
|
14
|
+
# Embed in a String to clear previous color selection.
|
15
|
+
CLEAR = "</span>"
|
16
|
+
|
17
|
+
# Set the terminal's foreground HTML color to black.
|
18
|
+
BLACK = '<span style="color: black;">'
|
19
|
+
# Set the terminal's foreground HTML color to red.
|
20
|
+
RED = '<span style="color: red;">'
|
21
|
+
# Set the terminal's foreground HTML color to green.
|
22
|
+
GREEN = '<span style="color: green;">'
|
23
|
+
# Set the terminal's foreground HTML color to yellow.
|
24
|
+
YELLOW = '<span style="color: yellow;">'
|
25
|
+
# Set the terminal's foreground HTML color to blue.
|
26
|
+
BLUE = '<span style="color: blue;">'
|
27
|
+
# Set the terminal's foreground HTML color to magenta.
|
28
|
+
MAGENTA = '<span style="color: magenta;">'
|
29
|
+
# Set the terminal's foreground HTML color to cyan.
|
30
|
+
CYAN = '<span style="color: cyan;">'
|
31
|
+
# Set the terminal's foreground HTML color to white.
|
32
|
+
WHITE = '<span style="color: white;">'
|
33
|
+
|
34
|
+
# Set the terminal's background HTML color to black.
|
35
|
+
ON_BLACK = '<span style="background-color: black">'
|
36
|
+
# Set the terminal's background HTML color to red.
|
37
|
+
ON_RED = '<span style="background-color: red">'
|
38
|
+
# Set the terminal's background HTML color to green.
|
39
|
+
ON_GREEN = '<span style="background-color: green">'
|
40
|
+
# Set the terminal's background HTML color to yellow.
|
41
|
+
ON_YELLOW = '<span style="background-color: yellow">'
|
42
|
+
# Set the terminal's background HTML color to blue.
|
43
|
+
ON_BLUE = '<span style="background-color: blue">'
|
44
|
+
# Set the terminal's background HTML color to magenta.
|
45
|
+
ON_MAGENTA = '<span style="background-color: magenta">'
|
46
|
+
# Set the terminal's background HTML color to cyan.
|
47
|
+
ON_CYAN = '<span style="background-color: cyan">'
|
48
|
+
# Set the terminal's background HTML color to white.
|
49
|
+
ON_WHITE = '<span style="background-color: white">'
|
50
|
+
|
51
|
+
# Set color by using a string or one of the defined constants. If a third
|
52
|
+
# option is set to true, it also adds bold to the string. This is based
|
53
|
+
# on Highline implementation and it automatically appends CLEAR to the end
|
54
|
+
# of the returned String.
|
55
|
+
#
|
56
|
+
def set_color(string, color, bold=false)
|
57
|
+
color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
|
58
|
+
bold, end_bold = bold ? [BOLD, END_BOLD] : ['', '']
|
59
|
+
"#{bold}#{color}#{string}#{CLEAR}#{end_bold}"
|
60
|
+
end
|
61
|
+
|
62
|
+
# Ask something to the user and receives a response.
|
63
|
+
#
|
64
|
+
# ==== Example
|
65
|
+
# ask("What is your name?")
|
66
|
+
#
|
67
|
+
# TODO: Implement #ask for Thor::Shell::HTML
|
68
|
+
def ask(statement, color=nil)
|
69
|
+
raise NotImplementedError, "Implement #ask for Thor::Shell::HTML"
|
70
|
+
end
|
71
|
+
|
72
|
+
protected
|
73
|
+
|
74
|
+
# Overwrite show_diff to show diff with colors if Diff::LCS is
|
75
|
+
# available.
|
76
|
+
#
|
77
|
+
def show_diff(destination, content) #:nodoc:
|
78
|
+
if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
|
79
|
+
actual = File.binread(destination).to_s.split("\n")
|
80
|
+
content = content.to_s.split("\n")
|
81
|
+
|
82
|
+
Diff::LCS.sdiff(actual, content).each do |diff|
|
83
|
+
output_diff_line(diff)
|
84
|
+
end
|
85
|
+
else
|
86
|
+
super
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def output_diff_line(diff) #:nodoc:
|
91
|
+
case diff.action
|
92
|
+
when '-'
|
93
|
+
say "- #{diff.old_element.chomp}", :red, true
|
94
|
+
when '+'
|
95
|
+
say "+ #{diff.new_element.chomp}", :green, true
|
96
|
+
when '!'
|
97
|
+
say "- #{diff.old_element.chomp}", :red, true
|
98
|
+
say "+ #{diff.new_element.chomp}", :green, true
|
99
|
+
else
|
100
|
+
say " #{diff.old_element.chomp}", nil, true
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Check if Diff::LCS is loaded. If it is, use it to create pretty output
|
105
|
+
# for diff.
|
106
|
+
#
|
107
|
+
def diff_lcs_loaded? #:nodoc:
|
108
|
+
return true if defined?(Diff::LCS)
|
109
|
+
return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
|
110
|
+
|
111
|
+
@diff_lcs_loaded = begin
|
112
|
+
require 'diff/lcs'
|
113
|
+
true
|
114
|
+
rescue LoadError
|
115
|
+
false
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/lib/thor/shell.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'rbconfig'
|
2
|
+
|
3
|
+
class Thor
|
4
|
+
module Base
|
5
|
+
# Returns the shell used in all Thor classes. If you are in a Unix platform
|
6
|
+
# it will use a colored log, otherwise it will use a basic one without color.
|
7
|
+
#
|
8
|
+
def self.shell
|
9
|
+
@shell ||= if ENV['THOR_SHELL'] && ENV['THOR_SHELL'].size > 0
|
10
|
+
Thor::Shell.const_get(ENV['THOR_SHELL'])
|
11
|
+
elsif RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
|
12
|
+
Thor::Shell::Basic
|
13
|
+
else
|
14
|
+
Thor::Shell::Color
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Sets the shell used in all Thor classes.
|
19
|
+
#
|
20
|
+
def self.shell=(klass)
|
21
|
+
@shell = klass
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module Shell
|
26
|
+
SHELL_DELEGATED_METHODS = [:ask, :yes?, :no?, :say, :say_status, :print_table]
|
27
|
+
|
28
|
+
autoload :Basic, 'thor/shell/basic'
|
29
|
+
autoload :Color, 'thor/shell/color'
|
30
|
+
autoload :HTML, 'thor/shell/html'
|
31
|
+
|
32
|
+
# Add shell to initialize config values.
|
33
|
+
#
|
34
|
+
# ==== Configuration
|
35
|
+
# shell<Object>:: An instance of the shell to be used.
|
36
|
+
#
|
37
|
+
# ==== Examples
|
38
|
+
#
|
39
|
+
# class MyScript < Thor
|
40
|
+
# argument :first, :type => :numeric
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# MyScript.new [1.0], { :foo => :bar }, :shell => Thor::Shell::Basic.new
|
44
|
+
#
|
45
|
+
def initialize(args=[], options={}, config={})
|
46
|
+
super
|
47
|
+
self.shell = config[:shell]
|
48
|
+
self.shell.base ||= self if self.shell.respond_to?(:base)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Holds the shell for the given Thor instance. If no shell is given,
|
52
|
+
# it gets a default shell from Thor::Base.shell.
|
53
|
+
def shell
|
54
|
+
@shell ||= Thor::Base.shell.new
|
55
|
+
end
|
56
|
+
|
57
|
+
# Sets the shell for this thor class.
|
58
|
+
def shell=(shell)
|
59
|
+
@shell = shell
|
60
|
+
end
|
61
|
+
|
62
|
+
# Common methods that are delegated to the shell.
|
63
|
+
SHELL_DELEGATED_METHODS.each do |method|
|
64
|
+
module_eval <<-METHOD, __FILE__, __LINE__
|
65
|
+
def #{method}(*args)
|
66
|
+
shell.#{method}(*args)
|
67
|
+
end
|
68
|
+
METHOD
|
69
|
+
end
|
70
|
+
|
71
|
+
# Yields the given block with padding.
|
72
|
+
def with_padding
|
73
|
+
shell.padding += 1
|
74
|
+
yield
|
75
|
+
ensure
|
76
|
+
shell.padding -= 1
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
|
81
|
+
# Allow shell to be shared between invocations.
|
82
|
+
#
|
83
|
+
def _shared_configuration #:nodoc:
|
84
|
+
super.merge!(:shell => self.shell)
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
end
|
data/lib/thor/task.rb
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
class Thor
|
2
|
+
class Task < Struct.new(:name, :description, :long_description, :usage, :options)
|
3
|
+
FILE_REGEXP = /^#{Regexp.escape(File.dirname(__FILE__))}/
|
4
|
+
|
5
|
+
def initialize(name, description, long_description, usage, options=nil)
|
6
|
+
super(name.to_s, description, long_description, usage, options || {})
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize_copy(other) #:nodoc:
|
10
|
+
super(other)
|
11
|
+
self.options = other.options.dup if other.options
|
12
|
+
end
|
13
|
+
|
14
|
+
def hidden?
|
15
|
+
false
|
16
|
+
end
|
17
|
+
|
18
|
+
# By default, a task invokes a method in the thor class. You can change this
|
19
|
+
# implementation to create custom tasks.
|
20
|
+
def run_cmd(instance, args=[])
|
21
|
+
if private_method?(instance)
|
22
|
+
instance.class.handle_no_task_error(name)
|
23
|
+
elsif public_method?(instance)
|
24
|
+
instance.send(name, *args)
|
25
|
+
elsif local_method?(instance, :method_missing)
|
26
|
+
instance.send(:method_missing, name.to_sym, *args)
|
27
|
+
else
|
28
|
+
instance.class.handle_no_task_error(name)
|
29
|
+
end
|
30
|
+
rescue ArgumentError => e
|
31
|
+
handle_argument_error?(instance, e, caller) ?
|
32
|
+
instance.class.handle_argument_error(self, e) : (raise e)
|
33
|
+
rescue NoMethodError => e
|
34
|
+
handle_no_method_error?(instance, e, caller) ?
|
35
|
+
instance.class.handle_no_task_error(name) : (raise e)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns the formatted usage by injecting given required arguments
|
39
|
+
# and required options into the given usage.
|
40
|
+
def formatted_usage(klass, namespace = true, subcommand = false)
|
41
|
+
if namespace
|
42
|
+
namespace = klass.namespace
|
43
|
+
formatted = "#{namespace.gsub(/^(default)/,'')}:"
|
44
|
+
formatted.sub!(/.$/, ' ') if subcommand
|
45
|
+
end
|
46
|
+
|
47
|
+
formatted ||= ""
|
48
|
+
|
49
|
+
# Add usage with required arguments
|
50
|
+
formatted << if klass && !klass.arguments.empty?
|
51
|
+
usage.to_s.gsub(/^#{name}/) do |match|
|
52
|
+
match << " " << klass.arguments.map{ |a| a.usage }.compact.join(' ')
|
53
|
+
end
|
54
|
+
else
|
55
|
+
usage.to_s
|
56
|
+
end
|
57
|
+
|
58
|
+
# Add required options
|
59
|
+
formatted << " #{required_options}"
|
60
|
+
|
61
|
+
# Strip and go!
|
62
|
+
formatted.strip
|
63
|
+
end
|
64
|
+
|
65
|
+
protected
|
66
|
+
|
67
|
+
def not_debugging?(instance)
|
68
|
+
!(instance.class.respond_to?(:debugging) && instance.class.debugging)
|
69
|
+
end
|
70
|
+
|
71
|
+
def required_options
|
72
|
+
@required_options ||= options.map{ |_, o| o.usage if o.required? }.compact.sort.join(" ")
|
73
|
+
end
|
74
|
+
|
75
|
+
# Given a target, checks if this class name is a public method.
|
76
|
+
def public_method?(instance) #:nodoc:
|
77
|
+
!(instance.public_methods & [name.to_s, name.to_sym]).empty?
|
78
|
+
end
|
79
|
+
|
80
|
+
def private_method?(instance)
|
81
|
+
!(instance.private_methods & [name.to_s, name.to_sym]).empty?
|
82
|
+
end
|
83
|
+
|
84
|
+
def local_method?(instance, name)
|
85
|
+
methods = instance.public_methods(false) + instance.private_methods(false) + instance.protected_methods(false)
|
86
|
+
!(methods & [name.to_s, name.to_sym]).empty?
|
87
|
+
end
|
88
|
+
|
89
|
+
def sans_backtrace(backtrace, caller) #:nodoc:
|
90
|
+
saned = backtrace.reject { |frame| frame =~ FILE_REGEXP }
|
91
|
+
saned -= caller
|
92
|
+
end
|
93
|
+
|
94
|
+
def handle_argument_error?(instance, error, caller)
|
95
|
+
not_debugging?(instance) && error.message =~ /wrong number of arguments/ && begin
|
96
|
+
saned = sans_backtrace(error.backtrace, caller)
|
97
|
+
# Ruby 1.9 always include the called method in the backtrace
|
98
|
+
saned.empty? || (saned.size == 1 && RUBY_VERSION >= "1.9")
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def handle_no_method_error?(instance, error, caller)
|
103
|
+
not_debugging?(instance) &&
|
104
|
+
error.message =~ /^undefined method `#{name}' for #{Regexp.escape(instance.to_s)}$/
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# A task that is hidden in help messages but still invocable.
|
109
|
+
class HiddenTask < Task
|
110
|
+
def hidden?
|
111
|
+
true
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# A dynamic task that handles method missing scenarios.
|
116
|
+
class DynamicTask < Task
|
117
|
+
def initialize(name, options=nil)
|
118
|
+
super(name.to_s, "A dynamically-generated task", name.to_s, name.to_s, options)
|
119
|
+
end
|
120
|
+
|
121
|
+
def run_cmd(instance, args=[])
|
122
|
+
if (instance.methods & [name.to_s, name.to_sym]).empty?
|
123
|
+
super
|
124
|
+
else
|
125
|
+
instance.class.handle_no_task_error(name)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
data/lib/thor/util.rb
ADDED
@@ -0,0 +1,229 @@
|
|
1
|
+
require 'rbconfig'
|
2
|
+
|
3
|
+
class Thor
|
4
|
+
module Sandbox #:nodoc:
|
5
|
+
end
|
6
|
+
|
7
|
+
# This module holds several utilities:
|
8
|
+
#
|
9
|
+
# 1) Methods to convert thor namespaces to constants and vice-versa.
|
10
|
+
#
|
11
|
+
# Thor::Util.namespace_from_thor_class(Foo::Bar::Baz) #=> "foo:bar:baz"
|
12
|
+
#
|
13
|
+
# 2) Loading thor files and sandboxing:
|
14
|
+
#
|
15
|
+
# Thor::Util.load_thorfile("~/.thor/foo")
|
16
|
+
#
|
17
|
+
module Util
|
18
|
+
|
19
|
+
# Receives a namespace and search for it in the Thor::Base subclasses.
|
20
|
+
#
|
21
|
+
# ==== Parameters
|
22
|
+
# namespace<String>:: The namespace to search for.
|
23
|
+
#
|
24
|
+
def self.find_by_namespace(namespace)
|
25
|
+
namespace = "default#{namespace}" if namespace.empty? || namespace =~ /^:/
|
26
|
+
Thor::Base.subclasses.find { |klass| klass.namespace == namespace }
|
27
|
+
end
|
28
|
+
|
29
|
+
# Receives a constant and converts it to a Thor namespace. Since Thor tasks
|
30
|
+
# can be added to a sandbox, this method is also responsable for removing
|
31
|
+
# the sandbox namespace.
|
32
|
+
#
|
33
|
+
# This method should not be used in general because it's used to deal with
|
34
|
+
# older versions of Thor. On current versions, if you need to get the
|
35
|
+
# namespace from a class, just call namespace on it.
|
36
|
+
#
|
37
|
+
# ==== Parameters
|
38
|
+
# constant<Object>:: The constant to be converted to the thor path.
|
39
|
+
#
|
40
|
+
# ==== Returns
|
41
|
+
# String:: If we receive Foo::Bar::Baz it returns "foo:bar:baz"
|
42
|
+
#
|
43
|
+
def self.namespace_from_thor_class(constant)
|
44
|
+
constant = constant.to_s.gsub(/^Thor::Sandbox::/, "")
|
45
|
+
constant = snake_case(constant).squeeze(":")
|
46
|
+
constant
|
47
|
+
end
|
48
|
+
|
49
|
+
# Given the contents, evaluate it inside the sandbox and returns the
|
50
|
+
# namespaces defined in the sandbox.
|
51
|
+
#
|
52
|
+
# ==== Parameters
|
53
|
+
# contents<String>
|
54
|
+
#
|
55
|
+
# ==== Returns
|
56
|
+
# Array[Object]
|
57
|
+
#
|
58
|
+
def self.namespaces_in_content(contents, file=__FILE__)
|
59
|
+
old_constants = Thor::Base.subclasses.dup
|
60
|
+
Thor::Base.subclasses.clear
|
61
|
+
|
62
|
+
load_thorfile(file, contents)
|
63
|
+
|
64
|
+
new_constants = Thor::Base.subclasses.dup
|
65
|
+
Thor::Base.subclasses.replace(old_constants)
|
66
|
+
|
67
|
+
new_constants.map!{ |c| c.namespace }
|
68
|
+
new_constants.compact!
|
69
|
+
new_constants
|
70
|
+
end
|
71
|
+
|
72
|
+
# Returns the thor classes declared inside the given class.
|
73
|
+
#
|
74
|
+
def self.thor_classes_in(klass)
|
75
|
+
stringfied_constants = klass.constants.map { |c| c.to_s }
|
76
|
+
Thor::Base.subclasses.select do |subclass|
|
77
|
+
next unless subclass.name
|
78
|
+
stringfied_constants.include?(subclass.name.gsub("#{klass.name}::", ''))
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Receives a string and convert it to snake case. SnakeCase returns snake_case.
|
83
|
+
#
|
84
|
+
# ==== Parameters
|
85
|
+
# String
|
86
|
+
#
|
87
|
+
# ==== Returns
|
88
|
+
# String
|
89
|
+
#
|
90
|
+
def self.snake_case(str)
|
91
|
+
return str.downcase if str =~ /^[A-Z_]+$/
|
92
|
+
str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/
|
93
|
+
return $+.downcase
|
94
|
+
end
|
95
|
+
|
96
|
+
# Receives a string and convert it to camel case. camel_case returns CamelCase.
|
97
|
+
#
|
98
|
+
# ==== Parameters
|
99
|
+
# String
|
100
|
+
#
|
101
|
+
# ==== Returns
|
102
|
+
# String
|
103
|
+
#
|
104
|
+
def self.camel_case(str)
|
105
|
+
return str if str !~ /_/ && str =~ /[A-Z]+.*/
|
106
|
+
str.split('_').map { |i| i.capitalize }.join
|
107
|
+
end
|
108
|
+
|
109
|
+
# Receives a namespace and tries to retrieve a Thor or Thor::Group class
|
110
|
+
# from it. It first searches for a class using the all the given namespace,
|
111
|
+
# if it's not found, removes the highest entry and searches for the class
|
112
|
+
# again. If found, returns the highest entry as the class name.
|
113
|
+
#
|
114
|
+
# ==== Examples
|
115
|
+
#
|
116
|
+
# class Foo::Bar < Thor
|
117
|
+
# def baz
|
118
|
+
# end
|
119
|
+
# end
|
120
|
+
#
|
121
|
+
# class Baz::Foo < Thor::Group
|
122
|
+
# end
|
123
|
+
#
|
124
|
+
# Thor::Util.namespace_to_thor_class("foo:bar") #=> Foo::Bar, nil # will invoke default task
|
125
|
+
# Thor::Util.namespace_to_thor_class("baz:foo") #=> Baz::Foo, nil
|
126
|
+
# Thor::Util.namespace_to_thor_class("foo:bar:baz") #=> Foo::Bar, "baz"
|
127
|
+
#
|
128
|
+
# ==== Parameters
|
129
|
+
# namespace<String>
|
130
|
+
#
|
131
|
+
def self.find_class_and_task_by_namespace(namespace, fallback = true)
|
132
|
+
if namespace.include?(?:) # look for a namespaced task
|
133
|
+
pieces = namespace.split(":")
|
134
|
+
task = pieces.pop
|
135
|
+
klass = Thor::Util.find_by_namespace(pieces.join(":"))
|
136
|
+
end
|
137
|
+
unless klass # look for a Thor::Group with the right name
|
138
|
+
klass, task = Thor::Util.find_by_namespace(namespace), nil
|
139
|
+
end
|
140
|
+
if !klass && fallback # try a task in the default namespace
|
141
|
+
task = namespace
|
142
|
+
klass = Thor::Util.find_by_namespace('')
|
143
|
+
end
|
144
|
+
return klass, task
|
145
|
+
end
|
146
|
+
|
147
|
+
# Receives a path and load the thor file in the path. The file is evaluated
|
148
|
+
# inside the sandbox to avoid namespacing conflicts.
|
149
|
+
#
|
150
|
+
def self.load_thorfile(path, content=nil, debug=false)
|
151
|
+
content ||= File.binread(path)
|
152
|
+
|
153
|
+
begin
|
154
|
+
Thor::Sandbox.class_eval(content, path)
|
155
|
+
rescue Exception => e
|
156
|
+
$stderr.puts "WARNING: unable to load thorfile #{path.inspect}: #{e.message}"
|
157
|
+
if debug
|
158
|
+
$stderr.puts *e.backtrace
|
159
|
+
else
|
160
|
+
$stderr.puts e.backtrace.first
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def self.user_home
|
166
|
+
@@user_home ||= if ENV["HOME"]
|
167
|
+
ENV["HOME"]
|
168
|
+
elsif ENV["USERPROFILE"]
|
169
|
+
ENV["USERPROFILE"]
|
170
|
+
elsif ENV["HOMEDRIVE"] && ENV["HOMEPATH"]
|
171
|
+
File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"])
|
172
|
+
elsif ENV["APPDATA"]
|
173
|
+
ENV["APPDATA"]
|
174
|
+
else
|
175
|
+
begin
|
176
|
+
File.expand_path("~")
|
177
|
+
rescue
|
178
|
+
if File::ALT_SEPARATOR
|
179
|
+
"C:/"
|
180
|
+
else
|
181
|
+
"/"
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
# Returns the root where thor files are located, dependending on the OS.
|
188
|
+
#
|
189
|
+
def self.thor_root
|
190
|
+
File.join(user_home, ".thor").gsub(/\\/, '/')
|
191
|
+
end
|
192
|
+
|
193
|
+
# Returns the files in the thor root. On Windows thor_root will be something
|
194
|
+
# like this:
|
195
|
+
#
|
196
|
+
# C:\Documents and Settings\james\.thor
|
197
|
+
#
|
198
|
+
# If we don't #gsub the \ character, Dir.glob will fail.
|
199
|
+
#
|
200
|
+
def self.thor_root_glob
|
201
|
+
files = Dir["#{thor_root}/*"]
|
202
|
+
|
203
|
+
files.map! do |file|
|
204
|
+
File.directory?(file) ? File.join(file, "main.thor") : file
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
# Where to look for Thor files.
|
209
|
+
#
|
210
|
+
def self.globs_for(path)
|
211
|
+
["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/*.thor"]
|
212
|
+
end
|
213
|
+
|
214
|
+
# Return the path to the ruby interpreter taking into account multiple
|
215
|
+
# installations and windows extensions.
|
216
|
+
#
|
217
|
+
def self.ruby_command
|
218
|
+
@ruby_command ||= begin
|
219
|
+
ruby = File.join(RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name'])
|
220
|
+
ruby << RbConfig::CONFIG['EXEEXT']
|
221
|
+
|
222
|
+
# escape string in case path to ruby executable contain spaces.
|
223
|
+
ruby.sub!(/.*\s.*/m, '"\&"')
|
224
|
+
ruby
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
end
|
229
|
+
end
|
data/lib/thor/version.rb
ADDED