Class: Jake::Buildable

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

Direct Known Subclasses

Bundle, Package

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(build, name, config) ⇒ Buildable

Returns a new instance of Buildable.



6
7
8
9
10
11
12
13
14
# File 'lib/jake/buildable.rb', line 6

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.



4
5
6
# File 'lib/jake/buildable.rb', line 4

def name
  @name
end

Instance Method Details

#build_needed?(name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
# File 'lib/jake/buildable.rb', line 34

def build_needed?(name)
  return true if @build.forced?
  path = build_path(name)
  return true unless File.file?(path)
  build_time = File.mtime(path)
  
  input_files = files + @build.config_files
  input_files.any? { |path| File.mtime(path) > build_time }
end

#build_path(build_name) ⇒ Object



27
28
29
30
31
32
# File 'lib/jake/buildable.rb', line 27

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")
end

#directoryObject



21
22
23
24
25
# File 'lib/jake/buildable.rb', line 21

def directory
  dir = @config[:directory]
  return parent.directory if parent && !dir
  Jake.path(@build.source_directory, @config[:directory])
end

#headerObject



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

def header
  content = @config[:header] ?
      Jake.read(Jake.path( directory, @config[:header])) :
      (parent ? parent.header : @build.header)
  
  return nil unless content
  header = Jake.erb(content).result(@build.helper.scope)
  return nil if header == ''
  header
end

#metaObject



63
64
65
# File 'lib/jake/buildable.rb', line 63

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

#packer_settings(build_name) ⇒ Object



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

def packer_settings(build_name)
  global = @build.packer_settings(build_name)
  local  = @config[:packer]
  return parent.packer_settings(build_name) if parent && !local
  return {:minify => false} if global == false or local == false
  {}.merge(global || {}).merge(local || {})
end

#parentObject



16
17
18
19
# File 'lib/jake/buildable.rb', line 16

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

#write!Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/jake/buildable.rb', line 67

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))
    
    output_code = code(name)
    source_map = output_code.source_map if output_code.respond_to?(:source_map)
    
    File.open(path, 'w') { |f| f.write(output_code) }
    @build.fire(:file_created, self, name, path)
    
    if source_map and source_map.enabled?
      File.open(source_map.filename, 'w') { |f| f.write(source_map.to_s) }
      @build.fire(:file_created, self, name, source_map.filename)
    end
    
    size = (File.size(path)/1024.0).ceil
    path = path.sub(@build.build_directory, '')
  end
end