This repository has been archived by the owner on Mar 1, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding composer-scripts configurator (#69)
| Q | A | --------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Related tickets | - | License | MIT | Doc PR | -
- Loading branch information
Showing
15 changed files
with
665 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/Automatic/Configurator/ComposerAutoScriptsConfigurator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.