angry_mob 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (125) hide show
  1. data/LICENSE +21 -0
  2. data/README.md +123 -0
  3. data/bin/mob +139 -0
  4. data/lib/angry_mob.rb +28 -0
  5. data/lib/angry_mob/act.rb +111 -0
  6. data/lib/angry_mob/act/scheduler.rb +143 -0
  7. data/lib/angry_mob/action.rb +11 -0
  8. data/lib/angry_mob/builder.rb +115 -0
  9. data/lib/angry_mob/extend.rb +10 -0
  10. data/lib/angry_mob/extend/array.rb +30 -0
  11. data/lib/angry_mob/extend/blank.rb +108 -0
  12. data/lib/angry_mob/extend/blankslate.rb +109 -0
  13. data/lib/angry_mob/extend/dictionary.rb +140 -0
  14. data/lib/angry_mob/extend/hash.rb +67 -0
  15. data/lib/angry_mob/extend/object.rb +21 -0
  16. data/lib/angry_mob/extend/pathname.rb +23 -0
  17. data/lib/angry_mob/extend/string.rb +8 -0
  18. data/lib/angry_mob/log.rb +28 -0
  19. data/lib/angry_mob/mob.rb +77 -0
  20. data/lib/angry_mob/mob_loader.rb +115 -0
  21. data/lib/angry_mob/node.rb +44 -0
  22. data/lib/angry_mob/notifier.rb +76 -0
  23. data/lib/angry_mob/target.rb +257 -0
  24. data/lib/angry_mob/target/arguments.rb +71 -0
  25. data/lib/angry_mob/target/call.rb +57 -0
  26. data/lib/angry_mob/target/default_resource_locator.rb +11 -0
  27. data/lib/angry_mob/target/defaults.rb +23 -0
  28. data/lib/angry_mob/target/mother.rb +66 -0
  29. data/lib/angry_mob/target/notify.rb +57 -0
  30. data/lib/angry_mob/target/tracking.rb +96 -0
  31. data/lib/angry_mob/ui.rb +247 -0
  32. data/lib/angry_mob/util.rb +11 -0
  33. data/lib/angry_mob/vendored.rb +8 -0
  34. data/lib/angry_mob/version.rb +3 -0
  35. data/vendor/angry_hash/Rakefile +17 -0
  36. data/vendor/angry_hash/VERSION +1 -0
  37. data/vendor/angry_hash/angry_hash.gemspec +47 -0
  38. data/vendor/angry_hash/examples/accessors_eg.rb +46 -0
  39. data/vendor/angry_hash/examples/creation_eg.rb +43 -0
  40. data/vendor/angry_hash/examples/dsl.eg.rb +18 -0
  41. data/vendor/angry_hash/examples/dup_eg.rb +86 -0
  42. data/vendor/angry_hash/examples/eg_helper.rb +24 -0
  43. data/vendor/angry_hash/examples/merge_eg.rb +135 -0
  44. data/vendor/angry_hash/lib/angry_hash.rb +215 -0
  45. data/vendor/angry_hash/lib/angry_hash/dsl.rb +44 -0
  46. data/vendor/angry_hash/lib/angry_hash/extension_tracking.rb +12 -0
  47. data/vendor/angry_hash/lib/angry_hash/merge_string.rb +58 -0
  48. data/vendor/json/COPYING +58 -0
  49. data/vendor/json/GPL +340 -0
  50. data/vendor/json/README +360 -0
  51. data/vendor/json/lib/json/common.rb +371 -0
  52. data/vendor/json/lib/json/pure.rb +77 -0
  53. data/vendor/json/lib/json/pure/generator.rb +443 -0
  54. data/vendor/json/lib/json/pure/parser.rb +303 -0
  55. data/vendor/json/lib/json/version.rb +8 -0
  56. data/vendor/thor/CHANGELOG.rdoc +89 -0
  57. data/vendor/thor/LICENSE +20 -0
  58. data/vendor/thor/README.rdoc +297 -0
  59. data/vendor/thor/Thorfile +69 -0
  60. data/vendor/thor/bin/rake2thor +86 -0
  61. data/vendor/thor/bin/thor +6 -0
  62. data/vendor/thor/lib/thor.rb +244 -0
  63. data/vendor/thor/lib/thor/actions.rb +275 -0
  64. data/vendor/thor/lib/thor/actions/create_file.rb +103 -0
  65. data/vendor/thor/lib/thor/actions/directory.rb +91 -0
  66. data/vendor/thor/lib/thor/actions/empty_directory.rb +134 -0
  67. data/vendor/thor/lib/thor/actions/file_manipulation.rb +223 -0
  68. data/vendor/thor/lib/thor/actions/inject_into_file.rb +104 -0
  69. data/vendor/thor/lib/thor/base.rb +540 -0
  70. data/vendor/thor/lib/thor/core_ext/file_binary_read.rb +9 -0
  71. data/vendor/thor/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  72. data/vendor/thor/lib/thor/core_ext/ordered_hash.rb +100 -0
  73. data/vendor/thor/lib/thor/error.rb +30 -0
  74. data/vendor/thor/lib/thor/group.rb +271 -0
  75. data/vendor/thor/lib/thor/invocation.rb +180 -0
  76. data/vendor/thor/lib/thor/parser.rb +4 -0
  77. data/vendor/thor/lib/thor/parser/argument.rb +67 -0
  78. data/vendor/thor/lib/thor/parser/arguments.rb +150 -0
  79. data/vendor/thor/lib/thor/parser/option.rb +128 -0
  80. data/vendor/thor/lib/thor/parser/options.rb +169 -0
  81. data/vendor/thor/lib/thor/rake_compat.rb +66 -0
  82. data/vendor/thor/lib/thor/runner.rb +314 -0
  83. data/vendor/thor/lib/thor/shell.rb +83 -0
  84. data/vendor/thor/lib/thor/shell/basic.rb +239 -0
  85. data/vendor/thor/lib/thor/shell/color.rb +108 -0
  86. data/vendor/thor/lib/thor/task.rb +102 -0
  87. data/vendor/thor/lib/thor/util.rb +224 -0
  88. data/vendor/thor/lib/thor/version.rb +3 -0
  89. data/vendor/thor/spec/actions/create_file_spec.rb +170 -0
  90. data/vendor/thor/spec/actions/directory_spec.rb +131 -0
  91. data/vendor/thor/spec/actions/empty_directory_spec.rb +91 -0
  92. data/vendor/thor/spec/actions/file_manipulation_spec.rb +271 -0
  93. data/vendor/thor/spec/actions/inject_into_file_spec.rb +135 -0
  94. data/vendor/thor/spec/actions_spec.rb +292 -0
  95. data/vendor/thor/spec/base_spec.rb +263 -0
  96. data/vendor/thor/spec/core_ext/hash_with_indifferent_access_spec.rb +43 -0
  97. data/vendor/thor/spec/core_ext/ordered_hash_spec.rb +115 -0
  98. data/vendor/thor/spec/fixtures/application.rb +2 -0
  99. data/vendor/thor/spec/fixtures/bundle/execute.rb +6 -0
  100. data/vendor/thor/spec/fixtures/bundle/main.thor +1 -0
  101. data/vendor/thor/spec/fixtures/doc/%file_name%.rb.tt +1 -0
  102. data/vendor/thor/spec/fixtures/doc/README +3 -0
  103. data/vendor/thor/spec/fixtures/doc/config.rb +1 -0
  104. data/vendor/thor/spec/fixtures/group.thor +90 -0
  105. data/vendor/thor/spec/fixtures/invoke.thor +112 -0
  106. data/vendor/thor/spec/fixtures/script.thor +145 -0
  107. data/vendor/thor/spec/fixtures/task.thor +10 -0
  108. data/vendor/thor/spec/group_spec.rb +171 -0
  109. data/vendor/thor/spec/invocation_spec.rb +107 -0
  110. data/vendor/thor/spec/parser/argument_spec.rb +47 -0
  111. data/vendor/thor/spec/parser/arguments_spec.rb +64 -0
  112. data/vendor/thor/spec/parser/option_spec.rb +202 -0
  113. data/vendor/thor/spec/parser/options_spec.rb +292 -0
  114. data/vendor/thor/spec/rake_compat_spec.rb +68 -0
  115. data/vendor/thor/spec/runner_spec.rb +210 -0
  116. data/vendor/thor/spec/shell/basic_spec.rb +205 -0
  117. data/vendor/thor/spec/shell/color_spec.rb +41 -0
  118. data/vendor/thor/spec/shell_spec.rb +34 -0
  119. data/vendor/thor/spec/spec.opts +1 -0
  120. data/vendor/thor/spec/spec_helper.rb +54 -0
  121. data/vendor/thor/spec/task_spec.rb +69 -0
  122. data/vendor/thor/spec/thor_spec.rb +237 -0
  123. data/vendor/thor/spec/util_spec.rb +163 -0
  124. data/vendor/thor/thor.gemspec +120 -0
  125. metadata +199 -0
