thor-exclude_pattern 0.18.0

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 (107) hide show
  1. checksums.yaml +7 -0
  2. data/.document +5 -0
  3. data/CHANGELOG.md +130 -0
  4. data/LICENSE.md +20 -0
  5. data/README.md +34 -0
  6. data/Thorfile +30 -0
  7. data/bin/rake2thor +86 -0
  8. data/bin/thor +6 -0
  9. data/lib/thor.rb +458 -0
  10. data/lib/thor/actions.rb +318 -0
  11. data/lib/thor/actions/create_file.rb +105 -0
  12. data/lib/thor/actions/create_link.rb +60 -0
  13. data/lib/thor/actions/directory.rb +119 -0
  14. data/lib/thor/actions/empty_directory.rb +153 -0
  15. data/lib/thor/actions/file_manipulation.rb +314 -0
  16. data/lib/thor/actions/inject_into_file.rb +109 -0
  17. data/lib/thor/base.rb +649 -0
  18. data/lib/thor/command.rb +136 -0
  19. data/lib/thor/core_ext/hash_with_indifferent_access.rb +80 -0
  20. data/lib/thor/core_ext/io_binary_read.rb +12 -0
  21. data/lib/thor/core_ext/ordered_hash.rb +100 -0
  22. data/lib/thor/error.rb +32 -0
  23. data/lib/thor/exclude_pattern/version.rb +5 -0
  24. data/lib/thor/group.rb +287 -0
  25. data/lib/thor/invocation.rb +172 -0
  26. data/lib/thor/parser.rb +4 -0
  27. data/lib/thor/parser/argument.rb +74 -0
  28. data/lib/thor/parser/arguments.rb +171 -0
  29. data/lib/thor/parser/option.rb +121 -0
  30. data/lib/thor/parser/options.rb +218 -0
  31. data/lib/thor/rake_compat.rb +72 -0
  32. data/lib/thor/runner.rb +322 -0
  33. data/lib/thor/shell.rb +88 -0
  34. data/lib/thor/shell/basic.rb +393 -0
  35. data/lib/thor/shell/color.rb +148 -0
  36. data/lib/thor/shell/html.rb +127 -0
  37. data/lib/thor/util.rb +270 -0
  38. data/lib/thor/version.rb +3 -0
  39. data/spec/actions/create_file_spec.rb +170 -0
  40. data/spec/actions/create_link_spec.rb +95 -0
  41. data/spec/actions/directory_spec.rb +169 -0
  42. data/spec/actions/empty_directory_spec.rb +130 -0
  43. data/spec/actions/file_manipulation_spec.rb +382 -0
  44. data/spec/actions/inject_into_file_spec.rb +135 -0
  45. data/spec/actions_spec.rb +331 -0
  46. data/spec/base_spec.rb +294 -0
  47. data/spec/command_spec.rb +80 -0
  48. data/spec/core_ext/hash_with_indifferent_access_spec.rb +48 -0
  49. data/spec/core_ext/ordered_hash_spec.rb +115 -0
  50. data/spec/exit_condition_spec.rb +19 -0
  51. data/spec/fixtures/application.rb +2 -0
  52. data/spec/fixtures/app{1}/README +3 -0
  53. data/spec/fixtures/bundle/execute.rb +6 -0
  54. data/spec/fixtures/bundle/main.thor +1 -0
  55. data/spec/fixtures/command.thor +10 -0
  56. data/spec/fixtures/doc/%file_name%.rb.tt +1 -0
  57. data/spec/fixtures/doc/COMMENTER +11 -0
  58. data/spec/fixtures/doc/README +3 -0
  59. data/spec/fixtures/doc/block_helper.rb +3 -0
  60. data/spec/fixtures/doc/config.rb +1 -0
  61. data/spec/fixtures/doc/config.yaml.tt +1 -0
  62. data/spec/fixtures/doc/excluding/%file_name%.rb.tt +1 -0
  63. data/spec/fixtures/enum.thor +10 -0
  64. data/spec/fixtures/group.thor +128 -0
  65. data/spec/fixtures/invoke.thor +112 -0
  66. data/spec/fixtures/path with spaces b/data/spec/fixtures/path with → spaces +0 -0
  67. data/spec/fixtures/preserve/script.sh +3 -0
  68. data/spec/fixtures/script.thor +199 -0
  69. data/spec/fixtures/subcommand.thor +17 -0
  70. data/spec/group_spec.rb +216 -0
  71. data/spec/helper.rb +67 -0
  72. data/spec/invocation_spec.rb +100 -0
  73. data/spec/parser/argument_spec.rb +53 -0
  74. data/spec/parser/arguments_spec.rb +66 -0
  75. data/spec/parser/option_spec.rb +202 -0
  76. data/spec/parser/options_spec.rb +400 -0
  77. data/spec/rake_compat_spec.rb +72 -0
  78. data/spec/register_spec.rb +197 -0
  79. data/spec/runner_spec.rb +241 -0
  80. data/spec/sandbox/application.rb +2 -0
  81. data/spec/sandbox/app{1}/README +3 -0
  82. data/spec/sandbox/bundle/execute.rb +6 -0
  83. data/spec/sandbox/bundle/main.thor +1 -0
  84. data/spec/sandbox/command.thor +10 -0
  85. data/spec/sandbox/doc/%file_name%.rb.tt +1 -0
  86. data/spec/sandbox/doc/COMMENTER +11 -0
  87. data/spec/sandbox/doc/README +4 -0
  88. data/spec/sandbox/doc/block_helper.rb +3 -0
  89. data/spec/sandbox/doc/config.rb +1 -0
  90. data/spec/sandbox/doc/config.yaml.tt +1 -0
  91. data/spec/sandbox/doc/excluding/%file_name%.rb.tt +1 -0
  92. data/spec/sandbox/enum.thor +10 -0
  93. data/spec/sandbox/group.thor +128 -0
  94. data/spec/sandbox/invoke.thor +112 -0
  95. data/spec/sandbox/path with spaces b/data/spec/sandbox/path with → spaces +0 -0
  96. data/spec/sandbox/preserve/script.sh +3 -0
  97. data/spec/sandbox/script.thor +199 -0
  98. data/spec/sandbox/subcommand.thor +17 -0
  99. data/spec/shell/basic_spec.rb +311 -0
  100. data/spec/shell/color_spec.rb +95 -0
  101. data/spec/shell/html_spec.rb +32 -0
  102. data/spec/shell_spec.rb +47 -0
  103. data/spec/subcommand_spec.rb +30 -0
  104. data/spec/thor_spec.rb +469 -0
  105. data/spec/util_spec.rb +196 -0
  106. data/thor.gemspec +24 -0
  107. metadata +232 -0
