guard-rack 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2012 Daniel Doubrovkine
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ Guard::Rack [![Build Status](http://travis-ci.org/johnbintz/guard-rack.png)](http://travis-ci.org/dblock/guard-rack)
2
+ ===========
3
+
4
+ Want to restart your Rack development with *rackup* whilst you work? Now you can!
5
+
6
+ guard 'rack', :port => 9292 do
7
+ watch('Gemfile.lock')
8
+ watch(%r{^(config|lib|app)/.*})
9
+ end
10
+
11
+ Options
12
+ -------
13
+
14
+ * `:port` is the port number to run on (default `9292`)
15
+ * `:environment` is the environment to use (default `development`)
16
+ * `:start_on_start` will start the server when starting Guard (default `true`)
17
+ * `:force_run` kills any process that's holding open the listen port before attempting to (re)start Rack (default `false`).
18
+ * `:daemon` runs the server as a daemon, without any output to the terminal that ran `guard` (default `false`).
19
+ * `:debugger` runs the server with the debugger enabled (default `false`). Required ruby-debug gem.
20
+ * `:timeout` waits this number of seconds when restarting the Rack server before reporting there's a problem (default `20`).
21
+
22
+ Contributing
23
+ ------------
24
+
25
+ Fork the project. Make your feature addition or bug fix with tests. Send a pull request. Bonus points for topic branches.
26
+
27
+ Copyright and License
28
+ ---------------------
29
+
30
+ MIT License, see [LICENSE](http://github.com/dblock/guard-rack/raw/master/LICENSE.md) for details.
31
+
32
+ (c) 2012 [Daniel Doubrovkine](http://github.com/dblock)
33
+
34
+
data/lib/guard-rack.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'guard/rack'
2
+
data/lib/guard/rack.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'guard'
2
+ require 'guard/guard'
3
+ require 'guard/rack/runner'
4
+ require 'rbconfig'
5
+
6
+ module Guard
7
+ class Rack < ::Guard::Guard
8
+ attr_reader :options, :runner
9
+
10
+ DEFAULT_OPTIONS = {
11
+ :port => 9292,
12
+ :environment => 'development',
13
+ :start_on_start => true,
14
+ :force_run => false,
15
+ :timeout => 20,
16
+ :debugger => false
17
+ }
18
+
19
+ def initialize(watchers = [], options = {})
20
+ super
21
+ @options = DEFAULT_OPTIONS.merge(options)
22
+ @runner = ::Guard::RackRunner.new(@options)
23
+ end
24
+
25
+ def start
26
+ server = options[:server] ? "#{options[:server]} and " : ""
27
+ UI.info "Guard::Rack will now restart your app on port #{options[:port]} using #{server}#{options[:environment]} environment."
28
+ reload if options[:start_on_start]
29
+ end
30
+
31
+ def reload
32
+ UI.info "Restarting Rack..."
33
+ Notifier.notify("Rack restarting on port #{options[:port]} in #{options[:environment]} environment...", :title => "Restarting Rack...", :image => :pending)
34
+ if runner.restart
35
+ UI.info "Rack restarted, pid #{runner.pid}"
36
+ Notifier.notify("Rack restarted on port #{options[:port]}.", :title => "Rack restarted!", :image => :success)
37
+ else
38
+ UI.info "Rack NOT restarted, check your log files."
39
+ Notifier.notify("Rack NOT restarted, check your log files.", :title => "Rack NOT restarted!", :image => :failed)
40
+ end
41
+ end
42
+
43
+ def stop
44
+ Notifier.notify("Until next time...", :title => "Rack shutting down.", :image => :pending)
45
+ runner.stop
46
+ end
47
+
48
+ def run_on_change(paths)
49
+ reload
50
+ end
51
+ end
52
+ end
53
+
@@ -0,0 +1,110 @@
1
+ require 'fileutils'
2
+
3
+ module Guard
4
+ class RackRunner
5
+
6
+ MAX_WAIT_COUNT = 10
7
+
8
+ attr_reader :options
9
+
10
+ def initialize(options)
11
+ @options = options
12
+ end
13
+
14
+ def start
15
+ kill_unmanaged_pid! if options[:force_run]
16
+ run_rack_command!
17
+ wait_for_pid
18
+ end
19
+
20
+ def stop
21
+ if File.file?(pid_file)
22
+ system %{kill -KILL #{File.read(pid_file).strip}}
23
+ wait_for_no_pid if $?.exitstatus == 0
24
+ FileUtils.rm pid_file
25
+ end
26
+ end
27
+
28
+ def restart
29
+ stop
30
+ start
31
+ end
32
+
33
+ def build_rack_command
34
+ rack_options = [
35
+ '--env', options[:environment],
36
+ '--port', options[:port],
37
+ '--pid', pid_file
38
+ ]
39
+
40
+ rack_options << '--daemonize' if options[:daemon]
41
+ rack_options << '--debug' if options[:debugger]
42
+
43
+ %{sh -c 'cd #{Dir.pwd} && rackup #{rack_options.join(' ')} &'}
44
+ end
45
+
46
+ def pid_file
47
+ File.expand_path(".guard-rack-#{options[:environment]}.pid")
48
+ end
49
+
50
+ def pid
51
+ File.file?(pid_file) ? File.read(pid_file).to_i : nil
52
+ end
53
+
54
+ def sleep_time
55
+ options[:timeout].to_f / MAX_WAIT_COUNT.to_f
56
+ end
57
+
58
+ private
59
+
60
+ def run_rack_command!
61
+ system build_rack_command
62
+ end
63
+
64
+ def has_pid?
65
+ File.file?(pid_file)
66
+ end
67
+
68
+ def wait_for_pid_action
69
+ sleep sleep_time
70
+ end
71
+
72
+ def kill_unmanaged_pid!
73
+ if pid = unmanaged_pid
74
+ system %{kill -KILL #{pid}}
75
+ FileUtils.rm pid_file
76
+ wait_for_no_pid
77
+ end
78
+ end
79
+
80
+ def unmanaged_pid
81
+ %x{lsof -n -i TCP:#{options[:port]}}.each_line { |line|
82
+ if line["*:#{options[:port]} "]
83
+ return line.split("\s")[1]
84
+ end
85
+ }
86
+ nil
87
+ end
88
+
89
+ private
90
+
91
+ def wait_for_pid
92
+ wait_for_pid_loop
93
+ end
94
+
95
+ def wait_for_no_pid
96
+ wait_for_pid_loop(false)
97
+ end
98
+
99
+ def wait_for_pid_loop(check_for_existince = true)
100
+ count = 0
101
+ while !(check_for_existince ? has_pid? : !has_pid?) && count < MAX_WAIT_COUNT
102
+ wait_for_pid_action
103
+ count += 1
104
+ end
105
+ !(count == MAX_WAIT_COUNT)
106
+ end
107
+
108
+ end
109
+ end
110
+
@@ -0,0 +1,5 @@
1
+ guard 'rack' do
2
+ watch('Gemfile.lock')
3
+ watch(%r{^(config|lib|app)/.*})
4
+ end
5
+
@@ -0,0 +1,6 @@
1
+ module Guard
2
+ module RackVersion
3
+ VERSION = '1.0'
4
+ end
5
+ end
6
+
metadata ADDED
@@ -0,0 +1,310 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guard-rack
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Doubrovkine
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-23 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: guard-rack
16
+ requirement: &81364010 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *81364010
25
+ - !ruby/object:Gem::Dependency
26
+ name: guard
27
+ requirement: &81363220 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *81363220
36
+ - !ruby/object:Gem::Dependency
37
+ name: rb-inotify
38
+ requirement: &81362390 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 0.5.1
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *81362390
47
+ - !ruby/object:Gem::Dependency
48
+ name: libnotify
49
+ requirement: &81361750 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.3
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *81361750
58
+ - !ruby/object:Gem::Dependency
59
+ name: mocha
60
+ requirement: &81361280 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *81361280
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: &81360690 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *81360690
80
+ - !ruby/object:Gem::Dependency
81
+ name: bundler
82
+ requirement: &81360080 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ~>
86
+ - !ruby/object:Gem::Version
87
+ version: '1.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *81360080
91
+ - !ruby/object:Gem::Dependency
92
+ name: rspec
93
+ requirement: &81359470 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ~>
97
+ - !ruby/object:Gem::Version
98
+ version: '2.6'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *81359470
102
+ - !ruby/object:Gem::Dependency
103
+ name: jeweler
104
+ requirement: &81336310 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '1.6'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *81336310
113
+ - !ruby/object:Gem::Dependency
114
+ name: guard-rspec
115
+ requirement: &81335760 !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ type: :development
122
+ prerelease: false
123
+ version_requirements: *81335760
124
+ - !ruby/object:Gem::Dependency
125
+ name: fakefs
126
+ requirement: &81335090 !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: *81335090
135
+ - !ruby/object:Gem::Dependency
136
+ name: rake
137
+ requirement: &81334550 !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ type: :development
144
+ prerelease: false
145
+ version_requirements: *81334550
146
+ - !ruby/object:Gem::Dependency
147
+ name: bundler
148
+ requirement: &81333950 !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ~>
152
+ - !ruby/object:Gem::Version
153
+ version: '1.0'
154
+ type: :development
155
+ prerelease: false
156
+ version_requirements: *81333950
157
+ - !ruby/object:Gem::Dependency
158
+ name: rspec
159
+ requirement: &81333420 !ruby/object:Gem::Requirement
160
+ none: false
161
+ requirements:
162
+ - - ~>
163
+ - !ruby/object:Gem::Version
164
+ version: '2.6'
165
+ type: :development
166
+ prerelease: false
167
+ version_requirements: *81333420
168
+ - !ruby/object:Gem::Dependency
169
+ name: jeweler
170
+ requirement: &81332860 !ruby/object:Gem::Requirement
171
+ none: false
172
+ requirements:
173
+ - - ~>
174
+ - !ruby/object:Gem::Version
175
+ version: '1.6'
176
+ type: :development
177
+ prerelease: false
178
+ version_requirements: *81332860
179
+ - !ruby/object:Gem::Dependency
180
+ name: guard-rspec
181
+ requirement: &81332400 !ruby/object:Gem::Requirement
182
+ none: false
183
+ requirements:
184
+ - - ! '>='
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ type: :development
188
+ prerelease: false
189
+ version_requirements: *81332400
190
+ - !ruby/object:Gem::Dependency
191
+ name: fakefs
192
+ requirement: &81331910 !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ! '>='
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ type: :development
199
+ prerelease: false
200
+ version_requirements: *81331910
201
+ - !ruby/object:Gem::Dependency
202
+ name: rake
203
+ requirement: &81331400 !ruby/object:Gem::Requirement
204
+ none: false
205
+ requirements:
206
+ - - ! '>='
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ type: :development
210
+ prerelease: false
211
+ version_requirements: *81331400
212
+ - !ruby/object:Gem::Dependency
213
+ name: bundler
214
+ requirement: &81330920 !ruby/object:Gem::Requirement
215
+ none: false
216
+ requirements:
217
+ - - ~>
218
+ - !ruby/object:Gem::Version
219
+ version: '1.0'
220
+ type: :development
221
+ prerelease: false
222
+ version_requirements: *81330920
223
+ - !ruby/object:Gem::Dependency
224
+ name: rspec
225
+ requirement: &81330330 !ruby/object:Gem::Requirement
226
+ none: false
227
+ requirements:
228
+ - - ~>
229
+ - !ruby/object:Gem::Version
230
+ version: '2.6'
231
+ type: :development
232
+ prerelease: false
233
+ version_requirements: *81330330
234
+ - !ruby/object:Gem::Dependency
235
+ name: jeweler
236
+ requirement: &81329900 !ruby/object:Gem::Requirement
237
+ none: false
238
+ requirements:
239
+ - - ~>
240
+ - !ruby/object:Gem::Version
241
+ version: '1.6'
242
+ type: :development
243
+ prerelease: false
244
+ version_requirements: *81329900
245
+ - !ruby/object:Gem::Dependency
246
+ name: guard-rspec
247
+ requirement: &81329360 !ruby/object:Gem::Requirement
248
+ none: false
249
+ requirements:
250
+ - - ! '>='
251
+ - !ruby/object:Gem::Version
252
+ version: '0'
253
+ type: :development
254
+ prerelease: false
255
+ version_requirements: *81329360
256
+ - !ruby/object:Gem::Dependency
257
+ name: fakefs
258
+ requirement: &81328770 !ruby/object:Gem::Requirement
259
+ none: false
260
+ requirements:
261
+ - - ! '>='
262
+ - !ruby/object:Gem::Version
263
+ version: '0'
264
+ type: :development
265
+ prerelease: false
266
+ version_requirements: *81328770
267
+ description:
268
+ email: dblock@dblock.org
269
+ executables: []
270
+ extensions: []
271
+ extra_rdoc_files:
272
+ - LICENSE.md
273
+ - README.md
274
+ files:
275
+ - lib/guard-rack.rb
276
+ - lib/guard/rack.rb
277
+ - lib/guard/rack/runner.rb
278
+ - lib/guard/rack/templates/Guardfile
279
+ - lib/guard/rack/version.rb
280
+ - LICENSE.md
281
+ - README.md
282
+ homepage: http://github.com/dblock/guard-rack
283
+ licenses:
284
+ - MIT
285
+ post_install_message:
286
+ rdoc_options: []
287
+ require_paths:
288
+ - lib
289
+ required_ruby_version: !ruby/object:Gem::Requirement
290
+ none: false
291
+ requirements:
292
+ - - ! '>='
293
+ - !ruby/object:Gem::Version
294
+ version: '0'
295
+ segments:
296
+ - 0
297
+ hash: -587883089
298
+ required_rubygems_version: !ruby/object:Gem::Requirement
299
+ none: false
300
+ requirements:
301
+ - - ! '>='
302
+ - !ruby/object:Gem::Version
303
+ version: '0'
304
+ requirements: []
305
+ rubyforge_project:
306
+ rubygems_version: 1.8.10
307
+ signing_key:
308
+ specification_version: 3
309
+ summary: Restart Rack when files change.
310
+ test_files: []