osx_watchfolder 1.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.
data/.DS_Store ADDED
Binary file
data/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ == 1.0.0 / 2009-02-18
2
+
3
+ * First let out into the wild
data/README.rdoc ADDED
@@ -0,0 +1,58 @@
1
+ osx_watchfolder
2
+ by Paul Wilson
3
+ http://merecomplexities.com
4
+
5
+ == DESCRIPTION:
6
+
7
+ osx_watchfolder is a tiny gem to take advantages of OSX 10.5's folder watching functionality.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ osx\_watchfolder enables you to point a script at a directory, or group of directories, and be notified of changes. Example usages are _autotest_-like scripts.
12
+
13
+ Due to limitations with Ruby threading and Ruby-Cocoa, folders may only be watched from the main Ruby thread.
14
+
15
+ Only works on OSX 10.5+ (Leopard), obviously.
16
+
17
+
18
+ == SYNOPSIS:
19
+
20
+ To run a method 'run_tests' when a change is detected in a couple of folders:
21
+
22
+ require 'rubygems'
23
+ require 'osx_watchfolder'
24
+ OsxWatchfolder::FolderWatcher.new("lib", "test") { run_tests}.start
25
+
26
+ == REQUIREMENTS:
27
+
28
+ * Only works on OSX 10.5+ (Leopard)
29
+
30
+ == INSTALL:
31
+
32
+ sudo gem install paulanthonywilson-osx_watchfolder
33
+
34
+
35
+ == LICENSE:
36
+
37
+ (The MIT License)
38
+
39
+ Copyright (c) 2009
40
+
41
+ Permission is hereby granted, free of charge, to any person obtaining
42
+ a copy of this software and associated documentation files (the
43
+ 'Software'), to deal in the Software without restriction, including
44
+ without limitation the rights to use, copy, modify, merge, publish,
45
+ distribute, sublicense, and/or sell copies of the Software, and to
46
+ permit persons to whom the Software is furnished to do so, subject to
47
+ the following conditions:
48
+
49
+ The above copyright notice and this permission notice shall be
50
+ included in all copies or substantial portions of the Software.
51
+
52
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
53
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
54
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
55
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
56
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
57
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
58
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ # Look in the tasks/setup.rb file for the various options that can be
2
+ # configured in this Rakefile. The .rake files in the tasks directory
3
+ # are where the options are used.
4
+
5
+ begin
6
+ require 'bones'
7
+ Bones.setup
8
+ rescue LoadError
9
+ begin
10
+ load 'tasks/setup.rb'
11
+ rescue LoadError
12
+ raise RuntimeError, '### please install the "bones" gem ###'
13
+ end
14
+ end
15
+
16
+ ensure_in_path 'lib'
17
+ require 'osx_watchfolder'
18
+
19
+ task :default => 'test:run'
20
+
21
+ PROJ.name = 'osx_watchfolder'
22
+ PROJ.authors = 'Paul Wilson'
23
+ PROJ.email = 'paul.wilson@merecomplexities.com'
24
+ PROJ.url = 'http://github.com/paulanthonywilson/osx_watchfolder'
25
+ PROJ.version = OsxWatchfolder::VERSION
26
+ PROJ.exclude << '\.gitignore'
27
+ PROJ.notes.exclude = %w(^README\.txt$ ^data/)
28
+ PROJ.readme_file = 'README.rdoc'
29
+
30
+ # EOF
@@ -0,0 +1,49 @@
1
+
2
+ module OsxWatchfolder
3
+
4
+ # :stopdoc:
5
+ VERSION = '1.0.1'
6
+ LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
7
+ PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
8
+ # :startdoc:
9
+
10
+ # Returns the version string for the library.
11
+ #
12
+ def self.version
13
+ VERSION
14
+ end
15
+
16
+ # Returns the library path for the module. If any arguments are given,
17
+ # they will be joined to the end of the libray path using
18
+ # <tt>File.join</tt>.
19
+ #
20
+ def self.libpath( *args )
21
+ args.empty? ? LIBPATH : ::File.join(LIBPATH, args.flatten)
22
+ end
23
+
24
+ # Returns the lpath for the module. If any arguments are given,
25
+ # they will be joined to the end of the path using
26
+ # <tt>File.join</tt>.
27
+ #
28
+ def self.path( *args )
29
+ args.empty? ? PATH : ::File.join(PATH, args.flatten)
30
+ end
31
+
32
+ # Utility method used to require all files ending in .rb that lie in the
33
+ # directory below this file that has the same name as the filename passed
34
+ # in. Optionally, a specific _directory_ name can be passed in such that
35
+ # the _filename_ does not have to be equivalent to the directory.
36
+ #
37
+ def self.require_all_libs_relative_to( fname, dir = nil )
38
+ dir ||= ::File.basename(fname, '.*')
39
+ search_me = ::File.expand_path(
40
+ ::File.join(::File.dirname(fname), dir, '**', '*.rb'))
41
+
42
+ Dir.glob(search_me).sort.each {|rb| require rb}
43
+ end
44
+
45
+ end # module OsxWatchfolder
46
+
47
+ OsxWatchfolder.require_all_libs_relative_to(__FILE__)
48
+
49
+ # EOF
@@ -0,0 +1,82 @@
1
+ require 'osx/foundation'
2
+ OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework'
3
+
4
+ module OsxWatchfolder
5
+
6
+ # To run the method 'run_tests' when a change is detected in a couple of folders:
7
+ #
8
+ # folder_watcher = OsxWatchfolder::FolderWatcher.new("lib", "test") { run_tests}
9
+ # folder_wacher.start
10
+ #
11
+ class FolderWatcher
12
+
13
+ # directory update notification latency in seconds. Defaults to 1
14
+ attr_accessor :latency
15
+
16
+ # runloop timeout in seconds - on all but the 1st interrupt for the script it will take a maximum of
17
+ # this long for interrupts to be noticed. Defaults to 5
18
+ attr_accessor :runloop_interval
19
+
20
+ # how many times has this been interrupted?
21
+ attr_reader :interrupted_count
22
+
23
+
24
+ def stop
25
+ @running = false
26
+ OSX::CFRunLoopStop(@runloop)
27
+ end
28
+
29
+ def start
30
+ raise "May only be started from main thread" unless Thread.current == Thread.main
31
+ prepare_stream
32
+ enter_run_loop
33
+ rescue Interrupt
34
+ stop
35
+ @interrupted_count += 1
36
+ ensure
37
+ cleanup_stream
38
+ end
39
+
40
+
41
+ def initialize(*folders, &block)
42
+ @folders = Array === folders.first ? folders.first : folders
43
+ @block = block
44
+ @running = true
45
+ @latency = 1
46
+ @runloop_interval = 5
47
+ @interrupted_count = 0
48
+ end
49
+
50
+ private
51
+
52
+
53
+ def enter_run_loop
54
+ @running = true
55
+ OSX::CFRunLoopRunInMode(OSX::KCFRunLoopDefaultMode, @runloop_interval, true) while @running
56
+ end
57
+
58
+ def cleanup_stream
59
+ if (@stream)
60
+ OSX::FSEventStreamStop(@stream)
61
+ OSX::FSEventStreamInvalidate(@stream)
62
+ OSX::FSEventStreamRelease(@stream)
63
+ end
64
+ @stream = nil
65
+ end
66
+
67
+ def prepare_stream
68
+ callback = lambda do |streamRef, clientCallBackInfo, numEvents,eventPaths, eventFlags,eventIds|
69
+ @block.call
70
+ end
71
+ @stream = OSX::FSEventStreamCreate(nil,callback,nil,@folders,OSX::KFSEventStreamEventIdSinceNow, @latency,OSX::KFSEventStreamCreateFlagNone)
72
+
73
+ @runloop = OSX::CFRunLoopGetCurrent()
74
+
75
+ OSX::FSEventStreamScheduleWithRunLoop(@stream, @runloop, OSX::KCFRunLoopDefaultMode)
76
+ OSX::FSEventStreamStart(@stream)
77
+ end
78
+
79
+
80
+
81
+ end
82
+ end
Binary file
@@ -0,0 +1,89 @@
1
+ require 'test/unit'
2
+ require 'tmpdir'
3
+ require 'fileutils'
4
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/osx_watchfolder')
5
+
6
+ class TestFolderWatch < Test::Unit::TestCase
7
+ include FileUtils
8
+ include OsxWatchfolder
9
+
10
+ def setup
11
+ @some_folders = [Dir.tmpdir + "/folder_watch_test1", Dir.tmpdir + "/folder_watch_test2"]
12
+ @some_folders.each {|dir| rm_rf dir}
13
+ @some_folders.each {|dir| mkdir dir}
14
+ @folder_changed = false
15
+ end
16
+
17
+ def teardown
18
+ @some_folders.each {|dir| rm_rf dir}
19
+ end
20
+
21
+ def test_nothing_happens_if_a_watched_folder_does_not_change
22
+ in_a_second_yield_and_stop_watcher
23
+ watch_folders
24
+ assert !@folder_changed
25
+ end
26
+
27
+ def test_can_be_initialised_with_var_args_or_array
28
+ assert_equal @some_folders, FolderWatcher.new(*@some_folders).instance_variable_get(:@folders)
29
+ assert_equal @some_folders, FolderWatcher.new(@some_folders).instance_variable_get(:@folders)
30
+ end
31
+
32
+
33
+ def test_notified_if_first_watched_folder_changes
34
+ in_a_second_write_file_and_stop_watcher @some_folders.first + "/somefile"
35
+ watch_folders
36
+ assert @folder_changed
37
+ end
38
+
39
+
40
+ def test_notified_if_another_watched_folder_changes
41
+ in_a_second_write_file_and_stop_watcher @some_folders.last + "/somefile"
42
+ watch_folders
43
+ assert @folder_changed
44
+ end
45
+
46
+ def test_must_be_started_in_main_thread
47
+ @testee = FolderWatcher.new(*@some_folders) {}
48
+ t = Thread.new do
49
+ assert_raise(RuntimeError){ @testee.start}
50
+ end
51
+ t.join
52
+ end
53
+
54
+ def test_handles_interrupts
55
+ @testee = FolderWatcher.new(*@some_folders) {}
56
+ assert_equal 0, @testee.interrupted_count
57
+ class << @testee
58
+ def enter_run_loop
59
+ raise Interrupt, "stopped!"
60
+ end
61
+ end
62
+ @testee.start
63
+ assert_equal 1, @testee.interrupted_count
64
+ end
65
+
66
+
67
+ def in_a_second_yield_and_stop_watcher
68
+ Thread.new do
69
+ yield if block_given?
70
+ sleep 1
71
+ @testee.stop
72
+ end
73
+ end
74
+
75
+ def in_a_second_write_file_and_stop_watcher(file)
76
+ in_a_second_yield_and_stop_watcher do
77
+ File.open(file, "w"){|f| f.write "hello"}
78
+ end
79
+ end
80
+
81
+ def watch_folders
82
+ @testee = FolderWatcher.new(*@some_folders) {@folder_changed = true}
83
+ @testee.latency = 0.1
84
+ @testee.runloop_interval = 0.5
85
+ @testee.start
86
+ end
87
+
88
+
89
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: osx_watchfolder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paul Wilson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-18 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bones
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.4.0
24
+ version:
25
+ description: osx_watchfolder is a tiny gem to take advantages of OSX 10.5's folder watching functionality.
26
+ email: paul.wilson@merecomplexities.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - History.txt
33
+ - README.rdoc
34
+ files:
35
+ - .DS_Store
36
+ - History.txt
37
+ - README.rdoc
38
+ - Rakefile
39
+ - lib/osx_watchfolder.rb
40
+ - lib/osx_watchfolder/folder_watcher.rb
41
+ - osx_watchfolder.gemspec
42
+ - test/test_folder_watch.rb
43
+ has_rdoc: true
44
+ homepage: http://github.com/paulanthonywilson/osx_watchfolder
45
+ licenses: []
46
+
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --main
50
+ - README.rdoc
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ requirements: []
66
+
67
+ rubyforge_project: !binary |
68
+ AA==
69
+
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 2
73
+ summary: osx_watchfolder is a tiny gem to take advantages of OSX 10
74
+ test_files:
75
+ - test/test_folder_watch.rb