File: blade.rb

package info (click to toggle)
ruby-blade 0.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 208 kB
  • sloc: ruby: 819; makefile: 3; sh: 3
file content (155 lines) | stat: -rw-r--r-- 2,913 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
require "active_support/all"
require "eventmachine"
require "faye"
require "pathname"
require "yaml"

require "blade/version"
require "blade/cli"

module Blade
  extend self

  CONFIG_DEFAULTS = {
    framework: :qunit,
    port: 9876,
    build: { path: "." }
  }

  CONFIG_FILENAMES = %w( blade.yml .blade.yml )

  @components = []

  def register_component(component)
    @components << component
  end

  require "blade/component"
  require "blade/server"

  autoload :Model, "blade/model"
  autoload :Assets, "blade/assets"
  autoload :Config, "blade/config"
  autoload :RackAdapter, "blade/rack/adapter"
  autoload :RackRouter, "blade/rack/router"
  autoload :Session, "blade/session"
  autoload :TestResults, "blade/test_results"

  delegate :subscribe, :publish, to: Server

  attr_reader :config

  def start(options = {})
    return if running?
    ensure_tmp_path

    initialize!(options)
    load_interface

    handle_exit

    EM.run do
      @components.each { |c| c.try(:start) }
      @running = true
    end
  end

  def stop
    return if @stopping
    @stopping = true
    @components.each { |c| c.try(:stop) }
    EM.stop if EM.reactor_running?
    @running = false
  end

  def running?
    @running
  end

  def initialize!(options = {})
    return if @initialized
    @initialized = true

    options = CONFIG_DEFAULTS.deep_merge(blade_file_options).deep_merge(options)
    @config = Blade::Config.new options

    config.load_paths = Array(config.load_paths)
    config.logical_paths = Array(config.logical_paths)

    if config.build?
      config.build.logical_paths = Array(config.build.logical_paths)
      config.build.path ||= "."
    end

    config.plugins ||= {}

    load_requires
    load_plugins
    load_adapter
  end

  def build
    initialize!
    Assets.build
  end

  def url(path = "/")
    "http://#{Server.host}:#{config.port}#{path}"
  end

  def root_path
    Pathname.new(File.dirname(__FILE__)).join("../")
  end

  def tmp_path
    Pathname.new(".").join("tmp/blade")
  end

  def ensure_tmp_path
    tmp_path.mkpath
  end

  def clean_tmp_path
    tmp_path.rmtree if tmp_path.exist?
  end

  private
    def handle_exit
      at_exit do
        stop
        exit $!.status if $!.is_a?(SystemExit)
      end

      %w( INT ).each do |signal|
        trap(signal) { exit(1) }
      end
    end

    def blade_file_options
      if filename = CONFIG_FILENAMES.detect { |name| File.exists?(name) }
        YAML.load_file(filename)
      else
        {}
      end
    end

    def load_interface
      require "blade/interface/#{config.interface}"
    end

    def load_adapter
      require "blade/#{config.framework}_adapter"
    end

    def load_requires
      Array(config.require).each do |path|
        require path
      end
    end

    def load_plugins
      config.plugins.keys.each do |name|
        require "blade/#{name}_plugin"
      end
    end
end