3 * Minify a file or set of files
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup Maintenance
23 use Wikimedia\AtEase\AtEase
;
25 require_once __DIR__
. '/Maintenance.php';
28 * Maintenance script that minifies a file or set of files.
30 * @ingroup Maintenance
32 class MinifyScript
extends Maintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->addOption( 'outfile',
38 'Write minified output to this file (instead of standard out).',
41 $this->addOption( 'type',
42 'Override the input type (one of "js" or "css"). Defaults to file extension. ' .
43 'Required if reading from standard input.',
46 $this->addArg( 'file', 'Input file. Use - to read from standard input.' );
47 $this->addDescription(
48 "Minify one or more JavaScript or CSS files.\n" .
49 "If multiple input files are given, they will be concatenated."
53 public function execute() {
54 $outputFile = $this->getOption( 'outfile', false );
55 if ( $outputFile === false ) {
56 // Only output the minified result (or errors)
57 // Avoid output() because this should not honour --quiet
58 foreach ( $this->mArgs
as $arg ) {
59 print $this->minify( $arg ) . "\n";
63 foreach ( $this->mArgs
as $arg ) {
64 $this->output( "Minifying {$arg} ...\n" );
65 $result .= $this->minify( $arg );
67 $this->output( "Writing to {$outputFile} ...\n" );
68 file_put_contents( $outputFile, $result );
69 $this->output( "Done!\n" );
73 public function getExtension( $fileName ) {
74 $dotPos = strrpos( $fileName, '.' );
75 if ( $dotPos === false ) {
76 $this->fatalError( "Unknown file type ($fileName). Use --type." );
78 return substr( $fileName, $dotPos +
1 );
81 private function readFile( $fileName ) {
82 if ( $fileName === '-' ) {
83 $inText = $this->getStdin( self
::STDIN_ALL
);
85 AtEase
::suppressWarnings();
86 $inText = file_get_contents( $fileName );
87 AtEase
::restoreWarnings();
88 if ( $inText === false ) {
89 $this->fatalError( "Unable to open file $fileName for reading." );
95 public function minify( $inPath ) {
96 $extension = $this->getOption( 'type', null ) ??
$this->getExtension( $inPath );
97 $inText = $this->readFile( $inPath );
99 switch ( $extension ) {
101 $outText = JavaScriptMinifier
::minify( $inText );
104 $outText = CSSMin
::minify( $inText );
107 $this->fatalError( "Unsupported file type \"$extension\"." );
114 $maintClass = MinifyScript
::class;
115 require_once RUN_MAINTENANCE_IF_MAIN
;