Automation using-phing

5,734 views
5,589 views

Published on

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

Published in: Technology
1 Comment
11 Likes
Statistics
Notes
No Downloads
Views
Total views
5,734
On SlideShare
0
From Embeds
0
Number of Embeds
424
Actions
Shares
0
Downloads
72
Comments
1
Likes
11
Embeds 0
No embeds

No notes for slide

Automation using-phing

  1. 1. Automation Using Phing rajat_pandit@ipcmedia.com Sunday, 20 June 2010
  2. 2. 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
  3. 3. No compiling involved, so what does it build? • Automation of non-development tasks • Configuring • Packaging • Uploading to remote servers • Testing Sunday, 20 June 2010
  4. 4. 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
  5. 5. More automation... • setting up permissions • environment setup • app config • basically anywhere you can script, you can use phing Sunday, 20 June 2010
  6. 6. 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
  7. 7. 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
  8. 8. 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
  9. 9. 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
  10. 10. 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
  11. 11. Phing: Fine tuning the selection <fileset dir="${htdocs.dir}"> <includes name="**/*.html" /> <containsregexp expression="/prodd+.php" /> </fileset> <fileset dir="${dist}" includes="**"> <or> <present targetdir="${htdocs}" /> <date datetime="01/01/2010" when="before" /> </or> </fileset> Sunday, 20 June 2010
  12. 12. 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
  13. 13. 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
  14. 14. 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
  15. 15. Phing: Data Transformation <copy todir="${build}/htdocs"> <fileset includes="*.html" /> <filterchain> <replaceregexp> <regexp pattern="rn" replace="n" /> </replaceregexp> <tidyfilter encoding="utf8"> <config name="indent" value="true" /> </tidyfilter> </filterchain> </copy> Sunday, 20 June 2010
  16. 16. 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
  17. 17. Phing: More Data Transformations • <StripPHPComments> Takes out php comments • <replaceregexp> • <replacetokens> This can particularly be handy for spec files Sunday, 20 June 2010
  18. 18. 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
  19. 19. 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
  20. 20. 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
  21. 21. Phing: Support for DB Migration • <pdosqlexec> and <creole> provides execution of database statements • <dbdeploy> can take care of db migrations http://dbdeploy.com/documentation/getting-started/rules-for- using-dbdeploy/ • Drupal Deployment solution anyone? Sunday, 20 June 2010
  22. 22. 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
  23. 23. 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
  24. 24. 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
  25. 25. 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
  26. 26. Phing: Extending Functionality • <adhoc-task> allows you to define a task within your build file. Sunday, 20 June 2010
  27. 27. 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
  28. 28. 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
  29. 29. 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 }
  30. 30. 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
  31. 31. What Next? F P C I P Sunday, 20 June 2010
  32. 32. Questions? Sunday, 20 June 2010

×