00001 <?php
00006 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
00007
00008 class MinifyScript extends Maintenance {
00009 var $outDir;
00010
00011 public function __construct() {
00012 parent::__construct();
00013 $this->addOption( 'outfile',
00014 'File for output. Only a single file may be specified for input.',
00015 false, true );
00016 $this->addOption( 'outdir',
00017 "Directory for output. If this is not specified, and neither is --outfile, then the\n" .
00018 "output files will be sent to the same directories as the input files.",
00019 false, true );
00020 $this->mDescription = "Minify a file or set of files.\n\n" .
00021 "If --outfile is not specified, then the output file names will have a .min extension\n" .
00022 "added, e.g. jquery.js -> jquery.min.js.";
00023
00024 }
00025
00026 public function execute() {
00027 if ( !count( $this->mArgs ) ) {
00028 $this->error( "minify.php: At least one input file must be specified." );
00029 exit( 1 );
00030 }
00031
00032 if ( $this->hasOption( 'outfile' ) ) {
00033 if ( count( $this->mArgs ) > 1 ) {
00034 $this->error( '--outfile may only be used with a single input file.' );
00035 exit( 1 );
00036 }
00037
00038
00039 $this->minify( $this->getArg( 0 ), $this->getOption( 'outfile' ) );
00040 return;
00041 }
00042
00043 $outDir = $this->getOption( 'outdir', false );
00044
00045 foreach ( $this->mArgs as $arg ) {
00046 $inPath = realpath( $arg );
00047 $inName = basename( $inPath );
00048 $inDir = dirname( $inPath );
00049
00050 if ( strpos( $inName, '.min.' ) !== false ) {
00051 echo "Skipping $inName\n";
00052 continue;
00053 }
00054
00055 if ( !file_exists( $inPath ) ) {
00056 $this->error( "File does not exist: $arg" );
00057 exit( 1 );
00058 }
00059
00060 $extension = $this->getExtension( $inName );
00061 $outName = substr( $inName, 0, -strlen( $extension ) ) . 'min.' . $extension;
00062 if ( $outDir === false ) {
00063 $outPath = $inDir . '/' . $outName;
00064 } else {
00065 $outPath = $outDir . '/' . $outName;
00066 }
00067
00068 $this->minify( $inPath, $outPath );
00069 }
00070 }
00071
00072 public function getExtension( $fileName ) {
00073 $dotPos = strrpos( $fileName, '.' );
00074 if ( $dotPos === false ) {
00075 $this->error( "No file extension, cannot determine type: $arg" );
00076 exit( 1 );
00077 }
00078 return substr( $fileName, $dotPos + 1 );
00079 }
00080
00081 public function minify( $inPath, $outPath ) {
00082 $extension = $this->getExtension( $inPath );
00083 echo basename( $inPath ) . ' -> ' . basename( $outPath ) . '...';
00084
00085 $inText = file_get_contents( $inPath );
00086 if ( $inText === false ) {
00087 $this->error( "Unable to open file $inPath for reading." );
00088 exit( 1 );
00089 }
00090 $outFile = fopen( $outPath, 'w' );
00091 if ( !$outFile ) {
00092 $this->error( "Unable to open file $outPath for writing." );
00093 exit( 1 );
00094 }
00095
00096 switch ( $extension ) {
00097 case 'js':
00098 $outText = JSMin::minify( $inText );
00099 break;
00100 default:
00101 $this->error( "No minifier defined for extension \"$extension\"" );
00102 }
00103
00104 fwrite( $outFile, $outText );
00105 fclose( $outFile );
00106 echo " ok\n";
00107 }
00108 }
00109
00110 $maintClass = 'MinifyScript';
00111 require_once( DO_MAINTENANCE );