Using Phing to build your Joomla package!

Easy AdSense by Unreal

Are you losing your time to build your Joomla package?

For me, it’s a pain! Some thing have to be done each time we want to release a new version. And I really don’t like to repeat thing again and again!

Step example :
- export from svn,
- change version number,
- edit manifest to setup free or pro version,
- delete some file for free version
- zip subpackage for pro version and free version for J1.5 and J1.6
- create package installer for pro version and free version for J1.5 and J1.6
- upload file on ftp
- upload api documentation on ftp
- test package on online test website to see if it work.

And I am sure to forgot some steps. Lot of these manipulation can be automated!

Here is a full and complex example to automate thing. It’s not totally finish but it’s already very powerful. Package creation take me now 20 second for something that take me some hour to do before.

It use the power of PHP5 with an xml structure. It’s called Phing

The actual feature I implement into my build script is :
- update source from svn (to be sure you have latest revision)
- export source,
- prepare export file for free/pro version and j1.5/j1.6 package type.
- package all extension in one big install extension
- upload package and phpdoc on ftp.
- (in future, call webservice for UpdateServer (J1.6 only) to lets him know new version available.)

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
<?xml version="1.0" encoding="UTF-8"?>
<project name="Build Package" default="build">
 
	<property name="packages_list_free" value="com_zzz,plg_system_zzz,lib_smarty" />
	<property name="packages_list_pro" value="com_zzz,plg_system_zzz,plg_user_zzz,lib_smarty" />
 
	<!-- Directory Info -->
	<property name="dir.builds" value="." override="true" />
	<property name="dir.export" value="export" override="true" />
	<property name="dir.packages" value="packages" override="true" />
	<property name="dir.final" value="final" override="true" />
	<property name="dir.src" value="../" override="true" />
	<!-- Set some params outside of the svn. (user/password) -->
 
	<target name="clean">
	</target>
 
	<target name="build">
		<property file="c:\\Projects\\zzz\\zzz.ini" override="true" />
 
		<echo>Choose valid option:</echo>
		<input propertyname="buildjversion" validargs="j15,j16" defaultValue="j16">
			Which version of joomla would you want for this build?
		</input>
		<input propertyname="buildtype" validargs="free,pro" defaultValue="free">
			Which build would you want to create?
		</input>
		<input propertyname="buildstage" validargs="dev,prod" defaultValue="dev">
			Which build stage would you want to create?
		</input>
		<input propertyname="buildversion" defaultValue="2.0">
			What is the version number?
		</input>
		<input propertyname="buildstate" validargs="alfa,beta,rc,final" defaultValue="beta">
			What is the state of the version
		</input>
		<input propertyname="ftp-deploy" validargs="y,n" defaultValue="y">
			Deploy on FTP?
		</input>
		<input propertyname="phpdoc.build" validargs="y,n" defaultValue="n">
			Deploy documentation?
		</input>
 
 
 
		<!-- Subversion Info -->
		<property name="svn.bin" value="svn" override="true" />
 
		<property name="packages_list" value="${packages_list_${buildtype}}" override="true" />
 
		<property name="phpdoc.defaultpackagename" value="zzz" override="true" />
		<property name="phpdoc.defaultcategoryname" value="zzz" override="true" />
		<property name="phpdoc.templatebase" value="C:\\wamp\\bin\\php\\php5.3.3\\PEAR\\PhpDocumentor\\phpDocumentor" override="true" />
 
 
		<!-- FTP Default Info -->
		<property name="ftp.port" value="21" override="true" />
		<property name="ftp.mode" value="binary" override="true" />
		<property name="ftp.level" value="debug" override="true" />
		<property name="ftp.docdir" value="public_html/docs/zzz/" override="true" />
		<property name="ftp.dir" value="/downloads/joomla/${buildstate}/components/com_zzz/" override="true" />
 
		<echo msg="Building extensions package - Version ${buildversion} - State ${buildstate}" />
 
		<!-- Export file out of svn directory -->
		<echo msg="Export file out of svn directory" />
		<svnlastrevision svnpath="${svn.bin}" workingcopy="${dir.src}" propertyname="svn.lastrevision" />
		<phingcall target="_svn-export" />
 
		<!-- Setup the build for the good joomla version -->
		<phingcall target="_${buildjversion}" />
 
		<!-- Setup the build for the good version type of zzz-->
		<phingcall target="_type-${buildtype}" />
 
		<!-- Build extensions package -->
		<echo msg="Build extensions package" />
		<delete dir="${dir.packages}" includeemptydirs="true" failonerror="false" />
		<mkdir dir="${dir.packages}" />
		<foreach list="${packages_list}" param="i" target="_build-zip" />
 
		<!-- Build final package -->
		<echo msg="Building NotifyMe final package - Version ${buildversion} - State ${buildstate}" />
		<phingcall target="_final-package" />
 
		<!-- Deploy the final package -->
		<echo msg="Deploy the packages on ftp://${ftp.host}/${ftp.dir}" />
		<phingcall target="_ftp-deploy" />
 
		<!-- Build documentation -->
		<phingcall target="_doc-deploy" />
 
		<echo msg="." />
		<echo msg="." />
		<if>
			<equals arg1="${ftp-deploy}" arg2="y" />
			<then>
				<echo msg="http://www.zzz.com/downloads/joomla/${buildstate}/components/com_zzz/pkg_zzz-${buildversion}-${buildstate}-${buildtype}-rev${svn.lastrevision}-${buildjversion}.zip" />
			</then>
		</if>
		<echo msg="." />
		<echo msg="." />
		<echo msg="DONE!!!!!!!" />
	</target>
 
	<target name="_final-package">
		<mkdir dir="${dir.final}" />
		<zip destfile="${dir.final}/pkg_zzz-${buildversion}-${buildstate}-${buildtype}-rev${svn.lastrevision}-${buildjversion}.zip">
			<fileset dir="${dir.export}">
				<include name="CHANGELOG" />
				<include name="pkg_everything.xml" />
				<include name="misc/*" />
			</fileset>
			<fileset dir="${dir.packages}">
				<include name="*.zip" />
			</fileset>
			<fileset dir="${dir.export}/misc">
				<include name="**" />
			</fileset>
		</zip>
	</target>
 
	<target name="_ftp-deploy">
		<if>
			<equals arg1="${ftp-deploy}" arg2="y" />
			<then>
				<ftpdeploy host="${ftp.host}" port="${ftp.port}" username="${ftp.username}" password="${ftp.password}" dir="${ftp.dir}" mode="${ftp.mode}" level="${ftp.level}">
					<fileset dir="${dir.final}">
						<include name="**" />
					</fileset>
				</ftpdeploy>
			</then>
			<else>
				<echo message="FTP transfert ignored" />
			</else>
		</if>
	</target>
 
	<target name="_type-free">
		<delete dir="${dir.export}/com_zzz/admin/views/templates" includeemptydirs="true" failonerror="false" />
		<delete dir="${dir.export}/com_zzz/admin/views/templatelang" includeemptydirs="true" failonerror="false" />
		<delete dir="${dir.export}/com_zzz/admin/views/template" includeemptydirs="true" failonerror="false" />
		<delete file="${dir.export}/com_zzz/admin/controllers/template.php" failonerror="false" />
		<delete file="${dir.export}/com_zzz/admin/controllers/templatelang.php" failonerror="false" />
		<delete file="${dir.export}/com_zzz/admin/controllers/templates.php" failonerror="false" />
		<delete file="${dir.export}/com_zzz/admin/models/template.php" failonerror="false" />
		<delete file="${dir.export}/com_zzz/admin/models/templatelang.php" failonerror="false" />
		<delete file="${dir.export}/com_zzz/admin/models/templates.php" failonerror="false" />
		<delete file="${dir.export}/com_zzz/admin/models/forms/template.php" failonerror="false" />
		<delete file="${dir.export}/com_zzz/admin/models/forms/templatelang.php" failonerror="false" />
		<delete file="${dir.export}/com_zzz/admin/tables/templatelang.php" failonerror="false" />
		<delete file="${dir.export}/com_zzz/admin/tables/template.php" failonerror="false" />
	</target>
 
	<target name="_type-pro">
		<!-- Nothing to do. Its the full version. -->
	</target>
 
	<target name="_j15">
		<foreach list="${packages_list}" param="i" target="_edit-manifest" />
	</target>
 
	<target name="_j16">
		<foreach list="${packages_list}" param="i" target="_edit-manifest" />
	</target>
 
	<target name="_build-zip">
		<zip destfile="${dir.packages}/${i}.zip" basedir="${dir.export}/${i}" includeemptydirs="true" />
	</target>
 
	<target name="_doc-deploy">
		<if>
			<equals arg1="${phpdoc.build}" arg2="y" />
			<then>
				<echo message="Buid documentation" />
				<delete dir="${dir.export}/apidocs" includeemptydirs="true" failonerror="false" />
				<mkdir dir="${dir.export}/apidocs" />
				<phpdoc title="${phing.project.name} API Documentation" templatebase="${phpdoc.templatebase}" defaultcategoryname="${phpdoc.defaultcategoryname}" defaultpackagename="${phpdoc.defaultpackagename}" destdir="${dir.export}/apidocs" sourcecode="true" quiet="true" output="HTML:frames/Extjs:default">
					<fileset dir="${dir.export}">
						<include name="**/*.php" />
					</fileset>
					<projdocfileset dir="${dir.export}">
						<include name="README" />
						<include name="INSTALL" />
						<include name="CHANGELOG" />
					</projdocfileset>
				</phpdoc>
 
				<!-- Deploy the documentation -->
				<echo msg="Deploy the documentation on ftp://${ftp.host}/${ftp.docdir}" />
				<ftpdeploy host="${ftp.host}" port="${ftp.port}" username="${ftp.username}" password="${ftp.password}" dir="${ftp.docdir}" mode="${ftp.mode}" level="${ftp.level}">
					<fileset dir="${dir.export}/apidocs">
						<include name="**" />
					</fileset>
				</ftpdeploy>
 
			</then>
			<else>
				<echo message="Documentation build ignored" />
			</else>
		</if>
	</target>
 
	<target name="_svn-export">
		<delete dir="${dir.export}" includeemptydirs="true" failonerror="false" />
		<mkdir dir="${dir.export}" />
		<svncheckout svnpath="${svn.bin}" username="${svn.username}" password="${svn.password}" repositoryurl="${svn.repository}" todir="${dir.src}" />
		<svnexport svnpath="${svn.bin}" repositoryurl="${svn.repository}" todir="${dir.export}" username="${svn.username}" password="${svn.password}" force="true" />
		<svnlog svnpath="${svn.bin}" repositoryurl="${svn.repository}" file="${dir.export}/CHANGELOG" revision="HEAD:40"></svnlog>
	</target>
 
	<target name="_edit-manifest">
		<php expression="file_get_contents('${dir.export}/${i}/extension.txt')" returnProperty="extension_name" />
		<move file="${dir.export}/${i}/manifest-${buildjversion}.xml" tofile="${dir.export}/${i}/${extension_name}.xml" overwrite="true" />
		<delete file="${dir.export}/${i}/extension.txt" failonerror="false" />
		<removeproelement file="${dir.export}/${i}/${extension_name}.xml" buildtype="${buildtype}"></removeproelement>
	</target>
	<adhoc-task name="svnlog"><![CDATA[
class svnLog extends Task { 
    private $file;
    private $svnpath;
    private $repositoryurl;
    private $revision;
 
    function setFile($file) { 
        $this->file = $file; 
    }
 
    function setRevision($revision) { 
        $this->revision = $revision; 
    }
 
    function setSvnPath($svnpath) { 
        $this->svnpath = $svnpath; 
    }
 
    function setRepositoryUrl($repositoryurl) { 
        $this->repositoryurl = $repositoryurl; 
    }
 
    private function writeSvnLog($svnpath="/usr/bin/svn", $repositoryurl, $file){
        $cmd = $svnpath . " log " . $repositoryurl . " -r ".$revision." > " . $file;
        echo $cmd . "\n";
        system($cmd);
        return true;
    } 
 
    private function getSvnLog($svnpath="/usr/bin/svn", $repositoryurl, $file, $revision = "HEAD:1"){
        $cmd = $svnpath . " log " . $repositoryurl . " -r ".$revision." > " . $file;
        echo $cmd . "\n";
        system($cmd);
		return true;
    } 
 
    function parse($changelog){
    	$filename = $this->file;
		$fh = fopen($filename, "r");
		$contents = fread($fh, filesize($filename));
		fclose($fh);
 
    	//parse contents.
    	$contents = utf8_encode($contents);
 
    	$fh = fopen($filename, "w") or die("can't open file");
    	fwrite($fh, $contents);
    	fclose($fh);
    	return true;
    }
 
    function main() { 
        $changelog = $this->getSvnLog($this->svnpath, $this->repositoryurl, $this->file, $this->revision);
        $changelog = $this->parse($changelog);
    } 
}
]]></adhoc-task>
	<adhoc-task name="removeproelement"><![CDATA[
class removeproelement extends Task { 
    private $file;
    private $buildtype;
 
    function setFile($file) { 
        $this->file = $file; 
    }
 
    function setBuildtype($buildtype) { 
        $this->buildtype = $buildtype; 
    }
 
    function main() { 
        $doc = new DOMDocument;
 
		// We don't want to bother with white spaces
		$doc->preserveWhiteSpace = false;
        $doc->Load($this->file);
        $xpath = new DOMXPath($doc);
 
        if ($this->buildtype == "free"){
	        $elements = $xpath->query("//*[@buildtype='pro']");
	        if (!is_null($elements)) {
		       	foreach ($elements as $element){
		        	$element->parentNode->removeChild($element);   		
		        }
	        }
        }
        $doc->save($this->file);
    } 
}
]]></adhoc-task>
</project>

Here is my project file structure on my repository and Eclipse project.

Project ZZZ
..pkg_everything.xml
..builds
....build.xml
....export
....final
....packages
..com_zzz
....manifest-j16.xml
....manifest-j15.xml
....extension.txt (content only extension name for builder)
....admin
......admin component file...
....site
......frontent component file...
..lib_zzz
....manifest-j16.xml
....manifest-j15.xml
....extension.txt
..plg_system_zzz
....manifest-j16.xml
....manifest-j15.xml
....extension.txt
..plg_user_zzz
....manifest-j16.xml
....manifest-j15.xml
....extension.txt


To setup your Phing environment in Eclipse, read this very nice tutorial : Extension development using eclipse and phing

I hope this help you to release your Joomla extension faster!

Related posts:

  1. Joomla PHPInfo component
  2. Joomla : How to show Menu to Guest Only
  3. Howto setup Smarty3 Engine in Joomla!

Tags : , , , , ,

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Facebook comments:

3 Comments

Leave Comment