atli 0.1.2

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