module Seaweed

Constants

CONFIG_PATHS
PROJECT_ROOT
ROOT
VERSION

Public Class Methods

all_paths() click to toggle source
# File lib/seaweed.rb, line 57
def self.all_paths
  libs + specs
end
close_browser() click to toggle source
# File lib/seaweed.rb, line 109
def self.close_browser
  @browser.close
  @browser = nil
end
libs() click to toggle source
# File lib/seaweed.rb, line 49
def self.libs
  @configuration['libs']
end
load_configuration() click to toggle source
# File lib/seaweed.rb, line 22
def self.load_configuration
  # Set configuration defaults
  @configuration['port']    = 4567
  @configuration['libs']    = ['lib']
  @configuration['specs']   = ['spec']
  
  # Load custom configuration file
  CONFIG_PATHS.each do |path|
    if File.exists? path
      @configuration.merge! YAML.load(File.read(path))
      puts "Loaded configuration from “#{path}”"
    end
  end
end
port() click to toggle source
# File lib/seaweed.rb, line 37
def self.port
  @configuration['port']
end
port=(value) click to toggle source
# File lib/seaweed.rb, line 41
def self.port= value
  @configuration['port'] = value
end
root_url() click to toggle source
# File lib/seaweed.rb, line 45
def self.root_url
  "http://localhost:#{port}/"
end
run_suite() click to toggle source
# File lib/seaweed.rb, line 97
def self.run_suite
  if @browser
    @browser.navigate.refresh
  else
    @browser = Selenium::WebDriver.for :firefox, profile: Selenium::WebDriver::Firefox::Profile.new
    @browser.get "#{root_url}#terminal"
  end
  result = @browser[css: '.results'].text
  puts result
  !!result.match('passed, 0 failed')
end
spawn_server() click to toggle source
# File lib/seaweed.rb, line 84
def self.spawn_server
  # Start server in its own thread
  server = Thread.new &method(:start_server)
  
  # Keep trying to connect to server until we succeed
  begin
    page = Net::HTTP.get URI.parse(root_url)
  rescue Errno::ECONNREFUSED
    sleep 1
    retry
  end
end
specs() click to toggle source
# File lib/seaweed.rb, line 53
def self.specs
  @configuration['specs']
end
sprockets_environment() click to toggle source

Prepares a Sprockets::Environment object to serve coffeescript assets

# File lib/seaweed.rb, line 62
def self.sprockets_environment
  @environment ||= Sprockets::Environment.new.tap do |environment|
    environment.append_path File.join(Seaweed::ROOT, 'lib')
    all_paths.each do |path|
      environment.append_path path
    end
  end
end
start_server() click to toggle source
# File lib/seaweed.rb, line 71
def self.start_server
  app = Rack::Builder.app do
    map '/assets' do
      run Seaweed.sprockets_environment
    end
    
    map '/' do
      run Seaweed::Server
    end
  end
  Rack::Handler.default.run app, :Port => port
end
watch_for_changes() click to toggle source
# File lib/seaweed.rb, line 114
def self.watch_for_changes
  require 'watchr'
  
  # Build a regexp to match .coffee files in any project paths
  path_matcher = Regexp.new('^(' + all_paths.map{ |s| Regexp.escape s}.join('|') + ')\/.*\.coffee$')
  
  script = Watchr::Script.new
  script.watch(path_matcher) { run_suite }
  controller = Watchr::Controller.new(script, Watchr.handler.new)
  controller.run
end