forked from mynyml/watchr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchr.rb
102 lines (96 loc) · 2.71 KB
/
watchr.rb
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
require 'pathname'
require 'rbconfig'
# Agile development tool that monitors a directory recursively, and triggers a
# user defined action whenever an observed file is modified. Its most typical
# use is continuous testing.
#
# Usage:
#
# # on command line, from project's root dir
# $ watchr path/to/script
#
# See README for more details
#
module Watchr
autoload :Script, 'watchr/script'
autoload :Controller, 'watchr/controller'
module EventHandler
autoload :Base, 'watchr/event_handlers/base'
autoload :Unix, 'watchr/event_handlers/unix'
autoload :Portable, 'watchr/event_handlers/portable'
end
class << self
attr_accessor :options
attr_accessor :handler
# Options proxy.
#
# Currently supported options:
# * debug<Boolean> Debugging state. More verbose.
#
# ===== Examples
#
# Watchr.options.debug #=> false
# Watchr.options.debug = true
#
# ===== Returns
# options<Struct>:: options proxy.
#
#--
# On first use, initialize the options struct and default option values.
def options
@options ||= Struct.new(:debug).new
@options.debug ||= false
@options
end
# Outputs formatted debug statement to stdout, only if ::options.debug is true
#
# ===== Examples
#
# Watchr.options.debug = true
# Watchr.debug('im in ur codes, notifayinin u')
#
# outputs: "[watchr debug] im in ur codes, notifayinin u"
#
def debug(str)
puts "[watchr debug] #{str}" if options.debug
end
# Detect current OS and return appropriate handler.
#
# NOTE temporarily returns Portable handler for all platforms, until
# issue #1 is fixed
#
# ===== Examples
#
# Config::CONFIG['host_os'] #=> 'linux-gnu'
# Watchr.handler #=> Watchr::EventHandler::Unix
#
# Config::CONFIG['host_os'] #=> 'cygwin'
# Watchr.handler #=> Watchr::EventHandler::Portable
#
# ENV['HANDLER'] #=> 'unix'
# Watchr.handler #=> Watchr::EventHandler::Unix
#
# ENV['HANDLER'] #=> 'portable'
# Watchr.handler #=> Watchr::EventHandler::Portable
#
# ===== Returns
# handler<Class>:: handler class for current architecture
#
def handler
@handler ||=
case ENV['HANDLER'] || Config::CONFIG['host_os']
when /mswin|windows|cygwin/i
Watchr::EventHandler::Portable
when /sunos|solaris|darwin|mach|osx|bsd|linux/i, 'unix'
begin
require 'rev'
Watchr::EventHandler::Unix
rescue LoadError, RuntimeError
Watchr::EventHandler::Portable
end
else
Watchr::EventHandler::Portable
end
end
end
end