Created
October 18, 2012 21:08
-
-
Save rkmax/3914747 to your computer and use it in GitHub Desktop.
Assets compiling for non rails project with Sprockets
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
require 'sprockets' | |
require 'logger' | |
require 'fileutils' | |
require 'pathname' | |
require 'json' | |
ROOT = Pathname(File.dirname(__FILE__)) | |
LOGGER = Logger.new(STDOUT) | |
BASE_PATH = "path/to/assets" | |
BUILD_DIR = ROOT.join("#{BASE_PATH}", "public") | |
SOURCE_DIR = ROOT.join("#{BASE_PATH}", "assets") | |
BUNDLES = %w( app.js styles.css ) | |
DEBUG = true | |
task :default => :compile | |
desc "Compila Assets" | |
task :compile => :clean do | |
puts "Compilando assets #{BASE_PATH}" | |
sprockets = Sprockets::Environment.new(ROOT) do |env| | |
env.logger = LOGGER if DEBUG | |
end | |
sprockets.append_path SOURCE_DIR.join("scripts").to_s | |
sprockets.append_path SOURCE_DIR.join("styles").to_s | |
BUNDLES.each do |bundle| | |
puts "Generando: #{bundle}" | |
assets = sprockets.find_asset(bundle) | |
ext = bundle.split(".")[1] | |
name = assets.pathname.basename(assets.pathname.extname) | |
FileUtils.mkpath BUILD_DIR.join(ext) | |
assets.write_to BUILD_DIR.join(ext, "#{name}.#{ext}") | |
end | |
end | |
desc "Limpia los directorios de build" | |
task :clean do | |
puts "Limpiando directorio" | |
BUNDLES.each do |bundle| | |
ext = bundle.split(".")[1] | |
FileUtils.rm_rf BUILD_DIR.join(ext) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment