Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to force extracting archives #16

Open
wants to merge 5 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ This file is a manually maintained list of changes for each release. Feel free
to add your changes here when sending pull requests. Also send corrections if
you spot any mistakes.

## 0.5.0 (xxxx-xx-xx)

* Feature: Added support to force extracting phar archives. This options is
disabled by default for performance reasons, to enable use the config key
`extra.phar.force-extract` in the `composer.json` file:

```json
"extra": {
"phar": {
"force-extract": true
}
}
```

Or use `--force-extract` option in the command line:

```bash
$ phar-composer build --force-extract https://github.com/clue/phar-composer.git
```

CLI option will override the value in the config key.

## 0.4.0 (2013-09-12)

* Feature: New `install` command will now both build the given package and then
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ Again, you can specify either a tag or branch name very similar to how composer
$ phar-composer build https://github.com/composer/composer.git:dev-master
```

Some packages are very difficult to bundle as a phar, most often because they
work with their paths in a way that does not work within phars (most notably
using realpath() etc.). In this case it is possible to enforce extracting the
executable phar archive to a temporary directory. This option is disabled by
default for performance reasons, to enable use `--force-extract` option e.g.:

```bash
$ phar-composer build --force-extract https://github.com/clue/phar-composer.git
```

### phar-composer install

The `install` command will both build the given package and then
Expand Down
7 changes: 6 additions & 1 deletion src/Clue/PharComposer/Command/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ protected function configure()
$this->setName('build')
->setDescription('Build phar for the given composer project')
->addArgument('path', InputArgument::OPTIONAL, 'Path to project directory or composer.json', '.')
->addArgument('target', InputArgument::OPTIONAL, 'Path to write phar output to (defaults to project name)');
->addArgument('target', InputArgument::OPTIONAL, 'Path to write phar output to (defaults to project name)')
->addOption('force-extract', 'x', InputOption::VALUE_NONE, 'Enforce extracting the phar to a temporary directory.');
}

protected function execute(InputInterface $input, OutputInterface $output)
Expand All @@ -43,6 +44,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$pharer->setTarget($target);
}

if ($input->getOption('force-extract')) {
$pharer->setForceExtract(true);
}

$pharer->build();
}
}
9 changes: 9 additions & 0 deletions src/Clue/PharComposer/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,13 @@ public function getAbsolutePath($path)
{
return $this->directory . ltrim($path, '/');
}

public function getForceExtract()
{
$forceExtract = false;
if (!empty($this->package['extra']['phar']['force-extract'])) {
$forceExtract = true;
}
return $forceExtract;
}
}
21 changes: 20 additions & 1 deletion src/Clue/PharComposer/PharComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PharComposer
private $target = null;
private $output = true;
private $step = '?';
private $forceExtract = null;

public function __construct($path)
{
Expand Down Expand Up @@ -83,6 +84,20 @@ public function setMain($main)
return $this;
}

public function getForceExtract()
{
if ($this->forceExtract === null) {
$this->forceExtract = $this->getPackageRoot()->getForceExtract();
}
return (Boolean) $this->forceExtract;
}

public function setForceExtract($forceExtract)
{
$this->forceExtract = $forceExtract;
return $this;
}

/**
* base project path. all files MUST BE relative to this location
*
Expand Down Expand Up @@ -186,9 +201,13 @@ public function build()
if ($main === null) {
$this->log(' WARNING: No main bin file defined! Resulting phar will NOT be executable');
} else {
$forceExtract = $this->getForceExtract();
if ($forceExtract) {
$this->log(' Forcing the use of the Extract class');
}
$generator = StubGenerator::create()
->index($this->getPathLocalToBase($main))
->extract(true)
->extract(true, $forceExtract)
->banner("Bundled by phar-composer with the help of php-box.\n\n@link https://github.com/clue/phar-composer");

$lines = file($main, FILE_IGNORE_NEW_LINES);
Expand Down
14 changes: 14 additions & 0 deletions tests/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public function testConstructorDefaults()
$this->assertEquals('dir/', $package->getDirectory());
$this->assertEquals('unknown', $package->getName());
$this->assertEquals('dir/vendor/', $package->getPathVendor());
$this->assertFalse($package->getForceExtract());
}

public function testConstructorData()
Expand Down Expand Up @@ -59,4 +60,17 @@ public function testConstructorBundlerInvalid()

$package->getBundler();
}

public function testConstructorForceExtractConfig()
{
$package = new Package(array(
'extra' => array(
'phar' => array(
'force-extract' => true
)
)
), 'dir/');

$this->assertTrue($package->getForceExtract());
}
}
5 changes: 5 additions & 0 deletions tests/PharComposerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public function testConstructor()
$this->assertEquals($this->getPathProjectAbsolute('vendor') . '/', $pharcomposer->getPathVendor());
$this->assertEquals('phar-composer.phar', $pharcomposer->getTarget());

$this->assertFalse($pharcomposer->getForceExtract());

return $pharcomposer;
}

Expand All @@ -32,6 +34,9 @@ public function testSetters(PharComposer $pharcomposer)
$pharcomposer->setTarget('test.phar');
$this->assertEquals('test.phar', $pharcomposer->getTarget());

$pharcomposer->setForceExtract(true);
$this->assertTrue($pharcomposer->getForceExtract());

return $pharcomposer;
}

Expand Down