Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
You wouldnt believe how simple copy&adapting Composer\Compiler made g…
…enerating a PHAR.
  • Loading branch information
beberlei committed Apr 10, 2015
1 parent 09f4032 commit 6f302e0
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
/vendor/
fiddler.phar
1 change: 1 addition & 0 deletions LICENSE
@@ -0,0 +1 @@
MIT
21 changes: 18 additions & 3 deletions README.md
Expand Up @@ -22,7 +22,7 @@ More details about reasoning on Gregory Szorc's blog:

1. You can just `require "vendor/autoload.php;` in every component as if using composer.
But prevents grabbing any class by just autoloading by generating custom autoloaders.
Explicit dependencies necessary in `fiddle.json`.
Explicit dependencies necessary in `fiddler.json`.
2. No one-to-one git repository == composer package requirement anymore,
increasing productivity using Google/Facebook development model.
3. No composer.lock/Pulol Request issues that block your productivity with multi repository projects.
Expand All @@ -31,6 +31,12 @@ More details about reasoning on Gregory Szorc's blog:
6. Detect components that changed since a given commit and their dependants to allow efficient
build process on CI systems (only test components that changed, only regenerate assets for components that changed, ...)

## Usage

Fiddler offers a very simple command line interface for now:

$ php fiddler.phar build

## Implementation

This project assumes you have a single monolithic repository with
Expand All @@ -46,11 +52,10 @@ where you upate some basic library like "symfony/dependency-injection" in
10-20 components or worse, have massively out of date components and
many different versions everywhere.

Then every of your own components contains a `fiddle.json` using almost
Then every of your own components contains a `fiddler.json` using almost
the same syntax as Composer:

{
"name": "my-component",
"deps": [
"components/Foo"
"vendor/symfony/symfony"
Expand All @@ -70,3 +75,13 @@ code is always present in the correct versions in a monolithic repository).
Package names in `deps` are the relative directory names from the project root.
From the vendor directory `composer.json` are loaded to find out the dependency graph
and the autoload configuration.

## Configuration (fiddler.json)

For each component in your monolithic repository you have to add `fiddler.json`
that borrows from `composer.json` format. The following keys are usable:

- `autoload` - configures the autoload settings for the current components classes and files.
- `autoload-dev` - configures dev autoload requirements. Currently *always* evalauted.
- `deps` - configures the required dependencies in an array (no key-value pairs with versions)
using the relative path to the project root directory as a package name.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -5,5 +5,6 @@
},
"autoload": {
"psr-0": {"Fiddler\\": "src/main"}
}
},
"bin": ["src/bin/fiddler"]
}
18 changes: 18 additions & 0 deletions src/bin/compile
@@ -0,0 +1,18 @@
#!/usr/bin/env php
<?php

require __DIR__.'/../../vendor/autoload.php';

use Fiddler\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);
}

225 changes: 225 additions & 0 deletions src/main/Fiddler/Compiler.php
@@ -0,0 +1,225 @@
<?php

/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Fiddler;

use Composer\Json\JsonFile;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;

/**
* The Compiler class compiles composer into a phar
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class Compiler
{
private $version;
private $branchAliasVersion = '';
private $versionDate;

/**
* Compiles composer into a single phar file
*
* @throws \RuntimeException
* @param string $pharFile The full path to the file to create
*/
public function compile($pharFile = 'fiddler.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 fiddler 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 fiddler 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 --exact-match HEAD');
if ($process->run() == 0) {
$this->version = trim($process->getOutput());
} else {
// get branch-alias defined in composer.json for dev-master (if any)
$localConfig = __DIR__.'/../../../composer.json';
$file = new JsonFile($localConfig);
$localConfig = $file->read();
if (isset($localConfig['extra']['branch-alias']['dev-master'])) {
$this->branchAliasVersion = $localConfig['extra']['branch-alias']['dev-master'];
}
}

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

$phar->startBuffering();

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

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

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

$finder = new Finder();
$finder->files()
->ignoreVCS(true)
->name('*.php')
->name('LICENSE')
->exclude('Tests')
->exclude('tests')
->exclude('docs')
->in(__DIR__.'/../../../vendor/composer/')
->in(__DIR__.'/../../../vendor/symfony/')
->in(__DIR__.'/../../../vendor/seld/jsonlint/')
->in(__DIR__.'/../../../vendor/justinrainbow/json-schema/')
;

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

$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'));
if (file_exists(__DIR__.'/../../../vendor/composer/include_paths.php')) {
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../../vendor/composer/include_paths.php'));
}
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../../vendor/composer/ClassLoader.php'));
$this->addComposerBin($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(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";
}

echo $path . "\n";
$phar->addFromString($path, $content);
}

private function addComposerBin($phar)
{
$content = file_get_contents(__DIR__.'/../../bin/fiddler');
$content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);
$phar->addFromString('src/bin/fiddler', $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 Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view
* the license that is located at the bottom of this file.
*/
// Avoid APC causing random fatal errors per https://github.com/composer/composer/issues/264
if (extension_loaded('apc') && ini_get('apc.enable_cli') && ini_get('apc.cache_by_default')) {
if (version_compare(phpversion('apc'), '3.0.12', '>=')) {
ini_set('apc.cache_by_default', 0);
} else {
fwrite(STDERR, 'Warning: APC <= 3.0.12 may cause fatal errors when running composer commands.'.PHP_EOL);
fwrite(STDERR, 'Update APC, or set apc.enable_cli or apc.cache_by_default to 0 in your php.ini.'.PHP_EOL);
}
}
Phar::mapPhar('fiddler.phar');
EOF;

return $stub . <<<'EOF'
require 'phar://fiddler.phar/src/bin/fiddler';
__HALT_COMPILER();
EOF;
}
}

0 comments on commit 6f302e0

Please sign in to comment.