trellis 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -38,6 +38,7 @@ require 'markaby'
38
38
  require 'redcloth'
39
39
  require 'bluecloth'
40
40
  require 'english/inflect'
41
+ require 'directory_watcher'
41
42
 
42
43
  module Trellis
43
44
 
@@ -71,18 +72,26 @@ module Trellis
71
72
  # bootstrap the application
72
73
  def start(port = 3000)
73
74
  Application.logger.info "Starting Trellis Application #{self.class} on port #{port}"
75
+
76
+ directory_watcher = configure_directory_watcher
77
+ directory_watcher.start
78
+
74
79
  Rack::Handler::Mongrel.run configured_instance, :Port => port do |server|
75
80
  trap(:INT) do
76
81
  Application.logger.info "Exiting Trellis Application #{self.class}"
82
+ directory_watcher.stop
77
83
  server.stop
78
84
  end
79
85
  end
86
+ rescue Exception => e
87
+ Application.logger.warn "#{ e } (#{ e.class })!"
80
88
  end
81
89
 
82
90
  def configured_instance
83
91
  # configure rack middleware
84
92
  application = Rack::ShowStatus.new(self)
85
93
  application = Rack::ShowExceptions.new(application)
94
+ application = Rack::Reloader.new(application)
86
95
  application = Rack::CommonLogger.new(application, Application.logger)
87
96
  application = Rack::Session::Cookie.new(application)
88
97
 
@@ -140,6 +149,36 @@ module Trellis
140
149
  end
141
150
  response.finish
142
151
  end
152
+
153
+ private
154
+
155
+ def configure_directory_watcher(directory = nil)
156
+ # set directory watcher to reload templates
157
+ glob = []
158
+ Page::TEMPLATE_FORMATS.each do |format|
159
+ glob << "*.#{format}"
160
+ end
161
+
162
+ templates_directory = directory || "#{File.dirname($0)}/../html/"
163
+
164
+ directory_watcher = DirectoryWatcher.new templates_directory, :glob => glob, :pre_load => true
165
+ directory_watcher.add_observer do |*args|
166
+ args.each do |event|
167
+ Application.logger.debug "directory watcher: #{event}"
168
+ event_type = event.type.to_s
169
+ if (event_type == 'modified' || event_type == 'stable')
170
+ template = event.path
171
+ format = File.extname(template).delete('.').to_sym
172
+ page_locator = Page.template_registry[template]
173
+ page = Page.get_page(page_locator)
174
+ Application.logger.info "reloading template for page => #{page}: #{template}"
175
+ File.open(template, "r") { |f| page.template(f.read, :format => format) }
176
+ end
177
+ end
178
+ end
179
+ Application.logger.info "watching #{templates_directory} for template changes..."
180
+ directory_watcher
181
+ end
143
182
  end
144
183
 
145
184
  # -- Route --
@@ -271,8 +310,11 @@ module Trellis
271
310
  # A Trellis Page contains listener methods to respond to events trigger by
272
311
  # components in the same page or other pages
273
312
  class Page
313
+
314
+ TEMPLATE_FORMATS = [:html, :xhtml, :haml, :textile, :markdown]
274
315
 
275
316
  @@subclasses = Hash.new
317
+ @@template_registry = Hash.new
276
318
 
277
319
  attr_accessor :params, :path, :logger
278
320
 
@@ -409,10 +451,20 @@ module Trellis
409
451
  def self.inject_dependent_pages(target)
410
452
  @pages.each do |sym|
411
453
  clazz = Page.get_page(sym)
412
- target.instance_variable_set("@#{sym}".to_sym, clazz.new)
413
- target.meta_def(sym) { instance_variable_get("@#{sym}") }
454
+ if clazz
455
+ Application.logger.debug "injecting an instance of #{clazz} for #{sym}"
456
+ target.instance_variable_set("@#{sym}".to_sym, clazz.new)
457
+ target.meta_def(sym) { instance_variable_get("@#{sym}") }
458
+ else
459
+ # throw an exception in production mode or
460
+ # dynamically generate a page in development mode
461
+ end
414
462
  end
415
463
  end
464
+
465
+ def self.template_registry
466
+ @@template_registry
467
+ end
416
468
 
417
469
  private
418
470
 
@@ -427,12 +479,15 @@ module Trellis
427
479
 
428
480
  Application.logger.debug "looking for template #{base} in #{dir}"
429
481
 
430
- [:xhtml, :haml, :textile, :markdown].each do |format|
482
+ TEMPLATE_FORMATS.each do |format|
431
483
  filename = "#{base}.#{format}"
432
484
  file = File.find_first(dir, filename)
433
485
  if file
434
486
  Application.logger.debug "found template for page => #{clazz}: #{filename}"
435
487
  File.open(file, "r") { |f| clazz.template(f.read, :format => format) }
488
+ # add the template file to the external template registry so that we can reload it
489
+ @@template_registry["#{dir}#{filename}"] = clazz.class_to_sym
490
+ Application.logger.debug "registering template file for #{clazz.class_to_sym} => #{dir}#{filename}"
436
491
  break
437
492
  end
438
493
  end
@@ -28,7 +28,7 @@ module Trellis
28
28
  module VERSION #:nodoc:
29
29
  MAJOR = 0
30
30
  MINOR = 0
31
- TINY = 2
31
+ TINY = 3
32
32
 
33
33
  STRING = [MAJOR, MINOR, TINY].join('.')
34
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trellis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Sam-Bodden
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-20 00:00:00 -07:00
12
+ date: 2009-09-29 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency