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

removed post-messages from root composer.json #84

Merged
merged 2 commits into from
Sep 28, 2018
Merged
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
112 changes: 64 additions & 48 deletions src/Automatic/Automatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,20 +155,24 @@ public static function getSubscribedEvents(): array

return [
ScriptEvents::AUTO_SCRIPTS => 'executeAutoScripts',
ScriptEvents::POST_MESSAGES => 'postMessages',
InstallerEvents::PRE_DEPENDENCIES_SOLVING => [['onPreDependenciesSolving', \PHP_INT_MAX]],
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::PRE_PACKAGE_UNINSTALL => [['onPreUninstall', \PHP_INT_MAX - 1]],
PackageEvents::PRE_PACKAGE_UNINSTALL => 'onPreUninstall',
PackageEvents::POST_PACKAGE_INSTALL => 'record',
PackageEvents::POST_PACKAGE_UPDATE => 'record',
PackageEvents::POST_PACKAGE_UNINSTALL => [['onPostUninstall', \PHP_INT_MAX - 1]],
PackageEvents::POST_PACKAGE_UNINSTALL => 'onPostUninstall',
PluginEvents::PRE_FILE_DOWNLOAD => 'onFileDownload',
PluginEvents::INIT => 'initAutoScripts',
ComposerScriptEvents::POST_AUTOLOAD_DUMP => 'onPostAutoloadDump',
ComposerScriptEvents::POST_INSTALL_CMD => [['onPostInstall', \PHP_INT_MAX - 1]],
ComposerScriptEvents::POST_UPDATE_CMD => [['onPostUpdate', \PHP_INT_MAX - 1]],
ComposerScriptEvents::POST_CREATE_PROJECT_CMD => [['onPostCreateProject', \PHP_INT_MAX], ['runSkeletonGenerator', ~\PHP_INT_MAX]],
ComposerScriptEvents::POST_INSTALL_CMD => 'onPostInstall',
ComposerScriptEvents::POST_UPDATE_CMD => [['onPostUpdate', \PHP_INT_MAX], ['onPostUpdatePostMessages', ~\PHP_INT_MAX + 1]],
ComposerScriptEvents::POST_CREATE_PROJECT_CMD => [
['onPostCreateProject', \PHP_INT_MAX],
['runSkeletonGenerator', \PHP_INT_MAX - 1],
['initAutoScripts', \PHP_INT_MAX - 2],
],
];
}

Expand Down Expand Up @@ -225,16 +229,14 @@ public function activate(Composer $composer, IOInterface $io): void
}

/**
* Executes on composer post-messages event.
* Executes on composer post-update event.
*
* @param \Composer\Script\Event $event
*
* @return void
*/
public function postMessages(Event $event): void
public function onPostUpdatePostMessages(Event $event): void
{
$event->stopPropagation();

$this->container->get(IOInterface::class)->write($this->postMessages);
}

Expand Down Expand Up @@ -272,53 +274,42 @@ public function record(PackageEvent $event): void
}

