Automation using-phing

32
Automation Using Phing [email protected] Sunday, 20 June 2010

description

quick intro to phing and how it can be potentially be used for automation.

Transcript of Automation using-phing

Page 1: Automation using-phing

Automation Using Phing

[email protected]

Sunday, 20 June 2010

Page 2: Automation using-phing

What is phing

• Phing Is Not Gnumake

• Its a project build tool

• Based on Apache Ant

• Cross Platform (Runs everywhere php works)

• Lots of projects using it (Propel, Xinc, symfony, prada)

Sunday, 20 June 2010

Page 3: Automation using-phing

No compiling involved, so what does it build?

• Automation of non-development tasks

• Configuring

• Packaging

• Uploading to remote servers

• Testing

Sunday, 20 June 2010

Page 4: Automation using-phing

More automation...

• Run unit tests for you and publish reports

• Build API Docs

• Package and / or install PEAR packages

• Environment Setup

• App Configuration

Sunday, 20 June 2010

Page 5: Automation using-phing

More automation...

• setting up permissions

• environment setup

• app config

• basically anywhere you can script, you can use phing

Sunday, 20 June 2010

Page 6: Automation using-phing

Other Alternatives available

• Quite a few options available:

• ANT, Rake, NAnt

• Why Phing then?

• Dev already familiar with the language

• You can embed php straight in your build script

• Custom extensions are easy to write

• Works across platforms, small footprint

Sunday, 20 June 2010

Page 7: Automation using-phing

Phing: Basics

• Task: Built in custom piece of code to perform a specific function

• Target: Grouping of tasks to perform a more general function

• Project: Root node of build files containing one or more targets

Sunday, 20 June 2010

Page 8: Automation using-phing

Phing: Sample Build File<project name="trustedreviews" default="main"> <property name="ver" value="1.0.1" <property file="build.properties" /> <target name="main"> <mkdir dir="./build/${ver}"> <copy todir="./build/${veer}"> <fileset dir="." includes="*.txt" /> </copy> </target></project>

Sunday, 20 June 2010

Page 9: Automation using-phing

Phing: Selecting a bunch of files

• <fileset> is used to represent a bunch of files to do stuff with

• Many tasks support <fileset>

<fileset dir="/foo" includes="**/*.html" excludes="**/test-*" />

<fileset dir="/bla"> <includes name="img/${theme}/*.jpg" /> <includes name="js/*.js" /></fileset>

Sunday, 20 June 2010

Page 10: Automation using-phing

Phing: Fine tuning the selection

• You might need to fine tune your file selection further:

• Filter on date range?

• Filter on file size

• Type (File or Directory)

• At a particular depth

Sunday, 20 June 2010

Page 11: Automation using-phing

Phing: Fine tuning the selection

<fileset dir="${htdocs.dir}"> <includes name="**/*.html" /> <containsregexp expression="/prod\d+\.php" /></fileset>

<fileset dir="${dist}" includes="**"> <or> <present targetdir="${htdocs}" /> <date datetime="01/01/2010" when="before" /> </or></fileset>

Sunday, 20 June 2010

Page 12: Automation using-phing

Phing: Filesystem Transformation

• <mapper> element adds filesystem transformation capabilities for tasks that support it e.g. <copy>, <move> etc

<copy todir="/tmp"> <mapper type="glob" from="*.php" to="*.php.bak" /> <fileset dir="./app" includes="**/*.php" /></copy>

Sunday, 20 June 2010

Page 13: Automation using-phing

Phing: Filesystem Transformation

• Other kind of mappers present:

• Flatten Mapper: Gets base filename

• Regex Mapper: Changes filenames based on regular expressions

• Merge Mapper: change all source filenames to the same name

Sunday, 20 June 2010

Page 14: Automation using-phing

Phing: Data Transformation

• <filterchain> is used to transform the contents of the file. Supported by many tasks like <copy>, <move> etc

• Can perform various actions:

• Strip comments from your files

• Replace values in your file

• Perform XSLT Transformations

• Above all your chain these transformations

Sunday, 20 June 2010

Page 15: Automation using-phing

Phing: Data Transformation

<copy todir="${build}/htdocs"> <fileset includes="*.html" /> <filterchain> <replaceregexp> <regexp pattern="\r\n" replace="\n" /> </replaceregexp> <tidyfilter encoding="utf8"> <config name="indent" value="true" /> </tidyfilter> </filterchain></copy>

Sunday, 20 June 2010

Page 16: Automation using-phing

Phing: Data Transformation

• <headfilter> Reads only the first n lines of the file

• <linecontains> Filters out lines that contain a specific word

• <linecontainsregexp> Filters out lines that contain a specific regular expression

• <prefixlines> Adds stuff to the lines of the selected files

