Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudgg committed Oct 3, 2010
0 parents commit 4d3744f
Show file tree
Hide file tree
Showing 28 changed files with 655 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pkg/*
*.gem
.bundle

## MAC OS
.DS_Store
.Trashes
.com.apple.timemachine.supported
.fseventsd
Desktop DB
Desktop DF
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source "http://rubygems.org"

gemspec
45 changes: 45 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
PATH
remote: .
specs:
guard (0.1.0.beta.1)
growl (~> 1.0.3)
libnotify (~> 0.1.3)
rb-inotify
sys-uname (~> 0.8.4)
thor (~> 0.14.2)

GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.2)
ffi (0.6.3)
rake (>= 0.8.7)
growl (1.0.3)
libnotify (0.1.4)
ffi (>= 0.6.2)
rake (0.8.7)
rb-inotify (0.8.1)
ffi (>= 0.5.0)
rspec (2.0.0.beta.19)
rspec-core (= 2.0.0.beta.19)
rspec-expectations (= 2.0.0.beta.19)
rspec-mocks (= 2.0.0.beta.19)
rspec-core (2.0.0.beta.19)
rspec-expectations (2.0.0.beta.19)
diff-lcs (>= 1.1.2)
rspec-mocks (2.0.0.beta.19)
sys-uname (0.8.4)
thor (0.14.2)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.0.1)
growl (~> 1.0.3)
guard!
libnotify (~> 0.1.3)
rb-inotify
rspec (~> 2.0.0.beta.22)
sys-uname (~> 0.8.4)
thor (~> 0.14.2)
25 changes: 25 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# guard 'spork', :rspec_port => 9010 do
# watch('^config/initializers/.*')
# end

def super
`say yo`
end

guard 'rspec', :version => 2 do
watch('^spec/(.*)_spec.rb')
watch('^lib/(.*).rb') { |m| "spec/#{m[1]}_spec.rb" }
watch('^spec/spec_helper.rb') { "spec" }
# watch('^spec/spec_helper.rb') { `say hello` }
# watch('^spec/(.*)_spec\.rb')
# watch('^app/(.*)\.rb') { |m| "spec/#{m[1]}_spec.rb" }
# watch('^app/(.*)\.html.erb') { |m| "spec/#{m[1]}_spec.rb" }
# watch('^lib/(.*)\.rb') { |m| "spec/lib/#{m[1]}_spec.rb" }
# watch('^spec/spec_helper\.rb') { |m| "spec" }
# watch('^config/routes\.rb') { |m| "spec/routing" }
# watch('^spec/factories\.rb') { |m| "spec/model" }
# watch('^app/controllers/application_controller\.rb') { |m| "spec/controllers" }
end

# guard 'livereload' do
# end
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2010 Thibaud Guillaume-Gentil

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= Guard

Documentation is coming.
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'bundler'
Bundler::GemHelper.install_tasks

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task :default => :spec
6 changes: 6 additions & 0 deletions bin/guard
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby

require 'guard'
require 'guard/cli'

Guard::CLI.start
25 changes: 25 additions & 0 deletions bin/inotify_watch
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env ruby
require 'rubygems'
require 'rb-inotify'

folders = Array.new
notifier = INotify::Notifier.new

notifier.watch(ARGV.first || '.', :modify, :recursive) do |event|
dir = File.expand_path(File.dirname(event.absolute_name)) + '/'
if !folders.include?(dir)
folders << dir
end
end

while true do
notifier.process

if !folders.empty?
$stdout.puts folders.join(' ')
$stdout.flush
folders.clear
end

sleep(0.1)
end
18 changes: 18 additions & 0 deletions ext/extconf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Workaround to make Rubygems believe it builds a native gem
require 'mkmf'
create_makefile('none')

if `uname -s`.chomp == 'Darwin'
gem_root = File.expand_path(File.join('..'))
darwin_verion = `uname -r`.to_i
sdk_verion = { 9 => '10.5', 10 => '10.6', 11 => '10.7' }[darwin_verion]

raise "Darwin #{darwin_verion} is not supported" unless sdk_verion

# Compile the actual fsevent_watch binary
system("CFLAGS='-isysroot /Developer/SDKs/MacOSX#{sdk_verion}.sdk -mmacosx-version-min=#{sdk_verion}' /usr/bin/gcc -framework CoreServices -o '#{gem_root}/bin/fsevent_watch' fsevent/fsevent_watch.c")

unless File.executable?("#{gem_root}/bin/fsevent_watch")
raise "Compilation of fsevent_watch failed (see README)"
end
end
44 changes: 44 additions & 0 deletions ext/fsevent/fsevent_watch.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <CoreServices/CoreServices.h>

void callback(ConstFSEventStreamRef streamRef,
void *clientCallBackInfo,
size_t numEvents,
void *eventPaths,
const FSEventStreamEventFlags eventFlags[],
const FSEventStreamEventId eventIds[]
) {
// Print modified dirs
int i;
char **paths = eventPaths;
for (i = 0; i < numEvents; i++) {
printf("%s", paths[i]);
printf(" ");
}
printf("\n");
fflush(stdout);
}

int main (int argc, const char * argv[]) {
// Create event stream
CFStringRef pathToWatch = CFStringCreateWithCString(kCFAllocatorDefault, argv[1], kCFStringEncodingUTF8);
CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **)&pathToWatch, 1, NULL);
void *callbackInfo = NULL;
FSEventStreamRef stream;
CFAbsoluteTime latency = 0.1;
stream = FSEventStreamCreate(
kCFAllocatorDefault,
callback,
callbackInfo,
pathsToWatch,
kFSEventStreamEventIdSinceNow,
latency,
kFSEventStreamCreateFlagNone
);

// Add stream to run loop
FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
FSEventStreamStart(stream);
CFRunLoopRun();

return 2;
}
32 changes: 32 additions & 0 deletions guard.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path('../lib', __FILE__)
require 'guard/version'

Gem::Specification.new do |s|
s.name = 'guard'
s.version = Guard::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ['Thibaud Guillaume-Gentil']
s.email = ['[email protected]']
s.homepage = 'http://rubygems.org/gems/guard'
s.summary = 'Guard keep an eye on your files event'
s.description = 'Guard is a command line tool to easly manage script launch when your files change'

s.rubyforge_project = 'guard'

s.add_development_dependency 'bundler', '~> 1.0.1'
s.add_development_dependency 'rspec', '~> 2.0.0.beta.22'

s.add_dependency 'thor', '~> 0.14.2'
s.add_dependency 'sys-uname', '~> 0.8.4'
# Mac OS X
s.add_dependency 'growl', '~> 1.0.3'
# Linux
s.add_dependency 'rb-inotify'
s.add_dependency 'libnotify', '~> 0.1.3'

s.files = Dir.glob('{bin,images,lib,ext}/**/*') + %w[LICENSE README.rdoc]
s.extensions = ['ext/extconf.rb']
s.executable = 'guard'
s.require_path = 'lib'
end
Binary file added images/failed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pending.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions lib/guard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Guard

