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

Commit

Permalink
fixes #52 (#54)
Browse files Browse the repository at this point in the history
| Q               | A
| --------------- | ---
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | yes
| Deprecations?   | no
| Related tickets | fixes #52 #55
| License         | MIT
| Doc PR          | -
  • Loading branch information
prisis authored Aug 20, 2018
1 parent bcbddab commit 9e2229c
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 55 deletions.
14 changes: 12 additions & 2 deletions src/Automatic/AbstractConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function has(string $name): bool
public function configure(PackageContract $package): void
{
foreach (\array_keys($this->configurators) as $key) {
if ($package->hasConfig($key)) {
if ($package->hasConfig(ConfiguratorContract::TYPE, $key)) {
$this->get($key)->configure($package);
}
}
Expand All @@ -113,12 +113,22 @@ public function configure(PackageContract $package): void
public function unconfigure(PackageContract $package): void
{
foreach (\array_keys($this->configurators) as $key) {
if ($package->hasConfig($key)) {
if ($package->hasConfig(ConfiguratorContract::TYPE, $key)) {
$this->get($key)->unconfigure($package);
}
}
}

/**
* Get all registered configurators.
*
* @return array
*/
public function getConfigurators(): array
{
return $this->configurators;
}

/**
* Clear all configurators.
*
Expand Down
65 changes: 54 additions & 11 deletions src/Automatic/Automatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use FilesystemIterator;
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Common\Contract\Exception\InvalidArgumentException;
use Narrowspark\Automatic\Common\Contract\Exception\RuntimeException;
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;
Expand Down Expand Up @@ -369,8 +370,8 @@ public function onPostUpdate(Event $event, array $operations = []): void
'',
'<info>Some files may have been created or updated to configure your new packages.</info>',
'<comment>The automatic.lock file has all information about the installed packages.</comment>',
'Please <comment>review</comment>, <comment>edit</comment> and <comment>commit</comment> them: these files are <comment>yours</comment>.',
"\nTo show the package suggests run <comment>composer suggests</comment>."
'Please <comment>review</comment>, <comment>edit</comment> and <comment>commit</comment> them: these files are <comment>yours</comment>',
"\nTo show the package suggests run <comment>composer suggests</comment>"
);
}

Expand Down Expand Up @@ -410,7 +411,7 @@ public function executeAutoScripts(Event $event): void
$scriptExecutor->execute($type, $cmd);
}
} else {
$this->container->get(IOInterface::class)->write('No auto-scripts section was found under scripts.', true, IOInterface::VERBOSE);
$this->container->get(IOInterface::class)->write('No auto-scripts section was found under scripts', true, IOInterface::VERBOSE);
}
}

Expand Down Expand Up @@ -680,10 +681,14 @@ private function doInstall(PackageContract $package, PackageConfigurator $packag

$this->writeScriptExtenderToLock($package, $lock);

$this->container->get(Configurator::class)->configure($package);
/** @var \Narrowspark\Automatic\Configurator $configurator */
$configurator = $this->container->get(Configurator::class);

$configurator->configure($package);
$packageConfigurator->configure($package);

$this->showWarningOnRemainingConfigurators($package, $packageConfigurator, $configurator);

if ($package->hasConfig('post-install-output')) {
foreach ((array) $package->getConfig('post-install-output') as $line) {
$this->postInstallOutput[] = self::expandTargetDir($this->container->get('composer-extra'), $line);
Expand Down Expand Up @@ -713,10 +718,14 @@ private function doInstall(PackageContract $package, PackageConfigurator $packag
*/
private function doUninstall(PackageContract $package, PackageConfigurator $packageConfigurator): void
{
$this->container->get(Configurator::class)->unconfigure($package);
/** @var \Narrowspark\Automatic\Configurator $configurator */
$configurator = $this->container->get(Configurator::class);

$configurator->unconfigure($package);
$packageConfigurator->unconfigure($package);

$this->showWarningOnRemainingConfigurators($package, $packageConfigurator, $configurator);

/** @var \Narrowspark\Automatic\Lock $lock */
$lock = $this->container->get(Lock::class);

Expand Down Expand Up @@ -812,17 +821,17 @@ private function getErrorMessage(IOInterface $io): ?string
{
// @codeCoverageIgnoreStart
if (! \extension_loaded('openssl')) {
return 'You must enable the openssl extension in your "php.ini" file.';
return 'You must enable the openssl extension in your "php.ini" file';
}

if (\version_compare(self::getComposerVersion(), '1.6.0', '<')) {
return \sprintf('Your version "%s" of Composer is too old; Please upgrade.', Composer::VERSION);
return \sprintf('Your version "%s" of Composer is too old; Please upgrade', Composer::VERSION);
}
// @codeCoverageIgnoreEnd

// skip on no interactive mode
if (! $io->isInteractive()) {
return 'Composer running in a no interaction mode.';
return 'Composer running in a no interaction mode';
}

return null;
Expand All @@ -849,7 +858,7 @@ private static function getComposerVersion(): string
return $matches[0];
}

throw new RuntimeException('No composer version found.');
throw new RuntimeException('No composer version found');
}

/**
Expand All @@ -875,8 +884,8 @@ private function configureLegacyTagsManager(IOInterface $io, LegacyTagsManager $
}

$this->addLegacyTags($io, $requires, $tagsManager);
} elseif (isset($extra['require'])) {
$this->addLegacyTags($io, $extra['require'], $tagsManager);
} elseif (isset($extra[Util::COMPOSER_EXTRA_KEY]['require'])) {
$this->addLegacyTags($io, $extra[Util::COMPOSER_EXTRA_KEY]['require'], $tagsManager);
}
}

Expand Down Expand Up @@ -983,4 +992,38 @@ private function extendRepositoryManager(Composer $composer, IOInterface $io, Le

return $manager;
}

/**
* Show a waring if remaining configurators are found in package config.
*
* @param \Narrowspark\Automatic\Common\Contract\Package $package
* @param \Narrowspark\Automatic\PackageConfigurator $packageConfigurator
* @param \Narrowspark\Automatic\Configurator $configurator
*
* @return void
*/
private function showWarningOnRemainingConfigurators(PackageContract $package, PackageConfigurator $packageConfigurator, Configurator $configurator): void
{
$packageConfigurators = \array_keys((array) $package->getConfig(ConfiguratorContract::TYPE));

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

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

if (\count($packageConfigurators) !== 0) {
$this->container->get(IOInterface::class)->writeError(\sprintf(
'<warning>No configurators were run for [%s] in [%s]</warning>',
\implode(', ', $packageConfigurators),
$package->getPrettyName()
));
}
}
}
5 changes: 3 additions & 2 deletions src/Automatic/Configurator/CopyFromPackageConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Narrowspark\Automatic\Configurator;

use Narrowspark\Automatic\Common\Configurator\AbstractConfigurator;
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;
use Symfony\Component\Filesystem\Exception\IOException;

Expand All @@ -23,7 +24,7 @@ public function configure(PackageContract $package): void
{
$this->write('Copying files');

foreach ((array) $package->getConfig(self::getName()) as $from => $to) {
foreach ((array) $package->getConfig(ConfiguratorContract::TYPE, self::getName()) as $from => $to) {
$target = self::expandTargetDir($this->options, $to);

try {
Expand All @@ -50,7 +51,7 @@ public function unconfigure(PackageContract $package): void
{
$this->write('Removing files');

foreach ((array) $package->getConfig(self::getName()) as $source) {
foreach ((array) $package->getConfig(ConfiguratorContract::TYPE, self::getName()) as $source) {
$source = self::expandTargetDir($this->options, $source);

try {
Expand Down
3 changes: 2 additions & 1 deletion src/Automatic/Configurator/EnvConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Narrowspark\Automatic\Configurator;

use Narrowspark\Automatic\Common\Configurator\AbstractConfigurator;
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;

final class EnvConfigurator extends AbstractConfigurator
Expand Down Expand Up @@ -30,7 +31,7 @@ public function configure(PackageContract $package): void

$data = '';

foreach ((array) $package->getConfig(self::getName()) as $key => $value) {
foreach ((array) $package->getConfig(ConfiguratorContract::TYPE, self::getName()) as $key => $value) {
if ($key[0] === '#' && \is_numeric(\mb_substr($key, 1))) {
$data .= '# ' . $value . "\n";

Expand Down
7 changes: 4 additions & 3 deletions src/Automatic/Configurator/GitIgnoreConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Narrowspark\Automatic\Configurator;

use Narrowspark\Automatic\Common\Configurator\AbstractConfigurator;
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Common\Contract\Package as PackageContract;

final class GitIgnoreConfigurator extends AbstractConfigurator
Expand All @@ -20,7 +21,7 @@ public static function getName(): string
*/
public function configure(PackageContract $package): void
{
$this->write('Added entries to .gitignore.');
$this->write('Added entries to .gitignore');

$gitignore = $this->path->getWorkingDir() . \DIRECTORY_SEPARATOR . '.gitignore';

Expand All @@ -30,7 +31,7 @@ public function configure(PackageContract $package): void

$data = '';

foreach ((array) $package->getConfig(self::getName()) as $value) {
foreach ((array) $package->getConfig(ConfiguratorContract::TYPE, self::getName()) as $value) {
$value = self::expandTargetDir($this->options, $value);
$data .= "${value}\n";
}
Expand Down Expand Up @@ -63,7 +64,7 @@ public function unconfigure(PackageContract $package): void
return;
}

$this->write('Removed entries in .gitignore.');
$this->write('Removed entries in .gitignore');

\file_put_contents($file, \ltrim($contents, "\r\n"));
}
Expand Down
5 changes: 5 additions & 0 deletions src/Common/Contract/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

interface Configurator
{
/**
* @var string
*/
public const TYPE = 'configurators';

/**
* Return the configurator key name.
*
Expand Down
10 changes: 6 additions & 4 deletions src/Common/Contract/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,22 @@ public function setConfig(array $configs): self;
/**
* Checks if key exits in extra automatic config.
*
* @param string $key
* @param string $mainKey
* @param null|string $name
*
* @return bool
*/
public function hasConfig(string $key): bool;
public function hasConfig(string $mainKey, ?string $name = null): bool;

/**
* Get a automatic config value.
*
* @param string $key
* @param string $mainKey
* @param null|string $name
*
* @return null|array|string
*/
public function getConfig(string $key);
public function getConfig(string $mainKey, ?string $name = null);

/**
* Returns the automatic package configs.
Expand Down
28 changes: 24 additions & 4 deletions src/Common/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,17 +286,37 @@ public function setConfig(array $configs): PackageContract
/**
* {@inheritdoc}
*/
public function hasConfig(string $key): bool
public function hasConfig(string $mainKey, ?string $name = null): bool
{
return \array_key_exists($key, $this->configs);
$mainCheck = \array_key_exists($mainKey, $this->configs);

if ($name === null) {
return $mainCheck;
}

if ($mainCheck === true && \is_array($this->configs[$mainKey])) {
return \array_key_exists($name, $this->configs[$mainKey]);
}

return false;
}

/**
* {@inheritdoc}
*/
public function getConfig(string $key)
public function getConfig(string $mainKey, ?string $name = null)
{
return $this->configs[$key] ?? null;
if (\array_key_exists($mainKey, $this->configs)) {
if ($name === null) {
return $this->configs[$mainKey];
}

if (\is_array($this->configs[$mainKey]) && \array_key_exists($name, $this->configs[$mainKey])) {
return $this->configs[$mainKey][$name];
}
}

return null;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Common/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
declare(strict_types=1);
namespace Narrowspark\Automatic\Common;

/**
* @internal
*/
final class Path
{
/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Automatic/AutomaticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function testActivateWithNoInteractive(): void
->andReturn(false);
$this->ioMock->shouldReceive('writeError')
->once()
->with('<warning>Narrowspark Automatic has been disabled. Composer running in a no interaction mode.</warning>');
->with('<warning>Narrowspark Automatic has been disabled. Composer running in a no interaction mode</warning>');

$this->automatic->activate($this->composerMock, $this->ioMock);
}
Expand Down Expand Up @@ -393,7 +393,7 @@ public function testExecuteAutoScriptsWithoutScripts(): void

$this->ioMock->shouldReceive('write')
->once()
->with('No auto-scripts section was found under scripts.', true, IOInterface::VERBOSE);
->with('No auto-scripts section was found under scripts', true, IOInterface::VERBOSE);

$containerMock = $this->mock(ContainerContract::class);
$containerMock->shouldReceive('get')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Composer\Composer;
use Composer\IO\IOInterface;
use Narrowspark\Automatic\Common\Contract\Configurator as ConfiguratorContract;
use Narrowspark\Automatic\Common\Package;
use Narrowspark\Automatic\Configurator\CopyFromPackageConfigurator;
use Narrowspark\TestingHelper\Phpunit\MockeryTestCase;
Expand Down Expand Up @@ -261,7 +262,7 @@ private function arrangePackageWithConfig(string $from, string $to): Package
->andReturn(__DIR__);

$package = new Package('Stub/stub', '1.0.0');
$package->setConfig(['copy' => [$from => $to]]);
$package->setConfig([ConfiguratorContract::TYPE => ['copy' => [$from => $to]]]);

return $package;
}
Expand Down
Loading

0 comments on commit 9e2229c

Please sign in to comment.