guard-shotgun 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .rvmrc
6
+ vendor/bundle*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2011 Fletcher Nichol
2
+ Copyright (c) 2012 Romain Champourlier
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,89 @@
1
+ # Guard::Shotgun
2
+
3
+ Guard::Shotgun automatically starts and restarts Sinatra (through `rackup`) when needed. As useful as Shotgun when developing a Sinatra app.
4
+
5
+ * Tested on Ruby 1.9.2-p290 only.
6
+
7
+ ## Why?
8
+
9
+ * You are **developing with Sinatra** and you have to **restart your development server each time you change your source code**?
10
+ * You are **using Shotgun** to do this, but well, the **latest version is not showing the logs anymore** in your console, which makes developing a little harder?
11
+
12
+ If you have answered 'yes' to any of these questions, you may find some use to this plugin.
13
+
14
+ ## Install
15
+
16
+ Please be sure to have [Guard](http://github.com/guard/guard) installed before continue.
17
+
18
+ Install the gem:
19
+
20
+ gem install guard-shotgun
21
+
22
+ Or add it to your Gemfile (inside development group):
23
+
24
+ gem 'guard-shotgun', :git => https://github.com/rchampourlier/guard-shotgun.git
25
+
26
+ Add guard definition to your Guardfile by running this command:
27
+
28
+ guard init shotgun
29
+
30
+ ## Usage
31
+
32
+ This guard plugin is intended to be used when **developing a Sinatra application**, loaded through `rackup`.
33
+
34
+ **It allows automatic reloading of your Sinatra server when a file is changed.**
35
+
36
+ > It provides the same service as the **Shotgun** gem, relying on **Guard** to watch for your files.
37
+
38
+ Please read [Guard usage doc](http://github.com/guard/guard#readme)
39
+
40
+ ## Guardfile
41
+
42
+ For example, to look at the `main.rb` file in your application directory, just set this guard:
43
+
44
+ guard 'shotgun' do
45
+ watch('main.rb')
46
+ end
47
+
48
+ Please read [Guard doc](http://github.com/guard/guard#readme) for more info about Guardfile DSL.
49
+
50
+
51
+ ## Options
52
+
53
+ Currently there is no option.
54
+
55
+ ## History
56
+
57
+ #### v0.0.4
58
+
59
+ Killing Sinatra when reloading on change without waiting for requests to be completed.
60
+
61
+ #### v0.0.3
62
+
63
+ Initial release
64
+
65
+ ## TODOs
66
+
67
+ * Add some options: host, port...
68
+ * Allow starting Sinatra applications without using rackup.
69
+ * Tests.
70
+
71
+ Help is welcome!
72
+
73
+
74
+ ## Development
75
+
76
+ * Source hosted at [GitHub](http://github.com/rchampourlier/guard-shotgun)
77
+ * Report issues/Questions/Feature requests on [GitHub Issues](http://github.com/rchampourlier/guard-shotgun/issues)
78
+
79
+ Pull requests are very welcome! Make sure your patches are well tested. Please create a topic branch for every separate change
80
+ you make.
81
+
82
+ ## Authors
83
+
84
+ [Romain Champourlier](http://github.com/rchampourlier)
85
+
86
+
87
+ ## Credits
88
+
89
+ This gem has been built from [guard-webrick](https://github.com/guard/guard-webrick) of [Fletcher Nichol](http://github.com/fnichol).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "guard-shotgun"
5
+ gemspec.summary = "Guard gem for Sinatra (shotgun-like)"
6
+ gemspec.description = "Guard gem for Sinatra (shotgun-like)"
7
+ gemspec.email = "romain@softr.li"
8
+ gemspec.homepage = "http://github.com/rchampourlier/guard-shotgun"
9
+ gemspec.authors = ["romain@softr.li"]
10
+ end
11
+ rescue LoadError
12
+ puts "Jeweler not available. Install it with: gem install jeweler"
13
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.5
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/guard/shotgun/version', __FILE__)
3
+ require 'date'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "guard-shotgun"
7
+ gem.version = Guard::ShotgunVersion::VERSION
8
+ gem.authors = ["romain@softr.li", "Colin Rymer"]
9
+ gem.email = ["romain@softr.li", "colin.rymer@gmail.com"]
10
+ gem.description = 'This gem provides a Guard that restarts Rack apps when watched files are modified similar. Similar to the wonderful Shotgun by rtomayko'
11
+ gem.summary = 'Shotgun-like Guard for Rack apps'
12
+ gem.date = Date.today.to_s
13
+ gem.homepage = "http://github.com/crymer11/guard-shotgun"
14
+ gem.license = 'MIT'
15
+ gem.executables = []
16
+ gem.files = `git ls-files`.split("\n")
17
+ gem.test_files = `git ls-files -- spec/*`.split("\n")
18
+ gem.extra_rdoc_files = [
19
+ "LICENSE",
20
+ "README.md"
21
+ ]
22
+ gem.require_paths = ["lib"]
23
+ gem.required_ruby_version = '>= 1.9'
24
+
25
+ gem.add_dependency 'guard'
26
+ gem.add_dependency 'ffi'
27
+ gem.add_dependency 'spoon'
28
+ end
29
+
@@ -0,0 +1,136 @@
1
+ require 'guard'
2
+ require 'guard/guard'
3
+ require 'spoon'
4
+ require 'socket'
5
+ require 'timeout'
6
+
7
+ module Guard
8
+ class Shotgun < Guard
9
+ VALID_ARGS = %w{server host port env daemonize pid option}
10
+
11
+ autoload :Notifier, 'guard/shotgun/notifier'
12
+ attr_accessor :pid
13
+
14
+ def initialize(watchers=[], options={})
15
+ super
16
+ @options = {
17
+ host: 'localhost',
18
+ port: 9292,
19
+ server: "WEBrick"
20
+ }.update(options) { |key, oldval, newval| (newval.nil? || newval.empty?) ? oldval : newval }
21
+ @reloaded = false
22
+ end
23
+
24
+ # =================
25
+ # = Guard methods =
26
+ # =================
27
+
28
+ # Call once when guard starts
29
+ def start
30
+ UI.info "Starting up Rack..."
31
+ if running?
32
+ UI.error "Another instance of Rack is running."
33
+ false
34
+ else
35
+ @pid = Spoon.spawnp 'rackup', *options_array, (config_file if config_file)
36
+ end
37
+ wait_for_port
38
+ Notifier.notify(@reloaded ? 'reloaded' : 'up')
39
+ @reloaded = false
40
+ end
41
+
42
+ # Call with Ctrl-C signal (when Guard quit)
43
+ def stop
44
+ UI.info "Shutting down Rack..."
45
+ Process.kill("TERM", @pid)
46
+ Process.wait(@pid)
47
+ @pid = nil
48
+ true
49
+ end
50
+
51
+ def stop_without_waiting
52
+ UI.info "Shutting down Rack without waiting..."
53
+ Process.kill("KILL", @pid)
54
+ Process.wait(@pid)
55
+ @pid = nil
56
+ true
57
+ end
58
+
59
+ # Call with Ctrl-Z signal
60
+ def reload
61
+ @reloaded = true
62
+ restart
63
+ end
64
+
65
+ # Call on file(s) modifications
66
+ def run_on_change(paths = {})
67
+ @reloaded = true
68
+ restart_without_waiting
69
+ end
70
+
71
+ private
72
+
73
+ def config_file
74
+ @options.fetch :config, nil
75
+ end
76
+
77
+ def options_array
78
+ @options.each_with_object([]) do |(key, val), array|
79
+ key = key.to_s.downcase
80
+ if VALID_ARGS.include? key
81
+ array << "--#{key}" << val.to_s
82
+ end
83
+ end
84
+ end
85
+
86
+ def restart_without_waiting
87
+ UI.info "Restarting Rack without waiting..."
88
+ stop_without_waiting
89
+ start
90
+ end
91
+
92
+ def restart
93
+ UI.info "Restarting Rack..."
94
+ stop
95
+ start
96
+ end
97
+
98
+ def running?
99
+ begin
100
+ if @pid
101
+ Process.getpgid @pid
102
+ true
103
+ else
104
+ false
105
+ end
106
+ rescue Errno::ESRCH
107
+ false
108
+ end
109
+ end
110
+
111
+ def wait_for_port
112
+ while true do
113
+ sleep 0.2
114
+ break if port_open?(@options[:host], @options[:port])
115
+ end
116
+ end
117
+
118
+ # thanks to: http://bit.ly/bVN5AQ
119
+ def port_open?(addr, port)
120
+ begin
121
+ Timeout::timeout(1) do
122
+ begin
123
+ s = TCPSocket.new(addr, port)
124
+ s.close
125
+ return true
126
+ rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
127
+ return false
128
+ end
129
+ end
130
+ rescue Timeout::Error
131
+ end
132
+
133
+ return false
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,32 @@
1
+ module Guard
2
+ class Shotgun
3
+ class Notifier
4
+
5
+ def self.guard_message(result)
6
+ case result
7
+ when 'up'
8
+ "Sinatra up and running"
9
+ when 'reloaded'
10
+ "Sinatra reloaded"
11
+ end
12
+ end
13
+
14
+ # failed | success
15
+ def self.guard_image(result)
16
+ icon = if result == 'ready'
17
+ :success
18
+ else
19
+ :failed
20
+ end
21
+ end
22
+
23
+ def self.notify(result)
24
+ message = guard_message(result)
25
+ image = guard_image(result)
26
+
27
+ ::Guard::Notifier.notify(message, :title => 'guard-shotgun', :image => image)
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,2 @@
1
+ guard :shotgun do
2
+ end
@@ -0,0 +1,5 @@
1
+ module Guard
2
+ module ShotgunVersion
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
File without changes
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: guard-shotgun
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - romain@softr.li
9
+ - Colin Rymer
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-12-06 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ none: false
22
+ name: guard
23
+ type: :runtime
24
+ prerelease: false
25
+ requirement: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ none: false
31
+ - !ruby/object:Gem::Dependency
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ none: false
38
+ name: ffi
39
+ type: :runtime
40
+ prerelease: false
41
+ requirement: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ none: false
47
+ - !ruby/object:Gem::Dependency
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ none: false
54
+ name: spoon
55
+ type: :runtime
56
+ prerelease: false
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ none: false
63
+ description: This gem provides a Guard that restarts Rack apps when watched files
64
+ are modified similar. Similar to the wonderful Shotgun by rtomayko
65
+ email:
66
+ - romain@softr.li
67
+ - colin.rymer@gmail.com
68
+ executables: []
69
+ extensions: []
70
+ extra_rdoc_files:
71
+ - LICENSE
72
+ - README.md
73
+ files:
74
+ - .gitignore
75
+ - Gemfile
76
+ - LICENSE
77
+ - README.md
78
+ - Rakefile
79
+ - VERSION
80
+ - guard-shotgun.gemspec
81
+ - lib/guard/shotgun.rb
82
+ - lib/guard/shotgun/notifier.rb
83
+ - lib/guard/shotgun/templates/Guardfile
84
+ - lib/guard/shotgun/version.rb
85
+ - spec/spec_helper.rb
86
+ homepage: http://github.com/crymer11/guard-shotgun
87
+ licenses:
88
+ - MIT
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '1.9'
98
+ none: false
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ none: false
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 1.8.23
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Shotgun-like Guard for Rack apps
111
+ test_files:
112
+ - spec/spec_helper.rb