thor 0.16.0 → 1.2.1

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 (93) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +15 -0
  3. data/README.md +23 -6
  4. data/bin/thor +1 -1
  5. data/lib/thor/actions/create_file.rb +34 -35
  6. data/lib/thor/actions/create_link.rb +9 -5
  7. data/lib/thor/actions/directory.rb +33 -23
  8. data/lib/thor/actions/empty_directory.rb +75 -85
  9. data/lib/thor/actions/file_manipulation.rb +103 -36
  10. data/lib/thor/actions/inject_into_file.rb +46 -36
  11. data/lib/thor/actions.rb +90 -68
  12. data/lib/thor/base.rb +302 -244
  13. data/lib/thor/command.rb +142 -0
  14. data/lib/thor/core_ext/hash_with_indifferent_access.rb +52 -24
  15. data/lib/thor/error.rb +90 -10
  16. data/lib/thor/group.rb +70 -74
  17. data/lib/thor/invocation.rb +63 -55
  18. data/lib/thor/line_editor/basic.rb +37 -0
  19. data/lib/thor/line_editor/readline.rb +88 -0
  20. data/lib/thor/line_editor.rb +17 -0
  21. data/lib/thor/nested_context.rb +29 -0
  22. data/lib/thor/parser/argument.rb +24 -28
  23. data/lib/thor/parser/arguments.rb +110 -102
  24. data/lib/thor/parser/option.rb +53 -15
  25. data/lib/thor/parser/options.rb +174 -97
  26. data/lib/thor/parser.rb +4 -4
  27. data/lib/thor/rake_compat.rb +12 -11
  28. data/lib/thor/runner.rb +159 -155
  29. data/lib/thor/shell/basic.rb +216 -93
  30. data/lib/thor/shell/color.rb +53 -40
  31. data/lib/thor/shell/html.rb +61 -58
  32. data/lib/thor/shell.rb +29 -36
  33. data/lib/thor/util.rb +231 -213
  34. data/lib/thor/version.rb +1 -1
  35. data/lib/thor.rb +303 -166
  36. data/thor.gemspec +27 -24
  37. metadata +36 -226
  38. data/.gitignore +0 -44
  39. data/.rspec +0 -2
  40. data/.travis.yml +0 -7
  41. data/CHANGELOG.rdoc +0 -134
  42. data/Gemfile +0 -15
  43. data/Thorfile +0 -30
  44. data/bin/rake2thor +0 -86
  45. data/lib/thor/core_ext/dir_escape.rb +0 -0
  46. data/lib/thor/core_ext/file_binary_read.rb +0 -9
  47. data/lib/thor/core_ext/ordered_hash.rb +0 -100
  48. data/lib/thor/task.rb +0 -132
  49. data/spec/actions/create_file_spec.rb +0 -170
  50. data/spec/actions/create_link_spec.rb +0 -81
  51. data/spec/actions/directory_spec.rb +0 -149
  52. data/spec/actions/empty_directory_spec.rb +0 -130
  53. data/spec/actions/file_manipulation_spec.rb +0 -370
  54. data/spec/actions/inject_into_file_spec.rb +0 -135
  55. data/spec/actions_spec.rb +0 -331
  56. data/spec/base_spec.rb +0 -279
  57. data/spec/core_ext/hash_with_indifferent_access_spec.rb +0 -43
  58. data/spec/core_ext/ordered_hash_spec.rb +0 -115
  59. data/spec/exit_condition_spec.rb +0 -19
  60. data/spec/fixtures/application.rb +0 -2
  61. data/spec/fixtures/app{1}/README +0 -3
  62. data/spec/fixtures/bundle/execute.rb +0 -6
  63. data/spec/fixtures/bundle/main.thor +0 -1
  64. data/spec/fixtures/doc/%file_name%.rb.tt +0 -1
  65. data/spec/fixtures/doc/COMMENTER +0 -10
  66. data/spec/fixtures/doc/README +0 -3
  67. data/spec/fixtures/doc/block_helper.rb +0 -3
  68. data/spec/fixtures/doc/components/.empty_directory +0 -0
  69. data/spec/fixtures/doc/config.rb +0 -1
  70. data/spec/fixtures/doc/config.yaml.tt +0 -1
  71. data/spec/fixtures/enum.thor +0 -10
  72. data/spec/fixtures/group.thor +0 -114
  73. data/spec/fixtures/invoke.thor +0 -112
  74. data/spec/fixtures/path with spaces +0 -0
  75. data/spec/fixtures/script.thor +0 -190
  76. data/spec/fixtures/task.thor +0 -10
  77. data/spec/group_spec.rb +0 -216
  78. data/spec/invocation_spec.rb +0 -100
  79. data/spec/parser/argument_spec.rb +0 -53
  80. data/spec/parser/arguments_spec.rb +0 -66
  81. data/spec/parser/option_spec.rb +0 -202
  82. data/spec/parser/options_spec.rb +0 -330
  83. data/spec/rake_compat_spec.rb +0 -72
  84. data/spec/register_spec.rb +0 -135
  85. data/spec/runner_spec.rb +0 -241
  86. data/spec/shell/basic_spec.rb +0 -300
  87. data/spec/shell/color_spec.rb +0 -81
  88. data/spec/shell/html_spec.rb +0 -32
  89. data/spec/shell_spec.rb +0 -47
  90. data/spec/spec_helper.rb +0 -59
  91. data/spec/task_spec.rb +0 -80
  92. data/spec/thor_spec.rb +0 -418
  93. data/spec/util_spec.rb +0 -196
