Skip to content

Commit b614d94

Browse files
committed
Extract listen watcher to separate gem
https://github.com/jonleighton/spring-watcher-listen The reason to do this is that I want to minimise the amount of code which needs to be maintained in the core spring project. I don't use the listen watcher and therefore don't want to maintain it. If it's a separate gem, others can maintain it on their own schedule.
1 parent 403a6da commit b614d94

File tree

10 files changed

+83
-158
lines changed

10 files changed

+83
-158
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* Watch `config/secrets.yml` by default. #289 - @morgoth
55
* Change monkey-patched `Kernel.raise` from public to private (to match default Ruby behavior) #351 - @mattbrictson
66
* Let application_id also respect RUBY_VERSION for the use case of switching between Ruby versions for a given Rails app - @methodmissing
7+
* Extract the 'listen' watcher to a separate `spring-watcher-listen`
8+
gem. This allows it to be developed/maintained separately.
79

810
## 1.1.3
911

Gemfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@ source 'https://rubygems.org'
22

33
# Specify your gem's dependencies in spring.gemspec
44
gemspec
5-
6-
gem 'listen', "~> 1.0", :require => false

README.md

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -346,23 +346,10 @@ Spring.watch "config/some_config_file.yml"
346346

347347
By default Spring polls the filesystem for changes once every 0.2 seconds. This
348348
method requires zero configuration, but if you find that it's using too
349-
much CPU, then you can turn on event-based file system listening:
350-
351-
```ruby
352-
Spring.watch_method = :listen
353-
```
354-
355-
You will need to add the [`listen` gem](https://github.com/guard/listen) to your `Gemfile`
356-
if you don't already have it:
357-
358-
```ruby
359-
group :development do
360-
gem 'listen', '~> 1.0'
361-
end
362-
```
363-
364-
Note that Spring does not currently support version 2 of the listen gem.
365-
See pull request [#194](https://github.com/rails/spring/pull/194) for more details.
349+
much CPU, then you can use event-based file system listening by
350+
installing the
351+
[spring-watcher-listen](https://github.com/jonleighton/spring-watcher-listen)
352+
gem.
366353

367354
## Troubleshooting
368355

lib/spring/commands.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,17 @@ def self.command(name)
3232
# then we need to be under bundler.
3333
require "bundler/setup"
3434

35-
Gem::Specification.map(&:name).grep(/^spring-commands-/).each do |command|
36-
require command
35+
# Auto-require any spring extensions which are in the Gemfile
36+
Gem::Specification.map(&:name).grep(/^spring-/).each do |command|
37+
begin
38+
require command
39+
rescue LoadError => error
40+
if error.message.include?(command)
41+
require command.gsub("-", "/")
42+
else
43+
raise
44+
end
45+
end
3746
end
3847

3948
config = File.expand_path("./config/spring.rb")

lib/spring/test/acceptance_test.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ def rails_version
1616
ENV['RAILS_VERSION'] || '~> 4.0.0'
1717
end
1818

19+
# Extension point for spring-watchers-listen
20+
def generator_klass
21+
Spring::Test::ApplicationGenerator
22+
end
23+
1924
def generator
20-
@@generator ||= Spring::Test::ApplicationGenerator.new(rails_version)
25+
@@generator ||= generator_klass.new(rails_version)
2126
end
2227

2328
def app

lib/spring/test/application_generator.rb

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ def initialize(version_constraint)
1010
@bundled = false
1111
end
1212

13+
def test_root
14+
Pathname.new Spring::Test.root
15+
end
16+
1317
def root
14-
"#{Spring::Test.root}/apps/rails-#{version.major}-#{version.minor}-spring-#{Spring::VERSION}"
18+
test_root.join("apps/rails-#{version.major}-#{version.minor}-spring-#{Spring::VERSION}")
1519
end
1620

1721
def system(command)
@@ -25,41 +29,40 @@ def system(command)
2529
puts if ENV["SPRING_DEBUG"]
2630
end
2731

28-
# Sporadic SSL errors keep causing test failures so there are anti-SSL workarounds here
2932
def generate
30-
Bundler.with_clean_env do
31-
system("gem list rails --installed --version '#{version_constraint}' || " \
32-
"gem install rails --clear-sources --source http://rubygems.org --version '#{version_constraint}'")
33-
34-
@version = RailsVersion.new(`ruby -e 'puts Gem::Specification.find_by_name("rails", "#{version_constraint}").version'`.chomp)
33+
Bundler.with_clean_env { generate_files }
34+
install_spring
35+
generate_scaffold
36+
end
3537

36-
skips = %w(--skip-bundle --skip-javascript --skip-sprockets)
37-
skips << "--skip-spring" if version.bundles_spring?
38+
# Sporadic SSL errors keep causing test failures so there are anti-SSL workarounds here
39+
def generate_files
40+
system("gem list rails --installed --version '#{version_constraint}' || " \
41+
"gem install rails --clear-sources --source http://rubygems.org --version '#{version_constraint}'")
3842

39-
system("rails _#{version}_ new #{application.root} #{skips.join(' ')}")
40-
raise "application generation failed" unless application.exists?
43+
@version = RailsVersion.new(`ruby -e 'puts Gem::Specification.find_by_name("rails", "#{version_constraint}").version'`.chomp)
4144

42-
FileUtils.mkdir_p(application.gem_home)
43-
FileUtils.mkdir_p(application.user_home)
44-
FileUtils.rm_rf(application.path("test/performance"))
45+
skips = %w(--skip-bundle --skip-javascript --skip-sprockets)
46+
skips << "--skip-spring" if version.bundles_spring?
4547

46-
File.write(application.gemfile, "#{application.gemfile.read}gem 'spring', '#{Spring::VERSION}'\n")
48+
system("rails _#{version}_ new #{application.root} #{skips.join(' ')}")
49+
raise "application generation failed" unless application.exists?
4750

48-
if version.needs_testunit?
49-
File.write(application.gemfile, "#{application.gemfile.read}gem 'spring-commands-testunit'\n")
50-
end
51+
FileUtils.mkdir_p(application.gem_home)
52+
FileUtils.mkdir_p(application.user_home)
53+
FileUtils.rm_rf(application.path("test/performance"))
5154

52-
File.write(application.gemfile, application.gemfile.read.sub("https://rubygems.org", "http://rubygems.org"))
55+
File.write(application.gemfile, "#{application.gemfile.read}gem 'spring', '#{Spring::VERSION}'\n")
5356

54-
if application.path("bin").exist?
55-
FileUtils.cp_r(application.path("bin"), application.path("bin_original"))
56-
end
57+
if version.needs_testunit?
58+
File.write(application.gemfile, "#{application.gemfile.read}gem 'spring-commands-testunit'\n")
5759
end
5860

59-
install_spring
61+
File.write(application.gemfile, application.gemfile.read.sub("https://rubygems.org", "http://rubygems.org"))
6062

61-
application.run! "bundle exec rails g scaffold post title:string"
62-
application.run! "bundle exec rake db:migrate db:test:clone"
63+
if application.path("bin").exist?
64+
FileUtils.cp_r(application.path("bin"), application.path("bin_original"))
65+
end
6366
end
6467

6568
def generate_if_missing
@@ -69,8 +72,7 @@ def generate_if_missing
6972
def install_spring
7073
return if @installed
7174

72-
system("gem build spring.gemspec 2>&1")
73-
application.run! "gem install ../../../spring-#{Spring::VERSION}.gem", timeout: nil
75+
build_and_install_gems
7476

7577
application.bundle
7678

@@ -84,10 +86,36 @@ def install_spring
8486
@installed = true
8587
end
8688

89+
def manually_built_gems
90+
%w(spring)
91+
end
92+
93+
def build_and_install_gems
94+
manually_built_gems.each do |name|
95+
spec = Gem::Specification.find_by_name(name)
96+
97+
FileUtils.cd(spec.gem_dir) do
98+
FileUtils.rm(Dir.glob("#{name}-*.gem"))
99+
system("gem build #{name}.gemspec 2>&1")
100+
end
101+
102+
application.run! "gem install #{spec.gem_dir}/#{name}-*.gem", timeout: nil
103+
end
104+
end
105+
87106
def copy_to(path)
88107
system("rm -rf #{path}")
89108
system("cp -r #{application.root} #{path}")
90109
end
110+
111+
def generate_scaffold
112+
application.run! "bundle exec rails g scaffold post title:string"
113+
application.run! "bundle exec rake db:migrate db:test:clone"
114+
end
115+
116+
def gemspec(name)
117+
"#{Gem::Specification.find_by_name(name).gem_dir}/#{name}.gemspec"
118+
end
91119
end
92120
end
93121
end

lib/spring/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Spring
2-
VERSION = "1.1.3"
2+
VERSION = "1.2.0"
33
end

lib/spring/watcher.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@ class << self
99
end
1010

1111
def self.watch_method=(method)
12-
case method
13-
when :polling
14-
require_relative "watcher/polling"
15-
@watch_method = Watcher::Polling
16-
when :listen
17-
require_relative "watcher/listen"
18-
@watch_method = Watcher::Listen
19-
else
12+
if method.is_a?(Class)
2013
@watch_method = method
14+
else
15+
require "spring/watcher/#{method}"
16+
@watch_method = Watcher.const_get(method.to_s.gsub(/(^.|_.)/) { $1[-1].upcase })
2117
end
2218
end
2319

lib/spring/watcher/listen.rb

Lines changed: 0 additions & 54 deletions
This file was deleted.

test/unit/watcher_test.rb

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,6 @@
11
require "helper"
22
require "spring/test/watcher_test"
33
require "spring/watcher/polling"
4-
require "spring/watcher/listen"
5-
6-
class ListenWatcherTest < Spring::Test::WatcherTest
7-
def watcher_class
8-
Spring::Watcher::Listen
9-
end
10-
11-
test "root directories" do
12-
begin
13-
other_dir_1 = File.realpath(Dir.mktmpdir)
14-
other_dir_2 = File.realpath(Dir.mktmpdir)
15-
File.write("#{other_dir_1}/foo", "foo")
16-
File.write("#{dir}/foo", "foo")
17-
18-
watcher.add "#{other_dir_1}/foo"
19-
watcher.add other_dir_2
20-
watcher.add "#{dir}/foo"
21-
22-
assert_equal [dir, other_dir_1, other_dir_2].sort, watcher.base_directories.sort
23-
ensure
24-
FileUtils.rmdir other_dir_1
25-
FileUtils.rmdir other_dir_2
26-
end
27-
end
28-
29-
test "root directories with a root subpath directory" do
30-
begin
31-
other_dir_1 = "#{dir}_other"
32-
other_dir_2 = "#{dir}_core"
33-
# same subpath as dir but with _other or _core appended
34-
FileUtils::mkdir_p(other_dir_1)
35-
FileUtils::mkdir_p(other_dir_2)
36-
File.write("#{other_dir_1}/foo", "foo")
37-
File.write("#{other_dir_2}/foo", "foo")
38-
File.write("#{dir}/foo", "foo")
39-
40-
watcher.add "#{other_dir_1}/foo"
41-
watcher.add other_dir_2
42-
43-
assert_equal [dir, other_dir_1, other_dir_2].sort, watcher.base_directories.sort
44-
ensure
45-
FileUtils.rmdir other_dir_1
46-
FileUtils.rmdir other_dir_2
47-
end
48-
end
49-
end
504

515
class PollingWatcherTest < Spring::Test::WatcherTest
526
def watcher_class

0 commit comments

Comments
 (0)