fwatch 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fwatch.gemspec
4
+ gemspec
File without changes
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # This script is slightly cobbled together and not tested properly
4
+
5
+ if ARGV.size < 2
6
+ puts "Usage : #{$0} [-s SIGNAL] FILE COMMAND"
7
+ exit -1
8
+ end
9
+
10
+ # gotcha - this uses the installed gem
11
+ require 'rubygems'
12
+ require 'fwatch'
13
+
14
+
15
+ @signal = ARGV[0] == '-s' ? "#{ARGV.shift} #{ARGV.shift}" : ''
16
+
17
+ file = ARGV.shift
18
+ cmd = ARGV.join ' '
19
+
20
+
21
+ def tell message
22
+ puts "› \033[36m#{message}\033[0m"
23
+ end
24
+
25
+ def kill
26
+ unless @pid.nil?
27
+ tell "killing: #{@pid}"
28
+ `kill #{@signal} #{@pid}`
29
+ end
30
+ end
31
+
32
+
33
+ watcher = FWatch.new file do
34
+ kill
35
+
36
+ @pid = fork do
37
+ tell "running: #{cmd}"
38
+ exec cmd
39
+ end
40
+
41
+ end
42
+
43
+ # touch the file, so that the command runs
44
+ `touch #{file}`
45
+
46
+ loop do
47
+ begin
48
+ sleep 1
49
+ rescue Interrupt => e
50
+ kill
51
+ puts
52
+ break
53
+ end
54
+ watcher.process
55
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "fwatch/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "fwatch"
7
+ s.version = Filewatcher::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Ben Foxall"]
10
+ s.email = ["benfoxall@gmail.com"]
11
+ s.homepage = "http://github.com/benfoxall/fwatch"
12
+ s.summary = 'run commands when a file changes'
13
+ s.description = 'This is a development tool for restarting processes when you update a file'
14
+
15
+ s.rubyforge_project = "fwatch"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,30 @@
1
+ class FWatch
2
+
3
+ def initialize file, &block
4
+ @file = File.new file
5
+ @block = block
6
+ @mtime = @file.stat.mtime
7
+ end
8
+
9
+
10
+ # Check for file modifications and call the block
11
+ # if the file has been updated
12
+ def process
13
+ if @mtime < @file.stat.mtime
14
+ @mtime = @file.stat.mtime
15
+ @block.call unless @block.nil?
16
+ end
17
+ end
18
+
19
+ # Starts a thread that keeps to check the
20
+ # file on a set interval
21
+ def auto_reprocess timeout = 1
22
+ Thread.new do
23
+ while true
24
+ sleep timeout
25
+ process
26
+ end
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,3 @@
1
+ module Filewatcher
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,88 @@
1
+ require 'lib/fwatch.rb'
2
+
3
+ describe FWatch do
4
+
5
+ before(:all) do
6
+ `touch file`
7
+ end
8
+ after(:all) do
9
+ `rm file`
10
+ end
11
+
12
+
13
+ it 'should throw an error when file doesn\'t exist' do
14
+
15
+ lambda {
16
+ FWatch.new 'nofile' do
17
+ puts 'changed'
18
+ end
19
+ }.should raise_error
20
+
21
+ end
22
+
23
+
24
+ it 'should call the block when file is touched' do
25
+
26
+ count = 0
27
+
28
+ watcher = FWatch.new 'file' do
29
+ count += 1
30
+ end
31
+
32
+ sleep 1
33
+ `touch file`
34
+
35
+ watcher.process
36
+
37
+ count.should == 1
38
+
39
+ end
40
+
41
+ it 'should not call on unmodified file' do
42
+
43
+
44
+ count = 0
45
+
46
+ watcher = FWatch.new 'file' do
47
+ count += 1
48
+ end
49
+
50
+ watcher.process
51
+
52
+ count.should == 0
53
+
54
+
55
+ end
56
+
57
+
58
+ it 'should autoreprocess' do
59
+
60
+ count = 0
61
+
62
+ watcher = FWatch.new 'file' do
63
+ count += 1
64
+ end
65
+
66
+ watcher.auto_reprocess 0.5
67
+
68
+ count.should == 0
69
+
70
+ sleep 1
71
+
72
+ `touch file`
73
+
74
+ sleep 1
75
+
76
+ count.should == 1
77
+
78
+
79
+ `touch file`
80
+
81
+ sleep 1
82
+
83
+ count.should == 2
84
+
85
+ end
86
+
87
+
88
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fwatch
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Ben Foxall
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-28 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: This is a development tool for restarting processes when you update a file
23
+ email:
24
+ - benfoxall@gmail.com
25
+ executables:
26
+ - fwatch
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README.md
35
+ - Rakefile
36
+ - bin/fwatch
37
+ - fwatch.gemspec
38
+ - lib/fwatch.rb
39
+ - lib/fwatch/version.rb
40
+ - spec/fwatch_spec.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/benfoxall/fwatch
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project: fwatch
71
+ rubygems_version: 1.4.2
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: run commands when a file changes
75
+ test_files:
76
+ - spec/fwatch_spec.rb