Skip to content

Commit

Permalink
Refactor to use the new Packr header API.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Mar 24, 2012
1 parent ffd13df commit dd3b812
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 19 deletions.
14 changes: 7 additions & 7 deletions lib/jake.rb
@@ -1,8 +1,6 @@
require 'erb' require 'erb'
require 'fileutils' require 'fileutils'
require 'observer'
require 'yaml' require 'yaml'
require 'rubygems'
require 'packr' require 'packr'
require 'eventful' require 'eventful'


Expand All @@ -18,6 +16,13 @@ module Jake
HELPER_FILE = 'Jakefile' HELPER_FILE = 'Jakefile'
EXTENSION = '.js' EXTENSION = '.js'


dir = File.expand_path('../jake', __FILE__)
autoload :Build, dir + '/build'
autoload :Buildable, dir + '/buildable'
autoload :Bundle, dir + '/bundle'
autoload :Helper, dir + '/helper'
autoload :Package, dir + '/package'

# Runs a build in the given directory. The directory must contain a jake.yml # Runs a build in the given directory. The directory must contain a jake.yml
# file, and may contain a Jakefile. See README for example YAML configurations. # file, and may contain a Jakefile. See README for example YAML configurations.
def self.build!(path, options = {}) def self.build!(path, options = {})
Expand Down Expand Up @@ -59,11 +64,6 @@ def self.symbolize_hash(hash, deep = true)
def self.erb(template) def self.erb(template)
defined?(Erubis) ? Erubis::Eruby.new(template) : ERB.new(template) defined?(Erubis) ? Erubis::Eruby.new(template) : ERB.new(template)
end end

end

%w(helper build buildable package bundle).each do |file|
require File.dirname(__FILE__) + '/jake/' + file
end end


# Adds a helper method that can be called from ERB. # Adds a helper method that can be called from ERB.
Expand Down
7 changes: 5 additions & 2 deletions lib/jake/buildable.rb
Expand Up @@ -57,7 +57,10 @@ def header
content = @config[:header] ? content = @config[:header] ?
Jake.read(Jake.path( directory, @config[:header])) : Jake.read(Jake.path( directory, @config[:header])) :
(parent ? parent.header : @build.header) (parent ? parent.header : @build.header)
Jake.erb(content).result(@build.helper.scope).strip
header = Jake.erb(content).result(@build.helper.scope).strip
return nil if header == ''
header << "\n"
end end


# Returns the PackR settings to use for this package during the given build. # Returns the PackR settings to use for this package during the given build.
Expand Down Expand Up @@ -85,7 +88,7 @@ def write!


@build.helper.build = name.to_s @build.helper.build = name.to_s
FileUtils.mkdir_p(File.dirname(path)) FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'wb') { |f| f.write( (header + "\n\n" + code(name)).strip ) } File.open(path, 'wb') { |f| f.write(code(name).strip) }


@build.fire(:file_created, self, name, path) @build.fire(:file_created, self, name, path)


Expand Down
14 changes: 11 additions & 3 deletions lib/jake/bundle.rb
Expand Up @@ -17,9 +17,17 @@ def source


# Returns the result of building the source template and minifying # Returns the result of building the source template and minifying
# the output using the given named set of PackR settings. # the output using the given named set of PackR settings.
def code(name) def code(build_name)
joiner = (packer_settings(name) == false) ? "\n\n\n" : "\n" return @code[build_name] if @code[build_name]
@code[name] ||= @config[:files].map { |pkg| @build.package(pkg).code(name) }.join(joiner)
joiner = (packer_settings(build_name) == false) ? "\n\n" : ""

code = @config[:files].map { |pkg| @build.package(pkg).code(build_name, false) }.join(joiner)
if head = header
code = head + code
end

@code[build_name] = code
end end


end end
Expand Down
27 changes: 22 additions & 5 deletions lib/jake/package.rb
Expand Up @@ -20,11 +20,28 @@ def source


# Returns the result of building the source template and minifying # Returns the result of building the source template and minifying
# the output using the given named set of PackR settings. # the output using the given named set of PackR settings.
def code(name) def code(build_name, with_header = true)
return @code[name] if @code[name] if cached = @code[build_name]
settings = packer_settings(name) return with_header ? cached[:with_header] : cached[:code]
output = Jake.erb(source).result(@build.helper.scope) end
@code[name] = settings ? Packr.pack(output, settings) : output
code = Jake.erb(source).result(@build.helper.scope)
head = header

if packer = packer_settings(build_name)
packer = packer.merge(:header => head)
code = Packr.pack(code, packer)
else
code = head + "\n" + code if head
end

@code[name] = {
:with_header => code,
:code => head && code[head.size..-1]
}
with_header ?
@code[name][:with_header] :
@code[name][:code]
end end


end end
Expand Down
9 changes: 8 additions & 1 deletion test/jake.yml
@@ -1,16 +1,21 @@
--- ---
source_directory: src source_directory: src
build_directory: output build_directory: output

builds: builds:
min: min:
shrink_vars: true shrink_vars: true
private: true private: true
source_map: src
src: src:
packer: false packer: false
suffix: false suffix: false

header: head header: head

packages: packages:
basic: basic basic: basic

ext: ext:
extends: basic extends: basic
packer: packer:
Expand All @@ -22,17 +27,19 @@ packages:
- Basic - Basic
requires: requires:
- Foo - Foo

sub/dir/foo: sub/dir/foo:
- foo/foo - foo/foo

sub/path/bar: sub/path/bar:
directory: foo directory: foo
header: ../head2 header: ../head2
packer: packer:
shrink_vars: false shrink_vars: false
files: files:
- bar - bar

bundles: bundles:
combo/box: combo/box:
- sub/dir/foo - sub/dir/foo
- ext - ext

2 changes: 2 additions & 0 deletions test/test_jake.rb
Expand Up @@ -17,8 +17,10 @@ def setup
def test_build def test_build
Jake.clear_hooks! Jake.clear_hooks!
Jake.build!(DIR) Jake.build!(DIR)

expected = File.join(DIR, 'expected') expected = File.join(DIR, 'expected')
actual = File.join(DIR, 'output') actual = File.join(DIR, 'output')

Find.find(expected) do |path| Find.find(expected) do |path|
next unless File.file?(path) next unless File.file?(path)
actual_path = actual + path.gsub(expected, '') actual_path = actual + path.gsub(expected, '')
Expand Down

0 comments on commit dd3b812

Please sign in to comment.