1 | desc %(Starts the test server and opens it in a web browser)
|
2 | multitask :default => ['test:server', 'test:open']
|
3 |
|
4 | PORT = 4567
|
5 |
|
6 | namespace :test do
|
7 | desc %(Starts the test server)
|
8 | task :server do
|
9 | system 'bundle exec ruby test/server.rb'
|
10 | end
|
11 |
|
12 | desc %(Starts the test server which reloads everything on each refresh)
|
13 | task :reloadable do
|
14 | exec "bundle exec shotgun test/config.ru -p #{PORT} --server thin"
|
15 | end
|
16 |
|
17 | task :open do
|
18 | url = "http://localhost:#{PORT}"
|
19 | puts "Opening test app at #{url} ..."
|
20 | sleep 3
|
21 | system( *browse_cmd(url) )
|
22 | end
|
23 | end
|
24 |
|
25 | # Returns an array e.g.: ['open', 'http://example.com']
|
26 | def browse_cmd(url)
|
27 | require 'rbconfig'
|
28 | browser = ENV['BROWSER'] ||
|
29 | (RbConfig::CONFIG['host_os'].include?('darwin') && 'open') ||
|
30 | (RbConfig::CONFIG['host_os'] =~ /msdos|mswin|djgpp|mingw|windows/ && 'start') ||
|
31 | %w[xdg-open x-www-browser firefox opera mozilla netscape].find { |comm| which comm }
|
32 |
|
33 | abort('ERROR: no web browser detected') unless browser
|
34 | Array(browser) << url
|
35 | end
|
36 |
|
37 | # which('ruby') #=> /usr/bin/ruby
|
38 | def which cmd
|
39 | exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
|
40 | ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
|
41 | exts.each { |ext|
|
42 | exe = "#{path}/#{cmd}#{ext}"
|
43 | return exe if File.executable? exe
|
44 | }
|
45 | end
|
46 | return nil
|
47 | end
|