Skip to content

Instantly share code, notes, and snippets.

@rkmax
Created October 18, 2012 21:08
Show Gist options
  • Save rkmax/3914747 to your computer and use it in GitHub Desktop.
Save rkmax/3914747 to your computer and use it in GitHub Desktop.
Assets compiling for non rails project with Sprockets
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