-
Notifications
You must be signed in to change notification settings - Fork 6
/
jake.rb
81 lines (68 loc) · 2.12 KB
/
jake.rb
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
require 'erb'
require 'fileutils'
require 'yaml'
require 'packr'
require 'eventful'
begin
require 'erubis'
rescue LoadError
end
module Jake
VERSION = '1.0.1'
CONFIG_FILE = 'jake.yml'
HELPER_FILE = 'Jakefile'
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
# file, and may contain a Jakefile. See README for example YAML configurations.
def self.build!(path, options = {})
build = Build.new(path, options)
build.run!
end
# Removes all registered build event hooks.
def self.clear_hooks!
Build.delete_observers
end
# Returns a path made by joining the given pieces and removing unnecessary
# /. sections using expand_path.
def self.path(*parts)
parts = parts.compact.map { |p| p.to_s }
File.expand_path(File.join(*parts))
end
# Returns the contents of the given path, which may be missing a .js extension.
def self.read(path)
path = File.expand_path(path)
[path, "#{path}#{EXTENSION}"].each do |file|
return File.read(file) if File.file?(file)
end
return nil
end
# Returns a copy of the given hash with the keys cast to symbols.
def self.symbolize_hash(hash, deep = true)
hash.inject({}) do |output, (key, value)|
value = Jake.symbolize_hash(value) if deep and value.is_a?(Hash)
output[(key.to_sym rescue key) || key] = value
output
end
end
# Returns either an Erubis or ERB instance, depending on what's available.
def self.erb(template)
defined?(Erubis) ? Erubis::Eruby.new(template) : ERB.new(template)
end
end
# Adds a helper method that can be called from ERB.
def jake_helper(name, &block)
Jake::Helper.class_eval do
define_method(name, &block)
end
end
alias :jake :jake_helper
# Registers an event listener that will fire whenever a build is run.
def jake_hook(type, &block)
Jake::Build.on(type, &block)
end