Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: b1360a0f00
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 123 lines (99 sloc) 3.834 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
module Jake
  # A +Build+ encapsulates a single instance of running <tt>Jake.build!</tt>. It
  # is responsible for running the build and provides access to any configuration
  # data used to set up the build.
  class Build
    extend Observable
    
    # Calls all registered +Observer+ instances with any arguments passed in. Note
    # that it is the +Build+ class that is +Observable+, not its instances.
    def self.notify_observers(*args)
      self.changed(true)
      super
    end
    
    DEFAULT_LAYOUT = 'together'
    
    include Enumerable
    attr_reader :helper
    
    # Builds are initialized using a directory in which to run the build, and an
    # options hash. Options are passed through to helper methods in the +options+
    # object.
    def initialize(dir, options = {})
      @dir = File.expand_path(dir)
      @helper = Helper.new(options)
      force! if options[:force]
      
      path = "#{dir}/#{CONFIG_FILE}"
      yaml = File.read(path)
      
      @config = Jake.symbolize_hash( YAML.load(Jake.erb(yaml).result(@helper.scope)) )
      
      helpers = "#{dir}/#{HELPER_FILE}"
      load helpers if File.file?(helpers)
      
      @builds = @config[:builds] || {:src => false, :min => @config[:packer]}
      
      @packages = @config[:packages].inject({}) do |pkgs, (name, conf)|
        pkgs[name] = Package.new(self, name, conf)
        pkgs
      end
      
      @bundles = (@config[:bundles] || {}).inject({}) do |pkgs, (name, conf)|
        pkgs[name] = Bundle.new(self, name, conf)
        pkgs
      end
    end
    
    # Yields to the block for each build in the group.
    def each(&block)
      @builds.each(&block)
    end
    
    # Forces the build to generate new files even if target files appear up-to-date.
    def force!
      @forced = true
    end
    
    # Returns +true+ iff this is a forced build.
    def forced?
      defined?(@forced)
    end
    
    # Returns the +Buildable+ with the given name.
    def package(name)
      @packages[name.to_sym] || @bundles[name.to_sym]
    end
    
    # Runs the build, generating new files in +build_directory+.
    def run!
      FileUtils.cd(@dir) do
        @packages.each { |name, pkg| pkg.write! }
        @bundles.each { |name, pkg| pkg.write! }
        self.class.notify_observers(:build_complete, self)
      end
    end
    
    # Returns the path to the build directory, where generated files appear.
    def build_directory
      "#{ @dir }/#{ @config[:build_directory] || '.' }"
    end
    alias :build_dir :build_directory
    
    # Returns the path to the source directory, where source code is read from.
    def source_directory
      "#{ @dir }/#{ @config[:source_directory] || '.' }"
    end
    alias :source_dir :source_directory
    
    # Returns the header string used for the build, or an empty string if no
    # header file has been set.
    def header
      @config[:header] ?
          Jake.read("#{ source_directory }/#{ @config[:header] }") :
          ""
    end
    
    # Returns the minification settings for PackR for the given build name. Each
    # +Build+ object can build all its packages multiple times with different
    # minification settings in each run.
    def packer_settings(build_name)
      build = @builds[build_name.to_sym]
      return false unless build
      build[:packer].nil? ? build : build[:packer]
    end
    
    # Returns +true+ iff filename suffixes based on build name should be added
    # to generated files for the given build name.
    def use_suffix?(build_name)
      build = @builds[build_name.to_sym]
      return true unless build
      build[:suffix] != false
    end
    
    # Returns the name of the layout being used, either +together+ or +apart+.
    def layout
      @config[:layout] || DEFAULT_LAYOUT
    end
    
  end
end

Something went wrong with that request. Please try again.