cuco 0.1.8 → 0.2.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 170a17c7fb9c122b0308d11cd3d1da6a9af94a1fb6ec3ff5a7d1c73545f3a125
4
- data.tar.gz: e2a66d97e7ce648d003b24cd55314765c88deae7ba064e5ead72100b5d332afd
3
+ metadata.gz: 6d6144cef839b535db00e4439b5013de49ca3cc67293abca5d251ca3e388ef37
4
+ data.tar.gz: 005d5b032ed825155bcb20b5710ab5b20cd80ec405af6580fa33ec858c503208
5
5
  SHA512:
6
- metadata.gz: a96e3539b23c5cc3805f2640e0423b7eccd6e4db658dae160c36da20d9241561d493cc7707b4f1e624870aff232212dc52a7aea6b5c1288ba293d0fa414c964e
7
- data.tar.gz: 21be157050b093ca131478fe9ab93b5076d3b94bee7aaaf06fbb93ddf6f6f5b2a041b78db88cc77797d5e31c0e4bd4d3d7875f1e99246ab24c82ce2e876a76d9
6
+ metadata.gz: 6474b8de689e65a5aec3ca18978b20a9cea74f69acaaad93f98a2c17fef6ca0fa7b23e867f2c929ab61618529bd918fabf62f14be036d45ff824c7df6eb71217
7
+ data.tar.gz: 853d7d93375c265244078a34a697007da143aee862306920307ea0ff3a7fbeaba654a7e4fedf5ed68fd19b2629155a78cf026eb8136c8280ff20b71d32b77b92
data/README.md CHANGED
@@ -6,82 +6,300 @@
6
6
  [![Ruby Style Guide](https://img.shields.io/badge/code_style-standard-168AFE.svg)](https://github.com/standardrb/standard)
7
7
  [![MIT License](https://img.shields.io/badge/license-MIT-168AFE.svg)](http://choosealicense.com/licenses/mit/)
8
8
 
9
- *Cuco* watches files in a directory and take an action when they change.
9
+ *Cuco* is a simple and flexible file-watching utility written in Ruby.
10
10
 
11
- *Cuco* is controlled by a user-supplied script file.
12
- Intermixed "watch" commands specify what to do
13
- for the particular modified files.
11
+ It monitors a directory for filesystem changes and
12
+ executes Ruby callbacks whenever filenames match registered patterns.
13
+
14
+ Watch rules are defined in a Ruby script, usually named .watchr.
15
+
16
+ ```ruby
17
+ watch('\.rb$') do |match|
18
+ puts "Ruby file changed: #{match[0]}"
19
+ end
20
+ ```
21
+ That's all it takes to get started.
22
+
23
+ ### Features
24
+
25
+ * Watch files using strings or regular expressions
26
+ * React to added, modified, and removed files
27
+ * Filter rules by event type
28
+ * Access regular-expression matches inside callbacks
29
+ * Register multiple rules and execute them in order
30
+ * Reload the watch script automatically when it changes
31
+ * Preserve previously loaded rules when a script fails to load
32
+ * Execute arbitrary Ruby code in callbacks.
14
33
 
15
34
  *Cuco* is:
16
35
 
17
- * Simple to use
18
- * Highly flexible
19
- * Evented ( uses gem listen )
20
- * Portable ( Ruby )
36
+ | | |
37
+ |---|----|
38
+ | Simple | a small and straightforward API |
39
+ | Flexible | watch rules are ordinary Ruby code |
40
+ | Event-driven | powered by the listen gem |
41
+ | Portable | runs wherever Ruby runs |
21
42
 
22
- Most importantly it is **agnostic** to:
43
+ ### Requirements
23
44
 
24
- * Test frameworks
25
- * Web frameworks
45
+ - Ruby 3.2 or newer
26
46
 
27
47
  ## Installation
28
48
 
49
+ Install *Cuco* using RubyGems:
50
+
29
51
  ```ruby
30
52
  $ gem install cuco
31
53
  ```
32
54
 
33
- ## Usage
55
+ This automatically installs its runtime dependencies.
56
+
57
+ ## Quick Start
58
+
59
+ Create a _.watchr_ file in your project directory:
60
+
61
+ ```ruby
62
+ # .watchr
63
+
64
+ watch(".*\.rb$") do |match|
65
+ # Callback
66
+ puts "Ruby file changed: #{match[0]}"
67
+ end
68
+ ```
69
+
70
+ Start Cuco:
71
+
72
+ ```ruby
73
+ $ cuco
74
+ ```
75
+
76
+ *Cuco* now monitors the current working directory.
77
+
78
+ Whenever a matching file is added, modified, or removed,
79
+ the callback is executed.
80
+
81
+ You can also specify a different watch script:
82
+
83
+ ```ruby
84
+ $ cuco scripts/.watchr-rails
85
+ ```
86
+
87
+ ## Watch Scripts
88
+
89
+ Watch scripts are ordinary Ruby files.
90
+
91
+ They can contain arbitrary Ruby code, including method definitions,
92
+ constants, and watch declarations.
93
+
94
+ The watch method registers a rule consisting of:
95
+
96
+ - A filename pattern
97
+ - An optional filesystem event type
98
+ - A callback block
99
+
100
+ ### Basic rules
101
+
102
+ A pattern can be either a String or a Regexp.
103
+
104
+ String patterns are automatically converted into regular expressions.
105
+ ```ruby
106
+ watch('\.rb$') do |match|
107
+ puts "Ruby file changed: #{match[0]}"
108
+ end
109
+
110
+ watch(/\.erb$/) do |match|
111
+ puts "ERB file changed: #{match[0]}"
112
+ end
113
+ ```
114
+
115
+ Patterns are matched against filenames relative to the watched directory.
116
+
117
+ Remember that patterns are regular expressions, not shell globs.
118
+
119
+ ### Event types
120
+
121
+ By default, a rule responds to all three filesystem event types:
122
+
123
+ | Event | Description |
124
+ |-----------|---------------------|
125
+ | :added | A file was added |
126
+ | :modified | A file was modified |
127
+ | :removed | A file was removed |
128
+
129
+ You can restrict a rule to a particular event type:
130
+ ```ruby
131
+ watch('\.rb$', :added) do |match|
132
+ puts "New Ruby file: #{match[0]}"
133
+ end
134
+
135
+ watch('\.rb$', :modified) do |match|
136
+ puts "Modified Ruby file: #{match[0]}"
137
+ end
138
+
139
+ watch('\.rb$', :removed) do |match|
140
+ puts "Removed Ruby file: #{match[0]}"
141
+ end
142
+ ```
143
+
144
+ A rule without an explicit event type responds to all three events.
145
+
146
+ ### Match data
147
+
148
+ Callbacks receive a Ruby MatchData object.
149
+
150
+ This provides access to the complete matching filename
151
+ and any captured groups.
152
+
153
+ For example:
154
+ ```ruby
155
+ watch('lib/(.+)\.rb$') do |match|
156
+ puts "File: #{match[0]}"
157
+ puts "Name: #{match[1]}"
158
+ end
159
+ ```
160
+
161
+ For a file named lib/controller.rb, the callback receives:
162
+
163
+ | | |
164
+ |-|-|
165
+ | match[0] | lib/controller.rb |
166
+ | match[1] | controller |
167
+
168
+ ### Rule execution order
169
+
170
+ Rules are executed in their registration order.
171
+
172
+ Multiple rules can match the same filename:
173
+ ```ruby
174
+
175
+ watch('\.rb$') do |match|
176
+ puts "Ruby file detected: #{match[0]}"
177
+ end
178
+
179
+ watch('_test\.rb$') do |match|
180
+ puts "Test file detected: #{match[0]}"
181
+ end
182
+ ```
183
+
184
+ Both callbacks execute when a matching test file changes.
185
+
186
+ ## Practical Examples
187
+
188
+ ### Run tests when Ruby files change
189
+
190
+ ```ruby
191
+ watch('test/.*_test\.rb$') do |match|
192
+ system("bundle exec ruby -I test #{match[0]}")
193
+ end
194
+
195
+ watch('lib/(.+)\.rb$') do |match|
196
+ test_file = "test/#{match[1]}_test.rb"
197
+
198
+ system("bundle exec ruby -I test #{test_file}") if File.file?(test_file)
199
+ end
200
+ ```
201
+
202
+ ### Run the complete test suite
34
203
 
35
204
  ```ruby
36
- $ cuco # default script file is .watchr
37
- Or
38
- $ cuco path/to/script/file
205
+ watch('\.rb$', :modified) do
206
+ system("bundle exec rake test")
207
+ end
39
208
  ```
40
209
 
41
- will monitor files in the current directory tree
42
- and react to events on those files in accordance with the script.
210
+ ### Watch multiple file types
211
+
212
+ ```ruby
213
+ watch('\.(css|scss)$') do |match|
214
+ puts "Stylesheet changed: #{match[0]}"
215
+ end
216
+
217
+ watch('\.(js|ts)$') do |match|
218
+ puts "JavaScript changed: #{match[0]}"
219
+ end
43
220
 
44
- ## A simple example of a script file
221
+ watch('\.(html|erb)$') do |match|
222
+ puts "Template changed: #{match[0]}"
223
+ end
224
+ ```
225
+
226
+ ### Use helper methods
45
227
 
46
- This sample script is intended for testing purposes with Minitest.
47
- *md* is the match-data (see Ruby regular expressions).
228
+ Since watch scripts are ordinary Ruby,
229
+ you can organize your code however you like.
48
230
 
49
231
  ```ruby
50
- watch( 'test/.*_test\.rb$' ) { |md| run_it(md[0]) }
51
- watch( 'lib/(.*)\.rb$' ) { |md| run_it("test/#{md[1]}_test.rb") }
232
+ def run_tests(file)
233
+ system("bundle exec ruby -I test #{file}")
234
+ end
52
235
 
53
- def run_it(file)
54
- system %(bundle exec ruby -I test #{file})
236
+ watch('test/.*_test\.rb$') do |match|
237
+ run_tests(match[0])
55
238
  end
56
239
 
57
- Signal.trap("QUIT") { system("bundle exec rake") } # Ctrl-\ or ctrl-4
58
- Signal.trap("INT") { abort("Interrupted\n") } # Ctrl-C
240
+ watch('lib/(.+)\.rb$') do |match|
241
+ run_tests("test/#{match[1]}_test.rb")
242
+ end
59
243
  ```
60
244
 
61
- ## Script
245
+ ## Automatic Script Reloading
246
+
247
+ *Cuco* monitors its own watch script.
248
+
249
+ Whenever the script changes, *Cuco* automatically reloads it.
250
+ There is no need to restart the process.
251
+
252
+ This makes experimenting with watch rules particularly convenient.
253
+
254
+ *Cuco* also handles unsuccessful reloads gracefully:
255
+
256
+ - Successful reload: The previous rules are replaced with the new rules
257
+ - Failed reload: The previous rules remain active
258
+ - Script removed: All registered rules are cleared.
259
+
260
+ The last successfully loaded rules therefore remain available
261
+ even if an edited script contains a syntax or runtime error.
62
262
 
63
- Scripts are pure Ruby.
263
+ ## Command-line options
64
264
 
65
- Intermixed are "watch" rules that match observed files to an action.
66
- The matching is achieved by a pattern (a regular expression) parameter.
67
- The action is specified by a block (see above sample).
265
+ ```ruby
266
+ Options:
267
+ -d, --debug Debugging output
268
+ -v, --version Show version
269
+ -h, --help Show this message
270
+ ```
68
271
 
69
- Updates to script files are picked up on the fly (no need to restart *cuco*)
70
- so experimenting is painless.
272
+ ### Examples:
273
+
274
+ Start Cuco using the default .watchr script:
275
+ ```ruby
276
+ $ cuco
277
+ ```
278
+
279
+ Specify a custom script:
280
+ ```ruby
281
+ $ cuco scripts/development.watchr
282
+ ```
71
283
 
72
- It's easy to see why *cuco* is so flexible,
73
- since the whole command is custom.
74
- The above actions could just as easily call "jruby", "ruby --rubygems",
75
- "ruby -I lib", etc. or any combination of these.
284
+ Enable debugging output:
285
+ ```ruby
286
+ $ cuco --debug
287
+ ```
76
288
 
77
- See directory _scripts_ for samples.
289
+ Display the installed version:
290
+ ```ruby
291
+ $ cuco --version
292
+ ```
78
293
 
79
- ## Miscellaneous
294
+ ## Inspiration
80
295
 
81
296
  *Cuco* is heavily inspired by:
82
297
 
83
298
  * gem watchr
84
- * gem observr (a follow-up of gem watchr)
299
+ * gem observr
300
+
301
+ ## License
302
+
303
+ Copyright (c) 2024-2026 Dittmar Krall (www.matiq.com).
85
304
 
86
- Copyright (c) 2024-2026 Dittmar Krall (www.matiq.com),
87
- released under the [MIT license](https://opensource.org/licenses/MIT).
305
+ Released under the [MIT license](https://opensource.org/licenses/MIT).
data/bin/cuco CHANGED
@@ -1,20 +1,27 @@
1
- #! /usr/bin/env ruby
1
+ #!/usr/bin/env ruby
2
2
 
3
3
  lib = File.expand_path("../lib/", __dir__)
4
4
  $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
5
5
 
6
- require "cuco/version"
7
- require "cuco/cuco"
8
- require "cuco/controller"
9
6
  require "micro-optparse"
7
+ require "cuco"
10
8
 
11
9
  options = Parser.new do |p|
12
- p.banner = "Usage: cuco [options] [.watchr] # a file watcher"
10
+ p.banner = "Usage: cuco [options] [.watchr]"
13
11
  p.version = "cuco #{Cuco::VERSION}"
14
12
  p.option :debug, "Debugging output", default: false
15
13
  end.process!
16
14
 
17
15
  G.init(options, ARGV)
18
16
 
19
- G.script = Script.new File.read(G.scriptname)
17
+ unless File.file?(G.scriptname) && File.readable?(G.scriptname)
18
+ abort "Cuco: cannot read script: #{G.scriptname}"
19
+ end
20
+
21
+ begin
22
+ Script.load(File.read(G.scriptname), G.scriptname)
23
+ rescue StandardError, SyntaxError => e
24
+ abort "Cuco: #{e.class}: #{e.message}"
25
+ end
26
+
20
27
  Controller.instance.run
data/lib/controller.rb ADDED
@@ -0,0 +1,109 @@
1
+ require "listen"
2
+ require "singleton"
3
+
4
+ class Controller
5
+ include Singleton
6
+
7
+ attr_reader :listener
8
+
9
+ def run
10
+ G.log "Controller.run"
11
+ return if listener
12
+
13
+ start_listener
14
+ sleep
15
+ rescue
16
+ stop
17
+ raise
18
+ end
19
+
20
+ def stop
21
+ G.log "Controller.stop"
22
+ listener&.stop
23
+ ensure
24
+ @listener = nil
25
+ end
26
+
27
+ private
28
+
29
+ def start_listener
30
+ @listener = Listen.to(G.pwd) do |modified, added, removed|
31
+ process_events(modified, added, removed)
32
+ end
33
+
34
+ listener.start
35
+ G.log "Listen started"
36
+ end
37
+
38
+ def process_events(modified, added, removed)
39
+ G.log "Controller.process_events(#{modified})"
40
+
41
+ events = build_events(modified, added, removed)
42
+ reload_script(events)
43
+
44
+ process_remaining_events(events)
45
+ end
46
+
47
+ def process_remaining_events(events)
48
+ events.each do |type, files|
49
+ run_files(files, type)
50
+ end
51
+ end
52
+
53
+ def build_events(modified, added, removed)
54
+ {
55
+ modified: modified.dup,
56
+ added: added.dup,
57
+ removed: removed.dup
58
+ }
59
+ end
60
+
61
+ def reload_script(events)
62
+ events.each do |type, files|
63
+ next unless files.delete(G.scriptname)
64
+
65
+ reload_or_reset_script(type)
66
+ end
67
+ end
68
+
69
+ def reload_or_reset_script(type)
70
+ if type == :removed
71
+ reset_script
72
+ else
73
+ load_script
74
+ end
75
+ end
76
+
77
+ def reset_script
78
+ warn "Watch script was removed: #{G.scriptname}"
79
+ Script.reset
80
+ end
81
+
82
+ def load_script
83
+ source = File.read(G.scriptname)
84
+ Script.load(source, G.scriptname)
85
+ rescue StandardError, SyntaxError => e
86
+ warn "Cuco: #{e.class}: #{e.message}"
87
+ G.log e.backtrace.first(5).join("\n")
88
+ end
89
+
90
+ def run_files(files, type)
91
+ return if files.empty?
92
+
93
+ G.log "Controller.run_files(#{files}, #{type})"
94
+
95
+ base = Pathname.new(G.pwd)
96
+
97
+ files.each do |filename|
98
+ absolute = Pathname.new(filename).expand_path
99
+ relative = absolute.relative_path_from(base).to_s
100
+ run_file(relative, type)
101
+ end
102
+ end
103
+
104
+ def run_file(filename, type = nil)
105
+ G.log "run_file(#{filename}, #{type})"
106
+
107
+ Script.dispatch(filename, type)
108
+ end
109
+ end
@@ -0,0 +1,93 @@
1
+ require "listen"
2
+ require "singleton"
3
+
4
+ class Controller
5
+ include Singleton
6
+
7
+ attr_reader :listener
8
+
9
+ def run
10
+ G.log "Controller.run"
11
+ return if listener
12
+
13
+ start_listener
14
+ sleep
15
+ rescue
16
+ stop
17
+ raise
18
+ end
19
+
20
+ def stop
21
+ G.log "Controller.stop"
22
+ listener&.stop
23
+ ensure
24
+ @listener = nil
25
+ end
26
+
27
+ private
28
+
29
+ def start_listener
30
+ @listener = Listen.to(G.pwd) do |modified, added, removed|
31
+ process_events(modified, added, removed)
32
+ end
33
+
34
+ listener.start
35
+ G.log "Listen started"
36
+ end
37
+
38
+ def process_events(modified, added, removed)
39
+ G.log "Controller.process_events(#{modified})"
40
+
41
+ events = {
42
+ modified: modified.dup,
43
+ added: added.dup,
44
+ removed: removed.dup
45
+ }
46
+
47
+ reload_script(events)
48
+
49
+ events.each do |type, files|
50
+ run_files(files, type)
51
+ end
52
+ end
53
+
54
+ def reload_script(events)
55
+ events.each do |type, files|
56
+ next unless files.delete(G.scriptname)
57
+
58
+ if type == :removed
59
+ warn "Watch script was removed: #{G.scriptname}"
60
+ Script.reset
61
+ else
62
+ begin
63
+ Script.load(File.read(G.scriptname), G.scriptname)
64
+ rescue StandardError, SyntaxError => e
65
+ warn "Cuco: #{e.class}: #{e.message}"
66
+ G.log e.backtrace.first(5).join("\n")
67
+ end
68
+ end
69
+
70
+ files.delete(G.scriptname)
71
+ end
72
+ end
73
+
74
+ def run_files(files, type)
75
+ return if files.empty?
76
+
77
+ G.log "Controller.run_files(#{files}, #{type})"
78
+
79
+ base = Pathname.new(G.pwd)
80
+
81
+ files.each do |filename|
82
+ absolute = Pathname.new(filename).expand_path
83
+ relative = absolute.relative_path_from(base).to_s
84
+ run_file(relative, type)
85
+ end
86
+ end
87
+
88
+ def run_file(filename, type = nil)
89
+ G.log "run_file(#{filename}, #{type})"
90
+
91
+ Script.dispatch(filename, type)
92
+ end
93
+ end
data/lib/cuco.rb ADDED
@@ -0,0 +1,6 @@
1
+ # lib/cuco.rb
2
+
3
+ require "controller"
4
+ require "global"
5
+ require "script"
6
+ require "version"
data/lib/global.rb ADDED
@@ -0,0 +1,25 @@
1
+ module G
2
+ class << self
3
+ attr_reader :debug, :pwd, :scriptname
4
+
5
+ def init(options = {}, argv = [])
6
+ @options = options
7
+ @debug = options[:debug]
8
+ @pwd = File.realpath(Dir.pwd)
9
+ @scriptname = File.expand_path(argv.first || ".watchr", pwd)
10
+
11
+ log "Cuco starts at #{Time.now}"
12
+ print_config if debug
13
+ end
14
+
15
+ def log(message)
16
+ puts "*** #{message}" if debug
17
+ end
18
+
19
+ def print_config
20
+ log "options #{@options.inspect}"
21
+ log "pwd <#{pwd}>"
22
+ log "scriptname <#{scriptname}>"
23
+ end
24
+ end
25
+ end
data/lib/global.rb.bak ADDED
@@ -0,0 +1,25 @@
1
+ module G
2
+ class << self
3
+ attr_reader :debug, :pwd, :scriptname
4
+
5
+ def init(options = {}, argv = [])
6
+ @options = options
7
+ @debug = options[:debug]
8
+ @pwd = File.realpath(Dir.pwd)
9
+ @scriptname = File.expand_path(argv.first || ".watchr", pwd)
10
+
11
+ puts "*** Cuco starts at #{Time.now}" if debug
12
+ print_config if debug
13
+ end
14
+
15
+ def log(message)
16
+ puts "*** #{message}" if debug
17
+ end
18
+
19
+ def print_config
20
+ log "options #{@options.inspect}"
21
+ log "pwd <#{pwd}>"
22
+ log "scriptname <#{scriptname}>"
23
+ end
24
+ end
25
+ end
data/lib/script.rb ADDED
@@ -0,0 +1,50 @@
1
+ module Script
2
+ VALID_EVENTS = %i[added modified removed].freeze
3
+
4
+ class << self
5
+ Rule = Data.define(:pattern, :event_type, :callback)
6
+
7
+ attr_reader :rules
8
+
9
+ def reset = @rules = []
10
+
11
+ def load(source, filename = "(cuco)")
12
+ previous = rules
13
+ reset
14
+
15
+ instance_eval(source, filename, 1)
16
+ rescue StandardError, SyntaxError
17
+ @rules = previous
18
+ raise
19
+ end
20
+
21
+ def dispatch(filename, event_type = nil)
22
+ G.log "Script.run(#{filename}, #{event_type})"
23
+ rules.dup.each do |rule|
24
+ next if rule.event_type && rule.event_type != event_type
25
+
26
+ match = rule.pattern.match(filename)
27
+ rule.callback.call(match) if match
28
+ end
29
+ end
30
+
31
+ @rules = []
32
+
33
+ private
34
+
35
+ def watch(pattern, type = nil, &callback)
36
+ raise ArgumentError, "watch requires a block" unless callback
37
+
38
+ unless type.nil? || VALID_EVENTS.include?(type)
39
+ raise ArgumentError, "invalid event type: #{type.inspect}"
40
+ end
41
+
42
+ unless pattern.is_a?(String) || pattern.is_a?(Regexp)
43
+ raise ArgumentError, "pattern must be a String or Regexp"
44
+ end
45
+
46
+ regexp = pattern.is_a?(Regexp) ? pattern : Regexp.new(pattern)
47
+ rules << Rule.new(regexp, type, callback)
48
+ end
49
+ end
50
+ end
data/lib/script.rb.bak ADDED
@@ -0,0 +1,51 @@
1
+ module Script
2
+ VALID_EVENTS = %i[added modified removed].freeze
3
+
4
+ class << self
5
+ Rule = Data.define(:pattern, :event_type, :callback)
6
+
7
+ attr_reader :rules
8
+
9
+ def reset = @rules = []
10
+ def debug = G.debug
11
+
12
+ def load(source, filename = "(cuco)")
13
+ previous = rules
14
+ reset
15
+
16
+ instance_eval(source, filename, 1)
17
+ rescue StandardError, SyntaxError
18
+ @rules = previous
19
+ raise
20
+ end
21
+
22
+ def dispatch(filename, event_type = nil)
23
+ puts "*** Script.run(#{filename}, #{event_type})" if debug
24
+ rules.dup.each do |rule|
25
+ next if rule.event_type && rule.event_type != event_type
26
+
27
+ match = rule.pattern.match(filename)
28
+ rule.callback.call(match) if match
29
+ end
30
+ end
31
+
32
+ @rules = []
33
+
34
+ private
35
+
36
+ def watch(pattern, type = nil, &callback)
37
+ raise ArgumentError, "watch requires a block" unless callback
38
+
39
+ unless type.nil? || VALID_EVENTS.include?(type)
40
+ raise ArgumentError, "invalid event type: #{type.inspect}"
41
+ end
42
+
43
+ unless pattern.is_a?(String) || pattern.is_a?(Regexp)
44
+ raise ArgumentError, "pattern must be a String or Regexp"
45
+ end
46
+
47
+ regexp = pattern.is_a?(Regexp) ? pattern : Regexp.new(pattern)
48
+ rules << Rule.new(regexp, type, callback)
49
+ end
50
+ end
51
+ end
data/lib/version.rb ADDED
@@ -0,0 +1,7 @@
1
+ module Cuco
2
+ VERSION = "0.2.3" # 2026-11-19
3
+ # VERSION = "0.2.0" # 2026-11-18
4
+ # VERSION = "0.1.9" # 2026-11-18
5
+ # VERSION = "0.1.8" # 2026-04-07
6
+ # VERSION = "0.0.1" # 2024-03-05
7
+ end
@@ -0,0 +1,7 @@
1
+ module Cuco
2
+ VERSION = "0.2.2" # 2026-11-19
3
+ # VERSION = "0.2.0" # 2026-11-18
4
+ # VERSION = "0.1.9" # 2026-11-18
5
+ # VERSION = "0.1.8" # 2026-04-07
6
+ # VERSION = "0.0.1" # 2024-03-05
7
+ end
metadata CHANGED
@@ -1,120 +1,120 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuco
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
- - Dittmar Krall
7
+ - Dittmar Krall
8
8
  bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: listen
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - ">="
17
- - !ruby/object:Gem::Version
18
- version: '0'
19
- type: :runtime
20
- prerelease: false
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - ">="
24
- - !ruby/object:Gem::Version
25
- version: '0'
26
- - !ruby/object:Gem::Dependency
27
- name: logger
28
- requirement: !ruby/object:Gem::Requirement
29
- requirements:
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: '0'
33
- type: :runtime
34
- prerelease: false
35
- version_requirements: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- version: '0'
40
- - !ruby/object:Gem::Dependency
41
- name: micro-optparse
42
- requirement: !ruby/object:Gem::Requirement
43
- requirements:
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: '0'
47
- type: :runtime
48
- prerelease: false
49
- version_requirements: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- - !ruby/object:Gem::Dependency
55
- name: minitest
56
- requirement: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: '0'
61
- type: :development
62
- prerelease: false
63
- version_requirements: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - ">="
66
- - !ruby/object:Gem::Version
67
- version: '0'
68
- - !ruby/object:Gem::Dependency
69
- name: rake
70
- requirement: !ruby/object:Gem::Requirement
71
- requirements:
72
- - - ">="
73
- - !ruby/object:Gem::Version
74
- version: '0'
75
- type: :development
76
- prerelease: false
77
- version_requirements: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - ">="
80
- - !ruby/object:Gem::Version
81
- version: '0'
12
+ - !ruby/object:Gem::Dependency
13
+ name: listen
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ -
17
+ - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: "0"
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ -
25
+ - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: "0"
28
+ - !ruby/object:Gem::Dependency
29
+ name: micro-optparse
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ -
33
+ - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ -
41
+ - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ - !ruby/object:Gem::Dependency
45
+ name: minitest
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ -
49
+ - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ type: :development
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ -
57
+ - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ - !ruby/object:Gem::Dependency
61
+ name: rake
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ -
65
+ - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ -
73
+ - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
82
76
  description: A simple and flexible file watcher
83
77
  email: dittmar.krall@matiq.de
84
78
  executables:
85
- - cuco
79
+ - cuco
86
80
  extensions: []
87
81
  extra_rdoc_files:
88
- - MIT-LICENSE
89
- - README.md
82
+ - MIT-LICENSE
83
+ - README.md
90
84
  files:
91
- - MIT-LICENSE
92
- - README.md
93
- - bin/cuco
94
- - lib/cuco/controller.rb
95
- - lib/cuco/cuco.rb
96
- - lib/cuco/script.rb
97
- - lib/cuco/version.rb
98
- - lib/cuco/version.rb.bak
99
- homepage: http://www.matiq.de
85
+ - MIT-LICENSE
86
+ - README.md
87
+ - bin/cuco
88
+ - lib/controller.rb
89
+ - lib/controller.rb.bak
90
+ - lib/cuco.rb
91
+ - lib/global.rb
92
+ - lib/global.rb.bak
93
+ - lib/script.rb
94
+ - lib/script.rb.bak
95
+ - lib/version.rb
96
+ - lib/version.rb.bak
97
+ homepage: "https://www.matiq.de"
100
98
  licenses:
101
- - MIT
99
+ - MIT
102
100
  metadata: {}
103
101
  rdoc_options: []
104
102
  require_paths:
105
- - lib
103
+ - lib
106
104
  required_ruby_version: !ruby/object:Gem::Requirement
107
105
  requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '3'
106
+ -
107
+ - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: "3.2"
111
110
  required_rubygems_version: !ruby/object:Gem::Requirement
112
111
  requirements:
113
- - - ">="
114
- - !ruby/object:Gem::Version
115
- version: '0'
112
+ -
113
+ - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: "0"
116
116
  requirements: []
117
- rubygems_version: 4.0.6
117
+ rubygems_version: 4.1.0
118
118
  specification_version: 4
119
119
  summary: A file watcher
120
120
  test_files: []
@@ -1,67 +0,0 @@
1
- require "listen"
2
- require "singleton"
3
- require "cuco/cuco"
4
- require "cuco/script"
5
-
6
- class Controller
7
- include Singleton
8
-
9
- attr_reader :listener
10
-
11
- def run
12
- puts "*** Controller.run" if debug
13
- @listener = Listen.to(".") do |modified, added, removed|
14
- run_files(modified, :modified)
15
- run_files(added, :added)
16
- end
17
-
18
- @listener.start
19
- puts "*** Listen started" if debug
20
- sleep # fails with Ruby 4.0.0
21
- # sleep 24 * 60 * 60
22
- end
23
-
24
- def stop
25
- puts "*** Controller.stop" if debug
26
- @listener&.stop
27
- G.script = nil
28
- @listener = nil
29
- end
30
-
31
- def run_files(files, type)
32
- puts "*** Controller.run_files(#{files}, #{type})" if debug
33
- if files.include?(G.scriptname)
34
- G.script = Script.new File.read(G.scriptname)
35
- return
36
- end
37
-
38
- files.map! { |filename| filename[G.pwd_length + 1..] }
39
- files.each { |filename| file_run(filename, type) }
40
- end
41
-
42
- def file_run(pattern, type = nil)
43
- puts "*** file_run(#{pattern}, #{type})" if debug
44
- rules = find_rules(pattern, type)
45
- rules.each { |rule| match_run(rule, pattern) }
46
- end
47
-
48
- private
49
-
50
- def find_rules(pattern, type)
51
- puts "*** find_rules(#{pattern}, #{type})" if debug
52
- G.script.__rules.reverse.select { |rule|
53
- pattern.match(rule.pattern) &&
54
- (rule.event_type.nil? || rule.event_type == type)
55
- }
56
- end
57
-
58
- def match_run(rule, pattern)
59
- md = pattern.match(rule.pattern)
60
- puts "*** match_run #{rule}" if debug
61
- rule.proc.call(md) if md
62
- end
63
-
64
- def debug
65
- G.options[:debug]
66
- end
67
- end
data/lib/cuco/cuco.rb DELETED
@@ -1,29 +0,0 @@
1
- Rule = Struct.new(:pattern, :event_type, :proc)
2
-
3
- class G
4
- class << self
5
- attr_accessor :options
6
- attr_accessor :pwd
7
- attr_accessor :pwd_length
8
- attr_accessor :script
9
- attr_accessor :scriptname
10
-
11
- def init(options, argv)
12
- @options = options
13
- @scriptname = File.expand_path(argv.first || ".watchr")
14
- @pwd = Dir.pwd
15
- @pwd_length = @pwd.length
16
-
17
- puts "*** Cuco starts at #{Time.now}" if @options[:debug]
18
- print if @options[:debug]
19
- end
20
-
21
- def print
22
- puts "*** options #{@options}"
23
- puts "*** pwd <#{@pwd}>"
24
- puts "*** pwd_length <#{@pwd_length}>"
25
- puts "*** scriptname <#{@scriptname}>"
26
- puts "*** script <#{@script}>"
27
- end
28
- end
29
- end
data/lib/cuco/script.rb DELETED
@@ -1,15 +0,0 @@
1
- class Script
2
- attr_reader :__rules
3
-
4
- def initialize(str)
5
- @__rules = []
6
- instance_eval str
7
- end
8
-
9
- def watch(pattern, type = nil, &block)
10
- @__rules << Rule.new(pattern, type, block)
11
- end
12
- end
13
-
14
- # $ sudo sh -c "echo fs.inotify.max_user_watches=524288 >> /etc/sysctl.conf"
15
- # $ sudo sysctl -p
data/lib/cuco/version.rb DELETED
@@ -1,15 +0,0 @@
1
- module Cuco
2
- VERSION = "0.1.8" # 2026-04-07
3
- # VERSION = "0.1.6" # 2025-12-28
4
- # VERSION = "0.1.5" # 2025-12-28
5
- # VERSION = "0.1.4" # 2025-06-21
6
- # VERSION = "0.1.3" # 2025-05-28
7
- # VERSION = "0.1.2" # 2024-05-21
8
- # VERSION = "0.1.1" # 2024-03-23
9
- # VERSION = "0.1.0" # 2024-03-18
10
- # VERSION = "0.0.8" # 2024-03-18
11
- # VERSION = "0.0.7" # 2024-03-18
12
- # VERSION = "0.0.3" # 2024-03-12
13
- # VERSION = "0.0.2" # 2024-03-11
14
- # VERSION = "0.0.1" # 2024-03-05
15
- end
@@ -1,15 +0,0 @@
1
- module Cuco
2
- VERSION = "0.1.7" # 2026-04-07
3
- # VERSION = "0.1.6" # 2025-12-28
4
- # VERSION = "0.1.5" # 2025-12-28
5
- # VERSION = "0.1.4" # 2025-06-21
6
- # VERSION = "0.1.3" # 2025-05-28
7
- # VERSION = "0.1.2" # 2024-05-21
8
- # VERSION = "0.1.1" # 2024-03-23
9
- # VERSION = "0.1.0" # 2024-03-18
10
- # VERSION = "0.0.8" # 2024-03-18
11
- # VERSION = "0.0.7" # 2024-03-18
12
- # VERSION = "0.0.3" # 2024-03-12
13
- # VERSION = "0.0.2" # 2024-03-11
14
- # VERSION = "0.0.1" # 2024-03-05
15
- end