forked from NETTUTS/Phing-Build-Script-for-Web-Designers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.xml
187 lines (147 loc) · 5.84 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
<?xml version="1.0"?>
<project name="demo" default="build">
<property file="build.properties" />
<property name="build.jsPath" value="${build.dir}/${base.js.path}" />
<property name="build.cssPath" value="${build.dir}/${base.css.path}" />
<!-- Delete the Publish directory to start from scratch -->
<target name="build.delete">
<delete dir="${build.dir}" />
</target>
<!-- Create the Build Directory -->
<target name="build.createDir">
<mkdir dir="${build.dir}" />
</target>
<target name="copy.all">
<copy todir="${build.dir}">
<fileset dir="${base.dir}">
<exclude name="${build.publishName}" />
<exclude name="build/" />
</fileset>
</copy>
</target>
<target name="minify.loop">
<foreach param="filename" absparam="absfilename" target="minify.file">
<fileset dir="${build.jsPath}" includes="${js.concat.min}" />
<fileset dir="${build.cssPath}" includes="${css.concat.min}" />
</foreach>
</target>
<target name="minify.file">
<exec command="java -jar yuicompressor-2.4.6.jar ${absfilename} -o ${absfilename}" />
</target>
<!-- Concatenate JS -->
<target name="js.concat">
<echo msg="Concatenating JavaScript files." />
<echo msg="${js.linkPaths}" />
<mkdir dir="${build.jsPath}" />
<append destfile="${build.jsPath}/${js.concat.min}">
<filelist dir="${base.dir}" files="${js.linkPaths}" />
</append>
</target>
<!-- Concatenate CSS -->
<target name="css.concat">
<echo msg="Concatenating CSS files." />
<mkdir dir="${build.cssPath}" />
<append destfile="${build.cssPath}/${css.concat.min}">
<filelist dir="${base.dir}" files="${css.linkPaths}" />
</append>
</target>
<target name="html.minifyLoop">
<foreach param="filename" absparam="absfilename" target="html.minify">
<fileset dir="${build.dir}" includes="**/*.html, **/*.php">
<contains text="<body>" />
</fileset>
</foreach>
</target>
<!-- Minify HTML Files -->
<target name="html.minify">
<exec command="java -jar htmlcompressor-1.4.3.jar ${absfilename} -o ${absfilename}" />
</target>
<!-- Update HTML Files with new Links -->
<target name="assets.update">
<echo message="Replacing old script links with new ones." />
<reflexive>
<fileset dir="${build.dir}" includes="**/*.html, **/*.php" />
<filterchain>
<replaceregexp>
<regexp pattern="<!--\s*js\s*-->[\s\S]+?<!--\s*end js\s*-->" replace="<script src='./${base.js.path}/${js.concat.min}/${js.concat.min}'></script>" />
<regexp pattern="<!--\s*css\s*-->[\s\S]+?<!--\s*end css\s*-->" replace="<link rel='stylesheet' href='./${base.css.path}/${css.concat.min}' />" />
</replaceregexp>
</filterchain>
</reflexive>
</target>
<target name="assets.createLinks">
<adhoc-task name="get_link_paths">
<![CDATA[
class Links extends Task {
private $type;
private $property;
private $files;
private $links;
function setType($type) {
$this->type = $type;
}
function setProperty($property) {
$this->property = $property;
}
function main() {
$this->getFiles();
foreach($this->files as $file) {
$links = $this->findLinks($file);
if ( false !== $links ) {
$this->links .= empty($this->links) ? $links : ",$links";
}
}
// css/style2.css,css/style.css,css/style3.css,css/style2.css,css/style.css
$this->links = implode(',', array_unique(explode(',', $this->links)));
$this->project->setProperty($this->property, $this->links);
}
function getFiles() {
// Get list of all HTML and PHP files in the base directory.
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('${base.dir}'));
$objects = new RegexIterator($objects, '/\.(?:html|php)$/i', RecursiveRegexIterator::GET_MATCH);
$files = array();
foreach($objects as $name => $object) {
$files[] = str_replace('../', '', $name);
}
$this->files = $files;
}
function findLinks($file) {
$file = file_get_contents("${base.dir}/$file");
$this->log("Stripping out the '$this->type' path.");
$links = trim(preg_replace('/[\s\S]+?<!--\s*' . $this->type . '\s*-->([\s\S]+)<!--\s*end ' . $this->type . '\s*-->[\s\S]+/', "$1", $file, -1, $count));
// If the returned val from the preg_replace is the same, there's no match. // Remove this one from the list. It does not have script or link tags.
if ( $count === 0 ) return false;
if ( $this->type === 'css' ) {
$links = preg_replace("/<link.+?href=[\"']?([^'\"]+)['\"]?[^>]+>/i", "$1", $links);
} else if ( $this->type === 'js' ) {
$links = preg_replace("/<script.+?src=[\"']?([^\"']+)[\"']?[\s\S]+?<\/script>/", "$1", $links);
}
$links = preg_replace('/\s+/', ',', $links);
return $links;
}
}
]]>
</adhoc-task>
</target>
<target name="assets.getPaths" depends="assets.createLinks">
<get_link_paths type="js" property="js.linkPaths" />
<get_link_paths type="css" property="css.linkPaths" />
</target>
<!-- Delete all files that have been concatenated. -->
<target name="delete.unnecessaryFiles">
<echo message="Deleting any files that aren't necessary for your build directory." />
<delete>
<filelist dir="${build.dir}" files="${js.linkPaths}" />
<filelist dir="${build.dir}" files="${css.linkPaths}" />
</delete>
</target>
<!-- Run CSS through Prefixr.com -->
<target name="prefixr">
<echo msg="Updating your stylesheet" />
<loadfile property="css.min" file="${build.cssPath}/${css.concat.min}" />
<exec command="curl -sSd css='${css.min}' http://prefixr.com/api/index.php > ${build.cssPath}/${css.concat.min}" />
<phingcall target="minify.loop" />
</target>
<target name="build" depends="build.delete, build.createDir, assets.getPaths, copy.all, js.concat, css.concat, minify.loop, assets.update, html.minifyLoop, delete.unnecessaryFiles">
</target>
</project>