@@ -0,0 +1,69 @@
1
+ # enconding: utf-8
2
+
3
+ require File.join(File.dirname(__FILE__), "lib", "thor", "version")
4
+ require 'rubygems'
5
+ require 'thor/rake_compat'
6
+ require 'spec/rake/spectask'
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ end
11
+
12
+ GEM_NAME = 'thor'
13
+ EXTRA_RDOC_FILES = ["README.rdoc", "LICENSE", "CHANGELOG.rdoc", "VERSION", "Thorfile"]
14
+
15
+ class Default < Thor
16
+ include Thor::RakeCompat
17
+
18
+ Spec::Rake::SpecTask.new(:spec) do |t|
19
+ t.libs << 'lib'
20
+ t.spec_opts = ['--options', "spec/spec.opts"]
21
+ t.spec_files = FileList['spec/**/*_spec.rb']
22
+ end
23
+
24
+ Spec::Rake::SpecTask.new(:rcov) do |t|
25
+ t.libs << 'lib'
26
+ t.spec_opts = ['--options', "spec/spec.opts"]
27
+ t.spec_files = FileList['spec/**/*_spec.rb']
28
+ t.rcov = true
29
+ t.rcov_dir = "rcov"
30
+ end
31
+
32
+ if defined?(RDoc)
33
+ RDoc::Task.new do |rdoc|
34
+ rdoc.main = "README.rdoc"
35
+ rdoc.rdoc_dir = "rdoc"
36
+ rdoc.title = GEM_NAME
37
+ rdoc.rdoc_files.include(*EXTRA_RDOC_FILES)
38
+ rdoc.rdoc_files.include('lib/**/*.rb')
39
+ rdoc.options << '--line-numbers' << '--inline-source'
40
+ end
41
+ end
42
+
43
+ begin
44
+ require 'jeweler'
45
+ Jeweler::Tasks.new do |s|
46
+ s.name = GEM_NAME
47
+ s.version = Thor::VERSION
48
+ s.rubyforge_project = "textmate"
49
+ s.platform = Gem::Platform::RUBY
50
+ s.summary = "A scripting framework that replaces rake, sake and rubigen"
51
+ s.email = "ruby-thor@googlegroups.com"
52
+ s.homepage = "http://yehudakatz.com"
53
+ s.description = "A scripting framework that replaces rake, sake and rubigen"
54
+ s.authors = ['Yehuda Katz', 'José Valim']
55
+ s.has_rdoc = true
56
+ s.extra_rdoc_files = EXTRA_RDOC_FILES
57
+ s.require_path = 'lib'
58
+ s.bindir = "bin"
59
+ s.executables = %w( thor rake2thor )
60
+ s.files = s.extra_rdoc_files + Dir.glob("{bin,lib}/**/*")
61
+ s.test_files.include 'spec/**/*'
62
+ s.test_files.exclude 'spec/sandbox/**/*'
63
+ end
64
+
65
+ Jeweler::GemcutterTasks.new
66
+ rescue LoadError
67
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler"
68
+ end
69
+ end
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ruby2ruby'
4
+ require 'parse_tree'
5
+ if Ruby2Ruby::VERSION >= "1.2.0"
6
+ require 'parse_tree_extensions'
7
+ end
8
+ require 'rake'
9
+
10
+ input = ARGV[0] || 'Rakefile'
11
+ output = ARGV[1] || 'Thorfile'
12
+
13
+ $requires = []
14
+
15
+ module Kernel
16
+ def require_with_record(file)
17
+ $requires << file if caller[1] =~ /rake2thor:/
18
+ require_without_record file
19
+ end
20
+ alias_method :require_without_record, :require
21
+ alias_method :require, :require_with_record
22
+ end
23
+
24
+ load input
25
+
26
+ @private_methods = []
27
+
28
+ def file_task_name(name)
29
+ "compile_" + name.gsub('/', '_slash_').gsub('.', '_dot_').gsub(/\W/, '_')
30
+ end
31
+
32
+ def method_for_task(task)
33
+ file_task = task.is_a?(Rake::FileTask)
34
+ comment = task.instance_variable_get('@comment')
35
+ prereqs = task.instance_variable_get('@prerequisites').select(&Rake::Task.method(:task_defined?))
36
+ actions = task.instance_variable_get('@actions')
37
+ name = task.name.gsub(/^([^:]+:)+/, '')
38
+ name = file_task_name(name) if file_task
39
+ meth = ''
40
+
41
+ meth << "desc #{name.inspect}, #{comment.inspect}\n" if comment
42
+ meth << "def #{name}\n"
43
+
44
+ meth << prereqs.map do |pre|
45
+ pre = pre.to_s
46
+ pre = file_task_name(pre) if Rake::Task[pre].is_a?(Rake::FileTask)
47
+ ' ' + pre
48
+ end.join("\n")
49
+
50
+ meth << "\n\n" unless prereqs.empty? || actions.empty?
51
+
52
+ meth << actions.map do |act|
53
+ act = act.to_ruby
54
+ unless act.gsub!(/^proc \{ \|(\w+)\|\n/,
55
+ " \\1 = Struct.new(:name).new(#{name.inspect}) # A crude mock Rake::Task object\n")
56
+ act.gsub!(/^proc \{\n/, '')
57
+ end
58
+ act.gsub(/\n\}$/, '')
59
+ end.join("\n")
60
+
61
+ meth << "\nend"
62
+
63
+ if file_task
64
+ @private_methods << meth
65
+ return
66
+ end
67
+
68
+ meth
69
+ end
70
+
71
+ body = Rake::Task.tasks.map(&method(:method_for_task)).compact.map { |meth| meth.gsub(/^/, ' ') }.join("\n\n")
72
+
73
+ unless @private_methods.empty?
74
+ body << "\n\n private\n\n"
75
+ body << @private_methods.map { |meth| meth.gsub(/^/, ' ') }.join("\n\n")
76
+ end
77
+
78
+ requires = $requires.map { |r| "require #{r.inspect}" }.join("\n")
79
+
80
+ File.open(output, 'w') { |f| f.write(<<END.lstrip) }
81
+ #{requires}
82
+
83
+ class Default < Thor
84
+ #{body}
85
+ end
86
+ END
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby -*-
3
+
4
+ require 'thor/runner'
5
+ $thor_runner = true
6
+ Thor::Runner.start
@@ -0,0 +1,244 @@
1
+ require 'thor/base'
2
+
3
+ # TODO: Update thor to allow for git-style CLI (git bisect run)
4
+ class Thor
5
+ class << self
6
+ # Sets the default task when thor is executed without an explicit task to be called.
7
+ #
8
+ # ==== Parameters
9
+ # meth<Symbol>:: name of the defaut task
10
+ #
11
+ def default_task(meth=nil)
12
+ case meth
13
+ when :none
14
+ @default_task = 'help'
15
+ when nil
16
+ @default_task ||= from_superclass(:default_task, 'help')
17
+ else
18
+ @default_task = meth.to_s
19
+ end
20
+ end
21
+
22
+ # Defines the usage and the description of the next task.
23
+ #
24
+ # ==== Parameters
25
+ # usage<String>
26
+ # description<String>
27
+ #
28
+ def desc(usage, description, options={})
29
+ if options[:for]
30
+ task = find_and_refresh_task(options[:for])
31
+ task.usage = usage if usage
32
+ task.description = description if description
33
+ else
34
+ @usage, @desc = usage, description
35
+ end
36
+ end
37
+
38
+ # Maps an input to a task. If you define:
39
+ #
40
+ # map "-T" => "list"
41
+ #
42
+ # Running:
43
+ #
44
+ # thor -T
45
+ #
46
+ # Will invoke the list task.
47
+ #
48
+ # ==== Parameters
49
+ # Hash[String|Array => Symbol]:: Maps the string or the strings in the array to the given task.
50
+ #
51
+ def map(mappings=nil)
52
+ @map ||= from_superclass(:map, {})
53
+
54
+ if mappings
55
+ mappings.each do |key, value|
56
+ if key.respond_to?(:each)
57
+ key.each {|subkey| @map[subkey] = value}
58
+ else
59
+ @map[key] = value
60
+ end
61
+ end
62
+ end
63
+
64
+ @map
65
+ end
66
+
67
+ # Declares the options for the next task to be declared.
68
+ #
69
+ # ==== Parameters
70
+ # Hash[Symbol => Object]:: The hash key is the name of the option and the value
71
+ # is the type of the option. Can be :string, :array, :hash, :boolean, :numeric
72
+ # or :required (string). If you give a value, the type of the value is used.
73
+ #
74
+ def method_options(options=nil)
75
+ @method_options ||= {}
76
+ build_options(options, @method_options) if options
77
+ @method_options
78
+ end
79
+
80
+ # Adds an option to the set of method options. If :for is given as option,
81
+ # it allows you to change the options from a previous defined task.
82
+ #
83
+ # def previous_task
84
+ # # magic
85
+ # end
86
+ #
87
+ # method_option :foo => :bar, :for => :previous_task
88
+ #
89
+ # def next_task
90
+ # # magic
91
+ # end
92
+ #
93
+ # ==== Parameters
94
+ # name<Symbol>:: The name of the argument.
95
+ # options<Hash>:: Described below.
96
+ #
97
+ # ==== Options
98
+ # :desc - Description for the argument.
99
+ # :required - If the argument is required or not.
100
+ # :default - Default value for this argument. It cannot be required and have default values.
101
+ # :aliases - Aliases for this option.
102
+ # :type - The type of the argument, can be :string, :hash, :array, :numeric or :boolean.
103
+ # :banner - String to show on usage notes.
104
+ #
105
+ def method_option(name, options={})
106
+ scope = if options[:for]
107
+ find_and_refresh_task(options[:for]).options
108
+ else
109
+ method_options
110
+ end
111
+
112
+ build_option(name, options, scope)
113
+ end
114
+
115
+ # Parses the task and options from the given args, instantiate the class
116
+ # and invoke the task. This method is used when the arguments must be parsed
117
+ # from an array. If you are inside Ruby and want to use a Thor class, you
118
+ # can simply initialize it:
119
+ #
120
+ # script = MyScript.new(args, options, config)
121
+ # script.invoke(:task, first_arg, second_arg, third_arg)
122
+ #
123
+ def start(original_args=ARGV, config={})
124
+ super do |given_args|
125
+ meth = normalize_task_name(given_args.shift)
126
+ task = all_tasks[meth]
127
+
128
+ if task
129
+ args, opts = Thor::Options.split(given_args)
130
+ config.merge!(:task_options => task.options)
131
+ else
132
+ args, opts = given_args, {}
133
+ end
134
+
135
+ task ||= Thor::Task::Dynamic.new(meth)
136
+ trailing = args[Range.new(arguments.size, -1)]
137
+ new(args, opts, config).invoke(task, trailing || [])
138
+ end
139
+ end
140
+
141
+ # Prints help information for the given task.
142
+ #
143
+ # ==== Parameters
144
+ # shell<Thor::Shell>
145
+ # task_name<String>
146
+ #
147
+ def task_help(shell, task_name)
148
+ meth = normalize_task_name(task_name)
149
+ task = all_tasks[meth]
150
+ handle_no_task_error(meth) unless task
151
+
152
+ shell.say "Usage:"
153
+ shell.say " #{banner(task)}"
154
+ shell.say
155
+ class_options_help(shell, nil => task.options.map { |_, o| o })
156
+ shell.say task.description
157
+ end
158
+
159
+ # Prints help information for this class.
160
+ #
161
+ # ==== Parameters
162
+ # shell<Thor::Shell>
163
+ #
164
+ def help(shell)
165
+ list = printable_tasks
166
+ Thor::Util.thor_classes_in(self).each do |klass|
167
+ list += klass.printable_tasks(false)
168
+ end
169
+ list.sort!{ |a,b| a[0] <=> b[0] }
170
+
171
+ shell.say "Tasks:"
172
+ shell.print_table(list, :ident => 2, :truncate => true)
173
+ shell.say
174
+ class_options_help(shell)
175
+ end
176
+
177
+ # Returns tasks ready to be printed.
178
+ def printable_tasks(all=true)
179
+ (all ? all_tasks : tasks).map do |_, task|
180
+ item = []
181
+ item << banner(task)
182
+ item << (task.description ? "# #{task.description.gsub(/\s+/m,' ')}" : "")
183
+ item
184
+ end
185
+ end
186
+
187
+ def handle_argument_error(task, error) #:nodoc:
188
+ raise InvocationError, "#{task.name.inspect} was called incorrectly. Call as #{task.formatted_usage(self, banner_base == "thor").inspect}."
189
+ end
190
+
191
+ protected
192
+
193
+ # The banner for this class. You can customize it if you are invoking the
194
+ # thor class by another ways which is not the Thor::Runner. It receives
195
+ # the task that is going to be invoked and a boolean which indicates if
196
+ # the namespace should be displayed as arguments.
197
+ #
198
+ def banner(task)
199
+ "#{banner_base} #{task.formatted_usage(self, banner_base == "thor")}"
200
+ end
201
+
202
+ def baseclass #:nodoc:
203
+ Thor
204
+ end
205
+
206
+ def create_task(meth) #:nodoc:
207
+ if @usage && @desc
208
+ tasks[meth.to_s] = Thor::Task.new(meth, @desc, @usage, method_options)
209
+ @usage, @desc, @method_options = nil
210
+ true
211
+ elsif self.all_tasks[meth.to_s] || meth.to_sym == :method_missing
212
+ true
213
+ else
214
+ puts "[WARNING] Attempted to create task #{meth.inspect} without usage or description. " <<
215
+ "Call desc if you want this method to be available as task or declare it inside a " <<
216
+ "no_tasks{} block. Invoked from #{caller[1].inspect}."
217
+ false
218
+ end
219
+ end
220
+
221
+ def initialize_added #:nodoc:
222
+ class_options.merge!(method_options)
223
+ @method_options = nil
224
+ end
225
+
226
+ # Receives a task name (can be nil), and try to get a map from it.
227
+ # If a map can't be found use the sent name or the default task.
228
+ #
229
+ def normalize_task_name(meth) #:nodoc:
230
+ mapping = map[meth.to_s]
231
+ meth = mapping || meth || default_task
232
+ meth.to_s.gsub('-','_') # treat foo-bar > foo_bar
233
+ end
234
+ end
235
+
236
+ include Thor::Base
237
+
238
+ map HELP_MAPPINGS => :help
239
+
240
+ desc "help [TASK]", "Describe available tasks or one specific task"
241
+ def help(task=nil)
242
+ task ? self.class.task_help(shell, task) : self.class.help(shell)
243
+ end
244
+ end
@@ -0,0 +1,275 @@
1
+ require 'fileutils'
2
+ require 'thor/core_ext/file_binary_read'
3
+
4
+ Dir[File.join(File.dirname(__FILE__), "actions", "*.rb")].each do |action|
5
+ require action
6
+ end
7
+
8
+ class Thor
9
+ module Actions
10
+ attr_accessor :behavior
11
+
12
+ def self.included(base) #:nodoc:
13
+ base.extend ClassMethods
14
+ end
15
+
16
+ module ClassMethods
17
+ # Hold source paths for one Thor instance. source_paths_for_search is the
18
+ # method responsible to gather source_paths from this current class,
19
+ # inherited paths and the source root.
20
+ #
21
+ def source_paths
22
+ @source_paths ||= []
23
+ end
24
+
25
+ # Returns the source paths in the following order:
26
+ #
27
+ # 1) This class source paths
28
+ # 2) Source root
29
+ # 3) Parents source paths
30
+ #
31
+ def source_paths_for_search
32
+ paths = []
33
+ paths += self.source_paths
34
+ paths << self.source_root if self.respond_to?(:source_root)
35
+ paths += from_superclass(:source_paths, [])
36
+ paths
37
+ end
38
+
39
+ # Add runtime options that help actions execution.
40
+ #
41
+ def add_runtime_options!
42
+ class_option :force, :type => :boolean, :aliases => "-f", :group => :runtime,
43
+ :desc => "Overwrite files that already exist"
44
+
45
+ class_option :pretend, :type => :boolean, :aliases => "-p", :group => :runtime,
46
+ :desc => "Run but do not make any changes"
47
+
48
+ class_option :quiet, :type => :boolean, :aliases => "-q", :group => :runtime,
49
+ :desc => "Supress status output"
50
+
51
+ class_option :skip, :type => :boolean, :aliases => "-s", :group => :runtime,
52
+ :desc => "Skip files that already exist"
53
+ end
54
+ end
55
+
56
+ # Extends initializer to add more configuration options.
57
+ #
58
+ # ==== Configuration
59
+ # behavior<Symbol>:: The actions default behavior. Can be :invoke or :revoke.
60
+ # It also accepts :force, :skip and :pretend to set the behavior
61
+ # and the respective option.
62
+ #
63
+ # destination_root<String>:: The root directory needed for some actions.
64
+ #
65
+ def initialize(args=[], options={}, config={})
66
+ self.behavior = case config[:behavior].to_s
67
+ when "force", "skip"
68
+ _cleanup_options_and_set(options, config[:behavior])
69
+ :invoke
70
+ when "revoke"
71
+ :revoke
72
+ else
73
+ :invoke
74
+ end
75
+
76
+ super
77
+ self.destination_root = config[:destination_root]
78
+ end
79
+
80
+ # Wraps an action object and call it accordingly to the thor class behavior.
81
+ #
82
+ def action(instance) #:nodoc:
83
+ if behavior == :revoke
84
+ instance.revoke!
85
+ else
86
+ instance.invoke!
87
+ end
88
+ end
89
+
90
+ # Returns the root for this thor class (also aliased as destination root).
91
+ #
92
+ def destination_root
93
+ @destination_stack.last
94
+ end
95
+
96
+ # Sets the root for this thor class. Relatives path are added to the
97
+ # directory where the script was invoked and expanded.
98
+ #
99
+ def destination_root=(root)
100
+ @destination_stack ||= []
101
+ @destination_stack[0] = File.expand_path(root || '')
102
+ end
103
+
104
+ # Returns the given path relative to the absolute root (ie, root where
105
+ # the script started).
106
+ #
107
+ def relative_to_original_destination_root(path, remove_dot=true)
108
+ path = path.gsub(@destination_stack[0], '.')
109
+ remove_dot ? (path[2..-1] || '') : path
110
+ end
111
+
112
+ # Holds source paths in instance so they can be manipulated.
113
+ #
114
+ def source_paths
115
+ @source_paths ||= self.class.source_paths_for_search
116
+ end
117
+
118
+ # Receives a file or directory and search for it in the source paths.
119
+ #
120
+ def find_in_source_paths(file)
121
+ relative_root = relative_to_original_destination_root(destination_root, false)
122
+
123
+ source_paths.each do |source|
124
+ source_file = File.expand_path(file, File.join(source, relative_root))
125
+ return source_file if File.exists?(source_file)
126
+ end
127
+
128
+ if source_paths.empty?
129
+ raise Error, "You don't have any source path defined for class #{self.class.name}. To fix this, " <<
130
+ "you can define a source_root in your class."
131
+ else
132
+ raise Error, "Could not find #{file.inspect} in source paths: \n#{source_paths.join("\n")}"
133
+ end
134
+ end
135
+
136
+ # Do something in the root or on a provided subfolder. If a relative path
137
+ # is given it's referenced from the current root. The full path is yielded
138
+ # to the block you provide. The path is set back to the previous path when
139
+ # the method exits.
140
+ #
141
+ # ==== Parameters
142
+ # dir<String>:: the directory to move to.
143
+ # config<Hash>:: give :verbose => true to log and use padding.
144
+ #
145
+ def inside(dir='', config={}, &block)
146
+ verbose = config.fetch(:verbose, false)
147
+
148
+ say_status :inside, dir, verbose
149
+ shell.padding += 1 if verbose
150
+ @destination_stack.push File.expand_path(dir, destination_root)
151
+
152
+ FileUtils.mkdir_p(destination_root) unless File.exist?(destination_root)
153
+ FileUtils.cd(destination_root) { block.arity == 1 ? yield(destination_root) : yield }
154
+
155
+ @destination_stack.pop
156
+ shell.padding -= 1 if verbose
157
+ end
158
+
159
+ # Goes to the root and execute the given block.
160
+ #
161
+ def in_root
162
+ inside(@destination_stack.first) { yield }
163
+ end
164
+
165
+ # Loads an external file and execute it in the instance binding.
166
+ #
167
+ # ==== Parameters
168
+ # path<String>:: The path to the file to execute. Can be a web address or
169
+ # a relative path from the source root.
170
+ #
171
+ # ==== Examples
172
+ #
173
+ # apply "http://gist.github.com/103208"
174
+ #
175
+ # apply "recipes/jquery.rb"
176
+ #
177
+ def apply(path, config={})
178
+ verbose = config.fetch(:verbose, true)
179
+ path = find_in_source_paths(path) unless path =~ /^http\:\/\//
180
+
181
+ say_status :apply, path, verbose
182
+ shell.padding += 1 if verbose
183
+ instance_eval(open(path).read, path)
184
+ shell.padding -= 1 if verbose
185
+ end
186
+
187
+ # Executes a command returning the contents of the command.
188
+ #
189
+ # ==== Parameters
190
+ # command<String>:: the command to be executed.
191
+ # config<Hash>:: give :verbose => false to not log the status. Specify :with
192
+ # to append an executable to command executation.
193
+ #
194
+ # ==== Example
195
+ #
196
+ # inside('vendor') do
197
+ # run('ln -s ~/edge rails')
198
+ # end
199
+ #
200
+ def run(command, config={})
201
+ return unless behavior == :invoke
202
+
203
+ destination = relative_to_original_destination_root(destination_root, false)
204
+ desc = "#{command} from #{destination.inspect}"
205
+
206
+ if config[:with]
207
+ desc = "#{File.basename(config[:with].to_s)} #{desc}"
208
+ command = "#{config[:with]} #{command}"
209
+ end
210
+
211
+ say_status :run, desc, config.fetch(:verbose, true)
212
+ `#{command}` unless options[:pretend]
213
+ end
214
+
215
+ # Executes a ruby script (taking into account WIN32 platform quirks).
216
+ #
217
+ # ==== Parameters
218
+ # command<String>:: the command to be executed.
219
+ # config<Hash>:: give :verbose => false to not log the status.
220
+ #
221
+ def run_ruby_script(command, config={})
222
+ return unless behavior == :invoke
223
+ run command, config.merge(:with => Thor::Util.ruby_command)
224
+ end
225
+
226
+ # Run a thor command. A hash of options can be given and it's converted to
227
+ # switches.
228
+ #
229
+ # ==== Parameters
230
+ # task<String>:: the task to be invoked
231
+ # args<Array>:: arguments to the task
232
+ # config<Hash>:: give :verbose => false to not log the status. Other options
233
+ # are given as parameter to Thor.
234
+ #
235
+ # ==== Examples
236
+ #
237
+ # thor :install, "http://gist.github.com/103208"
238
+ # #=> thor install http://gist.github.com/103208
239
+ #
240
+ # thor :list, :all => true, :substring => 'rails'
241
+ # #=> thor list --all --substring=rails
242
+ #
243
+ def thor(task, *args)
244
+ config = args.last.is_a?(Hash) ? args.pop : {}
245
+ verbose = config.key?(:verbose) ? config.delete(:verbose) : true
246
+ pretend = config.key?(:pretend) ? config.delete(:pretend) : false
247
+
248
+ args.unshift task
249
+ args.push Thor::Options.to_switches(config)
250
+ command = args.join(' ').strip
251
+
252
+ run command, :with => :thor, :verbose => verbose, :pretend => pretend
253
+ end
254
+
255
+ protected
256
+
257
+ # Allow current root to be shared between invocations.
258
+ #
259
+ def _shared_configuration #:nodoc:
260
+ super.merge!(:destination_root => self.destination_root)
261
+ end
262
+
263
+ def _cleanup_options_and_set(options, key) #:nodoc:
264
+ case options
265
+ when Array
266
+ %w(--force -f --skip -s).each { |i| options.delete(i) }
267
+ options << "--#{key}"
268
+ when Hash
269
+ [:force, :skip, "force", "skip"].each { |i| options.delete(i) }
270
+ options.merge!(key => true)
271
+ end
272
+ end
273
+
274
+ end
275
+ end