Skip to content

Commit

Permalink
Initial working version
Browse files Browse the repository at this point in the history
  • Loading branch information
Lar Van Der Jagt authored and hashrocketeer committed Oct 17, 2011
1 parent 24b054f commit 8101779
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 10 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
4 changes: 2 additions & 2 deletions guard-focus.gemspec
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
require "guard-focus/version"
require "guard/focus/version"

Gem::Specification.new do |s|
s.name = "guard-focus"
s.version = Guard::Focus::VERSION
s.version = Guard::FocusVersion::VERSION
s.authors = ["Lar Van Der Jagt"]
s.email = ["lar@hashrocket.com"]
s.homepage = "http://github.com/supaspoida/guard-focus"
Expand Down
7 changes: 0 additions & 7 deletions lib/guard-focus.rb

This file was deleted.

55 changes: 55 additions & 0 deletions lib/guard/focus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
require 'guard'
require 'guard/guard'
require 'forwardable'

module Guard
class Focus < Guard
autoload :Libraries, 'guard/focus/libraries'

extend Forwardable

def_delegators :library, :base_path, :executable, :extension, :focus_regexp

attr_reader :paths, :focused, :library

def initialize(watchers=[], options={})
super
@library = Libraries[options[:on]]
end

def run_all
success?([base_path])
end

def run_on_change(files)
success?(files)
end

private

def command
['time', executable, paths.join(' '), options].compact * ' '
end

def has_focus?(file)
begin
File.read(file) =~ focus_regexp
rescue Errno::EISDIR => e
Dir.glob("#{file}/**/*#{extension}").any? &method(:has_focus?)
rescue Errno::ENOENT => e
UI.debug "\e[31mMissing directory: #{base_path}"
end
end

def options
'--tag @focus' if focused
end

def success?(paths)
@paths = paths
@focused = paths.any? &method(:has_focus?)
UI.info "\nRunning: \e[33m#{command}\n"
system command
end
end
end
26 changes: 26 additions & 0 deletions lib/guard/focus/libraries.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'ostruct'

module Guard
class Focus
class Libraries < OpenStruct
def self.cucumber
new base_path: 'features',
focus_regexp: /^\s*@focus/,
executable: 'cucumber',
extension: '.feature'
end

def self.rspec
new base_path: 'spec',
focus_regexp: /^\s*(describe|context|it).*?(:focus|focus:)/,
executable: 'rspec',
extension: '_spec.rb'
end

def self.[](name)
{ cucumber: cucumber,
rspec: rspec }[name]
end
end
end
end
10 changes: 10 additions & 0 deletions lib/guard/focus/templates/Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
guard 'focus', on: :rspec do
watch(%r{^spec/.+_spec\.rb})
watch(%r{^app/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb}) { |m| "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb" }
end

guard 'focus', on: :cucumber do
watch(%r{^features/.+\.feature$})
end
2 changes: 1 addition & 1 deletion lib/guard-focus/version.rb → lib/guard/focus/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Guard
module Focus
module FocusVersion
VERSION = "0.0.1"
end
end

0 comments on commit 8101779

Please sign in to comment.