Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: fb37104530
Fetching contributors…

Cannot retrieve contributors at this time

103 lines (86 sloc) 3.728 kB
module Jake
# A +Buildable+ represents a group of files that may be merged to form a single
# build file. There are two subtypes of +Buildable+; a +Package+ takes several
# source files and produces a build file, and a +Bundle+ takes several +Package+
# build files to produce another build file. This class should be considered
# abstract as some of its methods must be filled in by subtypes.
class Buildable
attr_reader :name
# Create a new +Buildable+ using a +Build+ container, a name and a configuration
# hash, typically a subsection of jake.yml.
def initialize(build, name, config)
@build, @name = build, name
@config = case config
when Hash then config
when String then {:files => [config]}
when Array then {:files => config}
@code = {}
# Returns the parent +Buildable+ set using the +extends+ option, or +nil+ if
# there is no parent.
def parent
return nil unless @config[:extends]
@parent ||= @build.package(@config[:extends])
# Returns the source directory for this package.
def directory
dir = @config[:directory]
return if parent && !dir
Jake.path(@build.source_directory, @config[:directory])
# Returns the path to the output file from this package for the given build name.
def build_path(build_name)
suffix = @build.use_suffix?(build_name) ? "-#{ build_name }" : ""
@build.layout == 'together' ?
Jake.path(@build.build_directory, "#{ @name }#{ suffix }.js") :
Jake.path(@build.build_directory, build_name, "#{ @name }.js")
# Returns +true+ if the build file for the given build name is out of date and
# should be regenerated.
def build_needed?(name)
return true if @build.forced?
path = build_path(name)
return true unless File.file?(path)
build_time = File.mtime(path)
files.any? { |path| File.mtime(path) > build_time }
# Returns the header string being used for this package.
def header
content = @config[:header] ? directory, @config[:header])) :
(parent ? parent.header : @build.header)
header = Jake.erb(content).result(@build.helper.scope).strip
return nil if header == ''
header << "\n"
# Returns the Packr settings to use for this package during the given build.
def packer_settings(build_name)
global = @build.packer_settings(build_name)
local = @config[:packer]
return parent.packer_settings(build_name) if parent && !local
return false if global == false or local == false
{}.merge(global || {}).merge(local || {})
# Returns a hash containing any metadata attached to the package in the config.
def meta
@config[:meta] || {}
# Generates all the build files for this package by looping over all the
# required builds and compressing the source code using each set of minification
# options. Files are only generated if they are out of date or the build has
# been forced.
def write!
@build.each do |name, settings|
path = build_path(name), self, name, path) and next unless build_needed?(name) = name.to_s
FileUtils.mkdir_p(File.dirname(path)), 'wb') { |f| f.write(code(name).strip) }, self, name, path)
size = (File.size(path)/1024.0).ceil
path = path.sub(@build.build_directory, '')
Jump to Line
Something went wrong with that request. Please try again.