forked from strika/clearbooks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGuardfile
127 lines (93 loc) · 3.36 KB
/
Guardfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File: Guardfile
# System include
require 'tempfile'
guard :shell, :all_on_start => false do
# @fn watch( %r{lib/.+\.(rb)$} ) do |m| # {{{
# @brief Run metric fu on each file change
watch( %r{lib/.+\.(rb)$} ) do |m|
# Manually throttle how often we call metric fu
@counter.inc
if( @counter.execute? )
puts "(--) #{m[0].to_s} has changed re-running metric-fu"
# %x{rake metric} # shows NO STDOUT
system "rake metric" # shows STDOUT
puts "(--) Finished metric fu run"
else
STDOUT.puts "(--) Current skip counter tells us we shouldn't execute metric fu"
end
end # of watch( %r{src/.+\.(rb)$} ) do |m| # }}}
end # of guard :shell, :all_on_start => false do
guard :rspec, cmd: 'bundle exec rspec' do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/clearbooks/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('lib/clearbooks.rb') { 'spec' }
watch('spec/spec_helper.rb') { 'spec' }
end
guard 'rake', :task => 'docs:generate' do
watch(%r{^lib/clearbooks/(.+)\.rb$})
watch('lib/clearbooks.rb')
watch('README.md.template')
end
### Simple Helpers
# @class class Counter
# @brief Simple module to create a counter to trottle guard shell
class Counter
# @fn def initialize # {{{
# @brief Default constructor for Counter class
#
# @param [Integer] execute_on_mod Execute on modulo n of counter
def initialize execute_on_mod = 3
@file = Tempfile.new( "guard-shell" )
@path = @file.path
@execute_on_mod = execute_on_mod.to_i
puts "(--) Running execute on every #{execute_on_mod.to_s}"
puts "(--) Storing skip counter in #{@path.to_s}"
end # }}}
# @fn def get filename = @path # {{{
# @brief Get counter from dumpfile
#
# @param [String] filename Filename string
#
# @return [Integer] Returns integer counter
def get filename = @path
# Get current counter from file
current = File.open( filename, "r" ).read.strip.to_s
counter = current.tr( "^0-9", "" )
# Sanity check
counter = 0 if( counter == "" )
counter = counter.to_i if( counter.is_a?( String ) )
return counter
end # }}}
# @fn def put counter, filename = @path # {{{
# @brief Put counter to dumpfile
#
# @param [Integer] counter Counter
# @param [String] filename Filename string
def put counter, filename = @path
File.write( filename.to_s, counter )
end # }}}
# @fn def inc filename = @path # {{{
# @brief Increment counter on file
#
# @param [String] filename Filename string
def inc filename = @path
counter = get
counter += 1
put( counter )
end # }}}
# @fn def execute? # {{{
# @brief Boolean switch if counter is modulo the trigger index
#
# @return [Integer] mod Modulo trigger number
# @param [String] filename Filename string
#
# @return [Boolean] Returns true, if mod of trigger index from contstructor is correct, false if not.
def execute? mod = @execute_on_mod
counter = get
result = false
result = true if( (counter % mod) == 0 )
return result
end # }}}
end # of class Counter
@counter = Counter.new( 3 )
# vim:ts=2:tw=100:wm=100:syntax=ruby