thor 0.14.6 → 0.19.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/{CHANGELOG.rdoc → CHANGELOG.md} +91 -31
  4. data/CONTRIBUTING.md +15 -0
  5. data/{LICENSE → LICENSE.md} +2 -2
  6. data/README.md +36 -296
  7. data/bin/thor +1 -1
  8. data/lib/thor/actions/create_file.rb +33 -35
  9. data/lib/thor/actions/create_link.rb +7 -5
  10. data/lib/thor/actions/directory.rb +49 -24
  11. data/lib/thor/actions/empty_directory.rb +68 -67
  12. data/lib/thor/actions/file_manipulation.rb +85 -28
  13. data/lib/thor/actions/inject_into_file.rb +26 -32
  14. data/lib/thor/actions.rb +70 -66
  15. data/lib/thor/base.rb +314 -237
  16. data/lib/thor/command.rb +133 -0
  17. data/lib/thor/core_ext/hash_with_indifferent_access.rb +34 -24
  18. data/lib/thor/core_ext/io_binary_read.rb +12 -0
  19. data/lib/thor/core_ext/ordered_hash.rb +94 -65
  20. data/lib/thor/error.rb +10 -8
  21. data/lib/thor/group.rb +74 -66
  22. data/lib/thor/invocation.rb +65 -56
  23. data/lib/thor/line_editor/basic.rb +35 -0
  24. data/lib/thor/line_editor/readline.rb +88 -0
  25. data/lib/thor/line_editor.rb +17 -0
  26. data/lib/thor/parser/argument.rb +32 -29
  27. data/lib/thor/parser/arguments.rb +107 -93
  28. data/lib/thor/parser/option.rb +39 -13
  29. data/lib/thor/parser/options.rb +144 -97
  30. data/lib/thor/parser.rb +4 -4
  31. data/lib/thor/rake_compat.rb +21 -16
  32. data/lib/thor/runner.rb +166 -153
  33. data/lib/thor/shell/basic.rb +268 -122
  34. data/lib/thor/shell/color.rb +84 -43
  35. data/lib/thor/shell/html.rb +71 -66
  36. data/lib/thor/shell.rb +30 -37
  37. data/lib/thor/util.rb +227 -188
  38. data/lib/thor/version.rb +1 -1
  39. data/lib/thor.rb +289 -131
  40. data/thor.gemspec +21 -0
  41. metadata +50 -189
  42. data/Thorfile +0 -24
  43. data/bin/rake2thor +0 -86
  44. data/lib/thor/core_ext/file_binary_read.rb +0 -9
  45. data/lib/thor/task.rb +0 -114
  46. data/spec/actions/create_file_spec.rb +0 -170
  47. data/spec/actions/directory_spec.rb +0 -136
  48. data/spec/actions/empty_directory_spec.rb +0 -98
  49. data/spec/actions/file_manipulation_spec.rb +0 -310
  50. data/spec/actions/inject_into_file_spec.rb +0 -135
  51. data/spec/actions_spec.rb +0 -322
  52. data/spec/base_spec.rb +0 -269
  53. data/spec/core_ext/hash_with_indifferent_access_spec.rb +0 -43
  54. data/spec/core_ext/ordered_hash_spec.rb +0 -115
  55. data/spec/fixtures/application.rb +0 -2
  56. data/spec/fixtures/bundle/execute.rb +0 -6
  57. data/spec/fixtures/bundle/main.thor +0 -1
  58. data/spec/fixtures/doc/%file_name%.rb.tt +0 -1
  59. data/spec/fixtures/doc/README +0 -3
  60. data/spec/fixtures/doc/block_helper.rb +0 -3
  61. data/spec/fixtures/doc/components/.empty_directory +0 -0
  62. data/spec/fixtures/doc/config.rb +0 -1
  63. data/spec/fixtures/group.thor +0 -114
  64. data/spec/fixtures/invoke.thor +0 -112
  65. data/spec/fixtures/path with spaces +0 -0
  66. data/spec/fixtures/script.thor +0 -184
  67. data/spec/fixtures/task.thor +0 -10
  68. data/spec/group_spec.rb +0 -178
  69. data/spec/invocation_spec.rb +0 -100
  70. data/spec/parser/argument_spec.rb +0 -47
  71. data/spec/parser/arguments_spec.rb +0 -64
  72. data/spec/parser/option_spec.rb +0 -202
  73. data/spec/parser/options_spec.rb +0 -319
  74. data/spec/rake_compat_spec.rb +0 -68
  75. data/spec/register_spec.rb +0 -92
  76. data/spec/runner_spec.rb +0 -210
  77. data/spec/shell/basic_spec.rb +0 -223
  78. data/spec/shell/color_spec.rb +0 -41
  79. data/spec/shell/html_spec.rb +0 -27
  80. data/spec/shell_spec.rb +0 -47
  81. data/spec/spec_helper.rb +0 -54
  82. data/spec/task_spec.rb +0 -69
  83. data/spec/thor_spec.rb +0 -334
  84. data/spec/util_spec.rb +0 -163
