Skip to content

Commit

Permalink
Merge 0af4142 into 6a69a77
Browse files Browse the repository at this point in the history
  • Loading branch information
alias-mac committed Feb 3, 2014
2 parents 6a69a77 + 0af4142 commit b74a14c
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 6 deletions.
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Insulin documentation
docs
/docs

# Symfony
build/
vendor
bin
# Third party tools
/vendor
27 changes: 27 additions & 0 deletions bin/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env php
<?php

/*
* This file is part of the Insulin CLI
*
* Copyright (c) 2008-2012 Filipe Guerra, João Morais
* http://cli.sugarmeetsinsulin.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require_once __DIR__.'/../autoload.php';

use Insulin\Console\Compiler;

error_reporting(-1);
ini_set('display_errors', 1);

try {
$compiler = new Compiler();
$compiler->compile();
} catch (\Exception $e) {
echo 'Failed to compile phar: ['.get_class($e).'] '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine();
exit(1);
}
2 changes: 1 addition & 1 deletion insulin → bin/insulin
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

set_time_limit(0);

require_once __DIR__.'/autoload.php';
require_once __DIR__ . '/../autoload.php';

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
Expand Down
198 changes: 198 additions & 0 deletions src/Console/Compiler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

/*
* This file is part of the Insulin CLI
*
* Copyright (c) 2008-2014 Filipe Guerra, João Morais
* http://cli.sugarmeetsinsulin.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Insulin\Console;

use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;

/**
* The Compiler class compiles insulin cli into a phar.
*
* @codeCoverageIgnore since it's for internal use only (build insulin.phar).
*/
class Compiler
{
private $version;
private $versionDate;

/**
* Compiles insulin into a single phar file.
*
* @param string $pharFile The full path to the file to create.
*
* @throws \RuntimeException when running outside git repo or no git binary
* is available.
*/
public function compile($pharFile = 'insulin.phar')
{
if (file_exists($pharFile)) {
unlink($pharFile);
}

$process = new Process('git log --pretty="%H" -n1 HEAD', __DIR__);
if ($process->run() != 0) {
throw new \RuntimeException(
"Can't run git log. You must ensure to run compile from insulin/cli git repository clone " .
"and that git binary is available."
);
}
$this->version = trim($process->getOutput());

$process = new Process('git log -n1 --pretty=%ci HEAD', __DIR__);
if ($process->run() != 0) {
throw new \RuntimeException(
"Can't run git log. You must ensure to run compile from insulin/cli git repository clone " .
"and that git binary is available."
);
}
$date = new \DateTime(trim($process->getOutput()));
$date->setTimezone(new \DateTimeZone('UTC'));
$this->versionDate = $date->format('Y-m-d H:i:s');

$process = new Process('git describe --tags HEAD');
if ($process->run() == 0) {
$this->version = trim($process->getOutput());
}

$phar = new \Phar($pharFile, 0, 'insulin.phar');
$phar->setSignatureAlgorithm(\Phar::SHA1);

$phar->startBuffering();

$finder = new Finder();
$finder->files()
->ignoreVCS(true)
->name('*.php')
->notName('Compiler.php')
->notName('ClassLoader.php')
->in(__DIR__ . '/..');

foreach ($finder as $file) {
$this->addFile($phar, $file);
}

$finder = new Finder();
$finder->files()
->ignoreVCS(true)
->name('*.php')
->exclude('Tests')
->in(__DIR__ . '/../../vendor/symfony/')
->in(__DIR__ . '/../../vendor/doctrine/');

foreach ($finder as $file) {
$this->addFile($phar, $file);
}

$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../autoload.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/autoload.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_namespaces.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_psr4.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_classmap.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_real.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_files.php'));
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/ClassLoader.php'));
$this->addInsulinBin($phar);

// Stubs
$phar->setStub($this->getStub());

$phar->stopBuffering();

// disabled for interoperability with systems without gzip ext
// $phar->compressFiles(\Phar::GZ);

$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../LICENSE'), false);

unset($phar);
}

private function addFile($phar, $file, $strip = true)
{
$path = strtr(str_replace(dirname(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);
}

private function addInsulinBin($phar)
{
$content = file_get_contents(__DIR__.'/../../bin/insulin');
$content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);
$phar->addFromString('bin/insulin', $content);
}

/**
* 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;
} elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
$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()
{
$stub = <<<'EOF'
#!/usr/bin/env php
<?php
/*
* This file is part of the Insulin CLI
*
* Copyright (c) 2008-2012 Filipe Guerra, João Morais
* http://cli.sugarmeetsinsulin.com
*
* For the full copyright and license information, please view
* the license that is located at the bottom of this file.
*/
Phar::mapPhar('insulin.phar');
require 'phar://insulin.phar/bin/insulin';
__HALT_COMPILER();
EOF;

return $stub;
}
}

0 comments on commit b74a14c

Please sign in to comment.