@@ -0,0 +1,318 @@
1
+ require 'fileutils'
2
+ require 'uri'
3
+ require 'thor/core_ext/io_binary_read'
4
+ require 'thor/actions/create_file'
5
+ require 'thor/actions/create_link'
6
+ require 'thor/actions/directory'
7
+ require 'thor/actions/empty_directory'
8
+ require 'thor/actions/file_manipulation'
9
+ require 'thor/actions/inject_into_file'
10
+
11
+ class Thor
12
+ module Actions
13
+ attr_accessor :behavior
14
+
15
+ def self.included(base) #:nodoc:
16
+ base.extend ClassMethods
17
+ end
18
+
19
+ module ClassMethods
20
+ # Hold source paths for one Thor instance. source_paths_for_search is the
21
+ # method responsible to gather source_paths from this current class,
22
+ # inherited paths and the source root.
23
+ #
24
+ def source_paths
25
+ @_source_paths ||= []
26
+ end
27
+
28
+ # Stores and return the source root for this class
29
+ def source_root(path=nil)
30
+ @_source_root = path if path
31
+ @_source_root
32
+ end
33
+
34
+ # Returns the source paths in the following order:
35
+ #
36
+ # 1) This class source paths
37
+ # 2) Source root
38
+ # 3) Parents source paths
39
+ #
40
+ def source_paths_for_search
41
+ paths = []
42
+ paths += self.source_paths
43
+ paths << self.source_root if self.source_root
44
+ paths += from_superclass(:source_paths, [])
45
+ paths
46
+ end
47
+
48
+ # Add runtime options that help actions execution.
49
+ #
50
+ def add_runtime_options!
51
+ class_option :force, :type => :boolean, :aliases => "-f", :group => :runtime,
52
+ :desc => "Overwrite files that already exist"
53
+
54
+ class_option :pretend, :type => :boolean, :aliases => "-p", :group => :runtime,
55
+ :desc => "Run but do not make any changes"
56
+
57
+ class_option :quiet, :type => :boolean, :aliases => "-q", :group => :runtime,
58
+ :desc => "Suppress status output"
59
+
60
+ class_option :skip, :type => :boolean, :aliases => "-s", :group => :runtime,
61
+ :desc => "Skip files that already exist"
62
+ end
63
+ end
64
+
65
+ # Extends initializer to add more configuration options.
66
+ #
67
+ # ==== Configuration
68
+ # behavior<Symbol>:: The actions default behavior. Can be :invoke or :revoke.
69
+ # It also accepts :force, :skip and :pretend to set the behavior
70
+ # and the respective option.
71
+ #
72
+ # destination_root<String>:: The root directory needed for some actions.
73
+ #
74
+ def initialize(args=[], options={}, config={})
75
+ self.behavior = case config[:behavior].to_s
76
+ when "force", "skip"
77
+ _cleanup_options_and_set(options, config[:behavior])
78
+ :invoke
79
+ when "revoke"
80
+ :revoke
81
+ else
82
+ :invoke
83
+ end
84
+
85
+ super
86
+ self.destination_root = config[:destination_root]
87
+ end
88
+
89
+ # Wraps an action object and call it accordingly to the thor class behavior.
90
+ #
91
+ def action(instance) #:nodoc:
92
+ if behavior == :revoke
93
+ instance.revoke!
94
+ else
95
+ instance.invoke!
96
+ end
97
+ end
98
+
99
+ # Returns the root for this thor class (also aliased as destination root).
100
+ #
101
+ def destination_root
102
+ @destination_stack.last
103
+ end
104
+
105
+ # Sets the root for this thor class. Relatives path are added to the
106
+ # directory where the script was invoked and expanded.
107
+ #
108
+ def destination_root=(root)
109
+ @destination_stack ||= []
110
+ @destination_stack[0] = File.expand_path(root || '')
111
+ end
112
+
113
+ # Returns the given path relative to the absolute root (ie, root where
114
+ # the script started).
115
+ #
116
+ def relative_to_original_destination_root(path, remove_dot=true)
117
+ path = path.dup
118
+ if path.gsub!(@destination_stack[0], '.')
119
+ remove_dot ? (path[2..-1] || '') : path
120
+ else
121
+ path
122
+ end
123
+ end
124
+
125
+ # Holds source paths in instance so they can be manipulated.
126
+ #
127
+ def source_paths
128
+ @source_paths ||= self.class.source_paths_for_search
129
+ end
130
+
131
+ # Receives a file or directory and search for it in the source paths.
132
+ #
133
+ def find_in_source_paths(file)
134
+ relative_root = relative_to_original_destination_root(destination_root, false)
135
+
136
+ source_paths.each do |source|
137
+ source_file = File.expand_path(file, File.join(source, relative_root))
138
+ return source_file if File.exists?(source_file)
139
+ end
140
+
141
+ message = "Could not find #{file.inspect} in any of your source paths. "
142
+
143
+ unless self.class.source_root
144
+ message << "Please invoke #{self.class.name}.source_root(PATH) with the PATH containing your templates. "
145
+ end
146
+
147
+ if source_paths.empty?
148
+ message << "Currently you have no source paths."
149
+ else
150
+ message << "Your current source paths are: \n#{source_paths.join("\n")}"
151
+ end
152
+
153
+ raise Error, message
154
+ end
155
+
156
+ # Do something in the root or on a provided subfolder. If a relative path
157
+ # is given it's referenced from the current root. The full path is yielded
158
+ # to the block you provide. The path is set back to the previous path when
159
+ # the method exits.
160
+ #
161
+ # ==== Parameters
162
+ # dir<String>:: the directory to move to.
163
+ # config<Hash>:: give :verbose => true to log and use padding.
164
+ #
165
+ def inside(dir='', config={}, &block)
166
+ verbose = config.fetch(:verbose, false)
167
+ pretend = options[:pretend]
168
+
169
+ say_status :inside, dir, verbose
170
+ shell.padding += 1 if verbose
171
+ @destination_stack.push File.expand_path(dir, destination_root)
172
+
173
+ # If the directory doesnt exist and we're not pretending
174
+ if !File.exist?(destination_root) && !pretend
175
+ FileUtils.mkdir_p(destination_root)
176
+ end
177
+
178
+ if pretend
179
+ # In pretend mode, just yield down to the block
180
+ block.arity == 1 ? yield(destination_root) : yield
181
+ else
182
+ FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield }
183
+ end
184
+
185
+ @destination_stack.pop
186
+ shell.padding -= 1 if verbose
187
+ end
188
+
189
+ # Goes to the root and execute the given block.
190
+ #
191
+ def in_root
192
+ inside(@destination_stack.first) { yield }
193
+ end
194
+
195
+ # Loads an external file and execute it in the instance binding.
196
+ #
197
+ # ==== Parameters
198
+ # path<String>:: The path to the file to execute. Can be a web address or
199
+ # a relative path from the source root.
200
+ #
201
+ # ==== Examples
202
+ #
203
+ # apply "http://gist.github.com/103208"
204
+ #
205
+ # apply "recipes/jquery.rb"
206
+ #
207
+ def apply(path, config={})
208
+ verbose = config.fetch(:verbose, true)
209
+ is_uri = path =~ /^https?\:\/\//
210
+ path = find_in_source_paths(path) unless is_uri
211
+
212
+ say_status :apply, path, verbose
213
+ shell.padding += 1 if verbose
214
+
215
+ if is_uri
216
+ contents = open(path, "Accept" => "application/x-thor-template") {|io| io.read }
217
+ else
218
+ contents = open(path) {|io| io.read }
219
+ end
220
+
221
+ instance_eval(contents, path)
222
+ shell.padding -= 1 if verbose
223
+ end
224
+
225
+ # Executes a command returning the contents of the command.
226
+ #
227
+ # ==== Parameters
228
+ # command<String>:: the command to be executed.
229
+ # config<Hash>:: give :verbose => false to not log the status, :capture => true to hide to output. Specify :with
230
+ # to append an executable to command executation.
231
+ #
232
+ # ==== Example
233
+ #
234
+ # inside('vendor') do
235
+ # run('ln -s ~/edge rails')
236
+ # end
237
+ #
238
+ def run(command, config={})
239
+ return unless behavior == :invoke
240
+
241
+ destination = relative_to_original_destination_root(destination_root, false)
242
+ desc = "#{command} from #{destination.inspect}"
243
+
244
+ if config[:with]
245
+ desc = "#{File.basename(config[:with].to_s)} #{desc}"
246
+ command = "#{config[:with]} #{command}"
247
+ end
248
+
249
+ say_status :run, desc, config.fetch(:verbose, true)
250
+
251
+ unless options[:pretend]
252
+ config[:capture] ? `#{command}` : system("#{command}")
253
+ end
254
+ end
255
+
256
+ # Executes a ruby script (taking into account WIN32 platform quirks).
257
+ #
258
+ # ==== Parameters
259
+ # command<String>:: the command to be executed.
260
+ # config<Hash>:: give :verbose => false to not log the status.
261
+ #
262
+ def run_ruby_script(command, config={})
263
+ return unless behavior == :invoke
264
+ run command, config.merge(:with => Thor::Util.ruby_command)
265
+ end
266
+
267
+ # Run a thor command. A hash of options can be given and it's converted to
268
+ # switches.
269
+ #
270
+ # ==== Parameters
271
+ # command<String>:: the command to be invoked
272
+ # args<Array>:: arguments to the command
273
+ # config<Hash>:: give :verbose => false to not log the status, :capture => true to hide to output.
274
+ # Other options are given as parameter to Thor.
275
+ #
276
+ #
277
+ # ==== Examples
278
+ #
279
+ # thor :install, "http://gist.github.com/103208"
280
+ # #=> thor install http://gist.github.com/103208
281
+ #
282
+ # thor :list, :all => true, :substring => 'rails'
283
+ # #=> thor list --all --substring=rails
284
+ #
285
+ def thor(command, *args)
286
+ config = args.last.is_a?(Hash) ? args.pop : {}
287
+ verbose = config.key?(:verbose) ? config.delete(:verbose) : true
288
+ pretend = config.key?(:pretend) ? config.delete(:pretend) : false
289
+ capture = config.key?(:capture) ? config.delete(:capture) : false
290
+
291
+ args.unshift(command)
292
+ args.push Thor::Options.to_switches(config)
293
+ command = args.join(' ').strip
294
+
295
+ run command, :with => :thor, :verbose => verbose, :pretend => pretend, :capture => capture
296
+ end
297
+
298
+ protected
299
+
300
+ # Allow current root to be shared between invocations.
301
+ #
302
+ def _shared_configuration #:nodoc:
303
+ super.merge!(:destination_root => self.destination_root)
304
+ end
305
+
306
+ def _cleanup_options_and_set(options, key) #:nodoc:
307
+ case options
308
+ when Array
309
+ %w(--force -f --skip -s).each { |i| options.delete(i) }
310
+ options << "--#{key}"
311
+ when Hash
312
+ [:force, :skip, "force", "skip"].each { |i| options.delete(i) }
313
+ options.merge!(key => true)
314
+ end
315
+ end
316
+
317
+ end
318
+ end
@@ -0,0 +1,105 @@
1
+ require 'thor/actions/empty_directory'
2
+
3
+ class Thor
4
+ module Actions
5
+
6
+ # Create a new file relative to the destination root with the given data,
7
+ # which is the return value of a block or a data string.
8
+ #
9
+ # ==== Parameters
10
+ # destination<String>:: the relative path to the destination root.
11
+ # data<String|NilClass>:: the data to append to the file.
12
+ # config<Hash>:: give :verbose => false to not log the status.
13
+ #
14
+ # ==== Examples
15
+ #
16
+ # create_file "lib/fun_party.rb" do
17
+ # hostname = ask("What is the virtual hostname I should use?")
18
+ # "vhost.name = #{hostname}"
19
+ # end
20
+ #
21
+ # create_file "config/apache.conf", "your apache config"
22
+ #
23
+ def create_file(destination, *args, &block)
24
+ config = args.last.is_a?(Hash) ? args.pop : {}
25
+ data = args.first
26
+ action CreateFile.new(self, destination, block || data.to_s, config)
27
+ end
28
+ alias :add_file :create_file
29
+
30
+ # CreateFile is a subset of Template, which instead of rendering a file with
31
+ # ERB, it gets the content from the user.
32
+ #
33
+ class CreateFile < EmptyDirectory #:nodoc:
34
+ attr_reader :data
35
+
36
+ def initialize(base, destination, data, config={})
37
+ @data = data
38
+ super(base, destination, config)
39
+ end
40
+
41
+ # Checks if the content of the file at the destination is identical to the rendered result.
42
+ #
43
+ # ==== Returns
44
+ # Boolean:: true if it is identical, false otherwise.
45
+ #
46
+ def identical?
47
+ exists? && File.binread(destination) == render
48
+ end
49
+
50
+ # Holds the content to be added to the file.
51
+ #
52
+ def render
53
+ @render ||= if data.is_a?(Proc)
54
+ data.call
55
+ else
56
+ data
57
+ end
58
+ end
59
+
60
+ def invoke!
61
+ invoke_with_conflict_check do
62
+ FileUtils.mkdir_p(File.dirname(destination))
63
+ File.open(destination, 'wb') { |f| f.write render }
64
+ end
65
+ given_destination
66
+ end
67
+
68
+ protected
69
+
70
+ # Now on conflict we check if the file is identical or not.
71
+ #
72
+ def on_conflict_behavior(&block)
73
+ if identical?
74
+ say_status :identical, :blue
75
+ else
76
+ options = base.options.merge(config)
77
+ force_or_skip_or_conflict(options[:force], options[:skip], &block)
78
+ end
79
+ end
80
+
81
+ # If force is true, run the action, otherwise check if it's not being
82
+ # skipped. If both are false, show the file_collision menu, if the menu
83
+ # returns true, force it, otherwise skip.
84
+ #
85
+ def force_or_skip_or_conflict(force, skip, &block)
86
+ if force
87
+ say_status :force, :yellow
88
+ block.call unless pretend?
89
+ elsif skip
90
+ say_status :skip, :yellow
91
+ else
92
+ say_status :conflict, :red
93
+ force_or_skip_or_conflict(force_on_collision?, true, &block)
94
+ end
95
+ end
96
+
97
+ # Shows the file collision menu to the user and gets the result.
98
+ #
99
+ def force_on_collision?
100
+ base.shell.file_collision(destination){ render }
101
+ end
102
+
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,60 @@
1
+ require 'thor/actions/create_file'
2
+
3
+ class Thor
4
+ module Actions
5
+
6
+ # Create a new file relative to the destination root from the given source.
7
+ #
8
+ # ==== Parameters
9
+ # destination<String>:: the relative path to the destination root.
10
+ # source<String|NilClass>:: the relative path to the source root.
11
+ # config<Hash>:: give :verbose => false to not log the status.
12
+ # :: give :symbolic => false for hard link.
13
+ #
14
+ # ==== Examples
15
+ #
16
+ # create_link "config/apache.conf", "/etc/apache.conf"
17
+ #
18
+ def create_link(destination, *args, &block)
19
+ config = args.last.is_a?(Hash) ? args.pop : {}
20
+ source = args.first
21
+ action CreateLink.new(self, destination, source, config)
22
+ end
23
+ alias :add_link :create_link
24
+
25
+ # CreateLink is a subset of CreateFile, which instead of taking a block of
26
+ # data, just takes a source string from the user.
27
+ #
28
+ class CreateLink < CreateFile #:nodoc:
29
+ attr_reader :data
30
+
31
+ # Checks if the content of the file at the destination is identical to the rendered result.
32
+ #
33
+ # ==== Returns
34
+ # Boolean:: true if it is identical, false otherwise.
35
+ #
36
+ def identical?
37
+ exists? && File.identical?(render, destination)
38
+ end
39
+
40
+ def invoke!
41
+ invoke_with_conflict_check do
42
+ FileUtils.mkdir_p(File.dirname(destination))
43
+ # Create a symlink by default
44
+ config[:symbolic] = true if config[:symbolic].nil?
45
+ File.unlink(destination) if exists?
46
+ if config[:symbolic]
47
+ File.symlink(render, destination)
48
+ else
49
+ File.link(render, destination)
50
+ end
51
+ end
52
+ given_destination
53
+ end
54
+
55
+ def exists?
56
+ super || File.symlink?(destination)
57
+ end
58
+ end
59
+ end
60
+ end