Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
Change how various things are loaded to make config.ru cleaner.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Jul 3, 2011
1 parent 7009246 commit 961ed4d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
.redcar
Gemfile.lock Gemfile.lock
doc doc
pkg pkg
Expand Down
10 changes: 6 additions & 4 deletions lib/helium.rb
Expand Up @@ -7,7 +7,6 @@
require 'grit' require 'grit'
require 'jake' require 'jake'
require 'packr' require 'packr'
require 'oyster'


module Helium module Helium


Expand Down Expand Up @@ -37,9 +36,12 @@ module Helium


ERB_TRIM_MODE = '-' ERB_TRIM_MODE = '-'


%w[trie configurable deployer generator logger].each do |file| autoload :Configurable, File.join(ROOT, 'helium', 'configurable')
require File.join(ROOT, 'helium', file) autoload :Deployer, File.join(ROOT, 'helium', 'deployer')
end 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 = {}) def self.generate(template, dir, options = {})
Generator.new(template, dir, options).run! Generator.new(template, dir, options).run!
Expand Down
35 changes: 17 additions & 18 deletions lib/helium/web.rb
Expand Up @@ -4,26 +4,22 @@


module Helium module Helium
class Web < Sinatra::Base class Web < Sinatra::Base

extend Configurable


ROOT_DIR = File.expand_path(File.dirname(__FILE__)) ROOT_DIR = File.expand_path(File.dirname(__FILE__))
require File.join(ROOT_DIR, '..', 'helium')
require File.join(ROOT_DIR, 'web_helpers') require File.join(ROOT_DIR, 'web_helpers')


extend Configurable CONFIG = 'deploy.yml'

CUSTOM = 'custom.js'
LIB_DIR = 'lib' LIB_DIR = 'lib'

LOCK = '.lock'
CONFIG = File.join(APP_DIR, 'deploy.yml') PUBLIC = File.join('public', WEB_ROOT)
CUSTOM = File.join(APP_DIR, 'custom.js')
PUBLIC = File.join(APP_DIR, 'public', WEB_ROOT)
LOCK = File.join(APP_DIR, '.lock')


set :static, true set :static, true
set :public, File.join(APP_DIR, 'public')
set :views, File.join(ROOT_DIR, 'views') set :views, File.join(ROOT_DIR, 'views')


before do before do
self.class.set :public, File.join(app_directory, 'public')
@projects = project_config @projects = project_config
@location = get_location @location = get_location
end end
Expand All @@ -48,7 +44,7 @@ class Web < Sinatra::Base
halt(200, erb(:deploy)) if @error halt(200, erb(:deploy)) if @error


with_lock do with_lock do
deployer = Helium::Deployer.new(APP_DIR, LIB_DIR) deployer = Helium::Deployer.new(app_directory, LIB_DIR)
logger = Helium::Logger.new logger = Helium::Logger.new
deployer.add_observer(logger) deployer.add_observer(logger)


Expand All @@ -59,18 +55,21 @@ class Web < Sinatra::Base


deployer.cleanup! 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) 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| 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.mkdir_p(File.dirname(dest))
FileUtils.cp(source, dest) FileUtils.cp(source, dest)
end 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 erb :deploy
end end
end end
Expand All @@ -80,7 +79,7 @@ class Web < Sinatra::Base
## Save changes to the configuration file, making sure it validates as YAML. ## Save changes to the configuration file, making sure it validates as YAML.
post '/app/config' do post '/app/config' do
@action = 'config' @action = 'config'
@file = CONFIG @file = File.join(app_directory, CONFIG)
@contents = params[:contents] @contents = params[:contents]
if allow_write_access?(env) if allow_write_access?(env)
begin begin
Expand All @@ -102,7 +101,7 @@ class Web < Sinatra::Base
## Save changes to the custom loaders file. ## Save changes to the custom loaders file.
post '/app/custom' do post '/app/custom' do
@action = 'custom' @action = 'custom'
@file = CUSTOM @file = File.join(app_directory, CUSTOM)
@contents = params[:contents] @contents = params[:contents]
if allow_write_access?(env) if allow_write_access?(env)
File.open(@file, 'w') { |f| f.write(@contents) } File.open(@file, 'w') { |f| f.write(@contents) }
Expand Down
21 changes: 14 additions & 7 deletions lib/helium/web_helpers.rb
Expand Up @@ -4,12 +4,14 @@ class Web
helpers do helpers do
# Returns the data structure contained in the app's deploy.yml file. # Returns the data structure contained in the app's deploy.yml file.
def project_config def project_config
Helium::Deployer.new(File.dirname(CONFIG)).projects config = File.join(app_directory, CONFIG)
Helium::Deployer.new(File.dirname(config)).projects
end end


# Returns the domain and path from which script files are served. # Returns the domain and path from which script files are served.
def get_location 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 env['HTTP_HOST'] + '/' + Helium::WEB_ROOT


location.gsub(/\/*$/, '') location.gsub(/\/*$/, '')
Expand All @@ -27,18 +29,23 @@ def allow_write_access?(env)
allowed_ips.include?(ip) allowed_ips.include?(ip)
end end


def app_directory
File.expand_path(Helium::Web.config.app_dir)
end

# Returns +true+ if a lock exists stopping other deploy processes running. # Returns +true+ if a lock exists stopping other deploy processes running.
def locked? def locked?
File.file?(LOCK) File.file?(File.join(app_directory, LOCK))
end end


# Places a lock in the filesystem while running a code block. This is # 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. # used to make sure no more than one deploy process runs at once.
def with_lock(&block) def with_lock(&block)
File.open(LOCK, 'w') { |f| f.write(Time.now.to_s) } lockfile = File.join(app_directory, LOCK)
at_exit { File.delete(LOCK) if File.exists?(LOCK) } File.open(lockfile, 'w') { |f| f.write(Time.now.to_s) }
at_exit { File.delete(lockfile) if File.exists?(lockfile) }
result = block.call result = block.call
File.delete(LOCK) if File.exists?(LOCK) File.delete(lockfile) if File.exists?(lockfile)
result result
end end


Expand All @@ -47,7 +54,7 @@ def view_file(name)
@error = 'You are not authorized to edit this file' unless allow_write_access?(env) @error = 'You are not authorized to edit this file' unless allow_write_access?(env)
@projects = project_config @projects = project_config
@action = name.to_s @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) : '' @contents = File.file?(@file) ? File.read(@file) : ''
erb :edit erb :edit
end end
Expand Down
5 changes: 2 additions & 3 deletions templates/web/config.ru
@@ -1,8 +1,7 @@
::APP_DIR = ::File.expand_path(::File.dirname(__FILE__)) require 'helium'

require 'helium/web'


Helium::Web.configure do |config| Helium::Web.configure do |config|
config.app_dir File.dirname(__FILE__)
config.allow_ips ['0.0.0.0', '127.0.0.1'] config.allow_ips ['0.0.0.0', '127.0.0.1']
end end


Expand Down
2 changes: 1 addition & 1 deletion test/deploy.yml
@@ -1,7 +1,7 @@
--- ---
js.class: js.class:
repository: git://github.com/jcoglan/js.class.git repository: git://github.com/jcoglan/js.class.git
version: 2.1.x version: 3.0.1


projects: projects:
bluff: git://github.com/jcoglan/bluff.git bluff: git://github.com/jcoglan/bluff.git
Expand Down

0 comments on commit 961ed4d

Please sign in to comment.