Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
adding composer-scripts configurator (#69)
Browse files Browse the repository at this point in the history
| Q               | A
| --------------- | ---
| Bug fix?        | no
| New feature?    | yes
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | -
| License         | MIT
| Doc PR          | -
  • Loading branch information
prisis authored Sep 11, 2018
1 parent ec4c895 commit 39ddeb9
Show file tree
Hide file tree
Showing 15 changed files with 665 additions and 86 deletions.
26 changes: 21 additions & 5 deletions doc/CONFIGURATORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Configurators define the different tasks executed when installing a dependency,

The package only contain the tasks needed to install and configure the dependency, because Narrowspark Automatic Configurators are smart enough to reverse those tasks when uninstalling and unconfiguring the dependencies.

Narrowspark Automatic comes with several types of tasks, which are called **configurators**: `copy`, `env`, `composer-scripts`, `gitignore` and a special configurator `post-install-output`.
Narrowspark Automatic comes with several types of tasks, which are called **configurators**: `copy`, `env`, `composer-scripts`, `composer-auto-scripts`, `gitignore` and a special configurator `post-install-output`.

### Copy Configurator `copy`

Expand All @@ -30,7 +30,7 @@ directory of the application:

The `%BIN_DIR%` string is a special value that it's turned into the absolute
path of the binaries directory. You can access any variable defined in
the `extra` section of your `composer.json` file:
the `extra` section of your root `composer.json` file:

```json
{
Expand Down Expand Up @@ -72,22 +72,38 @@ The `###> your-package-name-here ###` section separators are needed by Narrowspa
to detect the contents added by this dependency in case you uninstall it later.
> !!! Don't remove or modify these separators.
Composer Scripts Configurator `composer-scripts`
Composer Auto Scripts Configurator `composer-auto-scripts`

Registers scripts in the `auto-scripts` section of the `composer.json` file
Registers `auto-scripts` in the `composer-scripts` section of the root `composer.json` file
to execute them automatically when running `composer install` and `composer update`.
The value is an associative array where the key is the script to execute (including all its arguments and options) and the value is the type of script (`php-script` for PHP scripts, ``script`` for any shell script):

```json
{
"configurators": {
"composer-scripts": {
"composer-auto-scripts": {
"echo \"hallo\";": "php-script",
"bash -c \"echo hallo\"": "script"
}
}
}
```
Composer Scripts Configurator `composer-scripts`

Registers [composer scripts](https://getcomposer.org/doc/articles/scripts.md) in the `composer-scripts` section of the root `composer.json` file.
Only the composer `command`, `installer` and `package` events are supported, you will get a warning if other events a used.

```json
{
"configurators": {
"composer-auto-scripts": {
"post-autoload-dump" : [
"Your\\Namespace\\ComposerScripts::dump"
]
}
}
}
```

You can create your own script executor, create a new class inside your Package Repository in a `Automatic` folder and extend `Narrowspark\Automatic\Common\ScriptExtender\AbstractScriptExtender` the example below shows you how it should look:

Expand Down
14 changes: 7 additions & 7 deletions src/Automatic/Automatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,13 @@ public static function getSubscribedEvents(): array
InstallerEvents::POST_DEPENDENCIES_SOLVING => [['populateFilesCacheDir', \PHP_INT_MAX]],
PackageEvents::PRE_PACKAGE_INSTALL => [['populateFilesCacheDir', ~\PHP_INT_MAX]],
PackageEvents::PRE_PACKAGE_UPDATE => [['populateFilesCacheDir', ~\PHP_INT_MAX]],
PackageEvents::POST_PACKAGE_INSTALL => [['record'], ['auditPackage']],
PackageEvents::POST_PACKAGE_UPDATE => [['record'], ['auditPackage']],
PackageEvents::POST_PACKAGE_INSTALL => [['record'], ['auditPackage', \PHP_INT_MAX]],
PackageEvents::POST_PACKAGE_UPDATE => [['record'], ['auditPackage', \PHP_INT_MAX]],
PackageEvents::POST_PACKAGE_UNINSTALL => 'record',
PluginEvents::PRE_FILE_DOWNLOAD => 'onFileDownload',
ScriptEvents::POST_INSTALL_CMD => [['onPostInstall'], ['auditComposerLock']],
ScriptEvents::POST_UPDATE_CMD => [['onPostUpdate'], ['auditComposerLock']],
ScriptEvents::POST_CREATE_PROJECT_CMD => [['onPostCreateProject', \PHP_INT_MAX], ['runSkeletonGenerator']],
ScriptEvents::POST_INSTALL_CMD => [['onPostInstall', \PHP_INT_MAX - 1], ['auditComposerLock', \PHP_INT_MAX]],
ScriptEvents::POST_UPDATE_CMD => [['onPostUpdate', \PHP_INT_MAX - 1], ['auditComposerLock', \PHP_INT_MAX]],
ScriptEvents::POST_CREATE_PROJECT_CMD => [['onPostCreateProject', \PHP_INT_MAX], ['runSkeletonGenerator', ~\PHP_INT_MAX]],
];
}

