Skip to content

Commit

Permalink
Add events on build [ #17 ]
Browse files Browse the repository at this point in the history
  • Loading branch information
MacFJA committed Mar 26, 2016
1 parent 06d0c87 commit e6ebea5
Show file tree
Hide file tree
Showing 13 changed files with 523 additions and 54 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,28 @@ Help:
│ "entry-point": "./index.php",
│ "include": ["bin","js","css"],
│ "include-dev": false
│ "events": {
│ "build.before" : "git describe --tags > bin/version.txt"
│ "build.after": [
│ "rm bin/version.txt",
│ "chmod +x ../application.phar"
│ ]
│ }
│ }
│ }
```

[More information about the Composer configuration](docs/ComposerExtra.md)

### Command `build`

The command `build` doesn't take any argument. All options will be ask through the CLI

## Important

The only way to add none source code and none vendors files is to use `include` option of the `package` command
- The only way to add none source code and none vendors files is to use `include` option of the `package` command.
- The only way to trigger script on build is to use `composer.json` configuration with `package` command.

## Similar projects

Expand Down
46 changes: 44 additions & 2 deletions app/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@

namespace MacFJA\PharBuilder;

use League\Event\Emitter;
use League\Event\EventInterface;
use MacFJA\PharBuilder\Commands\Build;
use MacFJA\PharBuilder\Commands\Package;
use MacFJA\PharBuilder\Event\ApplicationListener;
use MacFJA\Symfony\Console\Filechooser\FilechooserHelper;
use Symfony\Component\Console\Application as Base;
use Symfony\Component\Console\Exception\LogicException;
Expand All @@ -31,6 +34,28 @@
*/
class Application extends Base
{
/**
* The phar builder
*
* @var PharBuilder
*/
protected $builder;
/**
* The events emitter
*
* @var Emitter
*/
protected $emitter;

/**
* Get the application PHAR Builder
*
* @return PharBuilder
*/
public function getBuilder()
{
return $this->builder;
}
/**
* Init the application and create the main and default command
*
Expand All @@ -40,6 +65,8 @@ public function __construct()
{
parent::__construct('MacFJA PharBuilder', '@dev');

$this->emitter = new Emitter();

$this->getHelperSet()->set(new FilechooserHelper());

// Add the full interactive command
Expand All @@ -52,6 +79,20 @@ public function __construct()
* So without argument, instead of the help command, the build command will be launch
*/
$this->setDefaultCommand('build');

$this->emitter->addListener('*', new ApplicationListener());
}

/**
* Emit an event.
*
* @param EventInterface $event The event to send
*
* @return EventInterface
*/
public function emit($event)
{
$this->emitter->emit($event);
}

/**
Expand Down Expand Up @@ -90,9 +131,10 @@ protected function configureIO(InputInterface $input, OutputInterface $output)
{
parent::configureIO($input, $output);

$ioStyle = new SymfonyStyle($input, $output);
$ioStyle = new SymfonyStyle($input, $output);
$this->builder = new PharBuilder($ioStyle);
foreach ($this->all() as $command) {
if ($command instanceof Commands\Base) {
if (method_exists($command, 'setIo')) {
$command->setIo($ioStyle);
}
}
Expand Down
15 changes: 14 additions & 1 deletion app/Commands/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
$outputDir = $this->askOutputDir($input, $output, dirname($composerFile));

// -- Build the Phar
new PharBuilder($this->ioStyle, $composerFile, $outputDir, $name, $stubFile, $compression, array(), $keepDev);
/**
* The application phar builder
*
* @var PharBuilder $builder
*/
$builder = $this->getApplication()->getBuilder();
$builder->setComposer($composerFile);
$builder->setOutputDir($outputDir);
$builder->setPharName($name);
$builder->setStubFile($stubFile);
$builder->setCompression($compression);
$builder->setKeepDev($keepDev);

$builder->buildPhar();
}
}
43 changes: 27 additions & 16 deletions app/Commands/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

namespace MacFJA\PharBuilder\Commands;

use MacFJA\PharBuilder\PharBuilder;
use League\Event\Event;
use MacFJA\PharBuilder\Application;
use MacFJA\PharBuilder\Event\ComposerAwareEvent;
use MacFJA\PharBuilder\Event\PharAwareEvent;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
Expand Down Expand Up @@ -33,6 +36,7 @@ class Package extends Base
*/
protected function configure()
{
$resourcePath = __DIR__.DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'resource';
$this->setName('package')
->addArgument(
'composer',
Expand Down Expand Up @@ -66,21 +70,9 @@ protected function configure()
'List of directories to add in Phar'
)
->setHelp(
file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'package-help.txt') .
file_get_contents($resourcePath . DIRECTORY_SEPARATOR .'package-help.txt') .
$this->codeHelpParagraph(
<<< CODE
<info>... The content of your composer.json file</info>
"extra": {
"phar-builder": {
"compression": "GZip",
"name": "application.phar",
"output-dir": "../",
"entry-point": "./index.php",
"include": ["bin","js","css"],
"include-dev": false
}
}
CODE
file_get_contents($resourcePath . DIRECTORY_SEPARATOR .'package-extra-example.txt')

This comment has been minimized.

Copy link
@kelunik

kelunik Mar 26, 2016

Contributor

Do you know that you can just use / and PHP will automatically normalize it?

This comment has been minimized.

Copy link
@MacFJA

MacFJA Mar 27, 2016

Author Owner

In all the code I check, the DIRECTORY_SEPARATOR constant is mostly use to build path.

But you right, Windows (and also PHP) can shameless use / or \ (even mix the usage) for path.


In fact PHP can convert Unix path into Windows path (with \) with the function realpath

This comment has been minimized.

Copy link
@kelunik

kelunik Mar 27, 2016

Contributor

I don't use realpath or DIRECTORY_SEPARATOR in acme-client and it works all fine on Windows.

)
)
->setDescription('Create a Phar file from a composer.json');
Expand Down Expand Up @@ -137,7 +129,26 @@ protected function execute(InputInterface $input, OutputInterface $output)
$outputDir = $this->readParamComposerAsk($extraData, $input, $output, $baseDir, 'output-dir');
$includes = $this->readParamComposerAsk($extraData, $input, $output, $baseDir, 'include');

new PharBuilder($this->ioStyle, $composerFile, $outputDir, $name, $stubFile, $compression, $includes, $keepDev);
/**
* The application
*
* @var Application $app
*/
$app = $this->getApplication();
$builder = $app->getBuilder();
$builder->setComposer($composerFile);
$builder->setOutputDir($outputDir);
$builder->setPharName($name);
$builder->setStubFile($stubFile);
$builder->setCompression($compression);
$builder->setIncludes($includes);
$builder->setKeepDev($keepDev);

$app->emit(new PharAwareEvent('build.before', $builder));

$builder->buildPhar();

$app->emit(new ComposerAwareEvent('build.after', $builder->getComposerReader()));
}

/**
Expand Down
47 changes: 47 additions & 0 deletions app/Event/ApplicationListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace MacFJA\PharBuilder\Event;

use League\Event\AbstractListener;
use League\Event\EventInterface;
use Symfony\Component\Process\Process;

/**
* Class ApplicationListener.
*
* @package Event
* @author MacFJA
* @license MIT
*/
class ApplicationListener extends AbstractListener
{

/**
* Handle an event.
*
* @param EventInterface $event The event to handle
*
* @return void
*/
public function handle(EventInterface $event)
{
if (!($event instanceof ComposerAwareEvent)) {
return;
}
$data = $event->getComposerReader()->getComposerConfig();

if (!array_key_exists('events', $data) || !array_key_exists($event->getName(), $data['events'])) {
return;
}

$actions = $data['events'][$event->getName()];

This comment has been minimized.

Copy link
@kelunik

kelunik Mar 27, 2016

Contributor

Do you know that you can simply use an (array) cast here and remove the following three lines then?

if (!is_array($actions)) {
$actions = array($actions);
}

foreach ($actions as $action) {
$process = new Process($action, dirname($event->getComposerReader()->getComposerJsonPath()));
$process->run();
}
}
}
57 changes: 57 additions & 0 deletions app/Event/ComposerAwareEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace MacFJA\PharBuilder\Event;

use League\Event\Event;
use MacFJA\PharBuilder\Utils\Composer;

/**
* Class ComposerAwareEvent.
*
* @package MacFJA\PharBuilder\Event
* @author MacFJA
* @license MIT
*/
class ComposerAwareEvent extends Event
{
/**
* The phar builder
*
* @var Composer
*/
protected $composerReader;
/**
* Create a new event instance.
*
* @param string $name The new event name
* @param Composer $composer The PharBuilder
*/
public function __construct($name, $composer)
{
$this->composerReader = $composer;
parent::__construct($name);
}

/**
* Get the composer reader object
*
* @return Composer
*/
public function getComposerReader()
{
return $this->composerReader;
}

/**
* Create a new event instance.
*
* @param string $name The new event name
* @param Composer $composer The PharBuilder
*
* @return static
*/
public static function named($name, $composer)
{
return new static($name, $composer);
}
}
56 changes: 56 additions & 0 deletions app/Event/PharAwareEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace MacFJA\PharBuilder\Event;

use MacFJA\PharBuilder\PharBuilder;

/**
* Class PharAwareEvent.
*
* @package MacFJA\PharBuilder\Event
* @author MacFJA
* @license MIT
*/
class PharAwareEvent extends ComposerAwareEvent
{
/**
* The phar builder
*
* @var PharBuilder
*/
protected $pharContext;
/**
* Create a new event instance.
*
* @param string $name The new event name
* @param PharBuilder $builder The PharBuilder
*/
public function __construct($name, $builder)
{
$this->pharContext = $builder;
parent::__construct($name, $builder->getComposerReader());
}

/**
* Get the PHAR builder
*
* @return PharBuilder
*/
public function getPhar()
{
return $this->pharContext;
}

/**
* Create a new event instance.
*
* @param string $name The new event name
* @param PharBuilder $builder The PharBuilder
*
* @return static
*/
public static function named($name, $builder)
{
return new static($name, $builder);
}
}
Loading

0 comments on commit e6ebea5

Please sign in to comment.