Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 283 lines (216 sloc) 9.937 kb
fca9e45 @jcoglan Improved documentation. Should probably add inline documentation to B…
authored
1 = Jake
2
3 * http://github.com/jcoglan/jake
4
5 Jake is a command line tool for building JavaScript packages from source
6 code. Using simple YAML config files, you can specify any number of build
7 files to be generated by concatenating and minifying groups of source files.
8 It allows ERB to be used inside source files to generate code, and provides
9 event hooks into the build process so you can extend it for your own needs.
10
11
cfa7cb5 @jcoglan Add a features list.
authored
12 == Features
13
14 * Concatenate and minify groups of source files for distribution
15 * Configured using simple YAML files
16 * Easily generate multiple distros with different compression settings
17 * Use ERB templating to generate source code
18 * Extensible using Ruby/ERB and event hooks during the build process
19 * Tested on Ruby 1.8.6, 1.8.7, 1.9.1
20
21
fca9e45 @jcoglan Improved documentation. Should probably add inline documentation to B…
authored
22 == Installation
23
24 sudo gem install jake
25
26
27 == Usage
28
29 To begin with, create a file called <tt>jake.yml</tt> in the root directory
30 of your project; you will run the +jake+ command from here. A basic config
31 looks like this:
32
33 ---
34 source_directory: source
35 build_directory: build
36
37 layout: together
38
39 header: COPYRIGHT
40
41 builds:
42 src:
43 packer: false
44 min:
45 shrink_vars: true
46 private: true
47
48 packages:
49 [ DESCRIBED BELOW ]
50
51 * +source_directory+ is the directory relative to <tt>jake.yml</tt> where
52 your source files are, and +build_directory+ is where all the generated
53 build files will be placed.
54 * +layout+ describes whether files from separate builds should go in
55 separate directories. For example if you have a package called +foo+,
56 with the above config the +together+ layout will generate
57 <tt>build/foo-src.js</tt> and <tt>build/foo-min.js</tt>, whereas a
58 +layout+ value of +apart+ will generate <tt>build/src/foo.js</tt> and
59 <tt>build/min/foo.js</tt>.
60 * +header+ specifies a file whose content should appear at the top of
61 all generated build files. The content of this file will typically be
62 JavaScript comments containing copyright and license information. This
63 content is never minified. The +header+ option may be omitted.
64
65
66 === Build listing
67
68 The build listing, given by the +builds+ option in the config file, lists
69 all the builds you want to produce for distribution, and what minification
70 settings that build should use. JavaScript projects typically distribute
71 both compressed and uncompressed copies of their code to suit both production
72 and development environments.
73
74 You can have as many builds as you like and the names are up to you. I'm
75 using +src+ and +min+ as readily understood examples. Each build may specify
76 some combination of the following options:
77
78 * <tt>packer: false</tt> - disables minification for this build. This
79 precludes use of further minification options.
80 * <tt>shrink_vars: true</tt> - tells the minifier to compress local
81 variable names inside functions.
82 * <tt>private: true</tt> - tells the minifier to obfuscate 'private'
83 variables with numeric replacements. JavaScript convention is that any
84 name beginning with an underscore, e.g. <tt>_foo</tt> or <tt>obj._bar</tt>
85 should be considered private. They are replaced with <tt>_0</tt>,
86 <tt>_1</tt>, etc.
87 * <tt>base62: true</tt> - produces base-62 encoded minification.
88 * <tt>suffix: false</tt> - files from this build should not have a
89 suffix if using the +together+ layout, so you get <tt>build/foo.js</tt>
90 rather than <tt>build/foo-src.js</tt>, for example. Only one build
91 may use this option, otherwise file name clashes will occur.
92
93
94 === Package listing
95
96 The package listing, given under the +packages+ config option, describes
97 the packages you want to produce and which source files are used to generate
98 them. A package is named using the path under +build_directory+ where it
99 should be generated, e.g. <tt>foo</tt> or <tt>ext/awesome</tt> (you may
100 omit the <tt>.js</tt> extension). Each package lists one or more source
101 files used to build it, and may optionally list some extra options as described
102 below.
103
104 For the examples, assume the source directory is +src+ and the build
105 directory is +dist+. This package uses a single source file <tt>src/foo.js</tt>
106 and generates <tt>dist/foo_dist.js</tt>:
107
108 foo_dist: foo
109
110 This package generates <tt>dist/bar.js</tt> from <tt>src/bar1.js</tt> and
111 <tt>src/bar2.js</tt>
112
113 bar:
114 - bar1
115 - bar2
116
117 This generates a package at <tt>dist/sub/dir.js</tt> from <tt>src/path/file.js</tt>
118 and <tt>src/path/baz.js</tt>:
119
120 sub/dir:
121 - path/file
122 - path/baz
123
124 If all the source files for a package live in the same subdirectory, you
125 can tidy things up using the +directory+ option. If you use any package-level
126 options, you must list the files under the +files+ option (the above examples
127 are just syntactic shorthands for this):
128
129 sub/dir:
130 directory: path
131 files:
132 - file
133 - baz
134
135 The full list of package options is as follows:
136
137 * +files+ - lists the source files used to build the package. Shorthand may
138 be used as above if no further options are used.
139 * +extends+ - name of another package from which to inherit configuration.
140 Useful for making a package that includes all the files from another,]
141 plus a few extras.
142 * +directory+ - the directory under +source_directory+ in which to find
143 source files. May be omitted.
144 * +header+ - a custom header file to use on this package. Overrides the
145 root +header+ option. May be omitted.
146 * +packer+ - lists minification settings that override settings being used
147 for the current build. If a build listed above uses <tt>packer: false</tt>,
148 this takes precedence over package-specific instructions. Typically used
149 to override options for the minified build.
06b1430 @jcoglan Adding documentation for event hooks, inline method docs, and making …
authored
150 * +meta+ - should be a YAML dictionary containing arbitrary data useful to
151 user-defined build events. May be omitted. See 'Event hooks' below.
fca9e45 @jcoglan Improved documentation. Should probably add inline documentation to B…
authored
152
153 For example, here's a package listing that uses all the options:
154
155 packages:
156 foo_dist: foo
157
158 bar:
159 - bar1
160 - bar2
161
162 sub/whizz:
163 extends: foo_dist
164 directory: path
165 header: CUSTOM_HEADER
166 files:
167 - file1
168 - file2
169
170 last:
171 packer:
172 private: false
cfa7cb5 @jcoglan Add a features list.
authored
173 meta:
174 requires:
175 - jQuery
176 - GMap2
fca9e45 @jcoglan Improved documentation. Should probably add inline documentation to B…
authored
177 files:
178 - one_file
179 - another_file
180
181 In conjunction with the build options listed above, this matches the
182 following project layout (omitting build name suffixes for brevity):
183
184 - build/
185 - sub/
186 - whizz.js
187 - bar.js
188 - foo_dist.js
189 - last.js
190 - source/
191 - path/
192 - CUSTOM_HEADER
193 - file1.js
194 - file2.js
195 - another_file.js
196 - bar1.js
197 - bar2.js
198 - foo.js
199 - one_file.js
200 - COPYRIGHT
201 - jake.yml
202
203
204 === Using ERB in source files
205
206 Jake lets you use Ruby's ERB templating system within your source code so
207 you can insert values generated from Ruby functions. To use this feature,
208 you need to create a file called <tt>Jakefile</tt> in the root of your project.
209 This contains helper functions that are called in your source code to inject data.
210
211 For example, say you want to extract a version number from your version control
212 system and inject it into your code along with the build name. Your source code
213 should contain something like this:
214
215 MyJavaScriptLib.VERSION = "<%= version %>-<%= build %>";
216
217 And your <tt>Jakefile</tt> should contain a helper called +version+:
218
219 jake_helper :version do
220 # extract version number from svn, git, whatever
221 # e.g. return '1.0'
222 end
223
224 Jake has a built-in helper called +build+ that returns the current build
225 name. When built, the output would contain the following:
226
227 MyJavaScriptLib.VERSION = "1.0-src"; // or "1.0-min" for the 'min' build
228
229
06b1430 @jcoglan Adding documentation for event hooks, inline method docs, and making …
authored
230 === Event hooks
231
232 The +Jakefile+ may also define event hooks that are fired during a build when
233 interesting things happen. This allows you to extend your build process using
234 configuration data from Jake. We currently have two events:
235
236 +file_created+ is fired whenever a new build file is created. The callback is
237 passed the +Buildable+ package object, the current build name (+src+ or +min+
238 using the above examples), and the full path to the newly created file.
239 The package object may contain metadata (set using the +meta+ option, see
240 above) which you can use for further code generation.
241
242 +build_complete+ is fired after a build has finished running, that is after
243 all sets of minification options have been run. At this point you can use any
244 metadata you've gathered to generate more code, copy files to your distribution
245 directory, etc.
246
247 $register = {}
248
249 jake_hook :file_created do |pkg, build_name, path|
250 $register[path] = pkg.meta
251 end
252
253 jake_hook :build_complete do |build|
254 FileUtils.cp 'README', build.build_directory + '/README'
255 # generate code from $register
256 end
257
258
fca9e45 @jcoglan Improved documentation. Should probably add inline documentation to B…
authored
259 == License
260
261 (The MIT License)
262
263 Copyright (c) 2008-2009 James Coglan
264
265 Permission is hereby granted, free of charge, to any person obtaining
266 a copy of this software and associated documentation files (the
267 'Software'), to deal in the Software without restriction, including
268 without limitation the rights to use, copy, modify, merge, publish,
269 distribute, sublicense, and/or sell copies of the Software, and to
270 permit persons to whom the Software is furnished to do so, subject to
271 the following conditions:
272
273 The above copyright notice and this permission notice shall be
274 included in all copies or substantial portions of the Software.
275
276 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
277 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
278 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
279 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
280 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
281 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
282 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Something went wrong with that request. Please try again.