Permalink
Browse files

Change how various things are loaded to make config.ru cleaner.

  • Loading branch information...
jcoglan committed Jul 3, 2011
1 parent 7009246 commit 961ed4d60cb2f2f9f41e6d73ddce32d595616a7f
Showing with 41 additions and 33 deletions.
  1. +1 −0 .gitignore
  2. +6 −4 lib/helium.rb
  3. +17 −18 lib/helium/web.rb
  4. +14 −7 lib/helium/web_helpers.rb
  5. +2 −3 templates/web/config.ru
  6. +1 −1 test/deploy.yml
View
@@ -1,3 +1,4 @@
+.redcar
Gemfile.lock
doc
pkg
View
@@ -7,7 +7,6 @@
require 'grit'
require 'jake'
require 'packr'
-require 'oyster'
module Helium
@@ -37,9 +36,12 @@ module Helium
ERB_TRIM_MODE = '-'
- %w[trie configurable deployer generator logger].each do |file|
- require File.join(ROOT, 'helium', file)
- end
+ autoload :Configurable, File.join(ROOT, 'helium', 'configurable')
+ autoload :Deployer, File.join(ROOT, 'helium', 'deployer')
+ autoload :Generator, File.join(ROOT, 'helium', 'generator')
+ autoload :Logger, File.join(ROOT, 'helium', 'logger')
+ autoload :Trie, File.join(ROOT, 'helium', 'trie')
+ autoload :Web, File.join(ROOT, 'helium', 'web')
def self.generate(template, dir, options = {})
Generator.new(template, dir, options).run!
View
@@ -4,26 +4,22 @@
module Helium
class Web < Sinatra::Base
-
+ extend Configurable
ROOT_DIR = File.expand_path(File.dirname(__FILE__))
- require File.join(ROOT_DIR, '..', 'helium')
require File.join(ROOT_DIR, 'web_helpers')
- extend Configurable
-
- LIB_DIR = 'lib'
-
- CONFIG = File.join(APP_DIR, 'deploy.yml')
- CUSTOM = File.join(APP_DIR, 'custom.js')
- PUBLIC = File.join(APP_DIR, 'public', WEB_ROOT)
- LOCK = File.join(APP_DIR, '.lock')
+ CONFIG = 'deploy.yml'
+ CUSTOM = 'custom.js'
+ LIB_DIR = 'lib'
+ LOCK = '.lock'
+ PUBLIC = File.join('public', WEB_ROOT)
set :static, true
- set :public, File.join(APP_DIR, 'public')
set :views, File.join(ROOT_DIR, 'views')
before do
+ self.class.set :public, File.join(app_directory, 'public')
@projects = project_config
@location = get_location
end
@@ -48,7 +44,7 @@ class Web < Sinatra::Base
halt(200, erb(:deploy)) if @error
with_lock do
- deployer = Helium::Deployer.new(APP_DIR, LIB_DIR)
+ deployer = Helium::Deployer.new(app_directory, LIB_DIR)
logger = Helium::Logger.new
deployer.add_observer(logger)
@@ -59,18 +55,21 @@ class Web < Sinatra::Base
deployer.cleanup!
- custom = File.file?(CUSTOM) ? File.read(CUSTOM) : nil
+ custom_path = File.join(app_directory, CUSTOM)
+ public_path = File.join(app_directory, PUBLIC)
+
+ custom = File.file?(custom_path) ? File.read(custom_path) : nil
files = deployer.run_builds!(:custom => custom, :location => @location)
- FileUtils.rm_rf(PUBLIC) if File.exists?(PUBLIC)
+ FileUtils.rm_rf(public_path) if File.exists?(public_path)
files.each do |path|
- source, dest = File.join(deployer.static_dir, path), File.join(PUBLIC, path)
+ source, dest = File.join(deployer.static_dir, path), File.join(public_path, path)
FileUtils.mkdir_p(File.dirname(dest))
FileUtils.cp(source, dest)
end
- @log = logger.messages.map { |msg| msg.sub(File.join(APP_DIR, LIB_DIR), '') }
+ @log = logger.messages.map { |msg| msg.sub(File.join(app_directory, LIB_DIR), '') }
erb :deploy
end
end
@@ -80,7 +79,7 @@ class Web < Sinatra::Base
## Save changes to the configuration file, making sure it validates as YAML.
post '/app/config' do
@action = 'config'
- @file = CONFIG
+ @file = File.join(app_directory, CONFIG)
@contents = params[:contents]
if allow_write_access?(env)
begin
@@ -102,7 +101,7 @@ class Web < Sinatra::Base
## Save changes to the custom loaders file.
post '/app/custom' do
@action = 'custom'
- @file = CUSTOM
+ @file = File.join(app_directory, CUSTOM)
@contents = params[:contents]
if allow_write_access?(env)
File.open(@file, 'w') { |f| f.write(@contents) }
View
@@ -4,12 +4,14 @@ class Web
helpers do
# Returns the data structure contained in the app's deploy.yml file.
def project_config
- Helium::Deployer.new(File.dirname(CONFIG)).projects
+ config = File.join(app_directory, CONFIG)
+ Helium::Deployer.new(File.dirname(config)).projects
end
# Returns the domain and path from which script files are served.
def get_location
- location = Helium::Deployer.new(File.dirname(CONFIG)).config['location'] ||
+ config = File.join(app_directory, CONFIG)
+ location = Helium::Deployer.new(File.dirname(config)).config['location'] ||
env['HTTP_HOST'] + '/' + Helium::WEB_ROOT
location.gsub(/\/*$/, '')
@@ -27,18 +29,23 @@ def allow_write_access?(env)
allowed_ips.include?(ip)
end
+ def app_directory
+ File.expand_path(Helium::Web.config.app_dir)
+ end
+
# Returns +true+ if a lock exists stopping other deploy processes running.
def locked?
- File.file?(LOCK)
+ File.file?(File.join(app_directory, LOCK))
end
# Places a lock in the filesystem while running a code block. This is
# used to make sure no more than one deploy process runs at once.
def with_lock(&block)
- File.open(LOCK, 'w') { |f| f.write(Time.now.to_s) }
- at_exit { File.delete(LOCK) if File.exists?(LOCK) }
+ lockfile = File.join(app_directory, LOCK)
+ File.open(lockfile, 'w') { |f| f.write(Time.now.to_s) }
+ at_exit { File.delete(lockfile) if File.exists?(lockfile) }
result = block.call
- File.delete(LOCK) if File.exists?(LOCK)
+ File.delete(lockfile) if File.exists?(lockfile)
result
end
@@ -47,7 +54,7 @@ def view_file(name)
@error = 'You are not authorized to edit this file' unless allow_write_access?(env)
@projects = project_config
@action = name.to_s
- @file = Helium::Web.const_get(name.to_s.upcase)
+ @file = File.join(app_directory, Helium::Web.const_get(name.to_s.upcase))
@contents = File.file?(@file) ? File.read(@file) : ''
erb :edit
end
View
@@ -1,8 +1,7 @@
-::APP_DIR = ::File.expand_path(::File.dirname(__FILE__))
-
-require 'helium/web'
+require 'helium'
Helium::Web.configure do |config|
+ config.app_dir File.dirname(__FILE__)
config.allow_ips ['0.0.0.0', '127.0.0.1']
end
View
@@ -1,7 +1,7 @@
---
js.class:
repository: git://github.com/jcoglan/js.class.git
- version: 2.1.x
+ version: 3.0.1
projects:
bluff: git://github.com/jcoglan/bluff.git

0 comments on commit 961ed4d

Please sign in to comment.