@@ -1,4 +1,4 @@
1
- require 'thor/shell/basic'
1
+ require "thor/shell/basic"
2
2
 
3
3
  class Thor
4
4
  module Shell
@@ -7,56 +7,58 @@ class Thor
7
7
  #
8
8
  class HTML < Basic
9
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>"
10
+ BOLD = "font-weight: bold"
16
11
 
17
12
  # Set the terminal's foreground HTML color to black.
18
- BLACK = '<span style="color: black;">'
13
+ BLACK = "color: black"
19
14
  # Set the terminal's foreground HTML color to red.
20
- RED = '<span style="color: red;">'
15
+ RED = "color: red"
21
16
  # Set the terminal's foreground HTML color to green.
22
- GREEN = '<span style="color: green;">'
17
+ GREEN = "color: green"
23
18
  # Set the terminal's foreground HTML color to yellow.
24
- YELLOW = '<span style="color: yellow;">'
19
+ YELLOW = "color: yellow"
25
20
  # Set the terminal's foreground HTML color to blue.
26
- BLUE = '<span style="color: blue;">'
21
+ BLUE = "color: blue"
27
22
  # Set the terminal's foreground HTML color to magenta.
28
- MAGENTA = '<span style="color: magenta;">'
23
+ MAGENTA = "color: magenta"
29
24
  # Set the terminal's foreground HTML color to cyan.
30
- CYAN = '<span style="color: cyan;">'
25
+ CYAN = "color: cyan"
31
26
  # Set the terminal's foreground HTML color to white.
32
- WHITE = '<span style="color: white;">'
27
+ WHITE = "color: white"
33
28
 
34
29
  # Set the terminal's background HTML color to black.
35
- ON_BLACK = '<span style="background-color: black">'
30
+ ON_BLACK = "background-color: black"
36
31
  # Set the terminal's background HTML color to red.
37
- ON_RED = '<span style="background-color: red">'
32
+ ON_RED = "background-color: red"
38
33
  # Set the terminal's background HTML color to green.
39
- ON_GREEN = '<span style="background-color: green">'
34
+ ON_GREEN = "background-color: green"
40
35
  # Set the terminal's background HTML color to yellow.
41
- ON_YELLOW = '<span style="background-color: yellow">'
36
+ ON_YELLOW = "background-color: yellow"
42
37
  # Set the terminal's background HTML color to blue.
43
- ON_BLUE = '<span style="background-color: blue">'
38
+ ON_BLUE = "background-color: blue"
44
39
  # Set the terminal's background HTML color to magenta.
45
- ON_MAGENTA = '<span style="background-color: magenta">'
40
+ ON_MAGENTA = "background-color: magenta"
46
41
  # Set the terminal's background HTML color to cyan.
47
- ON_CYAN = '<span style="background-color: cyan">'
42
+ ON_CYAN = "background-color: cyan"
48
43
  # Set the terminal's background HTML color to white.
49
- ON_WHITE = '<span style="background-color: white">'
44
+ ON_WHITE = "background-color: white"
50
45
 
51
46
  # Set color by using a string or one of the defined constants. If a third
52
47
  # option is set to true, it also adds bold to the string. This is based
53
48
  # on Highline implementation and it automatically appends CLEAR to the end
54
49
  # of the returned String.
55
50
  #
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}"
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
60
62
  end
61
63
 
62
64
  # Ask something to the user and receives a response.
@@ -65,57 +67,60 @@ class Thor
65
67
  # ask("What is your name?")
66
68
  #
67
69
  # TODO: Implement #ask for Thor::Shell::HTML
68
- def ask(statement, color=nil)
70
+ def ask(statement, color = nil)
69
71
  raise NotImplementedError, "Implement #ask for Thor::Shell::HTML"
70
72
  end
71
73
 
72
- protected
74
+ protected
75
+
76
+ def can_display_colors?
77
+ true
78
+ end
73
79
 
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")
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")
81
87
 
