cuco 0.0.7 → 0.1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df5d2e1075b623db0450271a4535f241da3946afc2b76091653a0a5b434d3a97
4
- data.tar.gz: 58c75efde02ac4b5b2637d6cc80b2e6a76589b477d7c162a84e7e6fe11aa8df8
3
+ metadata.gz: 5342bb409b3bd1e9febe557662bf308acc1ae727911401b4be2a0d0d8fb06c07
4
+ data.tar.gz: 4948f0decd177693378761e046875a2808d4af80cfd0b17d5166aedfa4fb326c
5
5
  SHA512:
6
- metadata.gz: 83a0f2b95909e51d131b8dcdf6c67cffbc7ccd9405da61bb75c0599a4f77eeef0b31425a81bc930d215524f93ddf32666b8cedcf750b6f867d388f6a95ba5308
7
- data.tar.gz: 26245ecc48a0d9e4f7c19890e683944c4f2c99fb4a12840cb6e440688069583c3c74c61726bf7ac1de490715bb4e6cdd7e748f95d57e4bb5835128bc1c7c65e4
6
+ metadata.gz: 6d0c34c445d0ac41e63589a8e240f2bf3b6fcdc3414b6f060009004b14bd720428a2c331637c8df9554ba73d824e76f25f54a52a9f245ca2f844abcf7a67e71e
7
+ data.tar.gz: 9a9a183c7ee4aff77eb749797792cb7b686d490453ab92c72b6a3f03ecc2c331c723c69e4fd74f1f6f3bac40e556267d848eaf57a6c1761d4b7352c94c5aa8bc
@@ -10,7 +10,7 @@ jobs:
10
10
  matrix:
11
11
  os: [ubuntu-latest]
12
12
  ruby: ["3.0", 3.3]
13
- test_command: ["bundle exec rake test"]
13
+ test_command: ["bundle exec rake"]
14
14
  runs-on: ${{ matrix.os }}
15
15
 
16
16
  steps:
@@ -19,6 +19,13 @@ jobs:
19
19
  uses: ruby/setup-ruby@v1
20
20
  with:
21
21
  ruby-version: ${{ matrix.ruby_version }}
22
- bundler-cache: true
23
- - name: Build and test with Rake
24
- run: bundle exec rake
22
+ # bundler-cache: true
23
+ # - name: Build and test with Rake
24
+ # run: bundle exec rake
25
+ - name: Bundle install
26
+ run: |
27
+ bundle config path /home/runner/bundle
28
+ bundle install
29
+ bundle update
30
+ - name: test with Rake
31
+ run: ${{ matrix.test_command }}
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- cuco (0.0.7)
4
+ cuco (0.1.1)
5
5
  listen
6
6
  micro-optparse
7
7
 
@@ -85,7 +85,7 @@ GEM
85
85
  psych (5.1.2)
86
86
  stringio
87
87
  racc (1.7.3)
88
- rack (3.0.9.1)
88
+ rack (3.0.10)
89
89
  rack-session (2.0.0)
90
90
  rack (>= 3.0.0)
91
91
  rack-test (2.1.0)
@@ -113,7 +113,7 @@ GEM
113
113
  rb-fsevent (0.11.2)
114
114
  rb-inotify (0.10.1)
115
115
  ffi (~> 1.0)
116
- rdoc (6.6.2)
116
+ rdoc (6.6.3.1)
117
117
  psych (>= 4.0.0)
118
118
  regexp_parser (2.9.0)
119
119
  reline (0.4.3)
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Cuco
2
2
 
