The Pope Is Dead ::

Main Navigation

The pope is dead. Long live the pope! Just another blog about web development. If you’re interested in web stuff you may find this blog useful.

Easy Javascript / CSS Compression with phing

Posted on 04/05/2008 at 12:08 PM

Hi Just thought I would blog a new phing task I just created that minifies css or javascript using YUI Compressor

This would be placed in build/extended/tasks/kpMinTask.php, also place the yuicompressor in build/tools/yuicompressor.jar

© 2010 Keith Pope
  1. <?php
  2. /**
  3.  * Uses the Phing Task
  4.  */
  5. require_once 'phing/Task.php';
  6.  
  7. /**
  8.  * Task to compress files using YUI Compressor.
  9.  *
  10.  * @author      Keith Pope
  11.  */
  12. class kpMinTask extends Task
  13. {
  14.     /**
  15.      * path to YuiCompressor
  16.      *
  17.      * @var  string
  18.      */
  19.     protected $yuiPath;
  20.    
  21.     /**
  22.      * the source files
  23.      *
  24.      * @var  FileSet
  25.      */
  26.     protected $filesets    = array();
  27.    
  28.     /**
  29.      * Whether the build should fail, if
  30.      * errors occured
  31.      *
  32.      * @var boolean
  33.      */
  34.     protected $failonerror = false;
  35.    
  36.     /**
  37.      * directory to put minified javascript files into
  38.      *
  39.      * @var  string
  40.      */
  41.     protected $targetDir;
  42.  
  43.     /**
  44.      * sets the path where JSmin can be found
  45.      *
  46.      * @param  string  $yuiPath
  47.      */
  48.     public function setYuiPath( $yuiPath )
  49.     {
  50.         $this->yuiPath = $yuiPath;
  51.     }
  52.  
  53.     /**
  54.      *  Nested creator, adds a set of files (nested fileset attribute).
  55.      */
  56.     public function createFileSet()
  57.     {
  58.         $num = array_push( $this->filesets, new FileSet() );
  59.         return $this->filesets[$num - 1];
  60.     }
  61.  
  62.     /**
  63.      * Whether the build should fail, if an error occured.
  64.      *
  65.      * @param boolean $value
  66.      */
  67.     public function setFailonerror( $value )
  68.     {
  69.         $this->failonerror = $value;
  70.     }
  71.  
  72.     /**
  73.      * sets the directory compressor traget dir
  74.      *
  75.      * @param  string  $targetDir
  76.      */
  77.     public function setTargetDir( $targetDir )
  78.     {
  79.         $this->targetDir = $targetDir;
  80.     }
  81.  
  82.     /**
  83.      * The init method: Do init steps.
  84.      */
  85.     public function init()
  86.     {
  87.         return true;
  88.     }
  89.  
  90.     /**
  91.      * The main entry point method.
  92.      */
  93.     public function main()
  94.     {
  95.         $command = 'java -jar {yuipath} {src} -o {target}';
  96.        
  97.         foreach( $this->filesets as $fs )
  98.         {
  99.             try
  100.             {
  101.                 $files    = $fs->getDirectoryScanner( $this->project )->getIncludedFiles();
  102.                 $fullPath = realpath( $fs->getDir( $this->project ) );
  103.                
  104.                 foreach( $files as $file )
  105.                 {
  106.                     $this->log( 'Minifying file ' . $file );
  107.  
  108.                     $target = $this->targetDir . '/' . str_replace( $fullPath, '', $file );
  109.                    
  110.                     if( file_exists( dirname( $target ) ) == false )
  111.                     {
  112.                         mkdir( dirname( $target ), 0700, true );
  113.                     }
  114.                    
  115.                     $cmd = str_replace( '{src}', $fullPath . DIRECTORY_SEPARATOR . $file, $command );
  116.                     $cmd = str_replace( '{target}', realpath( $target ), $cmd );
  117.                     $cmd = str_replace( '{yuipath}', realpath( $this->yuiPath ), $cmd );
  118.                    
  119.                     $output = array();
  120.                     $return = null;
  121.                    
  122.                     exec( $cmd, $output, $return );
  123.            
  124.                     foreach( $output as $line )
  125.                     {
  126.                         $this->log( $line, Project::MSG_VERBOSE );
  127.                     }
  128.                    
  129.                     if( $return != 0 )
  130.                     {
  131.                       throw new BuildException( "Task exited with code $return" );
  132.                     }
  133.  
  134.                 }
  135.             }
  136.            
  137.             catch( BuildException $be )
  138.             {
  139.                 // directory doesn't exist or is not readable
  140.                 if ($this->failonerror)
  141.                 {
  142.                     throw $be;
  143.                 }
  144.                 else
  145.                 {
  146.                     $this->log($be->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
  147.                 }
  148.             }
  149.         }
  150.     }
  151. }
  152. ?>
Parsed in 0.125 seconds, using GeSHi 1.0.7.21

Then in your phing build.xml

© 2010 Keith Pope
  1. <taskdef name="minify" classname="extended.tasks.kpMinTask" />
  2.  
  3. <target name="minify-js">
  4.     <echo>--------------------------------</echo>
  5.     <echo>| Minify javascript to release |</echo>
  6.     <echo>--------------------------------</echo>
  7.     <minify targetDir="../www/resources/js/min"
  8.               yuiPath="tools/yuicompressor.jar">
  9.         <fileset dir="../www/resources/js/">
  10.           <include name="*.js"/>
  11.         </fileset>
  12.     </minify>
  13. </target>
  14.  
  15. <target name="minify-css">
  16.     <echo>--------------------------------</echo>
  17.     <echo>| Minify CSS to release |</echo>
  18.     <echo>--------------------------------</echo>
  19.     <minify targetDir="../www/resources/v2_css/min"
  20.               yuiPath="tools/yuicompressor.jar">
  21.         <fileset dir="../www/resources/v2_css/">
  22.           <include name="*.css"/>
  23.         </fileset>
  24.     </minify>
  25. </target>
Parsed in 0.021 seconds, using GeSHi 1.0.7.21

Now we can can compress easily by running phing minify-js, sweet...

Comments

Eh, that’s a really good job. Very clean as well as elegant integration with phing.

I have added it as part of our framework build process (http://www.lionframework.org) as soon as we are using Phing right now.

Regards
Antonio
(http://www.lionframework.org)

Posted by Antonio Parraga Navarro  on  22/01/2009  at  02:13 AM
Page 1 of 1 comments

Next entry: Setting up my weblog. part 1 tagging

Previous entry: Finally its up ( still not finished hehe )

about me

I am a web developer/ project manager from Birmingham Uk, currently I am working for inflight productions as a technical project manager. Hopefully I will be posting here as regularly as possible on my experiences with various technologies that I am using and maybe the odd random thing..

the book

comment on this

Name:

Email:

Location:

URL:

Remember my personal information

Notify me of follow-up comments?