Skip to content

Instantly share code, notes, and snippets.

@rmrevin
Created January 30, 2015 19:41
Show Gist options
  • Save rmrevin/bc4c35dd0a049122c7e6 to your computer and use it in GitHub Desktop.
Save rmrevin/bc4c35dd0a049122c7e6 to your computer and use it in GitHub Desktop.
phing minify task
<project name="Minify build example" basedir=".">
<taskdef name="minify" classname="MinTask"/>
<target name="minify"
depends="minify/js,minify/css">
<echo>Ресурсы сжаты</echo>
</target>
<target name="minify/js">
<echo>Minify js...</echo>
<minify targetDir="frontend/js/"
cmd="node_modules/.bin/uglifyjs {input} -o {output} -c warnings=false">
<filelist dir="frontend/js" listfile="frontend/js/build"/>
</minify>
<delete file="frontend/min/all.js" verbose="true"/>
<append destFile="frontend/min/all.js">
<filelist dir="frontend/js/temp" listfile="frontend/js/build"/>
</append>
<echo>clear...</echo>
<delete dir="frontend/js/temp/" includeemptydirs="true"/>
</target>
<target name="minify/css">
<echo>Minify css...</echo>
<minify targetDir="frontend/css/"
cmd="node_modules/.bin/csso -i {input} -o {output}">
<filelist dir="frontend/css" listfile="frontend/css/build"/>
</minify>
<delete file="frontend/min/all.css" verbose="true"/>
<append destFile="frontend/min/all.css">
<filelist dir="frontend/css/temp" listfile="frontend/css/build"/>
</append>
<echo>clear...</echo>
<delete dir="frontend/css/temp/" includeemptydirs="true"/>
</target>
</project>
<?php
/**
* MinTask.php
* @author Revin Roman http://phptime.ru
*/
require_once 'phing/Task.php';
/**
* Task to compress files using YUI Compressor.
*
* @author Keith Pope
*/
class MinTask extends Task
{
/**
* path to processor
*
* @var string
*/
protected $cmd;
/**
* the source files
*
* @var FileSet[]
*/
protected $filesets = [];
/**
* the source files
*
* @var FileList[]
*/
protected $filelists = [];
/**
* Whether the build should fail, if
* errors occured
*
* @var boolean
*/
protected $failonerror = false;
/**
* directory to put minified javascript files into
*
* @var string
*/
protected $targetDir;
/**
* sets the path where JSmin can be found
*
* @param string $cmd
*/
public function setCmd($cmd)
{
$this->cmd = $cmd;
}
/**
* Nested creator, adds a set of files (nested fileset attribute).
*/
public function createFileSet()
{
$num = array_push($this->filesets, new FileSet());
return $this->filesets[$num - 1];
}
/**
* Supports embedded <filelist> element.
* @return FileList
*/
function createFileList()
{
$num = array_push($this->filelists, new FileList());
return $this->filelists[$num - 1];
}
/**
* Whether the build should fail, if an error occured.
*
* @param boolean $value
*/
public function setFailonerror($value)
{
$this->failonerror = $value;
}
/**
* sets the directory compressor traget dir
*
* @param string $targetDir
*/
public function setTargetDir($targetDir)
{
$this->targetDir = $targetDir;
}
/**
* The init method: Do init steps.
*/
public function init()
{
return true;
}
/**
* The main entry point method.
*/
public function main()
{
if (empty($this->filelists) && empty($this->filesets)) {
throw new BuildException("You must specify a file, use a filelist.");
}
foreach ($this->filelists as $fl) {
try {
$files = $fl->getFiles($this->project);
$fullPath = realpath($fl->getDir($this->project));
$this->processFiles($files, $fullPath);
} catch (BuildException $be) {
// directory doesn't exist or is not readable
if ($this->failonerror) {
throw $be;
} else {
$this->log($be->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
}
}
}
foreach ($this->filesets as $fs) {
try {
$files = $fs->getDirectoryScanner($this->project)->getIncludedFiles();
$fullPath = realpath($fs->getDir($this->project));
$this->processFiles($files, $fullPath);
} catch (BuildException $be) {
// directory doesn't exist or is not readable
if ($this->failonerror) {
throw $be;
} else {
$this->log($be->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
}
}
}
}
/**
* @param array $files
* @param string $fullPath
* @throws \BuildException
*/
private function processFiles($files, $fullPath)
{
foreach ($files as $file) {
$this->log('Minifying file ' . $file);
$target = $this->targetDir . '/temp/' . str_replace($fullPath, '', $file);
if (file_exists(dirname($target)) == false) {
mkdir(dirname($target), 0700, true);
}
$cmd = str_replace('{input}', $fullPath . DIRECTORY_SEPARATOR . $file, $this->cmd);
$cmd = str_replace('{output}', $target, $cmd);
$output = array();
$return = null;
exec($cmd, $output, $return);
foreach ($output as $line) {
$this->log($line, Project::MSG_VERBOSE);
}
if ($return != 0) {
throw new BuildException("Task exited with code $return");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment