Last active
August 29, 2015 14:05
-
-
Save nandosola/8e5ab9c2a648d1709d70 to your computer and use it in GitHub Desktop.
Manual asset pipeline for Sinatra
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace :assets do | |
... | |
def get_relative_path(file_path, base_path) | |
(Set.new(file_path.split(File::SEPARATOR)) - Set.new(base_path.split(File::SEPARATOR))). | |
to_a.join(File::SEPARATOR) | |
end | |
... | |
desc 'compile assets' | |
task :compile do | |
require 'sprockets-image_compressor' | |
require 'sass' | |
require 'closure-compiler' | |
require 'set' | |
::RideSpark::BaseWebApplication.apps.each do |app| | |
sprockets = app.settings.sprockets | |
sprockets.css_compressor = :sass | |
sprockets.js_compressor = :closure | |
Sprockets::ImageCompressor::Integration.setup sprockets | |
sprockets.paths.each do |path| | |
Dir.glob(File.join(path, '**/*')) do |asset_path| | |
asset = get_relative_path(asset_path, path) | |
app_dir = get_relative_path(asset_path, APPS_HOME).split(File::SEPARATOR).first | |
asset_dir = File.file?(asset_path) ? '' : asset | |
dst_path = File.join(APPS_HOME, 'assets', 'public', app_dir, asset_dir ) | |
dst_file = File.join(dst_path, asset) | |
FileUtils.mkdir_p(dst_path) unless Dir.exist?(dst_path) | |
File.write(dst_file, sprockets[asset].to_s) if asset_dir.empty? | |
$logger.info "Copied #{asset} to #{dst_path}" | |
end | |
end | |
end | |
end | |
desc 'clean assets' | |
task :clean do | |
Dir.chdir File.join(APPS_HOME, 'assets', 'public') | |
Dir.entries('.').each do |e| | |
if !%w(. ..).include?(e) && File.directory?(e) | |
FileUtils.rm_rf(e) | |
$logger.info "Recursively deleted #{e}" | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseWebApplication < BaseApplication | |
use Rack::UserLocale | |
I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) | |
I18n.default_locale = CONFIG['site']['lang'] | |
def self.apps | |
ObjectSpace.each_object(Class).select {|klazz| klazz < self} | |
end | |
def self.inherited(subklass) | |
super | |
subklass.instance_eval do | |
app_dirname = caller[3].split('/')[0..-2].join('/') | |
translations_dir = File.join(app_dirname, 'locales') | |
if File.directory?(translations_dir) | |
I18n.load_path += Dir[File.join(translations_dir, '*.yml')] | |
I18n.backend.load_translations | |
end | |
set :static_cache_control, [:public, :max_age => 604800] # one week | |
set :haml, ugly:true unless %w(development test).include?ENV['RACK_ENV'] | |
set :views, File.join(app_dirname, 'views') | |
set :public_folder => File.join(app_dirname, 'public') | |
set :sprockets, Sprockets::Environment.new(app_dirname){|env| env.logger = $logger} | |
%w(assets vendor).each do |dir| | |
%w(css js img ico).each do |type| | |
path = File.join(dir, type) | |
settings.sprockets.append_path(path) if File.directory?(File.join(app_dirname, path)) | |
end | |
end | |
get '/assets/*' do | |
file = params[:splat].first | |
if %w(development test).include?ENV['RACK_ENV'] | |
asset = settings.sprockets[file] | |
asset ? [200, {'Content-Type'=>asset.content_type}, asset.to_s] : 404 | |
else | |
send_file File.join(APPS_HOME, 'assets', 'public', File.basename(app_dirname), file) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment