Class: Jake::Buildable

Inherits:
Object
  • Object
show all
Defined in:
lib/jake/buildable.rb

Overview

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.

Direct Known Subclasses

Bundle, Package

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(build, name, config) ⇒ Buildable

Create a new Buildable using a Build container, a name and a configuration hash, typically a subsection of jake.yml.



13
14
15
16
17
18
19
20
21
# File 'lib/jake/buildable.rb', line 13

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}
  end
  @code = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/jake/buildable.rb', line 9

def name
  @name
end

Instance Method Details

#build_needed?(name) ⇒ Boolean

Returns true if the build file for the given build name is out of date and should be regenerated.

Returns:

  • (Boolean)


47
48
49
50
51
52
53
# File 'lib/jake/buildable.rb', line 47

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 }
end

#build_path(build_name) ⇒ Object

Returns the path to the output file from this package for the given build name.



38
39
40
41
42
43
# File 'lib/jake/buildable.rb', line 38

def build_path(build_name)
  suffix = @build.use_suffix?(build_name) ? "-#{ build_name }" : ""
  @build.layout == 'together' ?
      "#{ @build.build_directory }/#{ @name }#{ suffix }.js" :
      "#{ @build.build_directory }/#{ build_name }/#{ @name }.js"
end

#directoryObject

Returns the source directory for this package.



31
32
33
34
35
# File 'lib/jake/buildable.rb', line 31

def directory
  dir = @config[:directory]
  return parent.directory if parent && !dir
  "#{ @build.source_directory }/#{ @config[:directory] }"
end

#headerObject

Returns the header string being used for this package.



56
57
58
59
60
61
# File 'lib/jake/buildable.rb', line 56

def header
  content = @config[:header] ?
      Jake.read("#{ directory }/#{ @config[:header] }") :
      (parent ? parent.header : @build.header)
  Jake.erb(content).result(@build.helper.scope).strip
end

#metaObject

Returns a hash containing any metadata attached to the package in the config.



73
74
75
# File 'lib/jake/buildable.rb', line 73

def meta
  @config[:meta] || {}
end

#packer_settings(build_name) ⇒ Object

Returns the PackR settings to use for this package during the given build.



64
65
66
67
68
69
70
# File 'lib/jake/buildable.rb', line 64

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 || {})
end

#parentObject

Returns the parent Buildable set using the extends option, or nil if there is no parent.



25
26
27
28
# File 'lib/jake/buildable.rb', line 25

def parent
  return nil unless @config[:extends]
  @parent ||= @build.package(@config[:extends])
end

#write!Object

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.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jake/buildable.rb', line 81

def write!
  @build.each do |name, settings|
    path = build_path(name)
    @build.fire(:file_not_changed, self, name, path) and next unless build_needed?(name)
    
    @build.helper.build = name.to_s
    FileUtils.mkdir_p(File.dirname(path))
    File.open(path, 'wb') { |f| f.write( (header + "\n\n" + code(name)).strip ) }
    
    @build.fire(:file_created, self, name, path)
    
    size = (File.size(path)/1024.0).ceil
    path = path.sub(@build.build_directory, '')
  end
end