daemon-kit 0.1.7.12 → 0.1.8pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. data/History.txt +7 -0
  2. data/Manifest.txt +4 -0
  3. data/README.rdoc +11 -9
  4. data/Rakefile +0 -1
  5. data/TODO.txt +1 -2
  6. data/bin/daemon-kit +11 -11
  7. data/daemon-kit.gemspec +109 -78
  8. data/lib/daemon_kit.rb +7 -10
  9. data/lib/daemon_kit/abstract_logger.rb +22 -14
  10. data/lib/daemon_kit/application.rb +0 -1
  11. data/lib/daemon_kit/arguments.rb +1 -6
  12. data/lib/daemon_kit/commands/destroy.rb +10 -0
  13. data/lib/daemon_kit/commands/generate.rb +10 -0
  14. data/lib/daemon_kit/config.rb +7 -2
  15. data/lib/daemon_kit/generators.rb +67 -0
  16. data/lib/daemon_kit/generators/base.rb +60 -0
  17. data/lib/daemon_kit/initializer.rb +1 -1
  18. data/lib/daemon_kit/jabber.rb +1 -0
  19. data/lib/daemon_kit/tasks/framework.rake +6 -1
  20. data/lib/daemon_kit/vendor/thor-0.12.3/CHANGELOG.rdoc +80 -0
  21. data/lib/daemon_kit/vendor/thor-0.12.3/LICENSE +20 -0
  22. data/lib/daemon_kit/vendor/thor-0.12.3/README.rdoc +234 -0
  23. data/lib/daemon_kit/vendor/thor-0.12.3/Thorfile +64 -0
  24. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor.rb +242 -0
  25. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/actions.rb +274 -0
  26. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/actions/create_file.rb +103 -0
  27. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/actions/directory.rb +91 -0
  28. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/actions/empty_directory.rb +134 -0
  29. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/actions/file_manipulation.rb +223 -0
  30. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/actions/inject_into_file.rb +101 -0
  31. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/base.rb +515 -0
  32. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/core_ext/file_binary_read.rb +9 -0
  33. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/core_ext/hash_with_indifferent_access.rb +75 -0
  34. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/core_ext/ordered_hash.rb +100 -0
  35. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/error.rb +27 -0
  36. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/group.rb +271 -0
  37. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/invocation.rb +178 -0
  38. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/parser.rb +4 -0
  39. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/parser/argument.rb +67 -0
  40. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/parser/arguments.rb +145 -0
  41. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/parser/option.rb +132 -0
  42. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/parser/options.rb +142 -0
  43. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/rake_compat.rb +66 -0
  44. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/runner.rb +303 -0
  45. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/shell.rb +78 -0
  46. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/shell/basic.rb +239 -0
  47. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/shell/color.rb +108 -0
  48. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/task.rb +111 -0
  49. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/util.rb +233 -0
  50. data/lib/daemon_kit/vendor/thor-0.12.3/lib/thor/version.rb +3 -0
  51. data/lib/daemon_kit/xmpp.rb +75 -6
  52. data/{daemon_generators → lib/generators/daemon_kit}/amqp/USAGE +0 -0
  53. data/lib/generators/daemon_kit/amqp/amqp_generator.rb +24 -0
  54. data/{daemon_generators → lib/generators/daemon_kit}/amqp/templates/config/amqp.yml +0 -0
  55. data/{daemon_generators/amqp/templates/config/initializers → lib/generators/daemon_kit/amqp/templates/config/pre-daemonize}/amqp.rb +0 -0
  56. data/{daemon_generators/amqp/templates/libexec/daemon.rb → lib/generators/daemon_kit/amqp/templates/libexec/%app_name%-daemon.rb} +0 -0
  57. data/{app_generators/daemon_kit → lib/generators/daemon_kit/app}/USAGE +0 -0
  58. data/lib/generators/daemon_kit/app/app_generator.rb +140 -0
  59. data/lib/generators/daemon_kit/app/templates/Gemfile +8 -0
  60. data/{app_generators/daemon_kit → lib/generators/daemon_kit/app}/templates/README +0 -0
  61. data/{app_generators/daemon_kit → lib/generators/daemon_kit/app}/templates/Rakefile +0 -0
  62. data/lib/generators/daemon_kit/app/templates/bin/daemon.tt +7 -0
  63. data/{app_generators/daemon_kit → lib/generators/daemon_kit/app}/templates/config/arguments.rb +0 -0
  64. data/{app_generators/daemon_kit → lib/generators/daemon_kit/app}/templates/config/boot.rb +10 -3
  65. data/{app_generators/daemon_kit/templates/config/environment.rb → lib/generators/daemon_kit/app/templates/config/environment.rb.tt} +1 -1
  66. data/{app_generators/daemon_kit → lib/generators/daemon_kit/app}/templates/config/environments/development.rb +0 -0
  67. data/{app_generators/daemon_kit → lib/generators/daemon_kit/app}/templates/config/environments/production.rb +0 -0
  68. data/{app_generators/daemon_kit → lib/generators/daemon_kit/app}/templates/config/environments/test.rb +0 -0
  69. data/{app_generators/daemon_kit → lib/generators/daemon_kit/app}/templates/config/post-daemonize/readme +0 -0
  70. data/{app_generators/daemon_kit → lib/generators/daemon_kit/app}/templates/config/pre-daemonize/readme +0 -0
  71. data/{app_generators/daemon_kit/templates/lib/daemon.rb → lib/generators/daemon_kit/app/templates/lib/%app_name%.rb} +0 -0
  72. data/{app_generators/daemon_kit/templates/libexec/daemon.erb → lib/generators/daemon_kit/app/templates/libexec/%app_name%-daemon.rb} +0 -0
  73. data/{app_generators/daemon_kit → lib/generators/daemon_kit/app}/templates/script/console +0 -0
  74. data/lib/generators/daemon_kit/app/templates/script/destroy +2 -0
  75. data/lib/generators/daemon_kit/app/templates/script/generate +2 -0
  76. data/lib/generators/daemon_kit/capistrano/capistrano_generator.rb +21 -0
  77. data/{daemon_generators/deploy_capistrano → lib/generators/daemon_kit/capistrano}/templates/Capfile +0 -0
  78. data/{daemon_generators/deploy_capistrano → lib/generators/daemon_kit/capistrano}/templates/USAGE +0 -0
  79. data/{daemon_generators/deploy_capistrano/templates/config/deploy.rb → lib/generators/daemon_kit/capistrano/templates/config/deploy.rb.tt} +1 -1
  80. data/lib/generators/daemon_kit/capistrano/templates/config/deploy/production.rb.tt +6 -0
  81. data/lib/generators/daemon_kit/capistrano/templates/config/deploy/staging.rb.tt +6 -0
  82. data/{daemon_generators/deploy_capistrano → lib/generators/daemon_kit/capistrano}/templates/config/environments/staging.rb +0 -0
  83. data/{daemon_generators → lib/generators/daemon_kit}/cron/USAGE +0 -0
  84. data/lib/generators/daemon_kit/cron/cron_generator.rb +24 -0
  85. data/{daemon_generators/cron/templates/config/initializers → lib/generators/daemon_kit/cron/templates/config/pre-daemonize}/cron.rb +0 -0
  86. data/{daemon_generators/cron/templates/libexec/daemon.rb → lib/generators/daemon_kit/cron/templates/libexec/%app_name%-daemon.rb} +0 -0
  87. data/{daemon_generators → lib/generators/daemon_kit}/cucumber/USAGE +0 -0
  88. data/lib/generators/daemon_kit/cucumber/cucumber_generator.rb +45 -0
  89. data/{daemon_generators/cucumber/templates/cucumber_environment.rb → lib/generators/daemon_kit/cucumber/templates/config/environments/cucumber.rb} +0 -0
  90. data/lib/generators/daemon_kit/cucumber/templates/features/step_definitions/.empty_directory +0 -0
  91. data/{daemon_generators/cucumber/templates → lib/generators/daemon_kit/cucumber/templates/features/support}/env.rb +0 -0
  92. data/{daemon_generators/cucumber/templates → lib/generators/daemon_kit/cucumber/templates/script}/cucumber +1 -2
  93. data/{daemon_generators/cucumber/templates → lib/generators/daemon_kit/cucumber/templates/tasks}/cucumber.rake +0 -0
  94. data/{daemon_generators/jabber → lib/generators/daemon_kit/nanite_agent}/USAGE +0 -0
  95. data/lib/generators/daemon_kit/nanite_agent/nanite_agent_generator.rb +29 -0
  96. data/{daemon_generators → lib/generators/daemon_kit}/nanite_agent/templates/config/nanite.yml +0 -0
  97. data/{daemon_generators/nanite_agent/templates/config/initializers → lib/generators/daemon_kit/nanite_agent/templates/config/pre-daemonize}/nanite_agent.rb +0 -0
  98. data/{daemon_generators → lib/generators/daemon_kit}/nanite_agent/templates/lib/actors/sample.rb +0 -0
  99. data/{daemon_generators/nanite_agent/templates/libexec/daemon.rb → lib/generators/daemon_kit/nanite_agent/templates/libexec/%app_name%-daemon.rb} +0 -0
  100. data/{daemon_generators/nanite_agent → lib/generators/daemon_kit/rspec}/USAGE +0 -0
  101. data/lib/generators/daemon_kit/rspec/rspec_generator.rb +20 -0
  102. data/{daemon_generators/rspec/templates/spec.rb → lib/generators/daemon_kit/rspec/templates/spec/%app_name%_spec.rb} +0 -0
  103. data/{daemon_generators → lib/generators/daemon_kit}/rspec/templates/spec/spec.opts +0 -0
  104. data/{daemon_generators → lib/generators/daemon_kit}/rspec/templates/spec/spec_helper.rb +0 -0
  105. data/{daemon_generators → lib/generators/daemon_kit}/rspec/templates/tasks/rspec.rake +0 -0
  106. data/{daemon_generators/rspec → lib/generators/daemon_kit/ruote}/USAGE +0 -0
  107. data/lib/generators/daemon_kit/ruote/ruote_generator.rb +29 -0
  108. data/{daemon_generators → lib/generators/daemon_kit}/ruote/templates/config/amqp.yml +0 -0
  109. data/{daemon_generators/ruote/templates/config/initializers → lib/generators/daemon_kit/ruote/templates/config/pre-daemonize}/ruote.rb +0 -0
  110. data/{daemon_generators → lib/generators/daemon_kit}/ruote/templates/config/ruote.yml +0 -0
  111. data/{daemon_generators/ruote/templates/lib/daemon.rb → lib/generators/daemon_kit/ruote/templates/lib/%app_name%.rb} +0 -0
  112. data/{daemon_generators → lib/generators/daemon_kit}/ruote/templates/lib/sample.rb +0 -0
  113. data/{daemon_generators/ruote/templates/libexec/daemon.rb → lib/generators/daemon_kit/ruote/templates/libexec/%app_name%-daemon.rb} +0 -0
  114. data/{daemon_generators/ruote → lib/generators/daemon_kit/test_unit}/USAGE +0 -0
  115. data/{daemon_generators → lib/generators/daemon_kit}/test_unit/templates/tasks/test_unit.rake +0 -0
  116. data/{daemon_generators/test_unit/templates/test/test.rb → lib/generators/daemon_kit/test_unit/templates/test/%app_name%_test.rb.tt} +1 -1
  117. data/{daemon_generators → lib/generators/daemon_kit}/test_unit/templates/test/test_helper.rb +0 -0
  118. data/lib/generators/daemon_kit/test_unit/test_unit_generator.rb +20 -0
  119. data/lib/generators/daemon_kit/xmpp/templates/config/pre-daemonize/xmpp.rb +6 -0
  120. data/{daemon_generators/jabber/templates/config/jabber.yml → lib/generators/daemon_kit/xmpp/templates/config/xmpp.yml} +5 -2
  121. data/lib/generators/daemon_kit/xmpp/templates/libexec/%app_name%-daemon.rb +27 -0
  122. data/lib/generators/daemon_kit/xmpp/xmpp_generator.rb +24 -0
  123. data/spec/argument_spec.rb +1 -1
  124. data/spec/config_spec.rb +7 -3
  125. metadata +110 -86
  126. data/app_generators/daemon_kit/daemon_kit_generator.rb +0 -178
  127. data/app_generators/daemon_kit/templates/bin/daemon.erb +0 -7
  128. data/app_generators/daemon_kit/templates/script/destroy +0 -14
  129. data/app_generators/daemon_kit/templates/script/generate +0 -14
  130. data/daemon_generators/amqp/amqp_generator.rb +0 -65
  131. data/daemon_generators/cron/cron_generator.rb +0 -64
  132. data/daemon_generators/cucumber/cucumber_generator.rb +0 -38
  133. data/daemon_generators/deploy_capistrano/deploy_capistrano_generator.rb +0 -35
  134. data/daemon_generators/deploy_capistrano/templates/config/deploy/production.rb +0 -6
  135. data/daemon_generators/deploy_capistrano/templates/config/deploy/staging.rb +0 -6
  136. data/daemon_generators/jabber/jabber_generator.rb +0 -65
  137. data/daemon_generators/jabber/templates/config/initializers/jabber.rb +0 -7
  138. data/daemon_generators/jabber/templates/libexec/daemon.rb +0 -27
  139. data/daemon_generators/nanite_agent/nanite_agent_generator.rb +0 -68
  140. data/daemon_generators/rspec/rspec_generator.rb +0 -55
  141. data/daemon_generators/ruote/ruote_generator.rb +0 -67
  142. data/daemon_generators/test_unit/USAGE +0 -5
  143. data/daemon_generators/test_unit/test_unit_generator.rb +0 -51
  144. data/test/test_jabber_generator.rb +0 -49
