diff --git a/.gitignore b/.gitignore index 1faa701..e32c88e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.redcar Gemfile.lock doc pkg diff --git a/lib/helium.rb b/lib/helium.rb index 18f525c..dcc10e1 100644 --- a/lib/helium.rb +++ b/lib/helium.rb @@ -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! diff --git a/lib/helium/web.rb b/lib/helium/web.rb index 0643da2..2a71aef 100644 --- a/lib/helium/web.rb +++ b/lib/helium/web.rb @@ -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) } diff --git a/lib/helium/web_helpers.rb b/lib/helium/web_helpers.rb index 3122847..d6bc7c2 100644 --- a/lib/helium/web_helpers.rb +++ b/lib/helium/web_helpers.rb @@ -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 diff --git a/templates/web/config.ru b/templates/web/config.ru index ede1b9a..38e08d4 100644 --- a/templates/web/config.ru +++ b/templates/web/config.ru @@ -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 diff --git a/test/deploy.yml b/test/deploy.yml index 53428a8..32131fc 100644 --- a/test/deploy.yml +++ b/test/deploy.yml @@ -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