1
0
mirror of https://github.com/mrclay/minify.git synced 2025-03-13 08:59:38 +01:00

Merge pull request #490 from glensc/nailgun

add Nailgun based ClosureCompiler
This commit is contained in:
Elan Ruusamäe 2015-11-22 21:45:45 +02:00
commit 2819e133a5
2 changed files with 162 additions and 0 deletions

View File

@ -0,0 +1,113 @@
<?php
/**
* Class Minify_ClosureCompiler
* @package Minify
*/
/**
* Run Closure Compiler via NailGun
*
* @package Minify
* @author Elan Ruusamäe <glen@delfi.ee>
* @link https://github.com/martylamb/nailgun
*/
class Minify_NailgunClosureCompiler extends Minify_ClosureCompiler
{
const NG_SERVER = 'com.martiansoftware.nailgun.NGServer';
const CC_MAIN = 'com.google.javascript.jscomp.CommandLineRunner';
/**
* For some reasons Nailgun thinks that it's server
* broke the connection and returns 227 instead of 0
* We'll just handle this here instead of fixing
* the nailgun client itself.
*
* It also sometimes breaks on 229 on the devbox.
* To complete this whole madness and made future
* 'fixes' easier I added this nice little array...
* @var array
*/
private static $NG_EXIT_CODES = array(0, 227, 229);
/**
* Filepath of "ng" executable (from Nailgun package)
*
* @var string
*/
public static $ngExecutable = 'ng';
/**
* Filepath of the Nailgun jar file.
*
* @var string
*/
public static $ngJarFile;
/**
* Get command to launch NailGun server.
*
* @return array
*/
protected function getServerCommandLine()
{
$this->checkJar(self::$ngJarFile);
$this->checkJar(self::$jarFile);
$classPath = array(
self::$ngJarFile,
self::$jarFile,
);
// The command for the server that should show up in the process list
$server = array(
self::$javaExecutable,
'-server',
'-cp', join(':', $classPath),
self::NG_SERVER,
);
return $server;
}
/**
* @return array
* @throws Minify_ClosureCompiler_Exception
*/
protected function getCompilerCommandLine()
{
$server = array(
self::$ngExecutable,
escapeshellarg(self::CC_MAIN)
);
return $server;
}
/**
* @param string $tmpFile
* @param array $options
* @return string
* @throws Minify_ClosureCompiler_Exception
*/
protected function compile($tmpFile, $options)
{
$this->startServer();
$command = $this->getCommand($options, $tmpFile);
return implode("\n", $this->shell($command, self::$NG_EXIT_CODES));
}
private function startServer()
{
$serverCommand = join(' ', $this->getServerCommandLine());
$psCommand = $this->shell("ps -o cmd= -C " . self::$javaExecutable);
if (array_search($serverCommand, $psCommand) !== false) {
// already started!
return;
}
$this->shell("$serverCommand </dev/null >/dev/null 2>/dev/null & sleep 10");
}
}

View File

@ -0,0 +1,49 @@
<?php
class MinifyNailgunClosureCompilerTest extends TestCase
{
public static function setupBeforeClass()
{
parent::setupBeforeClass();
Minify_ClosureCompiler::$isDebug = true;
// To test more functionality, download a compiler.jar from
// https://github.com/google/closure-compiler#getting-started,
// put it under tests dir as 'compiler.jar'
// set minimum necessary settings
Minify_ClosureCompiler::$jarFile = __DIR__ . DIRECTORY_SEPARATOR . 'compiler.jar';
Minify_ClosureCompiler::$tempDir = sys_get_temp_dir();
Minify_NailgunClosureCompiler::$ngJarFile = __DIR__ . DIRECTORY_SEPARATOR . 'nailgun.jar';
}
/**
* Test minimisation with the minimum necessary settings
*/
public function test2()
{
$this->assertHasJar();
$src = "
(function (window, undefined){
function addOne(input) {
return 1 + input;
}
window.addOne = addOne;
window.undefined = undefined;
})(window);
";
$minExpected = "(function(a,b){a.addOne=function(a){return 1+a};a.undefined=b})(window);";
$minOutput = Minify_NailgunClosureCompiler::minify($src);
$this->assertSame($minExpected, $minOutput, 'minimum necessary settings');
}
protected function assertHasJar()
{
$this->assertNotEmpty(Minify_ClosureCompiler::$jarFile);
try {
$this->assertFileExists(Minify_ClosureCompiler::$jarFile, "Have closure compiler compiler.jar");
} catch (Exception $e) {
$this->markTestSkipped($e->getMessage());
}
}
}