autoload :UI, 'guard/ui'
autoload :Dsl, 'guard/dsl'
autoload :Interactor, 'guard/interactor'
autoload :Listener, 'guard/listener'
autoload :Watcher, 'guard/watcher'
autoload :Notifier, 'guard/notifier'

class << self
attr_accessor :options, :guards, :listener

def start(options = {})
@options = options
@listener = Listener.new
@guards = []

Dsl.evaluate_guardfile
Interactor.init_signal_traps

listener.on_change do |files|
run do
guards.each do |guard|
paths = Watcher.match_files(guard, files)
guard.run_on_change(paths) unless paths.empty?
end
end
end

UI.info "Guard is now watching at '#{Dir.pwd}'"
guards.each(&:start)
listener.start
end

def add_guard(name, watchers = [], options = {})
require "guard/#{name.downcase}"
guard_class = ObjectSpace.each_object(Class).detect { |c| c.to_s.downcase.match "^guard::#{name.downcase}" }
@guards << guard_class.new(watchers, options)
end

def run
listener.stop
yield
listener.start
end

end

end
20 changes: 20 additions & 0 deletions lib/guard/cli.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'thor'
require 'guard/version'

module Guard
class CLI < Thor
default_task :start

desc "start", "Starts guard"
method_option :clear, :type => :boolean, :default => false, :aliases => '-c', :banner => "Auto clear shell after each change"
def start
Guard.start(options)
end

desc "version", "Prints the guard's version information"
def version
Guard::UI.info "Guard version #{Guard::VERSION}"
end
map %w(-v --version) => :version
end
end
24 changes: 24 additions & 0 deletions lib/guard/dsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Guard
class Dsl

def self.evaluate_guardfile
guardfile = "#{Dir.pwd}/Guardfile"
dsl = new
dsl.instance_eval(File.read(guardfile.to_s), guardfile.to_s, 1)
rescue
UI.error "Guardfile not found or invalid"
exit 1
end

def guard(name, options = {}, &definition)
@watchers = []
definition.call
Guard.add_guard(name, @watchers, options)
end

def watch(pattern, &action)
@watchers << Guard::Watcher.new(pattern, action)
end

end
end
34 changes: 34 additions & 0 deletions lib/guard/guard.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Guard
class Guard
attr_accessor :watchers, :options

def initialize(watchers = [], options = {})
@watchers, @options = watchers, options
end

# ================
# = Guard method =
# ================

def start
true
end

def stop
true
end

def reload
true
end

def run_all
true
end

def run_on_change(paths)
true
end

end
end
Loading

0 comments on commit 4d3744f

Please sign in to comment.