class Synthesis::AssetPackage

Attributes

asset_type[RW]

instance methods

sources[RW]

instance methods

target[RW]

instance methods

target_dir[RW]

instance methods

Public Class Methods

build_all() click to toggle source
# File lib/synthesis/asset_package.rb, line 61
def build_all
  @@asset_packages_yml.keys.each do |asset_type|
    @@asset_packages_yml[asset_type].each { |p| self.new(asset_type, p).build }
  end
end
create_yml() click to toggle source
# File lib/synthesis/asset_package.rb, line 73
def create_yml
  unless File.exists?("#{config.root}/config/asset_packages.yml")
    asset_yml = Hash.new

    asset_yml['javascripts'] = [{"base" => build_file_list("#{config.root}/public/javascripts", "js")}]
    asset_yml['stylesheets'] = [{"base" => build_file_list("#{config.root}/public/stylesheets", "css")}]

    File.open("#{config.root}/config/asset_packages.yml", "w") do |out|
      YAML.dump(asset_yml, out)
    end

    log "config/asset_packages.yml example file created!"
    log "Please reorder files under 'base' so dependencies are loaded in correct order."
  else
    log "config/asset_packages.yml already exists. Aborting task..."
  end
end
delete_all() click to toggle source
# File lib/synthesis/asset_package.rb, line 67
def delete_all
  @@asset_packages_yml.keys.each do |asset_type|
    @@asset_packages_yml[asset_type].each { |p| self.new(asset_type, p).delete_previous_build }
  end
end
find_by_source(asset_type, source) click to toggle source
# File lib/synthesis/asset_package.rb, line 32
def find_by_source(asset_type, source)
  path_parts = parse_path(source)
  package_hash = @@asset_packages_yml[asset_type].find do |p|
    key = p.keys.first
    p[key].include?(path_parts[2]) && (parse_path(key)[1] == path_parts[1])
  end
  package_hash ? self.new(asset_type, package_hash) : nil
end
find_by_target(asset_type, target) click to toggle source
# File lib/synthesis/asset_package.rb, line 27
def find_by_target(asset_type, target)
  package_hash = @@asset_packages_yml[asset_type].find {|p| p.keys.first == target }
  package_hash ? self.new(asset_type, package_hash) : nil
end
find_by_type(asset_type) click to toggle source
# File lib/synthesis/asset_package.rb, line 23
def find_by_type(asset_type)
  @@asset_packages_yml[asset_type].map { |p| self.new(asset_type, p) }
end
merge_environments() click to toggle source
# File lib/synthesis/asset_package.rb, line 15
def merge_environments
  @@merge_environments ||= ["production"]
end
merge_environments=(environments) click to toggle source
# File lib/synthesis/asset_package.rb, line 11
def merge_environments=(environments)
  @@merge_environments = environments
end
new(asset_type, package_hash) click to toggle source
# File lib/synthesis/asset_package.rb, line 96
def initialize(asset_type, package_hash)
  target_parts = self.class.parse_path(package_hash.keys.first)
  @target_dir = target_parts[1].to_s
  @target = target_parts[2].to_s
  @sources = package_hash[package_hash.keys.first]
  @asset_type = asset_type
  @asset_path = ($asset_base_path ? "#{$asset_base_path}/" : "#{RAILS_ROOT}/public/") +
      "#{@asset_type}#{@target_dir.gsub(/^(.+)$/, '/\1')}"
  @extension = get_extension
  @file_name = "#{@target}_packaged.#{@extension}"
  @full_path = File.join(@asset_path, @file_name)
end
parse_path(path) click to toggle source
# File lib/synthesis/asset_package.rb, line 19
def parse_path(path)
  /^(?:(.*)\/)?([^\/]+)$/.match(path).to_a
end
sources_from_targets(asset_type, targets) click to toggle source
# File lib/synthesis/asset_package.rb, line 50
def sources_from_targets(asset_type, targets)
  source_names = Array.new
  targets.each do |target|
    package = find_by_target(asset_type, target)
    source_names += (package ? package.sources.collect do |src|
      package.target_dir.gsub(/^(.+)$/, '\1/') + src
    end : target.to_a)
  end
  source_names.uniq
end
targets_from_sources(asset_type, sources) click to toggle source
# File lib/synthesis/asset_package.rb, line 41
def targets_from_sources(asset_type, sources)
  package_names = Array.new
  sources.each do |source|
    package = find_by_target(asset_type, source) || find_by_source(asset_type, source)
    package_names << (package ? package.current_file : source)
  end
  package_names.uniq
end

Public Instance Methods

build() click to toggle source
# File lib/synthesis/asset_package.rb, line 120
def build
  delete_previous_build
  create_new_build
end
current_file() click to toggle source
# File lib/synthesis/asset_package.rb, line 113
def current_file
  build unless package_exists?

  path = @target_dir.gsub(/^(.+)$/, '\1/')
  "#{path}#{@target}_packaged"
end
delete_previous_build() click to toggle source
# File lib/synthesis/asset_package.rb, line 125
def delete_previous_build
  File.delete(@full_path) if File.exists?(@full_path)
end
package_exists?() click to toggle source
# File lib/synthesis/asset_package.rb, line 109
def package_exists?
  File.exists?(@full_path)
end