forked from ti2/soulwire.co.uk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.xml
407 lines (303 loc) · 10.6 KB
/
build.xml
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
ANT build file for soulwire.co.uk
@author Justin Windle
Combines blocks of CSS/JS files
Updates references is templates
Minifies CSS using YUI Compressor
Minifies JavaScript using YUI Compressor
Usage:
$ cd {project directory}
$ ant build
-->
<project name="Build" default="build" basedir=".">
<!--
Include the build properties
-->
<property file="build.properties"/>
<!--
Using Ant-Contrib
-->
<taskdef resource="net/sf/antcontrib/antlib.xml">
<classpath>
<pathelement location="${ANT_CONTRIB}"/>
</classpath>
</taskdef>
<!--
The main build task
-->
<target name="build" depends="clean, clone, make" />
<!--
Compile with Closure compiler
-->
<taskdef name="jscomp" classname="com.google.javascript.jscomp.ant.CompileTask" classpath="${COMPILER}" />
<!--
Minify with YUI
-->
<macrodef name="yui-minify">
<attribute name="target" default="" />
<attribute name="type" default="css" />
<sequential>
<echo message="YUI Compressor: Minifying '@{target}'..." />
<exec executable="java" failonerror="true">
<arg line="-jar ${COMPRESSOR}" />
<arg line="-o @{target} @{target}" />
<arg line="--line-break 0" />
<arg line="--type @{type}" />
<arg line="_--verbose" />
</exec>
</sequential>
</macrodef>
<!--
Combine & Compile
-->
<macrodef name="compile">
<attribute name="type"/>
<attribute name="inputs"/>
<attribute name="output"/>
<sequential>
<concat destfile="@{output}" append="true">
<filelist dir="/" files="@{inputs}"/>
</concat>
<if>
<equals arg1="@{type}" arg2="css" />
<then>
<yui-minify target="@{output}" type="css" />
</then>
<elseif>
<equals arg1="@{type}" arg2="js" />
<then>
<!--<yui-minify target="@{output}" type="js" />-->
<jscomp compilationLevel="${COMPILATION}" warning="${WARNINGS}" debug="false" output="@{output}">
<externs dir="/">
<file name="${EXTERNS}" />
</externs>
<sources dir="/">
<file name="@{output}" />
</sources>
</jscomp>
</then>
</elseif>
<else>
<echo>Unrecognised type: "@{type}"</echo>
</else>
</if>
</sequential>
</macrodef>
<!--
Delete
-->
<macrodef name="remove">
<attribute name="files"/>
<sequential>
<delete verbose="true" failonerror="true">
<fileset dir="${BUILD_DIR}" includes="@{files}" />
</delete>
</sequential>
</macrodef>
<!--
Updates a file
-->
<macrodef name="update">
<attribute name="file"/>
<attribute name="content"/>
<sequential>
<echo>Update: @{file}</echo>
<echo message="@{content}" output="@{file}" />
</sequential>
</macrodef>
<!--
Cleans the build directory
-->
<target name="clean">
<delete dir="${BUILD_DIR}"/>
<mkdir dir="${BUILD_DIR}"/>
</target>
<!--
Clones src into build
-->
<target name="clone">
<copy todir="${BUILD_DIR}">
<fileset dir="${SRC_DIR}" excludes="**/*.svn,${IGNORE_DIRS}"/>
</copy>
</target>
<!--
Process build folder
-->
<target name="make">
<echo>Building Site...</echo>
<!-- Process HTML files -->
<for param="file">
<!-- Loop through HTML files -->
<fileset dir="${BUILD_DIR}" includes="**/*.html"/>
<sequential>
<var name="file" value="@{file}" />
<var name="content" unset="true" />
<!-- Load the current file into a property -->
<loadfile property="content" srcFile="@{file}"/>
<!-- This script process the resources blocks within a file -->
<script language="javascript">
<![CDATA[
function log(msg) { echo = project.createTask("echo"); build.addTask(echo); echo.setMessage(msg); }
// The webroot for mapping filepaths (defined in build.properties)
var webroot = project.getProperty( "WEB_ROOT" );
// Get the content of the current file
var content = project.getProperty( "content" );
if( content ) {
var templates = {
// Template for embedding JavaScript
js: '<script type="text/javascript" src="${path}"></script>',
// Template for embedding CSS
css: '<link rel="stylesheet" type="text/css" href="${path}">'
};
var regex = {
// Matches a block of linked resources
block: /<\!\-\-\s?<block\s+?output\=['"]([^'"]+)['"][^]+?<\/block\>\s?\-\-\>/gi,
// Matches an individual resource
resource: /<(script|link)[^>]+(href|src)=["']([^"']+)/gi
};
var type, // The filetype of a block
embed, // Embed code for the resource
block, // The current matched block
blockID, // The ID of the current matched block
allBlocks, // The global list of blocks
resource, // A matched resource within a block
filePath, // Filepath of a resource within a block
fileList; // A list of resources for each block ID
var hasChanged = false;
var newContent = content.toString();
// Loop through all blocks in the current file
for(;block = regex.block.exec( content );) {
// The block id is the first capture group
blockID = block[1];
// Extract the filetype
type = blockID.match( /\w+$/ )[0].toLowerCase();
// Generate embed code
embed = templates[type].replace( /\${path}/gi, blockID );
// Update the content
newContent = newContent.replace( block[0], embed );
// Flag the content as changed
hasChanged = true;
// Grab the list of resources realting to this block type
list = project.getProperty( blockID ) || '';
// Grab the global property for this block ID or create a new one
allBlocks = project.getProperty( "blocks" ) || '';
// Add this block ID to the list if not already present
if( new RegExp( blockID ).test( allBlocks ) === false ) {
if( allBlocks.length !== 0 ) {
allBlocks += ',';
}
allBlocks += blockID;
}
fileList = project.getProperty( blockID ) || '';
// Find all resources linked within this block
for(;resource = regex.resource.exec( block[0] );) {
// The matched filepath of the resource
filePath = resource[3];
if( new RegExp( filePath ).test( fileList ) === false ) {
if( fileList.length !== 0 ) {
fileList += ',';
}
// Add the absolute path to the resource
fileList += (webroot + filePath);
}
// Update the global file list
project.setProperty( blockID, fileList );
}
// Update the blocks property
project.setProperty( "blocks", allBlocks );
// Update the content property
project.setProperty( "content", newContent );
}
if( hasChanged ) {
var update = project.createTask( "update" );
update.setDynamicAttribute( "content", newContent );
update.setDynamicAttribute( "file", project.getProperty( "file" ) );
update.execute();
}
}
]]>
</script>
</sequential>
</for>
<script language="javascript">
<![CDATA[
function log(msg) { echo = project.createTask("echo"); build.addTask(echo); echo.setMessage(msg); }
// DETECT & COMPILE
// The webroot for mapping filepaths (defined in build.properties)
var webroot = project.getProperty( "WEB_ROOT" );
// Store the project base directory
var baseDir = project.getProperty( "basedir" );
// Store the target build directory
var buildDir = project.getProperty( "BUILD_DIR" );
var regex = {
// Matches an item in a list separated by commas
item: /[^\,]+/g
};
var blocksProp, // The value of the global blocks property
blocksArray, // An array of block IDs within the blocks property
blockMatch, // The result of a regex match on a block list
filesProp, // The value of the global resources property for a block
filesArray, // An array of resource paths within a files property
fileType, // The filetype of the block being compiled
output; // The output file
// Process all block types we've found
blocksProp = project.getProperty( "blocks" );
if( blocksProp ) {
// Loop through all block IDs in the comma separated list
for(;blockMatch = regex.item.exec( blocksProp );) {
// Grab the global property for this block ID
filesProp = project.getProperty( blockMatch );
if( filesProp ) {
// Split the commas separated list
filesArray = filesProp.match( /[^\,]+/g );
output = (webroot + blockMatch.toString());
fileType = output.match( /\w+$/ );
// Create a compile task and call it with parameters
var compile = project.createTask( "compile" );
compile.setDynamicAttribute( "type", fileType );
compile.setDynamicAttribute( "output", output );
compile.setDynamicAttribute( "inputs", filesArray );
compile.execute();
}
}
}
// DETECT & DELETE
if( blocksProp ) {
// Loop through all block IDs in the comma separated list
for(;blockMatch = regex.item.exec( blocksProp );) {
// Grab the global property for this block ID
filesProp = project.getProperty( blockMatch );
if( filesProp ) {
// Split the commas separated list
filesArray = filesProp.match( /[^\,]+/g );
// Double check that we're only deleting inside the project!
var safe = [];
var filePath;
for( var i = 0; i < filesArray.length; ++i ) {
if( filesArray[i].match( baseDir ) ) {
// Format the filepath for the delete task
filePath = filesArray[i].replace( buildDir, '' );
safe.push( filePath.replace( /^\/+/, '' ) );
}
}
// Create a remove (delete) task and call it with the files
var remove = project.createTask( "remove" );
remove.setDynamicAttribute( "files", safe.toString() );
remove.execute();
}
}
}
]]>
</script>
<echo>Cleaning empty directories</echo>
<delete includeemptydirs="true" verbose="true" failonerror="true">
<fileset dir="${BUILD_DIR}">
<and>
<size value="0"/>
<type type="dir"/>
</and>
</fileset>
</delete>
</target>
</project>