Skip to content

Commit

Permalink
Run the install command after the composer autoload.php file is gener…
Browse files Browse the repository at this point in the history
…ated
  • Loading branch information
marcelthole committed Apr 20, 2021
1 parent eadedd0 commit 2a6edc8
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace CaptainHook\Composer;

use Composer\Installer\PackageEvent;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use PharIo\ComposerDistributor\ConfiguredMediator;
use RuntimeException;

Expand Down Expand Up @@ -49,11 +49,32 @@ class Plugin extends ConfiguredMediator
*/
private $executable;

/**
* Flag to determine to install Captain Hook after a package update or install
*
* @var bool
*/
private $isPackageUpdate = false;

protected function getDistributorConfig(): string
{
return __DIR__ . '/../distributor.xml';
}

public static function getSubscribedEvents()
{
$existingEvents = parent::getSubscribedEvents();

return array_merge_recursive(
$existingEvents,
[
ScriptEvents::POST_AUTOLOAD_DUMP => [
['installHooksAfterPackageUpdate', 0]
]
]
);
}

/**
* On install of update install hooks
*
Expand All @@ -65,17 +86,18 @@ public function installOrUpdateFunction(PackageEvent $event): void
// download phar and check signature
parent::installOrUpdateFunction($event);
// try to configure and install hooks
$this->installHooks();
$this->configureHooks();
}

/**
* Run the installer
* Configure the installer
*
* @return void
* @throws \Exception
*/
public function installHooks(): void
public function configureHooks(): void
{
$this->isPackageUpdate = true;
$this->getIO()->write('<info>CaptainHook</info>');

if ($this->isPluginDisabled()) {
Expand Down Expand Up @@ -106,9 +128,23 @@ public function installHooks(): void
}

$this->configure();
$this->install();
}

/**
* Install hooks to your .git/hooks directory
*/
public function installHooksAfterPackageUpdate(): void
{
if (!$this->isPackageUpdate) {
return;
}
$this->getIO()->write(' - Install hooks: ', false);
$runner = new Captain($this->executable, $this->configuration, $this->gitDirectory);
$runner->execute(Captain::COMMAND_INSTALL, $this->getIO());
$this->getIO()->write(('<comment> done</comment>'));
}


/**
* Create captainhook.json file if it does not exist
*/
Expand All @@ -124,18 +160,6 @@ private function configure(): void

$runner = new Captain($this->executable, $this->configuration, $this->gitDirectory);
$runner->execute(Captain::COMMAND_CONFIGURE, $this->getIO());

}

/**
* Install hooks to your .git/hooks directory
*/
private function install(): void
{
$this->getIO()->write(' - Install hooks: ', false);
$runner = new Captain($this->executable, $this->configuration, $this->gitDirectory);
$runner->execute(Captain::COMMAND_INSTALL, $this->getIO());
$this->getIO()->write(('<comment> done</comment>'));
}

/**
Expand Down

0 comments on commit 2a6edc8

Please sign in to comment.