mirror of
https://github.com/mrclay/minify.git
synced 2025-08-30 17:19:54 +02:00
CLI minify!
This commit is contained in:
88
min_extras/cli/minify.php
Executable file
88
min_extras/cli/minify.php
Executable file
@@ -0,0 +1,88 @@
|
|||||||
|
#!/usr/bin/php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
$pathToLib = dirname(dirname(__DIR__)) . '/min/lib';
|
||||||
|
|
||||||
|
// needed because of dumb require statements in class files :(
|
||||||
|
set_include_path($pathToLib . PATH_SEPARATOR . get_include_path());
|
||||||
|
|
||||||
|
// barebones autoloader
|
||||||
|
spl_autoload_register(function ($class) use ($pathToLib) {
|
||||||
|
$file = $pathToLib . '/' . str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $class) . '.php';
|
||||||
|
return is_file($file) ? ((require $file) || true) : false;
|
||||||
|
});
|
||||||
|
|
||||||
|
$cli = new MrClay\Cli;
|
||||||
|
|
||||||
|
$cli->addOptionalArg('d')->assertDir()->setDescription('Your webserver\'s DOCUMENT_ROOT: Relative paths will be rewritten relative to this path. This is required if you\'re passing in CSS.');
|
||||||
|
|
||||||
|
$cli->addOptionalArg('o')->useAsOutfile()->setDescription('Outfile: If given, output will be placed in this file.');
|
||||||
|
|
||||||
|
$cli->addOptionalArg('t')->mustHaveValue()->setDescription('Type: must be "css", "js", or "html". This must be provided if passing content via STDIN.');
|
||||||
|
|
||||||
|
if (! $cli->validate()) {
|
||||||
|
if ($cli->isHelpRequest) {
|
||||||
|
echo "The Minify CLI tool!\n\n";
|
||||||
|
}
|
||||||
|
echo "USAGE: ./minify.php [-t TYPE] [-d DOC_ROOT] [-o OUTFILE] file ...\n";
|
||||||
|
if ($cli->isHelpRequest) {
|
||||||
|
echo $cli->getArgumentsListing();
|
||||||
|
}
|
||||||
|
echo "EXAMPLE: ./minify.php ../../min_unit_tests/_test_files/js/*.js\n";
|
||||||
|
echo "EXAMPLE: ./minify.php -d../.. ../../min_unit_tests/_test_files/css/*.css\n";
|
||||||
|
echo "EXAMPLE: echo \"var js = 'Awesome' && /cool/;\" | ./minify.php -t js\n";
|
||||||
|
echo "EXAMPLE: echo \"sel > ector { prop: 'value '; }\" | ./minify.php -t css\n";
|
||||||
|
echo "\n";
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
$outfile = $cli->values['o'];
|
||||||
|
$docRoot = $cli->values['d'];
|
||||||
|
$type = $cli->values['t'];
|
||||||
|
|
||||||
|
if (is_string($type)) {
|
||||||
|
if (! in_array($type, array('js', 'css', 'html'))) {
|
||||||
|
echo "Type argument invalid\n";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
$type = constant('Minify::TYPE_' . strtoupper($type));
|
||||||
|
}
|
||||||
|
|
||||||
|
$paths = $cli->getPathArgs();
|
||||||
|
$sources = array();
|
||||||
|
|
||||||
|
if ($paths) {
|
||||||
|
foreach ($paths as $path) {
|
||||||
|
if (is_file($path)) {
|
||||||
|
$sources[] = new Minify_Source(array(
|
||||||
|
'filepath' => $path,
|
||||||
|
'minifyOptions' => array('docRoot' => $docRoot),
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
$sources[] = new Minify_Source(array(
|
||||||
|
'id' => $path,
|
||||||
|
'content' => "/*** $path not found ***/\n",
|
||||||
|
'minifier' => '',
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// not paths input, expect STDIN
|
||||||
|
if (! $type) {
|
||||||
|
echo "Type must be specified to use STDIN\n";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
$in = $cli->openInput();
|
||||||
|
$sources[] = new Minify_Source(array(
|
||||||
|
'id' => 'one',
|
||||||
|
'content' => stream_get_contents($in),
|
||||||
|
'contentType' => $type,
|
||||||
|
));
|
||||||
|
$cli->closeInput();
|
||||||
|
}
|
||||||
|
|
||||||
|
$combined = Minify::combine($sources) . "\n";
|
||||||
|
|
||||||
|
$fp = $cli->openOutput();
|
||||||
|
fwrite($fp, $combined);
|
||||||
|
$cli->closeOutput();
|
@@ -25,7 +25,7 @@ if (! $cli->validate()) {
|
|||||||
if ($cli->isHelpRequest) {
|
if ($cli->isHelpRequest) {
|
||||||
echo $cli->getArgumentsListing();
|
echo $cli->getArgumentsListing();
|
||||||
}
|
}
|
||||||
echo "EXAMPLE: ./rewrite-uris.php -t -d../.. ../../min_unit_tests/_test_files/css/paths_rewrite.css ../../min_unit_tests/_test_files/css/comments.css
|
echo "EXAMPLE: ./rewrite-uris.php -v -d../.. ../../min_unit_tests/_test_files/css/paths_rewrite.css ../../min_unit_tests/_test_files/css/comments.css
|
||||||
\n";
|
\n";
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
@@ -39,7 +39,6 @@ $pathRewriter = function($css, $options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
$paths = $cli->getPathArgs();
|
$paths = $cli->getPathArgs();
|
||||||
var_export($paths);
|
|
||||||
|
|
||||||
$sources = array();
|
$sources = array();
|
||||||
foreach ($paths as $path) {
|
foreach ($paths as $path) {
|
||||||
@@ -52,7 +51,7 @@ foreach ($paths as $path) {
|
|||||||
} else {
|
} else {
|
||||||
$sources[] = new Minify_Source(array(
|
$sources[] = new Minify_Source(array(
|
||||||
'id' => $path,
|
'id' => $path,
|
||||||
'content' => "/* $path not found */\n",
|
'content' => "/*** $path not found ***/\n",
|
||||||
'minifier' => '',
|
'minifier' => '',
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user