Expand Down Expand Up @@ -1121,13 +1121,13 @@ private function showWarningOnRemainingConfigurators(
): void {
$packageConfigurators = \array_keys((array) $package->getConfig(ConfiguratorContract::TYPE));

foreach (\array_keys($configurator->getConfigurators()) as $key) {
foreach (\array_keys($configurator->getConfigurators()) as $key => $value) {
if (isset($packageConfigurators[$key])) {
unset($packageConfigurators[$key]);
}
}

foreach (\array_keys($packageConfigurator->getConfigurators()) as $key) {
foreach (\array_keys($packageConfigurator->getConfigurators()) as $key => $value) {
if (isset($packageConfigurators[$key])) {
unset($packageConfigurators[$key]);
}
Expand Down
10 changes: 6 additions & 4 deletions src/Automatic/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Narrowspark\Automatic;

use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Configurator\ComposerAutoScriptsConfigurator;
use Narrowspark\Automatic\Configurator\ComposerScriptsConfigurator;
use Narrowspark\Automatic\Configurator\CopyFromPackageConfigurator;
use Narrowspark\Automatic\Configurator\EnvConfigurator;
Expand All @@ -16,10 +17,11 @@ final class Configurator extends AbstractConfigurator
* @var array
*/
protected $configurators = [
'composer-scripts' => ComposerScriptsConfigurator::class,
'copy' => CopyFromPackageConfigurator::class,
'env' => EnvConfigurator::class,
'gitignore' => GitIgnoreConfigurator::class,
'composer-auto-scripts' => ComposerAutoScriptsConfigurator::class,
'composer-scripts' => ComposerScriptsConfigurator::class,
'copy' => CopyFromPackageConfigurator::class,
'env' => EnvConfigurator::class,
'gitignore' => GitIgnoreConfigurator::class,
];

/**
Expand Down
100 changes: 100 additions & 0 deletions src/Automatic/Configurator/ComposerAutoScriptsConfigurator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
namespace Narrowspark\Automatic\Configurator;

use Composer\Composer;
use Composer\IO\IOInterface;
use Narrowspark\Automatic\Common\Configurator\AbstractConfigurator;
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;
use Narrowspark\Automatic\Common\Util;

final class ComposerAutoScriptsConfigurator extends AbstractConfigurator
{
/**
* A json instance.
*
* @var \Composer\Json\JsonFile
*/
private $json;

/**
* A json manipulator instance.
*
* @var \Composer\Json\JsonManipulator
*/
private $manipulator;

/**
* {@inheritdoc}
*/
public function __construct(Composer $composer, IOInterface $io, array $options = [])
{
parent::__construct($composer, $io, $options);

[$json, $manipulator] = Util::getComposerJsonFileAndManipulator();

$this->json = $json;
$this->manipulator = $manipulator;
}

/**
* {@inheritdoc}
*/
public static function getName(): string
{
return 'composer-auto-scripts';
}

/**
* {@inheritdoc}
*/
public function configure(PackageContract $package): void
{
$autoScripts = $this->getComposerAutoScripts();

$autoScripts = \array_merge($autoScripts, (array) $package->getConfig(ConfiguratorContract::TYPE, self::getName()));

$this->manipulateAndWrite($autoScripts);
}

/**
* {@inheritdoc}
*/
public function unconfigure(PackageContract $package): void
{
$autoScripts = $this->getComposerAutoScripts();

foreach (\array_keys((array) $package->getConfig(ConfiguratorContract::TYPE, self::getName())) as $cmd) {
unset($autoScripts[$cmd]);
}

$this->manipulateAndWrite($autoScripts);
}

/**
* Get root composer.json content and the auto-scripts section.
*
* @return array
*/
private function getComposerAutoScripts(): array
{
$jsonContents = $this->json->read();

return $jsonContents['scripts']['auto-scripts'] ?? [];
}

/**
* Manipulate the root composer.json with given auto-scripts.
*
* @param array $autoScripts
*
* @return void
*/
private function manipulateAndWrite(array $autoScripts): void
{
$this->manipulator->addSubNode('scripts', 'auto-scripts', $autoScripts);

\file_put_contents($this->json->getPath(), $this->manipulator->getContents());
}
}
Loading

0 comments on commit 39ddeb9

Please sign in to comment.