• <tabtospaces> Converts tabs to spaces (HURRAH!!)

Sunday, 20 June 2010

Page 17: Automation using-phing

Phing: More Data Transformations

• <StripPHPComments> Takes out php comments

• <replaceregexp>

• <replacetokens> This can particularly be handy for spec files

Sunday, 20 June 2010

Page 18: Automation using-phing

Phing: Data Transformations

• Consider a file that contains:The current user is ##current_user##

• Use this build target to replace the token<property environment="env" /><filterchain> <replacetokens begintoken="##" endtoken="##"> <token key="CURRENT" value="${env.LOGNAME}" /> <replacetoken></filterchain>

Sunday, 20 June 2010

Page 19: Automation using-phing

Phing: Packaging

• Tasks like <tar> <zip> can compress and package the set of files you want to compress

• <pearpkg> and <pearpkg2> allows you to build pear package using phing

Sunday, 20 June 2010

Page 20: Automation using-phing

Phing: Version Control and Deployment

• <svn*> tasks provide support for subversion:

• <svncheckout>

• <svncommit>

• <svnexport>

• <svnlastrevision>

• <scp> to move files to another server

• Support for CSV also present

Sunday, 20 June 2010

Page 21: Automation using-phing

Phing: Support for DB Migration

• <pdosqlexec> and <creole> provides execution of database statements

• <dbdeploy> can take care of db migrationshttp://dbdeploy.com/documentation/getting-started/rules-for-

using-dbdeploy/

• Drupal Deployment solution anyone?

Sunday, 20 June 2010

Page 22: Automation using-phing

Phing: Validating Code

• <jslint> using an external utility jsl

• <xmllint> uses internal DOM support for validating against given schema file

• <phplint> just uses php -l

• <tidy> can be use to validate markup and cleanup html

Sunday, 20 June 2010

Page 23: Automation using-phing

Phing: Php API Docs

• Support for phpDocumentor<phpdoc title="API Documentation" destdir="apidocs" sourcecode="no" output="HTML:Smarty:PHP"> <fileset dir="./classes"> <include name="**/*.php" /> </fileset></phpdoc>

Sunday, 20 June 2010

Page 24: Automation using-phing

Phing: Extending Functionalities

• Two ways to extend Phing:

• Embed PHP in the build file itself (not the cleanest solution)

• Write your own class to provide any of the following functioanlity:

• Task

• Type

• Selector

• Filter

• and more...

Sunday, 20 June 2010

Page 25: Automation using-phing

Phing: Extending Functionality

• <phpevaltask> lets you set a property to the results of evaluating a PHP Expression or the results by a function call.<php function="crypt" returnProperty="enc_passwd"> <param value="${auth.root_passwd}"/></php>

<php expression="3 + 4" returnProperty="sum"/><php expression="echo 'test';">

Sunday, 20 June 2010

Page 26: Automation using-phing

Phing: Extending Functionality

• <adhoc-task> allows you to define a task within your build file.

Sunday, 20 June 2010

Page 27: Automation using-phing

Phing: Extending Functionality

<target name="main" description="==>test AdhocTask "> <adhoc-task name="foo"><![CDATA[ class FooTest extends Task { private $bar; function setBar($bar) { $this->bar = $bar; } function main() { $this->log("In FooTest: " . $this->bar); } } ]]></adhoc-task> <foo bar="B.L.I.N.G"/></target>

Sunday, 20 June 2010

Page 28: Automation using-phing

Phing: Scripting & Logic• Phing also supports conditional tags

<if> <equals arg1="${foo}" arg2="bar" /> <then> <echo message="The value of property foo is 'bar'" /> </then> </elseif> <else> <echo message="The value of property foo is not 'foo' or 'bar'" /> </else></if>

Sunday, 20 June 2010

Page 29: Automation using-phing

Phing: Writing a custom task

require_once "phing/Task.php";class MyEchoTask extends Task { /** * The message passed in the buildfile. */ private $message = null; /** * The setter for the attribute "message" */ public function setMessage($str) { $this->message = $str; } /** * The init method: Do init steps. */ public function init() { // nothing to do here } /** * The main entry point method. */ public function main() { print($this->message); }}

Sunday, 20 June 2010

Page 30: Automation using-phing

Phing: Using the <myecho> task

<?xml version="1.0" ?><project name="test" basedir="." default="myecho"> <taskdef name="myecho" classname="phing.tasks.my.MyEcho" />

<target name="test.myecho"> <myecho message="Hello World" /> </target></project>

Sunday, 20 June 2010

Page 31: Automation using-phing

What Next?F!"!#$ P#$%$&"'"()& )& C)&"(&!)!% I&"$*#'"()& !%(&*

P +(&*

Sunday, 20 June 2010

Page 32: Automation using-phing

Questions?

Sunday, 20 June 2010