paulanthonywilson-osx_watchfolder 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
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,56 @@
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 the method 'run_tests' when a change is detected in a couple of folders:
21
+
22
+ OsxWatchFolder::FolderWatcher.new ("lib", "test") { run_tests}
23
+
24
+ == REQUIREMENTS:
25
+
26
+ * Only works on OSX 10.5+ (Leopard)
27
+
28
+ == INSTALL:
29
+
30
+ sudo gem install osx_watchfolder
31
+
32
+
33
+ == LICENSE:
34
+
35
+ (The MIT License)
36
+
37
+ Copyright (c) 2008 FIXME (different license?)
38
+
39
+ Permission is hereby granted, free of charge, to any person obtaining
40
+ a copy of this software and associated documentation files (the
41
+ 'Software'), to deal in the Software without restriction, including
42
+ without limitation the rights to use, copy, modify, merge, publish,
43
+ distribute, sublicense, and/or sell copies of the Software, and to
44
+ permit persons to whom the Software is furnished to do so, subject to
45
+ the following conditions:
46
+
47
+ The above copyright notice and this permission notice shall be
48
+ included in all copies or substantial portions of the Software.
49
+
50
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
51
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
52
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
53
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
54
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
55
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
56
+ 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.0'
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,44 @@
1
+ require 'osx/foundation'
2
+ OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework'
3
+
4
+ module OsxWatchfolder
5
+
6
+ class FolderWatcher
7
+
8
+ attr_accessor :latency, :runloop_interval
9
+
10
+
11
+ def stop
12
+ OSX::CFRunLoopStop(@runloop)
13
+ end
14
+
15
+ def start
16
+ raise "May only be started from main thread" unless Thread.current == Thread.main
17
+ callback = lambda do |streamRef, clientCallBackInfo, numEvents,eventPaths, eventFlags,eventIds|
18
+ @block.call
19
+ end
20
+ stream = OSX::FSEventStreamCreate(nil,
21
+ callback,
22
+ nil,
23
+ @folders,
24
+ OSX::KFSEventStreamEventIdSinceNow,
25
+ @latency,
26
+ OSX::KFSEventStreamCreateFlagNone)
27
+
28
+ @runloop = OSX::CFRunLoopGetCurrent()
29
+
30
+ OSX::FSEventStreamScheduleWithRunLoop(stream, @runloop, OSX::KCFRunLoopDefaultMode)
31
+ OSX::FSEventStreamStart(stream)
32
+ OSX::CFRunLoopRun()
33
+ end
34
+
35
+
36
+ def initialize(*folders, &block)
37
+ @folders = Array === folders.first ? folders.first : folders
38
+ @block = block
39
+ @running = true
40
+ @latency = 1
41
+ end
42
+
43
+ end
44
+ end
Binary file
@@ -0,0 +1,77 @@
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
+
55
+ def in_a_second_yield_and_stop_watcher
56
+ Thread.new do
57
+ yield if block_given?
58
+ sleep 1
59
+ @testee.stop
60
+ end
61
+ end
62
+
63
+ def in_a_second_write_file_and_stop_watcher(file)
64
+ in_a_second_yield_and_stop_watcher do
65
+ File.open(file, "w"){|f| f.write "hello"}
66
+ end
67
+ end
68
+
69
+ def watch_folders
70
+ @testee = FolderWatcher.new(*@some_folders) {@folder_changed = true}
71
+ @testee.latency = 0.1
72
+ @testee.runloop_interval = 0.5
73
+ @testee.start
74
+ end
75
+
76
+
77
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paulanthonywilson-osx_watchfolder
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
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 -08: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-1.0.0.gem
42
+ - osx_watchfolder.gemspec
43
+ - test/test_folder_watch.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/paulanthonywilson/osx_watchfolder
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --main
49
+ - README.rdoc
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project: !binary |
67
+ AA==
68
+
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: osx_watchfolder is a tiny gem to take advantages of OSX 10
73
+ test_files:
74
+ - test/test_folder_watch.rb