/**
* Executes on composer create project event.
*
* @param \Composer\Script\Event $event
* Add auto-scripts to root composer.json.
*
* @throws \Exception
*
* @return void
*/
public function onPostCreateProject(Event $event): void
public function initAutoScripts(): void
{
/** @var \Composer\Json\JsonFile $json */
/** @var \Composer\Json\JsonManipulator $manipulator */
[$json, $manipulator] = Util::getComposerJsonFileAndManipulator();

// new projects are most of the time proprietary
$manipulator->addMainKey('license', 'proprietary');
$scripts = $this->container->get(Composer::class)->getPackage()->getScripts();

// 'name' and 'description' are only required for public packages
$manipulator->removeProperty('name');
$manipulator->removeProperty('description');
$autoScript = '@' . ScriptEvents::AUTO_SCRIPTS;

foreach ($this->container->get('composer-extra') as $key => $value) {
if ($key !== self::COMPOSER_EXTRA_KEY) {
$manipulator->addSubNode('extra', $key, $value);
}
if (isset($scripts[ComposerScriptEvents::POST_INSTALL_CMD], $scripts[ComposerScriptEvents::POST_UPDATE_CMD]) &&
\in_array($autoScript, $scripts[ComposerScriptEvents::POST_INSTALL_CMD], true) &&
\in_array($autoScript, $scripts[ComposerScriptEvents::POST_UPDATE_CMD], true)
) {
return;
}

$scripts = $this->container->get(Composer::class)->getPackage()->getScripts();
/** @var \Composer\Json\JsonFile $json */
/** @var \Composer\Json\JsonManipulator $manipulator */
[$json, $manipulator] = Util::getComposerJsonFileAndManipulator();

if (\count($scripts) === 0) {
$manipulator->addMainKey('scripts', []);
}

$manipulator->addSubNode('scripts', ScriptEvents::POST_MESSAGES, 'This key is needed to show messages.');

$automaticScripts = [
'@' . ScriptEvents::AUTO_SCRIPTS,
'@' . ScriptEvents::POST_MESSAGES,
];

$manipulator->addSubNode(
'scripts',
ComposerScriptEvents::POST_INSTALL_CMD,
\array_merge($scripts[ComposerScriptEvents::POST_INSTALL_CMD] ?? [], [$automaticScripts])
\array_merge($scripts[ComposerScriptEvents::POST_INSTALL_CMD] ?? [], [$autoScript])
);
$manipulator->addSubNode(
'scripts',
ComposerScriptEvents::POST_UPDATE_CMD,
\array_merge($scripts[ComposerScriptEvents::POST_UPDATE_CMD] ?? [], [$automaticScripts])
\array_merge($scripts[ComposerScriptEvents::POST_UPDATE_CMD] ?? [], [$autoScript])
);

if (! isset($scripts[ScriptEvents::AUTO_SCRIPTS])) {
Expand All @@ -330,6 +321,37 @@ public function onPostCreateProject(Event $event): void
$this->updateComposerLock();
}

/**
* Executes on composer create project event.
*
* @param \Composer\Script\Event $event
*
* @throws \Exception
*/
public function onPostCreateProject(Event $event): void
{
/** @var \Composer\Json\JsonFile $json */
/** @var \Composer\Json\JsonManipulator $manipulator */
[$json, $manipulator] = Util::getComposerJsonFileAndManipulator();

// new projects are most of the time proprietary
$manipulator->addMainKey('license', 'proprietary');

// 'name' and 'description' are only required for public packages
$manipulator->removeProperty('name');
$manipulator->removeProperty('description');

foreach ($this->container->get('composer-extra') as $key => $value) {
if ($key !== self::COMPOSER_EXTRA_KEY) {
$manipulator->addSubNode('extra', $key, $value);
}
}

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

$this->updateComposerLock();
}

/**
* Run found skeleton generators.
*
Expand Down Expand Up @@ -413,30 +435,24 @@ public function onPostUninstall(PackageEvent $event): void
if ($operation->getPackage()->getName() === self::PACKAGE_NAME) {
$scripts = $this->container->get(Composer::class)->getPackage()->getScripts();

if (\count($scripts) === 0 || ! isset($scripts[ScriptEvents::POST_MESSAGES])) {
if (\count($scripts) === 0) {
return;
}

/** @var \Composer\Json\JsonFile $json */
/** @var \Composer\Json\JsonManipulator $manipulator */
[$json, $manipulator] = Util::getComposerJsonFileAndManipulator();

$manipulator->removeSubNode('scripts', ScriptEvents::POST_MESSAGES);

foreach ((array) $scripts[ComposerScriptEvents::POST_INSTALL_CMD] as $key => $script) {
if ($script === '@' . ScriptEvents::POST_MESSAGES) {
unset($scripts[ComposerScriptEvents::POST_INSTALL_CMD][$key]);
} elseif ($script === '@' . ScriptEvents::AUTO_SCRIPTS) {
if ($script === '@' . ScriptEvents::AUTO_SCRIPTS) {
unset($scripts[ComposerScriptEvents::POST_INSTALL_CMD][$key]);
}
}

$manipulator->addSubNode('scripts', ComposerScriptEvents::POST_INSTALL_CMD, $scripts[ComposerScriptEvents::POST_INSTALL_CMD]);

foreach ((array) $scripts[ComposerScriptEvents::POST_UPDATE_CMD] as $key => $script) {
if ($script === '@' . ScriptEvents::POST_MESSAGES) {
unset($scripts[ComposerScriptEvents::POST_UPDATE_CMD][$key]);
} elseif ($script === '@' . ScriptEvents::AUTO_SCRIPTS) {
if ($script === '@' . ScriptEvents::AUTO_SCRIPTS) {
unset($scripts[ComposerScriptEvents::POST_UPDATE_CMD][$key]);
}
}
Expand Down Expand Up @@ -576,8 +592,8 @@ public function onPostUpdate(Event $event, array $operations = []): void

$install->transform($package);

if ($package->hasConfig(ScriptEvents::POST_MESSAGES)) {
foreach ((array) $package->getConfig(ScriptEvents::POST_MESSAGES) as $line) {
if ($package->hasConfig('post-install-output')) {
foreach ((array) $package->getConfig('post-install-output') as $line) {
$this->postMessages[] = self::expandTargetDir($this->container->get('composer-extra'), $line);
}

Expand Down
9 changes: 0 additions & 9 deletions src/Automatic/ScriptEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,4 @@ class ScriptEvents
* @var string
*/
public const AUTO_SCRIPTS = 'auto-scripts';

/**
* The POST_MESSAGES event occurs after a package is installed or updated.
*
* The event listener method receives a Composer\Script\Event instance.
*
* @var string
*/
public const POST_MESSAGES = 'post-messages';
}
2 changes: 1 addition & 1 deletion src/Security/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function getSecurityAdvisories(?IOInterface $io = null): array
}

if ($io !== null) {
$io->writeError('Downloading the Security Advisories database...');
$io->writeError('Downloading the Security Advisories database...', true, IOInterface::VERBOSE);
}

$securityAdvisories = $this->downloader->download(self::SECURITY_ADVISORIES_BASE_URL . self::SECURITY_ADVISORIES);
Expand Down
Loading