3
- *Cuco* watch files in a directory and take an action when they change.
3
+ [![Gem Version](https://badge.fury.io/rb/cuco.png)](http://badge.fury.io/rb/cuco)
4
+ [![GEM Downloads](https://img.shields.io/gem/dt/cuco?color=168AFE&logo=ruby&logoColor=FE1616)](https://rubygems.org/gems/cuco)
5
+ [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](http://choosealicense.com/licenses/mit/)
6
+
7
+ *Cuco* watches files in a directory and take an action when they change.
4
8
 
5
9
  *Cuco* is controlled by a user-supplied script file.
6
10
  Intermixed "watch" commands specify what to do
@@ -35,7 +39,10 @@ $ cuco path/to/script/file
35
39
  will monitor files in the current directory tree
36
40
  and react to events on those files in accordance with the script.
37
41
 
38
- ## A simple example of a script file (using Minitest):
42
+ ## A simple example of a script file
43
+
44
+ This sample script is intended for testing purposes with Minitest.
45
+ *md* is the match-data (see Ruby regular expressions).
39
46
 
40
47
  ```ruby
41
48
  watch( 'test/.*_test\.rb$' ) { |md| run_it(md[0]) }
@@ -56,7 +63,6 @@ Scripts are pure Ruby.
56
63
  Intermixed are "watch" rules that match observed files to an action.
57
64
  The matching is achieved by a pattern (a regular expression) parameter.
58
65
  The action is specified by a block (see above sample).
59
- *md* is the match-data.
60
66
 
61
67
  Updates to script files are picked up on the fly (no need to restart *cuco*)
62
68
  so experimenting is painless.
data/bin/cuco CHANGED
@@ -4,6 +4,7 @@ lib = File.expand_path("../lib/", __dir__)
4
4
  $LOAD_PATH.unshift lib unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  require "version"
7
+ require "cuco"
7
8
  require "controller"
8
9
  require "micro-optparse"
9
10
 
@@ -13,6 +14,7 @@ options = Parser.new do |p|
13
14
  p.option :debug, "Debugging output", default: false
14
15
  end.process!
15
16
 
16
- cuco = Controller.instance
17
- cuco.init(options, ARGV)
18
- cuco.run
17
+ G.init(options, ARGV)
18
+
19
+ G.script = Script.new File.read(G.scriptname)
20
+ Controller.instance.run
data/cuco.gemspec CHANGED
@@ -16,8 +16,8 @@ Gem::Specification.new do |s|
16
16
  .split("\n").map { |f| File.basename(f) }
17
17
  s.require_paths = ["lib"]
18
18
 
19
- s.add_dependency "micro-optparse"
20
- s.add_dependency "listen"
19
+ s.add_runtime_dependency "micro-optparse"
20
+ s.add_runtime_dependency "listen"
21
21
 
22
22
  s.add_development_dependency "minitest"
23
23
  s.add_development_dependency "rake"
data/lib/controller.rb CHANGED
@@ -7,46 +7,60 @@ class Controller
7
7
  include Singleton
8
8
 
9
9
  attr_reader :listener
10
- attr_reader :options
11
- attr_reader :pwd_size
12
- attr_reader :script_name
13
- attr_reader :script
14
-
15
- def init(options, argv)
16
- @options = options
17
- @script_name = argv.first || ".watchr"
18
- @pwd_size = Dir.pwd.size
19
-
20
- if @options[:debug]
21
- puts "options #{@options}"
22
- puts "pwd <#{@pwd}>"
23
- puts "script_name <#{@script_name}>"
24
- end
25
- end
26
10
 
27
11
  def run
28
- stop
29
- @script = Script.new
30
- @script.load_file(@script_name)
12
+ puts "*** Controller.run" if debug
31
13
  @listener = Listen.to(".") do |modified, added, removed|
32
14
  run_files(modified, :modified)
33
15
  run_files(added, :added)
34
16
  end
35
17
 
36
18
  @listener.start
19
+ puts "*** Listen started" if debug
37
20
  sleep
38
21
  end
39
22
 
40
23
  def stop
24
+ puts "*** Controller.stop" if debug
41
25
  @listener&.stop
42
- @script = nil
26
+ G.script = nil
43
27
  @listener = nil
44
28
  end
45
29
 
30
+ def run_files(files, type)
31
+ puts "*** Controller.run_files(#{files}, #{type})" if debug
32
+ if files.include?(G.scriptname)
33
+ G.script = Script.new File.read(G.scriptname)
34
+ return
35
+ end
36
+
37
+ files.map! { |filename| filename[G.pwd_length + 1..] }
38
+ files.each { |filename| file_run(filename, type) }
39
+ end
40
+
41
+ def file_run(pattern, type = nil)
42
+ puts "*** file_run(#{pattern}, #{type})" if debug
43
+ rules = find_rules(pattern, type)
44
+ rules.each { |rule| match_run(rule, pattern) }
45
+ end
46
+
46
47
  private
47
48
 
48
- def run_files(files, type)
49
- files.map! { |filename| filename[@pwd_size + 1..] }
50
- files.each { |filename| @script.run(filename, type) }
49
+ def find_rules(pattern, type)
50
+ puts "*** find_rules(#{pattern}, #{type})" if debug
51
+ G.script.__rules.reverse.select { |rule|
52
+ pattern.match(rule.pattern) &&
53
+ (rule.event_type.nil? || rule.event_type == type)
54
+ }
55
+ end
56
+
57
+ def match_run(rule, pattern)
58
+ md = pattern.match(rule.pattern)
59
+ puts "*** match_run #{rule}" if debug
60
+ rule.proc.call(md) if md
61
+ end
62
+
63
+ def debug
64
+ G.options[:debug]
51
65
  end
52
66
  end
data/lib/cuco.rb CHANGED
@@ -1 +1,29 @@
1
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/script.rb CHANGED
@@ -1,38 +1,14 @@
1
1
  class Script
2
2
  attr_reader :__rules
3
3
 
4
- def initialize
5
- reset
6
- end
7
-
8
- def load(str)
9
- reset
4
+ def initialize(str)
5
+ @__rules = []
10
6
  instance_eval str
11
7
  end
12
8
 
13
- def load_file(filename)
14
- file_data = File.read(filename)
15
- load file_data
16
- end
17
-
18
9
  def watch(pattern, type = nil, &block)
19
10
  @__rules << Rule.new(pattern, type, block)
20
11
  end
21
-
22
- def match_run(rule, pattern)
23
- md = pattern.match(rule.pattern)
24
- rule.proc.call(md) if md
25
- end
26
-
27
- def run(pattern, type = nil)
28
- @__rules.select { |rule| match_run(rule, pattern) }
29
- end
30
-
31
- private
32
-
33
- def reset
34
- @__rules = []
35
- end
36
12
  end
37
13
 
38
14
  # $ sudo sh -c "echo fs.inotify.max_user_watches=524288 >> /etc/sysctl.conf"
data/lib/version.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  module Cuco
2
- VERSION = "0.0.7" # 2024-03-18
2
+ VERSION = "0.1.1" # 2024-03-23
3
+ # VERSION = "0.1.0" # 2024-03-18
4
+ # VERSION = "0.0.8" # 2024-03-18
5
+ # VERSION = "0.0.7" # 2024-03-18
3
6
  # VERSION = "0.0.3" # 2024-03-12
4
7
  # VERSION = "0.0.2" # 2024-03-11
5
8
  # VERSION = "0.0.1" # 2024-03-05
@@ -0,0 +1,59 @@
1
+ require "test_helper"
2
+
3
+ class R
4
+ class << self
5
+ attr_accessor :acc
6
+ end
7
+ end
8
+
9
+ describe Controller do
10
+ let(:cntrl) { Controller.instance }
11
+
12
+ def setup
13
+ R.acc = ""
14
+ G.init({}, [])
15
+ G.script = Script.new <<~EOS
16
+ watch('file') { R.acc << "n" }
17
+ watch('file', :added) { R.acc << "a" }
18
+ watch('file', :modified) { R.acc << "m" }
19
+ EOS
20
+ end
21
+
22
+ def teardown
23
+ cntrl.stop
24
+ end
25
+
26
+ it "fails with unknown what pattern" do
27
+ cntrl.file_run("fail", nil)
28
+ assert_equal "", R.acc
29
+ end
30
+
31
+ it "considers event_type nil" do
32
+ check nil, "n"
33
+ end
34
+
35
+ it "considers event_type :added" do
36
+ check :added, "an"
37
+ end
38
+
39
+ it "considers event_type :modified" do
40
+ check :modified, "mn"
41
+ end
42
+
43
+ it "receives a matchdata" do
44
+ G.script = Script.new "watch('ab(.)') { |m|
45
+ R.acc << m[0]
46
+ R.acc << m[1]
47
+ }"
48
+
49
+ cntrl.file_run("abc", nil)
50
+ assert_equal "abcc", R.acc
51
+ end
52
+
53
+ private
54
+
55
+ def check(type, expected)
56
+ cntrl.file_run("file", type)
57
+ assert_equal expected, R.acc
58
+ end
59
+ end
@@ -1,52 +1,45 @@
1
1
  require "test_helper"
2
- require "timeout"
3
- require "controller"
4
- require "script"
5
2
 
6
3
  describe Controller do
7
- let(:options) { {} }
8
- let(:cuco) { Controller.instance }
9
- let(:script) { Script.new }
4
+ let(:cntrl) { Controller.instance }
5
+ let(:regexp) { '.*\.rb' }
10
6
 
11
- def teardown
12
- cuco.stop
7
+ def setup
8
+ G.init({}, [])
13
9
  end
14
10
 
15
- it "stops" do
16
- cuco.stop
17
- assert_nil cuco.listener
11
+ def teardown
12
+ cntrl.stop
18
13
  end
19
14
 
20
- it "initializes" do
21
- cuco.init(options, [])
15
+ it "stops" do
16
+ cntrl.stop
17
+ assert_nil cntrl.listener
22
18
  end
23
19
 
24
20
  it "runs" do
25
- cuco.init(options, [])
26
21
  assert_raises(Timeout::Error) do
27
- Timeout.timeout(0.1) { cuco.run }
22
+ Timeout.timeout(0.1) { cntrl.run }
28
23
  end
29
24
 
30
- assert cuco.listener
25
+ assert cntrl.listener
31
26
  end
32
27
 
33
28
  it "read .watchr" do
34
- filename = ".watchr"
35
-
36
- script.load_file filename
37
- assert_equal 2, script.__rules.size
29
+ assert_raises(Timeout::Error) do
30
+ Timeout.timeout(0.1) { cntrl.run }
31
+ end
38
32
  end
39
33
 
40
- private
34
+ it "run" do
35
+ G.script = Script.new "watch('#{regexp}') { raise IOError }"
41
36
 
42
- def test_files
43
- dir = File.join(Dir.pwd, "test", "files")
44
- files = find_files_in(dir)
45
- files.collect { |file| File.join(dir, file) }
37
+ assert_raises(IOError) { cntrl.file_run "a.rb" }
46
38
  end
47
39
 
48
- def find_files_in(dir)
49
- entries = Dir.entries(dir)
50
- entries.reject { |entry| File.directory?(entry) }
40
+ it "does not run" do
41
+ G.script = Script.new "watch('#{regexp}') { raise IOError }"
42
+
43
+ cntrl.file_run "a.no"
51
44
  end
52
45
  end
@@ -0,0 +1,19 @@
1
+ require "test_helper"
2
+
3
+ describe "Coverage" do
4
+ let(:cntrl) { Controller.instance }
5
+
6
+ def setup
7
+ G.init({}, [])
8
+ end
9
+
10
+ it "coverage run files" do
11
+ names = []
12
+ cntrl.run_files(names, nil)
13
+ end
14
+
15
+ it "coverage run files script" do
16
+ names = [G.scriptname]
17
+ cntrl.run_files(names, nil)
18
+ end
19
+ end
data/test/cuco_test.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  require "test_helper"
2
2
 
3
3
  describe Cuco do
4
- let(:cuco) { Controller.instance }
4
+ def setup
5
+ G.init({}, [])
6
+ end
5
7
 
6
8
  it "usage" do
7
9
  out, _err = capture_io do
@@ -11,21 +13,22 @@ describe Cuco do
11
13
  assert_match(/Usage/, out)
12
14
  end
13
15
 
16
+ it "coverage G.print " do
17
+ out, err = capture_io do
18
+ G.print
19
+ end
20
+
21
+ assert out
22
+ assert_equal "", err
23
+ assert_match(/scriptname/, out)
24
+ assert_match(/.watchr/, out)
25
+ assert_match(/pwd/, out)
26
+ end
27
+
14
28
  it "options" do
15
29
  options = {opt: 1234}
16
- cuco.init(options, [])
30
+ G.init options, []
17
31
 
18
- assert_equal options, cuco.options
32
+ assert_equal options, G.options
19
33
  end
20
-
21
- # it "coverage options debug" do
22
- # out, _err = capture_io do
23
- # puts `bin/cuco -d`
24
- # # Timeout::timeout(0.1) { puts `bin/cuco -d` }
25
- # end
26
- #
27
- # assert_match(/:debug=>true/, out)
28
- # assert_match(/script <.watchr>/, out)
29
- # assert_match(/pwd/, out)
30
- # end
31
34
  end
data/test/script_test.rb CHANGED
@@ -1,21 +1,19 @@
1
1
  require "test_helper"
2
- require "script"
3
2
 
4
3
  describe Script do
5
- let(:script) { Script.new }
6
-
7
- it "checks script instance" do
4
+ it "checks script" do
5
+ script = Script.new ""
8
6
  assert script.instance_of?(Script)
9
- assert script.respond_to?(:__rules)
7
+ assert_equal %i[watch __rules].sort, script.public_methods(false).sort
10
8
  end
11
9
 
12
10
  it "has no watcher" do
13
- script.load "n = 123"
11
+ script = Script.new "n = 123"
14
12
  assert_equal 0, script.__rules.size
15
13
  end
16
14
 
17
15
  it "has one watcher" do
18
- script.load "n = 456; watch('.rb') { }"
16
+ script = Script.new "n = 456; watch('.rb') { }"
19
17
 
20
18
  assert_equal 1, script.__rules.size
21
19
  end
@@ -23,31 +21,9 @@ describe Script do
23
21
  it "calls a watcher" do
24
22
  pattern = '\.rb'
25
23
  value = 123
26
- # script.load "n = value; watch(pattern) { n }" # WHAT??
27
- script.load "n = #{value}; watch('#{pattern}') { n }"
24
+ script = Script.new "n = #{value}; watch('#{pattern}') { n }"
28
25
 
29
26
  rule = script.__rules.last
30
27
  assert_equal [pattern, value], [rule.pattern, rule.proc.call]
31
28
  end
32
-
33
- it "receives a matchdata" do
34
- script.load "watch('ab(.)') { |m| [m[0], m[1]] }"
35
-
36
- rule = script.__rules.last
37
- assert_equal ["abc", "c"], script.match_run(rule, "abc")
38
- end
39
-
40
- it "run" do
41
- pattern = '.*\.rb'
42
- script.load "watch('#{pattern}') { raise IOError }"
43
-
44
- assert_raises(IOError) { script.run "a.rb" }
45
- end
46
-
47
- it "does not run" do
48
- pattern = '.*\.rb'
49
- script.load "watch('#{pattern}') { raise IOError }"
50
-
51
- script.run "a.no" # no exception
52
- end
53
29
  end
data/test/test_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  if ENV["COVERAGE"]
2
2
  require "simplecov"
3
+
3
4
  SimpleCov.start do
4
5
  add_filter "/test/"
5
6
  end
@@ -7,6 +8,8 @@ end
7
8
 
8
9
  require "minitest/autorun"
9
10
  require "ricecream"
10
- require "cuco"
11
+
11
12
  require "controller"
13
+ require "cuco"
12
14
  require "script"
15
+ require "timeout"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuco
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dittmar Krall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-03-18 00:00:00.000000000 Z
11
+ date: 2024-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: micro-optparse
@@ -88,7 +88,9 @@ files:
88
88
  - lib/cuco.rb
89
89
  - lib/script.rb
90
90
  - lib/version.rb
91
+ - test/controller2_test.rb
91
92
  - test/controller_test.rb
93
+ - test/coverage_test.rb
92
94
  - test/cuco_test.rb
93
95
  - test/script_test.rb
94
96
  - test/test_helper.rb