@@ -1,4 +1,4 @@
1
- require 'thor/shell/basic'
1
+ require_relative "basic"
2
2
 
3
3
  class Thor
4
4
  module Shell
@@ -10,38 +10,38 @@ class Thor
10
10
  BOLD = "font-weight: bold"
11
11
 
12
12
  # Set the terminal's foreground HTML color to black.
13
- BLACK = 'color: black'
13
+ BLACK = "color: black"
14
14
  # Set the terminal's foreground HTML color to red.
15
- RED = 'color: red'
15
+ RED = "color: red"
16
16
  # Set the terminal's foreground HTML color to green.
17
- GREEN = 'color: green'
17
+ GREEN = "color: green"
18
18
  # Set the terminal's foreground HTML color to yellow.
19
- YELLOW = 'color: yellow'
19
+ YELLOW = "color: yellow"
20
20
  # Set the terminal's foreground HTML color to blue.
21
- BLUE = 'color: blue'
21
+ BLUE = "color: blue"
22
22
  # Set the terminal's foreground HTML color to magenta.
23
- MAGENTA = 'color: magenta'
23
+ MAGENTA = "color: magenta"
24
24
  # Set the terminal's foreground HTML color to cyan.
25
- CYAN = 'color: cyan'
25
+ CYAN = "color: cyan"
26
26
  # Set the terminal's foreground HTML color to white.
27
- WHITE = 'color: white'
27
+ WHITE = "color: white"
28
28
 
29
29
  # Set the terminal's background HTML color to black.
30
- ON_BLACK = 'background-color: black'
30
+ ON_BLACK = "background-color: black"
31
31
  # Set the terminal's background HTML color to red.
32
- ON_RED = 'background-color: red'
32
+ ON_RED = "background-color: red"
33
33
  # Set the terminal's background HTML color to green.
34
- ON_GREEN = 'background-color: green'
34
+ ON_GREEN = "background-color: green"
35
35
  # Set the terminal's background HTML color to yellow.
36
- ON_YELLOW = 'background-color: yellow'
36
+ ON_YELLOW = "background-color: yellow"
37
37
  # Set the terminal's background HTML color to blue.
38
- ON_BLUE = 'background-color: blue'
38
+ ON_BLUE = "background-color: blue"
39
39
  # Set the terminal's background HTML color to magenta.
40
- ON_MAGENTA = 'background-color: magenta'
40
+ ON_MAGENTA = "background-color: magenta"
41
41
  # Set the terminal's background HTML color to cyan.
42
- ON_CYAN = 'background-color: cyan'
42
+ ON_CYAN = "background-color: cyan"
43
43
  # Set the terminal's background HTML color to white.