@@ -0,0 +1,78 @@
1
+ require 'rbconfig'
2
+ require 'thor/shell/color'
3
+
4
+ class Thor
5
+ module Base
6
+ # Returns the shell used in all Thor classes. If you are in a Unix platform
7
+ # it will use a colored log, otherwise it will use a basic one without color.
8
+ #
9
+ def self.shell
10
+ @shell ||= if Config::CONFIG['host_os'] =~ /mswin|mingw/
11
+ Thor::Shell::Basic
12
+ else
13
+ Thor::Shell::Color
14
+ end
15
+ end
16
+
17
+ # Sets the shell used in all Thor classes.
18
+ #
19
+ def self.shell=(klass)
20
+ @shell = klass
21
+ end
22
+ end
23
+
24
+ module Shell
25
+ SHELL_DELEGATED_METHODS = [:ask, :yes?, :no?, :say, :say_status, :print_table]
26
+
27
+ # Add shell to initialize config values.
28
+ #
29
+ # ==== Configuration
30
+ # shell<Object>:: An instance of the shell to be used.
31
+ #
32
+ # ==== Examples
33
+ #
34
+ # class MyScript < Thor
35
+ # argument :first, :type => :numeric
36
+ # end
37
+ #
38
+ # MyScript.new [1.0], { :foo => :bar }, :shell => Thor::Shell::Basic.new
39
+ #
40
+ def initialize(args=[], options={}, config={})
41
+ super
42
+ self.shell = config[:shell]
43
+ self.shell.base ||= self if self.shell.respond_to?(:base)
44
+ end
45
+
46
+ # Holds the shell for the given Thor instance. If no shell is given,
47
+ # it gets a default shell from Thor::Base.shell.
48
+ #
49
+ def shell
50
+ @shell ||= Thor::Base.shell.new
51
+ end
52
+
53
+ # Sets the shell for this thor class.
54
+ #
55
+ def shell=(shell)
56
+ @shell = shell
57
+ end
58
+
59
+ # Common methods that are delegated to the shell.
60
+ #
61
+ SHELL_DELEGATED_METHODS.each do |method|
62
+ module_eval <<-METHOD, __FILE__, __LINE__
63
+ def #{method}(*args)
64
+ shell.#{method}(*args)
65
+ end
66
+ METHOD
67
+ end
68
+
69
+ protected
70
+
71
+ # Allow shell to be shared between invocations.
72
+ #
73
+ def _shared_configuration #:nodoc:
74
+ super.merge!(:shell => self.shell)
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,239 @@
1
+ require 'tempfile'
2
+
3
+ class Thor
4
+ module Shell
5
+ class Basic
6
+ attr_accessor :base, :padding
7
+
8
+ # Initialize base and padding to nil.
9
+ #
10
+ def initialize #:nodoc:
11
+ @base, @padding = nil, 0
12
+ end
13
+
14
+ # Sets the output padding, not allowing less than zero values.
15
+ #
16
+ def padding=(value)
17
+ @padding = [0, value].max
18
+ end
19
+
20
+ # Ask something to the user and receives a response.
21
+ #
22
+ # ==== Example
23
+ # ask("What is your name?")
24
+ #
25
+ def ask(statement, color=nil)
26
+ say("#{statement} ", color)
27
+ $stdin.gets.strip
28
+ end
29
+
30
+ # Say (print) something to the user. If the sentence ends with a whitespace
31
+ # or tab character, a new line is not appended (print + flush). Otherwise
32
+ # are passed straight to puts (behavior got from Highline).
33
+ #
34
+ # ==== Example
35
+ # say("I know you knew that.")
36
+ #
37
+ def say(message="", color=nil, force_new_line=(message.to_s !~ /( |\t)$/))
38
+ message = message.to_s
39
+ message = set_color(message, color) if color
40
+
41
+ if force_new_line
42
+ $stdout.puts(message)
43
+ else
44
+ $stdout.print(message)
45
+ $stdout.flush
46
+ end
47
+ end
48
+
49
+ # Say a status with the given color and appends the message. Since this
50
+ # method is used frequently by actions, it allows nil or false to be given
51
+ # in log_status, avoiding the message from being shown. If a Symbol is
52
+ # given in log_status, it's used as the color.
53
+ #
54
+ def say_status(status, message, log_status=true)
55
+ return if quiet? || log_status == false
56
+ spaces = " " * (padding + 1)
57
+ color = log_status.is_a?(Symbol) ? log_status : :green
58
+
59
+ status = status.to_s.rjust(12)
60
+ status = set_color status, color, true if color
61
+ say "#{status}#{spaces}#{message}", nil, true
62
+ end
63
+
64
+ # Make a question the to user and returns true if the user replies "y" or
65
+ # "yes".
66
+ #
67
+ def yes?(statement, color=nil)
68
+ ask(statement, color) =~ is?(:yes)
69
+ end
70
+
71
+ # Make a question the to user and returns true if the user replies "n" or
72
+ # "no".
73
+ #
74
+ def no?(statement, color=nil)
75
+ !yes?(statement, color)
76
+ end
77
+
78
+ # Prints a table.
79
+ #
80
+ # ==== Parameters
81
+ # Array[Array[String, String, ...]]
82
+ #
83
+ # ==== Options
84
+ # ident<Integer>:: Ident the first column by ident value.
85
+ #
86
+ def print_table(table, options={})
87
+ return if table.empty?
88
+
89
+ formats, ident = [], options[:ident].to_i
90
+ options[:truncate] = terminal_width if options[:truncate] == true
91
+
92
+ 0.upto(table.first.length - 2) do |i|
93
+ maxima = table.max{ |a,b| a[i].size <=> b[i].size }[i].size
94
+ formats << "%-#{maxima + 2}s"
95
+ end
96
+
97
+ formats[0] = formats[0].insert(0, " " * ident)
98
+ formats << "%s"
99
+
100
+ table.each do |row|
101
+ sentence = ""
102
+
103
+ row.each_with_index do |column, i|
104
+ sentence << formats[i] % column.to_s
105
+ end
106
+
107
+ sentence = truncate(sentence, options[:truncate]) if options[:truncate]
108
+ $stdout.puts sentence
109
+ end
110
+ end
111
+
112
+ # Deals with file collision and returns true if the file should be
113
+ # overwriten and false otherwise. If a block is given, it uses the block
114
+ # response as the content for the diff.
115
+ #
116
+ # ==== Parameters
117
+ # destination<String>:: the destination file to solve conflicts
118
+ # block<Proc>:: an optional block that returns the value to be used in diff
119
+ #
120
+ def file_collision(destination)
121
+ return true if @always_force
122
+ options = block_given? ? "[Ynaqdh]" : "[Ynaqh]"
123
+
124
+ while true
125
+ answer = ask %[Overwrite #{destination}? (enter "h" for help) #{options}]
126
+
127
+ case answer
128
+ when is?(:yes), is?(:force), ""
129
+ return true
130
+ when is?(:no), is?(:skip)
131
+ return false
132
+ when is?(:always)
133
+ return @always_force = true
134
+ when is?(:quit)
135
+ say 'Aborting...'
136
+ raise SystemExit
137
+ when is?(:diff)
138
+ show_diff(destination, yield) if block_given?
139
+ say 'Retrying...'
140
+ else
141
+ say file_collision_help
142
+ end
143
+ end
144
+ end
145
+
146
+ # Called if something goes wrong during the execution. This is used by Thor
147
+ # internally and should not be used inside your scripts. If someone went
148
+ # wrong, you can always raise an exception. If you raise a Thor::Error, it
149
+ # will be rescued and wrapped in the method below.
150
+ #
151
+ def error(statement)
152
+ $stderr.puts statement
153
+ end
154
+
155
+ # Apply color to the given string with optional bold. Disabled in the
156
+ # Thor::Shell::Basic class.
157
+ #
158
+ def set_color(string, color, bold=false) #:nodoc:
159
+ string
160
+ end
161
+
162
+ protected
163
+
164
+ def is?(value) #:nodoc:
165
+ value = value.to_s
166
+
167
+ if value.size == 1
168
+ /\A#{value}\z/i
169
+ else
170
+ /\A(#{value}|#{value[0,1]})\z/i
171
+ end
172
+ end
173
+
174
+ def file_collision_help #:nodoc:
175
+ <<HELP
176
+ Y - yes, overwrite
177
+ n - no, do not overwrite
178
+ a - all, overwrite this and all others
179
+ q - quit, abort
180
+ d - diff, show the differences between the old and the new
181
+ h - help, show this help
182
+ HELP
183
+ end
184
+
185
+ def show_diff(destination, content) #:nodoc:
186
+ diff_cmd = ENV['THOR_DIFF'] || ENV['RAILS_DIFF'] || 'diff -u'
187
+
188
+ Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp|
189
+ temp.write content
190
+ temp.rewind
191
+ system %(#{diff_cmd} "#{destination}" "#{temp.path}")
192
+ end
193
+ end
194
+
195
+ def quiet? #:nodoc:
196
+ base && base.options[:quiet]
197
+ end
198
+
199
+ # This code was copied from Rake, available under MIT-LICENSE
200
+ # Copyright (c) 2003, 2004 Jim Weirich
201
+ def terminal_width
202
+ if ENV['THOR_COLUMNS']
203
+ result = ENV['THOR_COLUMNS'].to_i
204
+ else
205
+ result = unix? ? dynamic_width : 80
206
+ end
207
+ (result < 10) ? 80 : result
208
+ rescue
209
+ 80
210
+ end
211
+
212
+ # Calculate the dynamic width of the terminal
213
+ def dynamic_width
214
+ @dynamic_width ||= (dynamic_width_stty.nonzero? || dynamic_width_tput)
215
+ end
216
+
217
+ def dynamic_width_stty
218
+ %x{stty size 2>/dev/null}.split[1].to_i
219
+ end
220
+
221
+ def dynamic_width_tput
222
+ %x{tput cols 2>/dev/null}.to_i
223
+ end
224
+
225
+ def unix?
226
+ RUBY_PLATFORM =~ /(aix|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i
227
+ end
228
+
229
+ def truncate(string, width)
230
+ if string.length <= width
231
+ string
232
+ else
233
+ ( string[0, width-3] || "" ) + "..."
234
+ end
235
+ end
236
+
237
+ end
238
+ end
239
+ end
@@ -0,0 +1,108 @@
1
+ require 'thor/shell/basic'
2
+
3
+ class Thor
4
+ module Shell
5
+ # Inherit from Thor::Shell::Basic and add set_color behavior. Check
6
+ # Thor::Shell::Basic to see all available methods.
7
+ #
8
+ class Color < Basic
9
+ # Embed in a String to clear all previous ANSI sequences.
10
+ CLEAR = "\e[0m"
11
+ # The start of an ANSI bold sequence.
12
+ BOLD = "\e[1m"
13
+
14
+ # Set the terminal's foreground ANSI color to black.
15
+ BLACK = "\e[30m"
16
+ # Set the terminal's foreground ANSI color to red.
17
+ RED = "\e[31m"
18
+ # Set the terminal's foreground ANSI color to green.
19
+ GREEN = "\e[32m"
20
+ # Set the terminal's foreground ANSI color to yellow.
21
+ YELLOW = "\e[33m"
22
+ # Set the terminal's foreground ANSI color to blue.
23
+ BLUE = "\e[34m"
24
+ # Set the terminal's foreground ANSI color to magenta.
25
+ MAGENTA = "\e[35m"
26
+ # Set the terminal's foreground ANSI color to cyan.
27
+ CYAN = "\e[36m"
28
+ # Set the terminal's foreground ANSI color to white.
29
+ WHITE = "\e[37m"
30
+
31
+ # Set the terminal's background ANSI color to black.
32
+ ON_BLACK = "\e[40m"
33
+ # Set the terminal's background ANSI color to red.
34
+ ON_RED = "\e[41m"
35
+ # Set the terminal's background ANSI color to green.
36
+ ON_GREEN = "\e[42m"
37
+ # Set the terminal's background ANSI color to yellow.
38
+ ON_YELLOW = "\e[43m"
39
+ # Set the terminal's background ANSI color to blue.
40
+ ON_BLUE = "\e[44m"
41
+ # Set the terminal's background ANSI color to magenta.
42
+ ON_MAGENTA = "\e[45m"
43
+ # Set the terminal's background ANSI color to cyan.
44
+ ON_CYAN = "\e[46m"
45
+ # Set the terminal's background ANSI color to white.
46
+ ON_WHITE = "\e[47m"
47
+
48
+ # Set color by using a string or one of the defined constants. If a third
49
+ # option is set to true, it also adds bold to the string. This is based
50
+ # on Highline implementation and it automatically appends CLEAR to the end
51
+ # of the returned String.
52
+ #
53
+ def set_color(string, color, bold=false)
54
+ color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
55
+ bold = bold ? BOLD : ""
56
+ "#{bold}#{color}#{string}#{CLEAR}"
57
+ end
58
+
59
+ protected
60
+
61
+ # Overwrite show_diff to show diff with colors if Diff::LCS is
62
+ # available.
63
+ #
64
+ def show_diff(destination, content) #:nodoc:
65
+ if diff_lcs_loaded? && ENV['THOR_DIFF'].nil? && ENV['RAILS_DIFF'].nil?
66
+ actual = File.binread(destination).to_s.split("\n")
67
+ content = content.to_s.split("\n")
68
+
69
+ Diff::LCS.sdiff(actual, content).each do |diff|
70
+ output_diff_line(diff)
71
+ end
72
+ else
73
+ super
74
+ end
75
+ end
76
+
77
+ def output_diff_line(diff) #:nodoc:
78
+ case diff.action
79
+ when '-'
80
+ say "- #{diff.old_element.chomp}", :red, true
81
+ when '+'
82
+ say "+ #{diff.new_element.chomp}", :green, true
83
+ when '!'
84
+ say "- #{diff.old_element.chomp}", :red, true
85
+ say "+ #{diff.new_element.chomp}", :green, true
86
+ else
87
+ say " #{diff.old_element.chomp}", nil, true
88
+ end
89
+ end
90
+
91
+ # Check if Diff::LCS is loaded. If it is, use it to create pretty output
92
+ # for diff.
93
+ #
94
+ def diff_lcs_loaded? #:nodoc:
95
+ return true if defined?(Diff::LCS)
96
+ return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
97
+
98
+ @diff_lcs_loaded = begin
99
+ require 'diff/lcs'
100
+ true
101
+ rescue LoadError
102
+ false
103
+ end
104
+ end
105
+
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,111 @@
1
+ class Thor
2
+ class Task < Struct.new(:name, :description, :usage, :options)
3
+ FILE_REGEXP = /^#{Regexp.escape(File.expand_path(__FILE__))}:[\w:]+ `run'$/
4
+
5
+ # A dynamic task that handles method missing scenarios.
6
+ class Dynamic < Task
7
+ def initialize(name, options=nil)
8
+ super(name.to_s, "A dynamically-generated task", name.to_s, options)
9
+ end
10
+
11
+ def run(instance, args=[])
12
+ unless (instance.methods & [name.to_s, name.to_sym]).empty?
13
+ raise Error, "could not find Thor class or task '#{name}'"
14
+ end
15
+ super
16
+ end
17
+ end
18
+
19
+ def initialize(name, description, usage, options=nil)
20
+ super(name.to_s, description, usage, options || {})
21
+ end
22
+
23
+ def initialize_copy(other) #:nodoc:
24
+ super(other)
25
+ self.options = other.options.dup if other.options
26
+ end
27
+
28
+ # By default, a task invokes a method in the thor class. You can change this
29
+ # implementation to create custom tasks.
30
+ def run(instance, args=[])
31
+ raise UndefinedTaskError, "the '#{name}' task of #{instance.class} is private" unless public_method?(instance)
32
+ instance.send(name, *args)
33
+ rescue ArgumentError => e
34
+ raise e if instance.class.respond_to?(:debugging) && instance.class.debugging
35
+ parse_argument_error(instance, e, caller)
36
+ rescue NoMethodError => e
37
+ raise e if instance.class.respond_to?(:debugging) && instance.class.debugging
38
+ parse_no_method_error(instance, e)
39
+ end
40
+
41
+ # Returns the formatted usage by injecting given required arguments
42
+ # and required options into the given usage.
43
+ def formatted_usage(klass, namespace=true)
44
+ namespace = klass.namespace unless namespace == false
45
+
46
+ # Add namespace
47
+ formatted = if namespace
48
+ "#{namespace.gsub(/^(default|thor:runner:)/,'')}:"
49
+ else
50
+ ""
51
+ end
52
+
53
+ # Add usage with required arguments
54
+ formatted << if klass && !klass.arguments.empty?
55
+ usage.to_s.gsub(/^#{name}/) do |match|
56
+ match << " " << klass.arguments.map{ |a| a.usage }.compact.join(' ')
57
+ end
58
+ else
59
+ usage.to_s
60
+ end
61
+
62
+ # Add required options
63
+ formatted << " #{required_options}"
64
+
65
+ # Strip and go!
66
+ formatted.strip
67
+ end
68
+
69
+ protected
70
+
71
+ def required_options
72
+ @required_options ||= options.map{ |_, o| o.usage if o.required? }.compact.sort.join(" ")
73
+ end
74
+
75
+ # Given a target, checks if this class name is not a private/protected method.
76
+ def public_method?(instance) #:nodoc:
77
+ collection = instance.private_methods + instance.protected_methods
78
+ (collection & [name.to_s, name.to_sym]).empty?
79
+ end
80
+
81
+ # For Ruby <= 1.8.7, we have to match the method name that we are trying to call.
82
+ # In Ruby >= 1.9.1, we have to match the method run in this file.
83
+ def backtrace_match?(backtrace) #:nodoc:
84
+ method_name = /`#{Regexp.escape(name.split(':').last)}'/
85
+ backtrace =~ method_name || backtrace =~ FILE_REGEXP
86
+ end
87
+
88
+ def parse_argument_error(instance, e, caller) #:nodoc:
89
+ if e.message =~ /wrong number of arguments/ && backtrace_match?(e.backtrace.first.to_s)
90
+ if instance.is_a?(Thor::Group)
91
+ raise e, "'#{name}' was called incorrectly. Are you sure it has arity equals to 0?"
92
+ else
93
+ raise InvocationError, "'#{name}' was called incorrectly. Call as " <<
94
+ "'#{formatted_usage(instance.class)}'"
95
+ end
96
+ else
97
+ raise e
98
+ end
99
+ end
100
+
101
+ def parse_no_method_error(instance, e) #:nodoc:
102
+ if e.message =~ /^undefined method `#{name}' for #{Regexp.escape(instance.to_s)}$/
103
+ raise UndefinedTaskError, "The #{instance.class.namespace} namespace " <<
104
+ "doesn't have a '#{name}' task"
105
+ else
106
+ raise e
107
+ end
108
+ end
109
+
110
+ end
111
+ end