Module: Seaweed

Defined in:
lib/seaweed.rb,
lib/seaweed/runner.rb,
lib/seaweed/server.rb,
lib/seaweed/version.rb

Defined Under Namespace

Classes: Runner, Server

Constant Summary collapse

ROOT =
File.expand_path File.join(File.dirname(__FILE__), '..')
PROJECT_ROOT =
File.expand_path "."
CONFIG_PATHS =
[
  File.join(PROJECT_ROOT, 'seaweed.yml'),
  File.join(PROJECT_ROOT, 'config', 'seaweed.yml')
]
VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.all_pathsObject



56
57
58
# File 'lib/seaweed.rb', line 56

def self.all_paths
  libs + specs
end

.libsObject



48
49
50
# File 'lib/seaweed.rb', line 48

def self.libs
  @configuration['libs']
end

.load_configurationObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/seaweed.rb', line 21

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

.portObject



36
37
38
# File 'lib/seaweed.rb', line 36

def self.port
  @configuration['port']
end

.port=(value) ⇒ Object



40
41
42
# File 'lib/seaweed.rb', line 40

def self.port= value
  @configuration['port'] = value
end

.root_urlObject



44
45
46
# File 'lib/seaweed.rb', line 44

def self.root_url
  "http://localhost:#{port}/"
end

.run_suiteObject



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/seaweed.rb', line 96

def self.run_suite
  require 'celerity'
  
  if @browser
    @browser.refresh
  else
    @browser = Celerity::Browser.new
    @browser.goto "#{root_url}#terminal"
  end
  puts @browser.text
end

.spawn_serverObject



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/seaweed.rb', line 83

def self.spawn_server
  # Start server in its own thread
  Thread.new &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

.specsObject



52
53
54
# File 'lib/seaweed.rb', line 52

def self.specs
  @configuration['specs']
end

.sprockets_environmentObject

Prepares a Sprockets::Environment object to serve coffeescript assets



61
62
63
64
65
66
67
68
# File 'lib/seaweed.rb', line 61

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_serverObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/seaweed.rb', line 70

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_changesObject



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/seaweed.rb', line 108

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