82
- Diff::LCS.sdiff(actual, content).each do |diff|
83
- output_diff_line(diff)
84
- end
85
- else
86
- super
88
+ Diff::LCS.sdiff(actual, content).each do |diff|
89
+ output_diff_line(diff)
87
90
  end
91
+ else
92
+ super
88
93
  end
94
+ end
89
95
 
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
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
102
107
  end
108
+ end
103
109
 
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
+ # 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?
110
116
 
111
- @diff_lcs_loaded = begin
112
- require 'diff/lcs'
113
- true
114
- rescue LoadError
115
- false
116
- end
117
+ @diff_lcs_loaded = begin
118
+ require "diff/lcs"
119
+ true
120
+ rescue LoadError
121
+ false
117
122
  end
118
-
123
+ end
119
124
  end
120
125
  end
121
126
  end
data/lib/thor/shell.rb CHANGED
@@ -1,33 +1,32 @@
1
- require 'rbconfig'
1
+ require "rbconfig"
2
2
 
3
3
  class Thor
4
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
5
+ class << self
6
+ attr_writer :shell
17
7
 
18
- # Sets the shell used in all Thor classes.
19
- #
20
- def self.shell=(klass)
21
- @shell = klass
8
+ # Returns the shell used in all Thor classes. If you are in a Unix platform
9
+ # it will use a colored log, otherwise it will use a basic one without color.
10
+ #
11
+ def shell
12
+ @shell ||= if ENV["THOR_SHELL"] && !ENV["THOR_SHELL"].empty?
13
+ Thor::Shell.const_get(ENV["THOR_SHELL"])
14
+ elsif RbConfig::CONFIG["host_os"] =~ /mswin|mingw/ && !ENV["ANSICON"]
15
+ Thor::Shell::Basic
16
+ else
17
+ Thor::Shell::Color
18
+ end
19
+ end
22
20
  end
23
21
  end
24
22
 
25
23
  module Shell
26
- SHELL_DELEGATED_METHODS = [:ask, :yes?, :no?, :say, :say_status, :print_table]
24
+ SHELL_DELEGATED_METHODS = [:ask, :error, :set_color, :yes?, :no?, :say, :say_status, :print_in_columns, :print_table, :print_wrapped, :file_collision, :terminal_width]
25
+ attr_writer :shell
27
26
 
28
- autoload :Basic, 'thor/shell/basic'
29
- autoload :Color, 'thor/shell/color'
30
- autoload :HTML, 'thor/shell/html'
27
+ autoload :Basic, "thor/shell/basic"
28
+ autoload :Color, "thor/shell/color"
29
+ autoload :HTML, "thor/shell/html"
31
30
 
32
31
  # Add shell to initialize config values.
33
32
  #
@@ -42,10 +41,10 @@ class Thor
42
41
  #
43
42
  # MyScript.new [1.0], { :foo => :bar }, :shell => Thor::Shell::Basic.new
44
43
  #
45
- def initialize(args=[], options={}, config={})
44
+ def initialize(args = [], options = {}, config = {})
46
45
  super
47
46
  self.shell = config[:shell]
48
- self.shell.base ||= self if self.shell.respond_to?(:base)
47
+ shell.base ||= self if shell.respond_to?(:base)
49
48
  end
50
49
 
51
50
  # Holds the shell for the given Thor instance. If no shell is given,
@@ -54,16 +53,11 @@ class Thor
54
53
  @shell ||= Thor::Base.shell.new
55
54
  end
56
55
 
57
- # Sets the shell for this thor class.
58
- def shell=(shell)
59
- @shell = shell
60
- end
61
-
62
56
  # Common methods that are delegated to the shell.
63
57
  SHELL_DELEGATED_METHODS.each do |method|
64
58
  module_eval <<-METHOD, __FILE__, __LINE__
65
- def #{method}(*args)
66
- shell.#{method}(*args)
59
+ def #{method}(*args,&block)
60
+ shell.#{method}(*args,&block)
67
61
  end
68
62
  METHOD
69
63
  end
@@ -76,13 +70,12 @@ class Thor
76
70
  shell.padding -= 1
77
71
  end
78
72
 
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
73
+ protected
86
74
 
75
+ # Allow shell to be shared between invocations.
76
+ #
77
+ def _shared_configuration #:nodoc:
78
+ super.merge!(:shell => shell)
79
+ end
87
80
  end
88
81
  end