wtch 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,114 @@
1
+ class Thor
2
+ class Task < Struct.new(:name, :description, :long_description, :usage, :options)
3
+ FILE_REGEXP = /^#{Regexp.escape(File.dirname(__FILE__))}/
4
+
5
+ def initialize(name, description, long_description, usage, options=nil)
6
+ super(name.to_s, description, long_description, usage, options || {})
7
+ end
8
+
9
+ def initialize_copy(other) #:nodoc:
10
+ super(other)
11
+ self.options = other.options.dup if other.options
12
+ end
13
+
14
+ def hidden?
15
+ false
16
+ end
17
+
18
+ # By default, a task invokes a method in the thor class. You can change this
19
+ # implementation to create custom tasks.
20
+ def run(instance, args=[])
21
+ public_method?(instance) ?
22
+ instance.send(name, *args) : instance.class.handle_no_task_error(name)
23
+ rescue ArgumentError => e
24
+ handle_argument_error?(instance, e, caller) ?
25
+ instance.class.handle_argument_error(self, e) : (raise e)
26
+ rescue NoMethodError => e
27
+ handle_no_method_error?(instance, e, caller) ?
28
+ instance.class.handle_no_task_error(name) : (raise e)
29
+ end
30
+
31
+ # Returns the formatted usage by injecting given required arguments
32
+ # and required options into the given usage.
33
+ def formatted_usage(klass, namespace = true, subcommand = false)
34
+ if namespace
35
+ namespace = klass.namespace
36
+ formatted = "#{namespace.gsub(/^(default)/,'')}:"
37
+ formatted.sub!(/.$/, ' ') if subcommand
38
+ end
39
+
40
+ formatted ||= ""
41
+
42
+ # Add usage with required arguments
43
+ formatted << if klass && !klass.arguments.empty?
44
+ usage.to_s.gsub(/^#{name}/) do |match|
45
+ match << " " << klass.arguments.map{ |a| a.usage }.compact.join(' ')
46
+ end
47
+ else
48
+ usage.to_s
49
+ end
50
+
51
+ # Add required options
52
+ formatted << " #{required_options}"
53
+
54
+ # Strip and go!
55
+ formatted.strip
56
+ end
57
+
58
+ protected
59
+
60
+ def not_debugging?(instance)
61
+ !(instance.class.respond_to?(:debugging) && instance.class.debugging)
62
+ end
63
+
64
+ def required_options
65
+ @required_options ||= options.map{ |_, o| o.usage if o.required? }.compact.sort.join(" ")
66
+ end
67
+
68
+ # Given a target, checks if this class name is not a private/protected method.
69
+ def public_method?(instance) #:nodoc:
70
+ collection = instance.private_methods + instance.protected_methods
71
+ (collection & [name.to_s, name.to_sym]).empty?
72
+ end
73
+
74
+ def sans_backtrace(backtrace, caller) #:nodoc:
75
+ saned = backtrace.reject { |frame| frame =~ FILE_REGEXP }
76
+ saned -= caller
77
+ end
78
+
79
+ def handle_argument_error?(instance, error, caller)
80
+ not_debugging?(instance) && error.message =~ /wrong number of arguments/ && begin
81
+ saned = sans_backtrace(error.backtrace, caller)
82
+ # Ruby 1.9 always include the called method in the backtrace
83
+ saned.empty? || (saned.size == 1 && RUBY_VERSION >= "1.9")
84
+ end
85
+ end
86
+
87
+ def handle_no_method_error?(instance, error, caller)
88
+ not_debugging?(instance) &&
89
+ error.message =~ /^undefined method `#{name}' for #{Regexp.escape(instance.to_s)}$/
90
+ end
91
+ end
92
+
93
+ # A task that is hidden in help messages but still invocable.
94
+ class HiddenTask < Task
95
+ def hidden?
96
+ true
97
+ end
98
+ end
99
+
100
+ # A dynamic task that handles method missing scenarios.
101
+ class DynamicTask < Task
102
+ def initialize(name, options=nil)
103
+ super(name.to_s, "A dynamically-generated task", name.to_s, name.to_s, options)
104
+ end
105
+
106
+ def run(instance, args=[])
107
+ if (instance.methods & [name.to_s, name.to_sym]).empty?
108
+ super
109
+ else
110
+ instance.class.handle_no_task_error(name)
111
+ end
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,229 @@
1
+ require 'rbconfig'
2
+
3
+ class Thor
4
+ module Sandbox #:nodoc:
5
+ end
6
+
7
+ # This module holds several utilities:
8
+ #
9
+ # 1) Methods to convert thor namespaces to constants and vice-versa.
10
+ #
11
+ # Thor::Utils.namespace_from_thor_class(Foo::Bar::Baz) #=> "foo:bar:baz"
12
+ #
13
+ # 2) Loading thor files and sandboxing:
14
+ #
15
+ # Thor::Utils.load_thorfile("~/.thor/foo")
16
+ #
17
+ module Util
18
+
19
+ # Receives a namespace and search for it in the Thor::Base subclasses.
20
+ #
21
+ # ==== Parameters
22
+ # namespace<String>:: The namespace to search for.
23
+ #
24
+ def self.find_by_namespace(namespace)
25
+ namespace = "default#{namespace}" if namespace.empty? || namespace =~ /^:/
26
+ Thor::Base.subclasses.find { |klass| klass.namespace == namespace }
27
+ end
28
+
29
+ # Receives a constant and converts it to a Thor namespace. Since Thor tasks
30
+ # can be added to a sandbox, this method is also responsable for removing
31
+ # the sandbox namespace.
32
+ #
33
+ # This method should not be used in general because it's used to deal with
34
+ # older versions of Thor. On current versions, if you need to get the
35
+ # namespace from a class, just call namespace on it.
36
+ #
37
+ # ==== Parameters
38
+ # constant<Object>:: The constant to be converted to the thor path.
39
+ #
40
+ # ==== Returns
41
+ # String:: If we receive Foo::Bar::Baz it returns "foo:bar:baz"
42
+ #
43
+ def self.namespace_from_thor_class(constant)
44
+ constant = constant.to_s.gsub(/^Thor::Sandbox::/, "")
45
+ constant = snake_case(constant).squeeze(":")
46
+ constant
47
+ end
48
+
49
+ # Given the contents, evaluate it inside the sandbox and returns the
50
+ # namespaces defined in the sandbox.
51
+ #
52
+ # ==== Parameters
53
+ # contents<String>
54
+ #
55
+ # ==== Returns
56
+ # Array[Object]
57
+ #
58
+ def self.namespaces_in_content(contents, file=__FILE__)
59
+ old_constants = Thor::Base.subclasses.dup
60
+ Thor::Base.subclasses.clear
61
+
62
+ load_thorfile(file, contents)
63
+
64
+ new_constants = Thor::Base.subclasses.dup
65
+ Thor::Base.subclasses.replace(old_constants)
66
+
67
+ new_constants.map!{ |c| c.namespace }
68
+ new_constants.compact!
69
+ new_constants
70
+ end
71
+
72
+ # Returns the thor classes declared inside the given class.
73
+ #
74
+ def self.thor_classes_in(klass)
75
+ stringfied_constants = klass.constants.map { |c| c.to_s }
76
+ Thor::Base.subclasses.select do |subclass|
77
+ next unless subclass.name
78
+ stringfied_constants.include?(subclass.name.gsub("#{klass.name}::", ''))
79
+ end
80
+ end
81
+
82
+ # Receives a string and convert it to snake case. SnakeCase returns snake_case.
83
+ #
84
+ # ==== Parameters
85
+ # String
86
+ #
87
+ # ==== Returns
88
+ # String
89
+ #
90
+ def self.snake_case(str)
91
+ return str.downcase if str =~ /^[A-Z_]+$/
92
+ str.gsub(/\B[A-Z]/, '_\&').squeeze('_') =~ /_*(.*)/
93
+ return $+.downcase
94
+ end
95
+
96
+ # Receives a string and convert it to camel case. camel_case returns CamelCase.
97
+ #
98
+ # ==== Parameters
99
+ # String
100
+ #
101
+ # ==== Returns
102
+ # String
103
+ #
104
+ def self.camel_case(str)
105
+ return str if str !~ /_/ && str =~ /[A-Z]+.*/
106
+ str.split('_').map { |i| i.capitalize }.join
107
+ end
108
+
109
+ # Receives a namespace and tries to retrieve a Thor or Thor::Group class
110
+ # from it. It first searches for a class using the all the given namespace,
111
+ # if it's not found, removes the highest entry and searches for the class
112
+ # again. If found, returns the highest entry as the class name.
113
+ #
114
+ # ==== Examples
115
+ #
116
+ # class Foo::Bar < Thor
117
+ # def baz
118
+ # end
119
+ # end
120
+ #
121
+ # class Baz::Foo < Thor::Group
122
+ # end
123
+ #
124
+ # Thor::Util.namespace_to_thor_class("foo:bar") #=> Foo::Bar, nil # will invoke default task
125
+ # Thor::Util.namespace_to_thor_class("baz:foo") #=> Baz::Foo, nil
126
+ # Thor::Util.namespace_to_thor_class("foo:bar:baz") #=> Foo::Bar, "baz"
127
+ #
128
+ # ==== Parameters
129
+ # namespace<String>
130
+ #
131
+ def self.find_class_and_task_by_namespace(namespace, fallback = true)
132
+ if namespace.include?(?:) # look for a namespaced task
133
+ pieces = namespace.split(":")
134
+ task = pieces.pop
135
+ klass = Thor::Util.find_by_namespace(pieces.join(":"))
136
+ end
137
+ unless klass # look for a Thor::Group with the right name
138
+ klass, task = Thor::Util.find_by_namespace(namespace), nil
139
+ end
140
+ if !klass && fallback # try a task in the default namespace
141
+ task = namespace
142
+ klass = Thor::Util.find_by_namespace('')
143
+ end
144
+ return klass, task
145
+ end
146
+
147
+ # Receives a path and load the thor file in the path. The file is evaluated
148
+ # inside the sandbox to avoid namespacing conflicts.
149
+ #
150
+ def self.load_thorfile(path, content=nil, debug=false)
151
+ content ||= File.binread(path)
152
+
153
+ begin
154
+ Thor::Sandbox.class_eval(content, path)
155
+ rescue Exception => e
156
+ $stderr.puts "WARNING: unable to load thorfile #{path.inspect}: #{e.message}"
157
+ if debug
158
+ $stderr.puts *e.backtrace
159
+ else
160
+ $stderr.puts e.backtrace.first
161
+ end
162
+ end
163
+ end
164
+
165
+ def self.user_home
166
+ @@user_home ||= if ENV["HOME"]
167
+ ENV["HOME"]
168
+ elsif ENV["USERPROFILE"]
169
+ ENV["USERPROFILE"]
170
+ elsif ENV["HOMEDRIVE"] && ENV["HOMEPATH"]
171
+ File.join(ENV["HOMEDRIVE"], ENV["HOMEPATH"])
172
+ elsif ENV["APPDATA"]
173
+ ENV["APPDATA"]
174
+ else
175
+ begin
176
+ File.expand_path("~")
177
+ rescue
178
+ if File::ALT_SEPARATOR
179
+ "C:/"
180
+ else
181
+ "/"
182
+ end
183
+ end
184
+ end
185
+ end
186
+
187
+ # Returns the root where thor files are located, dependending on the OS.
188
+ #
189
+ def self.thor_root
190
+ File.join(user_home, ".thor").gsub(/\\/, '/')
191
+ end
192
+
193
+ # Returns the files in the thor root. On Windows thor_root will be something
194
+ # like this:
195
+ #
196
+ # C:\Documents and Settings\james\.thor
197
+ #
198
+ # If we don't #gsub the \ character, Dir.glob will fail.
199
+ #
200
+ def self.thor_root_glob
201
+ files = Dir["#{thor_root}/*"]
202
+
203
+ files.map! do |file|
204
+ File.directory?(file) ? File.join(file, "main.thor") : file
205
+ end
206
+ end
207
+
208
+ # Where to look for Thor files.
209
+ #
210
+ def self.globs_for(path)
211
+ ["#{path}/Thorfile", "#{path}/*.thor", "#{path}/tasks/*.thor", "#{path}/lib/tasks/*.thor"]
212
+ end
213
+
214
+ # Return the path to the ruby interpreter taking into account multiple
215
+ # installations and windows extensions.
216
+ #
217
+ def self.ruby_command
218
+ @ruby_command ||= begin
219
+ ruby = File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
220
+ ruby << Config::CONFIG['EXEEXT']
221
+
222
+ # escape string in case path to ruby executable contain spaces.
223
+ ruby.sub!(/.*\s.*/m, '"\&"')
224
+ ruby
225
+ end
226
+ end
227
+
228
+ end
229
+ end
@@ -0,0 +1,3 @@
1
+ class Thor
2
+ VERSION = "0.14.0".freeze
3
+ end
@@ -0,0 +1,3 @@
1
+ module Wtch
2
+ VERSION = "0.0.1" unless defined?(::Wtch::VERSION)
3
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wtch
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Stephan Kaag
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-08-11 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Watch urls
22
+ email: stephan.kaag@holder.nl
23
+ executables:
24
+ - wtch
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - bin/wtch
31
+ - lib/wtch/cli.rb
32
+ - lib/wtch/ui.rb
33
+ - lib/wtch/vendor/thor/actions/create_file.rb
34
+ - lib/wtch/vendor/thor/actions/directory.rb
35
+ - lib/wtch/vendor/thor/actions/empty_directory.rb
36
+ - lib/wtch/vendor/thor/actions/file_manipulation.rb
37
+ - lib/wtch/vendor/thor/actions/inject_into_file.rb
38
+ - lib/wtch/vendor/thor/actions.rb
39
+ - lib/wtch/vendor/thor/base.rb
40
+ - lib/wtch/vendor/thor/core_ext/file_binary_read.rb
41
+ - lib/wtch/vendor/thor/core_ext/hash_with_indifferent_access.rb
42
+ - lib/wtch/vendor/thor/core_ext/ordered_hash.rb
43
+ - lib/wtch/vendor/thor/error.rb
44
+ - lib/wtch/vendor/thor/invocation.rb
45
+ - lib/wtch/vendor/thor/parser/argument.rb
46
+ - lib/wtch/vendor/thor/parser/arguments.rb
47
+ - lib/wtch/vendor/thor/parser/option.rb
48
+ - lib/wtch/vendor/thor/parser/options.rb
49
+ - lib/wtch/vendor/thor/parser.rb
50
+ - lib/wtch/vendor/thor/shell/basic.rb
51
+ - lib/wtch/vendor/thor/shell/color.rb
52
+ - lib/wtch/vendor/thor/shell/html.rb
53
+ - lib/wtch/vendor/thor/shell.rb
54
+ - lib/wtch/vendor/thor/task.rb
55
+ - lib/wtch/vendor/thor/util.rb
56
+ - lib/wtch/vendor/thor/version.rb
57
+ - lib/wtch/vendor/thor.rb
58
+ - lib/wtch/version.rb
59
+ - lib/wtch.rb
60
+ has_rdoc: true
61
+ homepage: http://www.holder.nl
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options: []
66
+
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ segments:
81
+ - 1
82
+ - 3
83
+ - 6
84
+ version: 1.3.6
85
+ requirements: []
86
+
87
+ rubyforge_project: wtch
88
+ rubygems_version: 1.3.6
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Watch a url
92
+ test_files: []
93
+