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.



9
10
11
12
13
14
15
16
17
# File 'lib/jake/buildable.rb', line 9

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.



7
8
9
# File 'lib/jake/buildable.rb', line 7

def name
  @name
end

Instance Method Details

#build_needed?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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



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

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

#headerObject



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

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

#packer_settings(build_name) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/jake/buildable.rb', line 52

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



19
20
21
22
# File 'lib/jake/buildable.rb', line 19

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

#write!Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/jake/buildable.rb', line 60

def write!
  puts "Package #{@name}..."
  
  @build.each do |name, settings|
    next unless build_needed?(name)
    
    @build.helper.build = name
    path = build_path(name)
    FileUtils.mkdir_p(File.dirname(path))
    File.open(path, 'wb') { |f| f.write( (header + "\n\n" + code(name)).strip ) }
    
    size = (File.size(path)/1024.0).ceil
    path = path.sub(@build.build_directory, '')
    puts "  -- build '#{ name }' created #{ path }, #{ size } kb"
  end
end