1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Plugins\CoreConsole\Commands;
use Piwik\Http;
use Piwik\Plugin\ConsoleCommand;
use Piwik\Plugin\Manager;
use Piwik\Unzip;
class BuildTracker extends ConsoleCommand
{
public const YUI_COMPRESSOR_URL = 'https://github.com/yui/yuicompressor/releases/download/v2.4.8/yuicompressor-2.4.8.zip';
protected function configure()
{
$this->setName('development:build-tracker-js');
$this->setDescription('Minifies tracker JavaScript for Matomo core or a single plugin.');
$this->addRequiredValueOption('plugin', null, 'The plugin to minify. If not supplied, minifies core tracker JS.');
}
public function isEnabled(): bool
{
return \Piwik\Development::isEnabled();
}
protected function doExecute(): int
{
$plugin = $this->getInput()->getOption('plugin');
if ($plugin && !Manager::getInstance()->isPluginInFilesystem($plugin)) {
throw new \InvalidArgumentException("Invalid plugin '$plugin'");
}
$this->installYuiCompressorIfNeeded();
$this->compress($plugin);
return self::SUCCESS;
}
private function compress($plugin)
{
$output = $this->getOutput();
$output->writeln("Minifying...");
$jsPath = MATOMO_PUBLIC_PATH . '/js';
if (!$plugin) {
$command = "cd $jsPath && sed '/<DEBUG>/,/<\\/DEBUG>/d' < piwik.js | sed 's/eval/replacedEvilString/' | java -jar yuicompressor-2.4.8.jar --type js --line-break 1000 | sed 's/replacedEvilString/eval/' | sed 's/^[/][*]/\\/*!/' > piwik.min.js && cp piwik.min.js ../piwik.js && cp piwik.min.js ../matomo.js";
if ($output->isVerbose()) {
$output->writeln("Command: $command");
}
passthru($command);
return;
}
$pluginTrackerJs = MATOMO_PLUGINS_PATH . '/plugins/' . $plugin . '/tracker.js';
$pluginTrackerMinJs = MATOMO_PLUGINS_PATH . '/plugins/' . $plugin . '/tracker.min.js';
$command = "cd $jsPath && cat $pluginTrackerJs | java -jar yuicompressor-2.4.8.jar --type js --line-break 1000 | sed 's/^[/][*]/\\/*!/' > $pluginTrackerMinJs";
if ($output->isVerbose()) {
$output->writeln("Command: $command");
}
passthru($command);
$output->writeln("Done.");
}
private function installYuiCompressorIfNeeded()
{
$zipPath = MATOMO_PUBLIC_PATH . '/js/yuicompressor-2.4.8.zip';
$jarPath = MATOMO_PUBLIC_PATH . '/js/yuicompressor-2.4.8.jar';
if (is_file($jarPath)) {
return;
}
$this->getOutput()->writeln("Downloading YUI compressor...");
Http::fetchRemoteFile(self::YUI_COMPRESSOR_URL, $zipPath);
$unzip = Unzip::factory('PclZip', $zipPath);
$unzip->extract(MATOMO_PUBLIC_PATH . '/js/');
}
}
|