cuco 0.0.7 → 0.1.0

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: ce231276ed05e26b295e643409792ab5e4260b80f44642438b70ab8f1d94b63e
4
+ data.tar.gz: 83e9536002c04ffcd28f21b14a14816a0c8347f46214bcacf78ea05a26e2369f
5
5
  SHA512:
6
- metadata.gz: 83a0f2b95909e51d131b8dcdf6c67cffbc7ccd9405da61bb75c0599a4f77eeef0b31425a81bc930d215524f93ddf32666b8cedcf750b6f867d388f6a95ba5308
7
- data.tar.gz: 26245ecc48a0d9e4f7c19890e683944c4f2c99fb4a12840cb6e440688069583c3c74c61726bf7ac1de490715bb4e6cdd7e748f95d57e4bb5835128bc1c7c65e4
6
+ metadata.gz: 3fad6b87f9ca62c99e4f65c6b69b388facaf9edfcc32ec58814bf5d1a666de597696f1106ac63a11b3a1534609da9b4efb32beead6c3c6140a299e58e7d6d6ef
7
+ data.tar.gz: 2fdee124e0b0930c5b31b231af0b9b77e27523651504cc35a7eba752f6dd8592018fd3ed8769829e762c1f95dba90fa9838eacc29cd45f0459cf0dcc9096e100
@@ -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.0)
5
5
  listen
6
6
  micro-optparse
7
7
 
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Cuco
2
2
 
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
+
3
6
  *Cuco* watch files in a directory and take an action when they change.
4
7
 
5
8
  *Cuco* is controlled by a user-supplied script file.
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/lib/controller.rb CHANGED
@@ -7,46 +7,49 @@ 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
 
46
- private
47
-
48
30
  def run_files(files, type)
49
- files.map! { |filename| filename[@pwd_size + 1..] }
50
- files.each { |filename| @script.run(filename, 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
+ G.script.__rules.select { |rule| match_run(rule, pattern) }
44
+ end
45
+
46
+ def match_run(rule, pattern)
47
+ md = pattern.match(rule.pattern)
48
+ puts "*** match_run #{rule}" if debug
49
+ rule.proc.call(md) if md
50
+ end
51
+
52
+ def debug
53
+ G.options[:debug]
51
54
  end
52
55
  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,7 @@
1
1
  module Cuco
2
- VERSION = "0.0.7" # 2024-03-18
2
+ VERSION = "0.1.0" # 2024-03-18
3
+ # VERSION = "0.0.8" # 2024-03-18
4
+ # VERSION = "0.0.7" # 2024-03-18
3
5
  # VERSION = "0.0.3" # 2024-03-12
4
6
  # VERSION = "0.0.2" # 2024-03-11
5
7
  # VERSION = "0.0.1" # 2024-03-05
@@ -1,12 +1,12 @@
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
4
  let(:cuco) { Controller.instance }
9
- let(:script) { Script.new }
5
+ let(:regexp) { '.*\.rb' }
6
+
7
+ def setup
8
+ G.init({}, [])
9
+ end
10
10
 
11
11
  def teardown
12
12
  cuco.stop
@@ -17,12 +17,7 @@ describe Controller do
17
17
  assert_nil cuco.listener
18
18
  end
19
19
 
20
- it "initializes" do
21
- cuco.init(options, [])
22
- end
23
-
24
20
  it "runs" do
25
- cuco.init(options, [])
26
21
  assert_raises(Timeout::Error) do
27
22
  Timeout.timeout(0.1) { cuco.run }
28
23
  end
@@ -31,22 +26,27 @@ describe Controller do
31
26
  end
32
27
 
33
28
  it "read .watchr" do
34
- filename = ".watchr"
29
+ assert_raises(Timeout::Error) do
30
+ Timeout.timeout(0.1) { cuco.run }
31
+ end
32
+ end
35
33
 
36
- script.load_file filename
37
- assert_equal 2, script.__rules.size
34
+ it "run" do
35
+ G.script = Script.new "watch('#{regexp}') { raise IOError }"
36
+
37
+ assert_raises(IOError) { cuco.file_run "a.rb" }
38
38
  end
39
39
 
40
- private
40
+ it "does not run" do
41
+ G.script = Script.new "watch('#{regexp}') { raise IOError }"
41
42
 
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) }
43
+ cuco.file_run "a.no"
46
44
  end
47
45
 
48
- def find_files_in(dir)
49
- entries = Dir.entries(dir)
50
- entries.reject { |entry| File.directory?(entry) }
46
+ it "receives a matchdata" do
47
+ G.script = Script.new "watch('ab(.)') { |m| [m[0], m[1]] }"
48
+
49
+ rule = G.script.__rules.last
50
+ assert_equal ["abc", "c"], cuco.match_run(rule, "abc")
51
51
  end
52
52
  end
data/test/cuco_test.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  require "test_helper"
2
2
 
3
3
  describe Cuco do
4
- let(:cuco) { Controller.instance }
5
-
6
4
  it "usage" do
7
5
  out, _err = capture_io do
8
6
  puts `bin/cuco -h`
@@ -13,19 +11,19 @@ describe Cuco do
13
11
 
14
12
  it "options" do
15
13
  options = {opt: 1234}
16
- cuco.init(options, [])
14
+ G.init options, []
17
15
 
18
- assert_equal options, cuco.options
16
+ assert_equal options, G.options
19
17
  end
20
18
 
21
19
  # it "coverage options debug" do
22
20
  # out, _err = capture_io do
23
- # puts `bin/cuco -d`
24
- # # Timeout::timeout(0.1) { puts `bin/cuco -d` }
21
+ # puts `bin/cuco -d`
22
+ # Timeout::timeout(0.1) { puts `bin/cuco -d` }
25
23
  # end
26
24
  #
27
25
  # assert_match(/:debug=>true/, out)
28
- # assert_match(/script <.watchr>/, out)
26
+ # assert_match(/scriptname <.watchr>/, out)
29
27
  # assert_match(/pwd/, out)
30
28
  # end
31
29
  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
@@ -7,6 +7,8 @@ end
7
7
 
8
8
  require "minitest/autorun"
9
9
  require "ricecream"
10
- require "cuco"
10
+
11
11
  require "controller"
12
+ require "cuco"
12
13
  require "script"
14
+ require "timeout"
metadata CHANGED
@@ -1,7 +1,7 @@
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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dittmar Krall