44
- ON_WHITE = 'background-color: white'
44
+ ON_WHITE = "background-color: white"
45
45
 
46
46
  # Set color by using a string or one of the defined constants. If a third
47
47
  # option is set to true, it also adds bold to the string. This is based
@@ -51,13 +51,13 @@ class Thor
51
51
  def set_color(string, *colors)
52
52
  if colors.all? { |color| color.is_a?(Symbol) || color.is_a?(String) }
53
53
  html_colors = colors.map { |color| lookup_color(color) }
54
- "<span style=\"#{html_colors.join("; ")};\">#{string}</span>"
54
+ "<span style=\"#{html_colors.join('; ')};\">#{Thor::Util.escape_html(string)}</span>"
55
55
  else
56
56
  color, bold = colors
57
57
  html_color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
58
58
  styles = [html_color]
59
59
  styles << BOLD if bold
60
- "<span style=\"#{styles.join("; ")};\">#{string}</span>"
60
+ "<span style=\"#{styles.join('; ')};\">#{Thor::Util.escape_html(string)}</span>"
61
61
  end
62
62
  end
63
63
 
@@ -67,57 +67,60 @@ class Thor
67
67
  # ask("What is your name?")
68
68
  #
69
69
  # TODO: Implement #ask for Thor::Shell::HTML
70
- def ask(statement, color=nil)
70
+ def ask(statement, color = nil)
71
71
  raise NotImplementedError, "Implement #ask for Thor::Shell::HTML"
72
72
  end
73
73
 
74
- protected
74
+ protected
75
75
 
76
- # Overwrite show_diff to show diff with colors if Diff::LCS is
77
- # available.
78
- #
79
- def show_diff(destination, content) #:nodoc:
80
- if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
81
- actual = File.binread(destination).to_s.split("\n")
82
- content = content.to_s.split("\n")
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")
83
87
 
84
- Diff::LCS.sdiff(actual, content).each do |diff|
85
- output_diff_line(diff)
86
- end
87
- else
88
- super
88
+ Diff::LCS.sdiff(actual, content).each do |diff|
89
+ output_diff_line(diff)
89
90
  end
91
+ else
92
+ super
90
93
  end
94
+ end
91
95
 
92
- def output_diff_line(diff) #:nodoc:
93
- case diff.action
94
- when '-'
95
- say "- #{diff.old_element.chomp}", :red, true
96
- when '+'
97
- say "+ #{diff.new_element.chomp}", :green, true
98
- when '!'
99
- say "- #{diff.old_element.chomp}", :red, true
100
- say "+ #{diff.new_element.chomp}", :green, true
101
- else
102
- say " #{diff.old_element.chomp}", nil, true
103
- 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
104
107
  end
108
+ end
105
109
 
106
- # Check if Diff::LCS is loaded. If it is, use it to create pretty output
107
- # for diff.
108
- #
109
- def diff_lcs_loaded? #:nodoc:
110
- return true if defined?(Diff::LCS)
111
- 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?
112
116
 
113
- @diff_lcs_loaded = begin
114
- require 'diff/lcs'
115
- true
116
- rescue LoadError
117
- false
118
- end
117
+ @diff_lcs_loaded = begin
118
+ require "diff/lcs"
119
+ true
120
+ rescue LoadError
121
+ false
119
122
  end
120
-
123
+ end
121
124
  end
122
125
  end
123
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/) && !(ENV['ANSICON']))
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, :error, :set_color, :yes?, :no?, :say, :say_status, :print_in_columns, :print_table, :print_wrapped, :file_collision, :terminal_width]
24
+ SHELL_DELEGATED_METHODS = [:ask, :error, :set_color, :yes?, :no?, :say, :say_error, :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, File.expand_path("shell/basic", __dir__)
28
+ autoload :Color, File.expand_path("shell/color", __dir__)
29
+ autoload :HTML, File.expand_path("shell/html", __dir__)
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,14 +53,9 @@ 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
- module_eval <<-METHOD, __FILE__, __LINE__
58
+ module_eval <<-METHOD, __FILE__, __LINE__ + 1
65
59
  def #{method}(*args,&block)
66
60
  shell.#{method}(*args,&block)
67
61
  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