thor_dleavitt 0.18.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/CHANGELOG.md +139 -0
- data/LICENSE.md +20 -0
- data/README.md +35 -0
- data/Thorfile +30 -0
- data/bin/thor_dleavitt +6 -0
- data/lib/thor/actions.rb +318 -0
- data/lib/thor/actions/create_file.rb +105 -0
- data/lib/thor/actions/create_link.rb +60 -0
- data/lib/thor/actions/directory.rb +119 -0
- data/lib/thor/actions/empty_directory.rb +137 -0
- data/lib/thor/actions/file_manipulation.rb +317 -0
- data/lib/thor/actions/inject_into_file.rb +109 -0
- data/lib/thor/base.rb +654 -0
- data/lib/thor/command.rb +136 -0
- data/lib/thor/core_ext/hash_with_indifferent_access.rb +80 -0
- data/lib/thor/core_ext/io_binary_read.rb +12 -0
- data/lib/thor/core_ext/ordered_hash.rb +100 -0
- data/lib/thor/error.rb +32 -0
- data/lib/thor/group.rb +282 -0
- data/lib/thor/invocation.rb +172 -0
- data/lib/thor/parser.rb +4 -0
- data/lib/thor/parser/argument.rb +74 -0
- data/lib/thor/parser/arguments.rb +171 -0
- data/lib/thor/parser/option.rb +121 -0
- data/lib/thor/parser/options.rb +218 -0
- data/lib/thor/rake_compat.rb +72 -0
- data/lib/thor/runner.rb +322 -0
- data/lib/thor/shell.rb +88 -0
- data/lib/thor/shell/basic.rb +422 -0
- data/lib/thor/shell/color.rb +148 -0
- data/lib/thor/shell/html.rb +127 -0
- data/lib/thor/util.rb +270 -0
- data/lib/thor/version.rb +3 -0
- data/lib/thor_dleavitt.rb +473 -0
- data/spec/actions/create_file_spec.rb +170 -0
- data/spec/actions/create_link_spec.rb +95 -0
- data/spec/actions/directory_spec.rb +169 -0
- data/spec/actions/empty_directory_spec.rb +129 -0
- data/spec/actions/file_manipulation_spec.rb +382 -0
- data/spec/actions/inject_into_file_spec.rb +135 -0
- data/spec/actions_spec.rb +331 -0
- data/spec/base_spec.rb +291 -0
- data/spec/command_spec.rb +80 -0
- data/spec/core_ext/hash_with_indifferent_access_spec.rb +48 -0
- data/spec/core_ext/ordered_hash_spec.rb +115 -0
- data/spec/exit_condition_spec.rb +19 -0
- data/spec/fixtures/application.rb +2 -0
- data/spec/fixtures/app{1}/README +3 -0
- data/spec/fixtures/bundle/execute.rb +6 -0
- data/spec/fixtures/bundle/main.thor +1 -0
- data/spec/fixtures/command.thor +10 -0
- data/spec/fixtures/doc/%file_name%.rb.tt +1 -0
- data/spec/fixtures/doc/COMMENTER +11 -0
- data/spec/fixtures/doc/README +3 -0
- data/spec/fixtures/doc/block_helper.rb +3 -0
- data/spec/fixtures/doc/config.rb +1 -0
- data/spec/fixtures/doc/config.yaml.tt +1 -0
- data/spec/fixtures/doc/excluding/%file_name%.rb.tt +1 -0
- data/spec/fixtures/enum.thor +10 -0
- data/spec/fixtures/group.thor +128 -0
- data/spec/fixtures/invoke.thor +118 -0
- data/spec/fixtures/path with spaces b/data/spec/fixtures/path with → spaces +0 -0
- data/spec/fixtures/preserve/script.sh +3 -0
- data/spec/fixtures/script.thor +220 -0
- data/spec/fixtures/subcommand.thor +17 -0
- data/spec/group_spec.rb +222 -0
- data/spec/helper.rb +67 -0
- data/spec/invocation_spec.rb +108 -0
- data/spec/parser/argument_spec.rb +53 -0
- data/spec/parser/arguments_spec.rb +66 -0
- data/spec/parser/option_spec.rb +202 -0
- data/spec/parser/options_spec.rb +400 -0
- data/spec/rake_compat_spec.rb +72 -0
- data/spec/register_spec.rb +197 -0
- data/spec/runner_spec.rb +241 -0
- data/spec/shell/basic_spec.rb +330 -0
- data/spec/shell/color_spec.rb +95 -0
- data/spec/shell/html_spec.rb +31 -0
- data/spec/shell_spec.rb +47 -0
- data/spec/subcommand_spec.rb +30 -0
- data/spec/thor_spec.rb +499 -0
- data/spec/util_spec.rb +196 -0
- data/thor.gemspec +24 -0
- metadata +191 -0
@@ -0,0 +1,148 @@
|
|
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
|
+
# Pass foreground, background and bold options to this method as
|
54
|
+
# symbols.
|
55
|
+
#
|
56
|
+
# Example:
|
57
|
+
#
|
58
|
+
# set_color "Hi!", :red, :on_white, :bold
|
59
|
+
#
|
60
|
+
# The available colors are:
|
61
|
+
#
|
62
|
+
# :bold
|
63
|
+
# :black
|
64
|
+
# :red
|
65
|
+
# :green
|
66
|
+
# :yellow
|
67
|
+
# :blue
|
68
|
+
# :magenta
|
69
|
+
# :cyan
|
70
|
+
# :white
|
71
|
+
# :on_black
|
72
|
+
# :on_red
|
73
|
+
# :on_green
|
74
|
+
# :on_yellow
|
75
|
+
# :on_blue
|
76
|
+
# :on_magenta
|
77
|
+
# :on_cyan
|
78
|
+
# :on_white
|
79
|
+
def set_color(string, *colors)
|
80
|
+
if colors.all? { |color| color.is_a?(Symbol) || color.is_a?(String) }
|
81
|
+
ansi_colors = colors.map { |color| lookup_color(color) }
|
82
|
+
"#{ansi_colors.join}#{string}#{CLEAR}"
|
83
|
+
else
|
84
|
+
# The old API was `set_color(color, bold=boolean)`. We
|
85
|
+
# continue to support the old API because you should never
|
86
|
+
# break old APIs unnecessarily :P
|
87
|
+
foreground, bold = colors
|
88
|
+
foreground = self.class.const_get(foreground.to_s.upcase) if foreground.is_a?(Symbol)
|
89
|
+
|
90
|
+
bold = bold ? BOLD : ""
|
91
|
+
"#{bold}#{foreground}#{string}#{CLEAR}"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
protected
|
96
|
+
|
97
|
+
def can_display_colors?
|
98
|
+
stdout.tty?
|
99
|
+
end
|
100
|
+
|
101
|
+
# Overwrite show_diff to show diff with colors if Diff::LCS is
|
102
|
+
# available.
|
103
|
+
#
|
104
|
+
def show_diff(destination, content) #:nodoc:
|
105
|
+
if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
|
106
|
+
actual = File.binread(destination).to_s.split("\n")
|
107
|
+
content = content.to_s.split("\n")
|
108
|
+
|
109
|
+
Diff::LCS.sdiff(actual, content).each do |diff|
|
110
|
+
output_diff_line(diff)
|
111
|
+
end
|
112
|
+
else
|
113
|
+
super
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def output_diff_line(diff) #:nodoc:
|
118
|
+
case diff.action
|
119
|
+
when '-'
|
120
|
+
say "- #{diff.old_element.chomp}", :red, true
|
121
|
+
when '+'
|
122
|
+
say "+ #{diff.new_element.chomp}", :green, true
|
123
|
+
when '!'
|
124
|
+
say "- #{diff.old_element.chomp}", :red, true
|
125
|
+
say "+ #{diff.new_element.chomp}", :green, true
|
126
|
+
else
|
127
|
+
say " #{diff.old_element.chomp}", nil, true
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
# Check if Diff::LCS is loaded. If it is, use it to create pretty output
|
132
|
+
# for diff.
|
133
|
+
#
|
134
|
+
def diff_lcs_loaded? #:nodoc:
|
135
|
+
return true if defined?(Diff::LCS)
|
136
|
+
return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
|
137
|
+
|
138
|
+
@diff_lcs_loaded = begin
|
139
|
+
require 'diff/lcs'
|
140
|
+
true
|
141
|
+
rescue LoadError
|
142
|
+
false
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -0,0 +1,127 @@
|
|
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 = "font-weight: bold"
|
11
|
+
|
12
|
+
# Set the terminal's foreground HTML color to black.
|
13
|
+
BLACK = 'color: black'
|
14
|
+
# Set the terminal's foreground HTML color to red.
|
15
|
+
RED = 'color: red'
|
16
|
+
# Set the terminal's foreground HTML color to green.
|
17
|
+
GREEN = 'color: green'
|
18
|
+
# Set the terminal's foreground HTML color to yellow.
|
19
|
+
YELLOW = 'color: yellow'
|
20
|
+
# Set the terminal's foreground HTML color to blue.
|
21
|
+
BLUE = 'color: blue'
|
22
|
+
# Set the terminal's foreground HTML color to magenta.
|
23
|
+
MAGENTA = 'color: magenta'
|
24
|
+
# Set the terminal's foreground HTML color to cyan.
|
25
|
+
CYAN = 'color: cyan'
|
26
|
+
# Set the terminal's foreground HTML color to white.
|
27
|
+
WHITE = 'color: white'
|
28
|
+
|
29
|
+
# Set the terminal's background HTML color to black.
|
30
|
+
ON_BLACK = 'background-color: black'
|
31
|
+
# Set the terminal's background HTML color to red.
|
32
|
+
ON_RED = 'background-color: red'
|
33
|
+
# Set the terminal's background HTML color to green.
|
34
|
+
ON_GREEN = 'background-color: green'
|
35
|
+
# Set the terminal's background HTML color to yellow.
|
36
|
+
ON_YELLOW = 'background-color: yellow'
|
37
|
+
# Set the terminal's background HTML color to blue.
|
38
|
+
ON_BLUE = 'background-color: blue'
|
39
|
+
# Set the terminal's background HTML color to magenta.
|
40
|
+
ON_MAGENTA = 'background-color: magenta'
|
41
|
+
# Set the terminal's background HTML color to cyan.
|
42
|
+
ON_CYAN = 'background-color: cyan'
|
43
|
+
# Set the terminal's background HTML color to white.
|
44
|
+
ON_WHITE = 'background-color: white'
|
45
|
+
|
46
|
+
# Set color by using a string or one of the defined constants. If a third
|
47
|
+
# option is set to true, it also adds bold to the string. This is based
|
48
|
+
# on Highline implementation and it automatically appends CLEAR to the end
|
49
|
+
# of the returned String.
|
50
|
+
#
|
51
|
+
def set_color(string, *colors)
|
52
|
+
if colors.all? { |color| color.is_a?(Symbol) || color.is_a?(String) }
|
53
|
+
html_colors = colors.map { |color| lookup_color(color) }
|
54
|
+
"<span style=\"#{html_colors.join("; ")};\">#{string}</span>"
|
55
|
+
else
|
56
|
+
color, bold = colors
|
57
|
+
html_color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
|
58
|
+
styles = [html_color]
|
59
|
+
styles << BOLD if bold
|
60
|
+
"<span style=\"#{styles.join("; ")};\">#{string}</span>"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# Ask something to the user and receives a response.
|
65
|
+
#
|
66
|
+
# ==== Example
|
67
|
+
# ask("What is your name?")
|
68
|
+
#
|
69
|
+
# TODO: Implement #ask for Thor::Shell::HTML
|
70
|
+
def ask(statement, color=nil)
|
71
|
+
raise NotImplementedError, "Implement #ask for Thor::Shell::HTML"
|
72
|
+
end
|
73
|
+
|
74
|
+
protected
|
75
|
+
|
76
|
+
def can_display_colors?
|
77
|
+
true
|
78
|
+
end
|
79
|
+
|
80
|
+
# Overwrite show_diff to show diff with colors if Diff::LCS is
|
81
|
+
# available.
|
82
|
+
#
|
83
|
+
def show_diff(destination, content) #:nodoc:
|
84
|
+
if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
|
85
|
+
actual = File.binread(destination).to_s.split("\n")
|
86
|
+
content = content.to_s.split("\n")
|
87
|
+
|
88
|
+
Diff::LCS.sdiff(actual, content).each do |diff|
|
89
|
+
output_diff_line(diff)
|
90
|
+
end
|
91
|
+
else
|
92
|
+
super
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def output_diff_line(diff) #:nodoc:
|
97
|
+
case diff.action
|
98
|
+
when '-'
|
99
|
+
say "- #{diff.old_element.chomp}", :red, true
|
100
|
+
when '+'
|
101
|
+
say "+ #{diff.new_element.chomp}", :green, true
|
102
|
+
when '!'
|
103
|
+
say "- #{diff.old_element.chomp}", :red, true
|
104
|
+
say "+ #{diff.new_element.chomp}", :green, true
|
105
|
+
else
|
106
|
+
say " #{diff.old_element.chomp}", nil, true
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# Check if Diff::LCS is loaded. If it is, use it to create pretty output
|
111
|
+
# for diff.
|
112
|
+
#
|
113
|
+
def diff_lcs_loaded? #:nodoc:
|
114
|
+
return true if defined?(Diff::LCS)
|
115
|
+
return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
|
116
|
+
|
117
|
+
@diff_lcs_loaded = begin
|
118
|
+
require 'diff/lcs'
|
119
|
+
true
|
120
|
+
rescue LoadError
|
121
|
+
false
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
data/lib/thor/util.rb
ADDED
@@ -0,0 +1,270 @@
|
|
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
|
+
class << self
|
20
|
+
|
21
|
+
# Receives a namespace and search for it in the Thor::Base subclasses.
|
22
|
+
#
|
23
|
+
# ==== Parameters
|
24
|
+
# namespace<String>:: The namespace to search for.
|
25
|
+
#
|
26
|
+
def find_by_namespace(namespace)
|
27
|
+
namespace = "default#{namespace}" if namespace.empty? || namespace =~ /^:/
|
28
|
+
Thor::Base.subclasses.find { |klass| klass.namespace == namespace }
|
29
|
+
end
|
30
|
+
|
31
|
+
# Receives a constant and converts it to a Thor namespace. Since Thor
|
32
|
+
# commands can be added to a sandbox, this method is also responsable for
|
33
|
+
# removing the sandbox namespace.
|
34
|
+
#
|
35
|
+
# This method should not be used in general because it's used to deal with
|
36
|
+
# older versions of Thor. On current versions, if you need to get the
|
37
|
+
# namespace from a class, just call namespace on it.
|
38
|
+
#
|
39
|
+
# ==== Parameters
|
40
|
+
# constant<Object>:: The constant to be converted to the thor path.
|
41
|
+
#
|
42
|
+
# ==== Returns
|
43
|
+
# String:: If we receive Foo::Bar::Baz it returns "foo:bar:baz"
|
44
|
+
#
|
45
|
+
def namespace_from_thor_class(constant)
|
46
|
+
constant = constant.to_s.gsub(/^Thor::Sandbox::/, "")
|
47
|
+
constant = snake_case(constant).squeeze(":")
|
48
|
+
constant
|
49
|
+
end
|
50
|
+
|
51
|
+
# Given the contents, evaluate it inside the sandbox and returns the
|
52
|
+
# namespaces defined in the sandbox.
|
53
|
+
#
|
54
|
+
# ==== Parameters
|
55
|
+
# contents<String>
|
56
|
+
#
|
57
|
+
# ==== Returns
|
58
|
+
# Array[Object]
|
59
|
+
#
|
60
|
+
def namespaces_in_content(contents, file=__FILE__)
|
61
|
+
old_constants = Thor::Base.subclasses.dup
|
62
|
+
Thor::Base.subclasses.clear
|
63
|
+
|
64
|
+
load_thorfile(file, contents)
|
65
|
+
|
66
|
+
new_constants = Thor::Base.subclasses.dup
|
67
|
+
Thor::Base.subclasses.replace(old_constants)
|
68
|
+
|
69
|
+
new_constants.map!{ |c| c.namespace }
|
70
|
+
new_constants.compact!
|
71
|
+
new_constants
|
72
|
+
end
|
73
|
+
|
74
|
+
# Returns the thor classes declared inside the given class.
|
75
|
+
#
|
76
|
+
def thor_classes_in(klass)
|
77
|
+
stringfied_constants = klass.constants.map { |c| c.to_s }
|
78
|
+
Thor::Base.subclasses.select do |subclass|
|
79
|
+
next unless subclass.name
|
80
|
+
stringfied_constants.include?(subclass.name.gsub("#{klass.name}::", ''))
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Receives a string and convert it to snake case. SnakeCase returns snake_case.
|
85
|
+
#
|
86
|
+
# ==== Parameters
|
87
|
+
# String
|
88
|
+
#
|
89
|
+
# ==== Returns
|
90
|
+
# String
|
91
|
+
#
|
92
|
+
def snake_case(str)
|
93
|
+
return str.downcase if str =~ /^[A-Z_]+$/
|
94
|
+
str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/
|
95
|
+
return $+.downcase
|
96
|
+
end
|
97
|
+
|
98
|
+
# Receives a string and convert it to camel case. camel_case returns CamelCase.
|
99
|
+
#
|
100
|
+
# ==== Parameters
|
101
|
+
# String
|
102
|
+
#
|
103
|
+
# ==== Returns
|
104
|
+
# String
|
105
|
+
#
|
106
|
+
def camel_case(str)
|
107
|
+
return str if str !~ /_/ && str =~ /[A-Z]+.*/
|
108
|
+
str.split('_').map { |i| i.capitalize }.join
|
109
|
+
end
|
110
|
+
|
111
|
+
# Receives a namespace and tries to retrieve a Thor or Thor::Group class
|
112
|
+
# from it. It first searches for a class using the all the given namespace,
|
113
|
+
# if it's not found, removes the highest entry and searches for the class
|
114
|
+
# again. If found, returns the highest entry as the class name.
|
115
|
+
#
|
116
|
+
# ==== Examples
|
117
|
+
#
|
118
|
+
# class Foo::Bar < Thor
|
119
|
+
# def baz
|
120
|
+
# end
|
121
|
+
# end
|
122
|
+
#
|
123
|
+
# class Baz::Foo < Thor::Group
|
124
|
+
# end
|
125
|
+
#
|
126
|
+
# Thor::Util.namespace_to_thor_class("foo:bar") #=> Foo::Bar, nil # will invoke default command
|
127
|
+
# Thor::Util.namespace_to_thor_class("baz:foo") #=> Baz::Foo, nil
|
128
|
+
# Thor::Util.namespace_to_thor_class("foo:bar:baz") #=> Foo::Bar, "baz"
|
129
|
+
#
|
130
|
+
# ==== Parameters
|
131
|
+
# namespace<String>
|
132
|
+
#
|
133
|
+
def find_class_and_command_by_namespace(namespace, fallback = true)
|
134
|
+
if namespace.include?(?:) # look for a namespaced command
|
135
|
+
pieces = namespace.split(":")
|
136
|
+
command = pieces.pop
|
137
|
+
klass = Thor::Util.find_by_namespace(pieces.join(":"))
|
138
|
+
end
|
139
|
+
unless klass # look for a Thor::Group with the right name
|
140
|
+
klass, command = Thor::Util.find_by_namespace(namespace), nil
|
141
|
+
end
|
142
|
+
if !klass && fallback # try a command in the default namespace
|
143
|
+
command = namespace
|
144
|
+
klass = Thor::Util.find_by_namespace('')
|
145
|
+
end
|
146
|
+
return klass, command
|
147
|
+
end
|
148
|
+
alias find_class_and_task_by_namespace find_class_and_command_by_namespace
|
149
|
+
|
150
|
+
# Receives a path and load the thor file in the path. The file is evaluated
|
151
|
+
# inside the sandbox to avoid namespacing conflicts.
|
152
|
+
#
|
153
|
+
def load_thorfile(path, content=nil, debug=false)
|
154
|
+
content ||= File.binread(path)
|
155
|
+
|
156
|
+
begin
|
157
|
+
Thor::Sandbox.class_eval(content, path)
|
158
|
+
rescue Exception => e
|
159
|
+
$stderr.puts("WARNING: unable to load thorfile #{path.inspect}: #{e.message}")
|
160
|
+
if debug
|
161
|
+
$stderr.puts(*e.backtrace)
|
162
|
+
else
|
163
|
+
$stderr.puts(e.backtrace.first)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def user_home
|
169
|
+
@@user_home ||= if ENV["HOME"]
|
170
|
+
ENV["HOME"]
|
171
|
+
elsif ENV["USERPROFILE"]
|
172
|
+
ENV["USERPROFILE"]
|
173
|
+
elsif ENV["HOMEDRIVE"] && ENV["HOMEPATH"]
|
174
|
+
File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"])
|
175
|
+
elsif ENV["APPDATA"]
|
176
|
+
ENV["APPDATA"]
|
177
|
+
else
|
178
|
+
begin
|
179
|
+
File.expand_path("~")
|
180
|
+
rescue
|
181
|
+
if File::ALT_SEPARATOR
|
182
|
+
"C:/"
|
183
|
+
else
|
184
|
+
"/"
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
# Returns the root where thor files are located, depending on the OS.
|
191
|
+
#
|
192
|
+
def thor_root
|
193
|
+
File.join(user_home, ".thor").gsub(/\\/, '/')
|
194
|
+
end
|
195
|
+
|
196
|
+
# Returns the files in the thor root. On Windows thor_root will be something
|
197
|
+
# like this:
|
198
|
+
#
|
199
|
+
# C:\Documents and Settings\james\.thor
|
200
|
+
#
|
201
|
+
# If we don't #gsub the \ character, Dir.glob will fail.
|
202
|
+
#
|
203
|
+
def thor_root_glob
|
204
|
+
files = Dir["#{escape_globs(thor_root)}/*"]
|
205
|
+
|
206
|
+
files.map! do |file|
|
207
|
+
File.directory?(file) ? File.join(file, "main.thor") : file
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
# Where to look for Thor files.
|
212
|
+
#
|
213
|
+
def globs_for(path)
|
214
|
+
path = escape_globs(path)
|
215
|
+
["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/*.thor"]
|
216
|
+
end
|
217
|
+
|
218
|
+
# Return the path to the ruby interpreter taking into account multiple
|
219
|
+
# installations and windows extensions.
|
220
|
+
#
|
221
|
+
def ruby_command
|
222
|
+
@ruby_command ||= begin
|
223
|
+
ruby_name = RbConfig::CONFIG['ruby_install_name']
|
224
|
+
ruby = File.join(RbConfig::CONFIG['bindir'], ruby_name)
|
225
|
+
ruby << RbConfig::CONFIG['EXEEXT']
|
226
|
+
|
227
|
+
# avoid using different name than ruby (on platforms supporting links)
|
228
|
+
if ruby_name != 'ruby' && File.respond_to?(:readlink)
|
229
|
+
begin
|
230
|
+
alternate_ruby = File.join(RbConfig::CONFIG['bindir'], 'ruby')
|
231
|
+
alternate_ruby << RbConfig::CONFIG['EXEEXT']
|
232
|
+
|
233
|
+
# ruby is a symlink
|
234
|
+
if File.symlink? alternate_ruby
|
235
|
+
linked_ruby = File.readlink alternate_ruby
|
236
|
+
|
237
|
+
# symlink points to 'ruby_install_name'
|
238
|
+
ruby = alternate_ruby if linked_ruby == ruby_name || linked_ruby == ruby
|
239
|
+
end
|
240
|
+
rescue NotImplementedError
|
241
|
+
# just ignore on windows
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
# escape string in case path to ruby executable contain spaces.
|
246
|
+
ruby.sub!(/.*\s.*/m, '"\&"')
|
247
|
+
ruby
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
# Returns a string that has had any glob characters escaped.
|
252
|
+
# The glob characters are `* ? { } [ ]`.
|
253
|
+
#
|
254
|
+
# ==== Examples
|
255
|
+
#
|
256
|
+
# Thor::Util.escape_globs('[apps]') # => '\[apps\]'
|
257
|
+
#
|
258
|
+
# ==== Parameters
|
259
|
+
# String
|
260
|
+
#
|
261
|
+
# ==== Returns
|
262
|
+
# String
|
263
|
+
#
|
264
|
+
def escape_globs(path)
|
265
|
+
path.to_s.gsub(/[*?{}\[\]]/, '\\\\\\&')
|
266
|
+
end
|
267
|
+
|
268
|
+
end
|
269
|
+
end
|
270
|
+
end
|