Skip to content

Instantly share code, notes, and snippets.

@meeech
Created March 4, 2010 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meeech/321761 to your computer and use it in GitHub Desktop.
Save meeech/321761 to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
/**
* YUI Compress CLI Util
*
* usage: (assuming the script is in your path, executable, and you are in the root directory of the files.)
*
* yuicompress.php javascript.js rollover.js links.js tabber/tabber-minimized.js utility_effects.js
*
* or
*
* yuicompress.php foo.css bar.css zee/alpha.css
*
* Infers CSS or JS based on file type passed in (the first one in list)
*
* @author Mitchell Amihod
*/
array_shift($argv);//Drop first arg
$files = $argv;//assume rest is files list
$yuiCompressor = '/Users/mitch/Sites/yuicompressor-2.4.2/build/yuicompressor-2.4.2.jar';
if(empty($files)) {
die( 'No files passed in.');
}
//Infer from first file type, since no mixin and matching
$mode = (strpos($files[0], '.js') > -1 ) ? 'js' : 'css' ;
$currentdir = getcwd();
$collector = array();
foreach ($files as $fileName) {
$filepath = $currentdir . '/' . $fileName;
if(!file_exists($filepath)) {
echo $filepath." no exist \n";
continue;
}
echo "adding $filepath\n";
$collector[] = file_get_contents($filepath);
}
$tempconcat = $currentdir . '/yuitocompresstemp.'.$mode;
$fliesListComment = "/*!\n" . implode(',', $files) . "\n*/\n" ;
file_put_contents($tempconcat, $fliesListComment . implode("\n\r", $collector));
$outfile = $currentdir . '/yui-compressed.'.$mode;
$command = 'java -jar %3$s --type js --preserve-semi -o %1$s %2$s';
if('css' == $mode) {
$command = 'java -jar %3$s --type css --line-break 0 -o %1$s %2$s';
}
exec(sprintf($command, $outfile, $tempconcat, $yuiCompressor));
//Clean up
unlink($tempconcat);
echo "\n{$outfile} is ready.\n\r";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment