daux.io/libs/Compiler.php

218 lignes
7.0 KiB
PHP
Brut Vue normale Historique

2015-07-14 23:35:47 +02:00
<?php
/*
* This class is inspired from Composer's compiler
* @see https://github.com/composer/composer/blob/master/src/Composer/Compiler.php
*/
namespace Todaymade\Daux;
use Symfony\Component\Finder\Finder;
/**
* The Compiler class compiles daux into a phar
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Stéphane Goetz <stephane.goetz@onigoetz.ch>
*/
class Compiler
{
/**
* Compiles composer into a single phar file
*
* @throws \RuntimeException
* @param string $pharFile The full path to the file to create
*/
public function compile($pharFile = 'daux.phar')
{
2016-07-29 22:46:48 +02:00
echo "Compiling a new $pharFile\n";
2015-07-14 23:35:47 +02:00
if (file_exists($pharFile)) {
2016-07-29 22:46:48 +02:00
echo "-> Deleting the existing phar\n";
2015-07-14 23:35:47 +02:00
unlink($pharFile);
}
2016-07-29 22:46:48 +02:00
echo "-> Creating the new phar\n";
2015-07-14 23:35:47 +02:00
$phar = new \Phar($pharFile, 0, 'daux.phar');
$phar->setSignatureAlgorithm(\Phar::SHA1);
$phar->startBuffering();
// Daux
2016-07-29 22:46:48 +02:00
echo "-> Adding all daux files\n";
2015-07-14 23:35:47 +02:00
$finder = new Finder();
$finder->files()
->ignoreVCS(true)
->name('*.php')
->notName('Compiler.php')
->in(__DIR__ . '/../templates')
->in(__DIR__);
foreach ($finder as $file) {
$this->addFile($phar, $file);
}
// Composer libraries
2016-07-29 22:46:48 +02:00
echo "-> Adding all composer dependencies\n";
2015-07-14 23:35:47 +02:00
$finder = new Finder();
$finder->files()
->ignoreVCS(true)
->exclude('Tests')
2016-07-29 22:46:48 +02:00
->notName("*.png")
->in(__DIR__ . '/../vendor/guzzlehttp/guzzle/src')
->in(__DIR__ . '/../vendor/guzzlehttp/promises/src')
->in(__DIR__ . '/../vendor/guzzlehttp/psr7/src')
->in(__DIR__ . '/../vendor/league/commonmark/src')
->in(__DIR__ . '/../vendor/league/plates/src')
2016-04-14 12:13:53 +02:00
->in(__DIR__ . '/../vendor/myclabs/deep-copy')
2016-07-29 22:46:48 +02:00
->in(__DIR__ . '/../vendor/psr/http-message/src')
2016-04-14 12:13:53 +02:00
->in(__DIR__ . '/../vendor/symfony/console')
->in(__DIR__ . '/../vendor/symfony/polyfill-mbstring')
2016-07-29 22:46:48 +02:00
->in(__DIR__ . '/../vendor/symfony/process')
->in(__DIR__ . '/../vendor/symfony/process')
->in(__DIR__ . '/../vendor/symfony/yaml')
->in(__DIR__ . '/../vendor/webuni/front-matter/src')
->in(__DIR__ . '/../vendor/webuni/commonmark-table-extension/src');
$excluded_files = [
"README.md",
"composer.json",
"LICENSE",
"CHANGELOG.md",
"phpunit.xml.dist",
];
/** @var \SplFileInfo $file */
$count = 0;
2015-07-14 23:35:47 +02:00
foreach ($finder as $file) {
2016-07-29 22:46:48 +02:00
if (in_array($file->getFilename(), $excluded_files)) {
continue;
}
$count++;
2015-07-14 23:35:47 +02:00
$this->addFile($phar, $file);
}
2016-07-29 22:46:48 +02:00
echo " Imported $count files\n";
2015-07-14 23:35:47 +02:00
// Composer autoload
2016-07-29 22:46:48 +02:00
echo "-> Adding the composer autoloader\n";
2015-07-17 18:34:00 +02:00
$this->addComposer($phar);
2016-07-29 22:46:48 +02:00
echo "-> Adding the main binary\n";
2015-07-14 23:35:47 +02:00
$this->addBinary($phar);
2016-07-29 22:46:48 +02:00
echo "-> Writing the stub\n";
2015-07-14 23:35:47 +02:00
$phar->setStub($this->getStub());
$phar->stopBuffering();
2016-07-29 22:46:48 +02:00
echo "-> Writing the licence\n";
2015-07-14 23:35:47 +02:00
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../LICENSE'), false);
2016-01-07 21:31:43 +01:00
chmod($pharFile, 0775);
2015-07-14 23:35:47 +02:00
unset($phar);
2016-07-29 22:46:48 +02:00
echo "Done.\n";
2015-07-14 23:35:47 +02:00
}
private function addFile($phar, $file, $strip = true)
{
$path = strtr(str_replace(dirname(__DIR__) . DIRECTORY_SEPARATOR, '', $file->getRealPath()), '\\', '/');
$content = file_get_contents($file);
if ($strip) {
$content = $this->stripWhitespace($content);
} elseif ('LICENSE' === basename($file)) {
$content = "\n" . $content . "\n";
}
$phar->addFromString($path, $content);
}
2015-07-17 18:34:00 +02:00
private function addComposer($phar)
{
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../vendor/autoload.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../vendor/composer/autoload_classmap.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../vendor/composer/autoload_files.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../vendor/composer/autoload_namespaces.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../vendor/composer/autoload_real.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__ . '/../vendor/composer/ClassLoader.php'));
if (file_exists(__DIR__ . '/../vendor/composer/autoload_static.php')) {
$content = file_get_contents(__DIR__ . '/../vendor/composer/autoload_static.php');
$content = str_replace('__DIR__ . \'/../..\' . \'/daux\'', 'PHAR_DIR . \'/daux\'', $content);
$phar->addFromString('vendor/composer/autoload_static.php', $content);
}
2015-07-17 18:34:00 +02:00
$content = file_get_contents(__DIR__ . '/../vendor/composer/autoload_psr4.php');
$content = str_replace('$baseDir . \'/daux\'', 'PHAR_DIR . \'/daux\'', $content);
$phar->addFromString('vendor/composer/autoload_psr4.php', $content);
}
2015-07-14 23:35:47 +02:00
private function addBinary($phar)
{
2016-07-29 22:46:48 +02:00
$content = file_get_contents(__DIR__ . '/../bin/daux');
2015-07-18 21:23:02 +02:00
$content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);
2016-07-29 22:46:48 +02:00
$phar->addFromString('bin/daux', $content);
2015-07-14 23:35:47 +02:00
}
/**
* Removes whitespace from a PHP source string while preserving line numbers.
*
* @param string $source A PHP string
* @return string The PHP string with the whitespace removed
*/
private function stripWhitespace($source)
{
if (!function_exists('token_get_all')) {
return $source;
}
$output = '';
foreach (token_get_all($source) as $token) {
if (is_string($token)) {
$output .= $token;
2016-07-27 21:32:51 +02:00
} elseif (in_array($token[0], [T_COMMENT, T_DOC_COMMENT])) {
2015-07-14 23:35:47 +02:00
$output .= str_repeat("\n", substr_count($token[1], "\n"));
} elseif (T_WHITESPACE === $token[0]) {
// reduce wide spaces
$whitespace = preg_replace('{[ \t]+}', ' ', $token[1]);
// normalize newlines to \n
$whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace);
// trim leading spaces
$whitespace = preg_replace('{\n +}', "\n", $whitespace);
$output .= $whitespace;
} else {
$output .= $token[1];
}
}
return $output;
}
private function getStub()
{
return <<<'EOF'
#!/usr/bin/env php
<?php
/*
* This file is part of Daux.
*
* (c) Stéphane Goetz <onigoetz@onigoetz.ch>
*
* For the full copyright and license information, please view
* the license that is located at the bottom of this file.
*/
define('PHAR_DIR', dirname(__FILE__));
Phar::mapPhar('daux.phar');
2016-07-29 22:46:48 +02:00
require 'phar://daux.phar/bin/daux';
2015-07-14 23:35:47 +02:00
__HALT_COMPILER();
EOF;
}
}