lib/assets_booster/packager.rb in assets_booster-0.0.1 vs lib/assets_booster/packager.rb in assets_booster-0.0.2

- old
+ new

@@ -1,33 +1,124 @@ +require 'yaml' module AssetsBooster class Packager - cattr_accessor :packages + attr_accessor :config + attr_accessor :configuration_filename + attr_accessor :packages - def self.init - require "assets_booster/configuration" - Configuration.load + def self.asset_path(file) + File.join(Rails.root, "public", file) end - def self.merge_all - each_package { |package| package.merge } + def initialize(config_filename = nil) + self.configuration_filename = config_filename || File.join(Rails.root, "config", "assets_booster.yml") end + + def config + @config ||= YAML.load_file(configuration_filename) + end + + def packages + @packages ||= begin + packages = {} + config['packages'].each_pair do |type, type_packages| + type = type.to_sym + packages[type] = {} + type_packages.each_pair do |name, assets| + package = get_package_class(type).new(name, assets) + package.compiler_class = get_compiler_class_for_type(type) + packages[type][name] = package + end + end + packages + end + end - def self.compile_all - each_package { |package| package.compile } + def boosted_environment? + @boosted_environment ||= config['options']['environments'].include?(Rails.env) end + + def merge_all + each_package{ |package| package.merge } + end + + def compile_all + each_package{ |package| package.compile } + end - def self.delete_all - each_package { |package| package.delete } + def delete_all + each_package{ |package| package.delete } end + + def create_configuration + if File.exists?(configuration_filename) + AssetsBooster.log("#{configuration_filename} already exists. Aborting task...") + return + end + config = { + 'packages' => { + 'javascript' => { + 'base' => file_list("#{Rails.root}/public/javascripts", "js"), + }, + 'stylesheet' => { + 'base' => file_list("#{Rails.root}/public/stylesheets", "css"), + }, + }, + 'options' => { + 'javascript' => { + 'compiler' => "closure", + }, + 'stylesheet' => { + 'compiler' => "rainpress", + }, + 'environments' => %w(staging production), + } + } + + File.open(configuration_filename, "w") do |file| + YAML.dump(config, file) + end + + AssetsBooster.log("Configuration file #{configuration_filename} created!") + AssetsBooster.log("Please check the generates packages so dependencies are loaded in correct order.") + end + private + + def file_list(path, extension) + Dir[File.join(path, "*.#{extension}")].map do |file| + file[path.length+1..-1].chomp(".#{extension}") + end + end - def self.each_package(&block) + def each_package(&block) packages.each_value do |packages| packages.each_value do |package| block.call(package) end end + end + + def get_package_class(type) + require "assets_booster/package/base" + require "assets_booster/package/#{type}" + klass = "AssetsBooster::Package::#{type.to_s.camelize}" + klass.constantize + rescue LoadError => e + raise "You've specified an invalid package type '#{type}'" + end + + def get_compiler_class(name) + require "assets_booster/compiler/#{name}" + klass = "AssetsBooster::Compiler::#{name.to_s.camelize}" + klass.constantize + rescue LoadError => e + raise "You've specified an invalid compiler '#{name}'" + end + + def get_compiler_class_for_type(type) + get_compiler_class(config['options'][type.to_s]['compiler']) end end end