Skip to content

Commit

Permalink
Rack support.
Browse files Browse the repository at this point in the history
  • Loading branch information
rstacruz committed Jul 24, 2011
1 parent 5994307 commit aefabc7
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 40 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
doc
/Gemfile.lock
1 change: 1 addition & 0 deletions Gemfile
@@ -0,0 +1 @@
gemspec
5 changes: 5 additions & 0 deletions config.ru
@@ -0,0 +1,5 @@
require 'bundler'
Bundler.setup

require './lib/proscribe'
run ProScribe.rack_app
15 changes: 15 additions & 0 deletions lib/proscribe.rb
Expand Up @@ -19,7 +19,22 @@ def self.root(*a)
autoload :Helpers, "#{PREFIX}/proscribe/helpers"
autoload :CLI, "#{PREFIX}/proscribe/cli"
autoload :Extractor, "#{PREFIX}/proscribe/extractor"
autoload :Watcher, "#{PREFIX}/proscribe/watcher"
autoload :RackApp, "#{PREFIX}/proscribe/rack_app"

require "#{PREFIX}/proscribe/version"

# Class method: rack_app (ProScribe)
# Returns a Rack app for the current dir's ProScribe project.
#
# ## Usage
#
# [config.ru (ruby)]
# require 'proscribe'
# run ProScribe.rack_app
#
def self.rack_app
RackApp.run! && Proton::Server
end
end

43 changes: 4 additions & 39 deletions lib/proscribe/cli.rb
Expand Up @@ -19,54 +19,19 @@ class ProScribe::CLI < Shake
task.description = "Builds the project files"

task(:start) do
require 'fssm'

project.make

threads = Array.new
monitor = lambda { |path, &blk|
updated = lambda { |path, file|
status "Updated #{file}"
blk.call
}

FSSM.monitor(path) do
update(&updated)
create(&updated)
delete(&updated)
end
}

# Monitor the project's files
project.config.files.each do |group|
threads << Thread.new {
path = project.root(group.source)
path = path.gsub(/\*.*$/, '')
path = File.realpath(path)

monitor.call(path) { project.make }
}
end

# Monitor the project's manual
threads << Thread.new {
monitor.call(project.manual) { project.make }
}


# Monitor ProScribe's default theme
threads << Thread.new {
monitor.call(ProScribe.root('data')) { project.make }
}
w = ProScribe::Watcher.new(project) { |path, file| status "Updated #{file}" }

threads << Thread.new {
server = Thread.new {
Dir.chdir(project.dir) {
status "Starting Proton server"
system "proton start", *ARGV[1..-1]
}
}

threads.each { |t| t.join }
w.join
server.join
end
task.description = "Starts a preview server on localhost"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/proscribe/project.rb
@@ -1,6 +1,10 @@
# Class: Project (ProScribe)
# A project.
#
# ## Internal usage
#
# p = Project.new(config_file)
#
module ProScribe
class Project
# Attribute: config (ProScribe::Project)
Expand Down
26 changes: 26 additions & 0 deletions lib/proscribe/rack_app.rb
@@ -0,0 +1,26 @@
require 'proton'
require 'proton/server'
require 'shake'

# Module: RackApp
# Provides a Rack app.
#
# ## Usage
#
# Use {ProScribe.rack_app}.
#
module ProScribe
module RackApp
def self.run!
config = Shake.find_in_project("Scribefile") or return
project = ProScribe::Project.new(config)
project.make

watcher = ProScribe::Watcher.new(project) { |_, file| puts "Updated #{file}" }

proton = Proton::Project.new(project.dir)

Proton::Server
end
end
end
63 changes: 63 additions & 0 deletions lib/proscribe/watcher.rb
@@ -0,0 +1,63 @@
# Class: Watcher (ProScribe)
# Monitors files for changes.
#
# ## Common usage
#
# w = Watcher.new(project) { puts 'update' }
# w.join
#
module ProScribe
class Watcher
def initialize(project, &blk)
require 'fssm'

@threads = Array.new

# Monitor the project's manual
@threads << Thread.new {
monitor(project.config.manual) { |*a| project.make; yield *a }
}

# Monitor ProScribe's default theme
@threads << Thread.new {
monitor(ProScribe.root('data')) { |*a| project.make; yield *a }
}

# Monitor the project's files
project.config.files.each do |group|
threads << Thread.new {
path = project.root(group.source)
path = path.gsub(/\*.*$/, '')
path = File.realpath(path)

monitor(path) { |*a| project.make; yield *a }
}
end
end

# Attribute: threads (ProScribe::Watcher)
# The threads.
#
attr_reader :threads

# Method: join (ProScribe::Watcher)
# Asks all threads to join.
#
def join
threads.each { |t| t.join }
end

private
def monitor(path, &blk)
updated = lambda { |path, file|
yield path, file
}

FSSM.monitor(path) do
update(&updated)
create(&updated)
delete(&updated)
end
end
end
end
3 changes: 2 additions & 1 deletion proscribe.gemspec
Expand Up @@ -13,7 +13,8 @@ Gem::Specification.new do |s|

s.add_dependency "hashie", "~> 1.0.0"
s.add_dependency "shake", "~> 0.1.2"
s.add_dependency "tilt", "~> 1.3.2"
s.add_dependency "tilt", "~> 1.2.2"
s.add_dependency "proton", "~> 0.3.3"
s.add_dependency "fssm", "~> 0.2.7"
s.add_dependency "rdiscount", "~> 1.6.8"
end

0 comments on commit aefabc7

Please sign in to comment.