From def8812945e0767a45fe0e4db5ed255b7093600a Mon Sep 17 00:00:00 2001 From: Sebastian Feldmann Date: Thu, 6 May 2021 17:40:54 +0200 Subject: [PATCH] Allow force installation of hooks You can now force the installation of all hooks by setting a flag in the composer.json configuration. "extra": { "captainhook": { "force-install": true } } --- src/ComposerPlugin.php | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/ComposerPlugin.php b/src/ComposerPlugin.php index afe0dc1..048c885 100644 --- a/src/ComposerPlugin.php +++ b/src/ComposerPlugin.php @@ -137,7 +137,7 @@ public function installHooks(Event $event): void $this->io->write(' plugin is disabled'); return; } - + if (getenv('CI') === 'true') { $this->io->write(' disabling plugin due to CI-environment'); return; @@ -205,15 +205,20 @@ private function runCaptainCommand(string $command): void // Respect composer CLI settings $ansi = $this->io->isDecorated() ? ' --ansi' : ' --no-ansi'; $interaction = $this->io->isInteractive() ? '' : ' --no-interaction'; + $executable = escapeshellarg($this->executable); // captainhook config and repository settings $configuration = ' -c ' . escapeshellarg($this->configuration); - $repository = $command === self::COMMAND_INSTALL ? ' -g ' . escapeshellarg($this->gitDirectory) : ''; - $skip = $command === self::COMMAND_INSTALL ? ' -s' : ''; - $executable = escapeshellarg($this->executable); + $repository = ''; + $forceOrSkip = ''; + + if ($command === self::COMMAND_INSTALL) { + $repository = ' -g ' . escapeshellarg($this->gitDirectory); + $forceOrSkip = $this->isForceInstall() ? ' -f' : ' -s'; + } // sub process settings - $cmd = $executable . ' ' . $command . $ansi . $interaction . $skip . $configuration . $repository; + $cmd = $executable . ' ' . $command . $ansi . $interaction . $forceOrSkip . $configuration . $repository; $pipes = []; $spec = [ 0 => ['file', 'php://stdin', 'r'], @@ -314,4 +319,15 @@ private function isPluginDisabled(): bool $extra = $this->composer->getPackage()->getExtra(); return (bool) ($extra['captainhook']['disable-plugin'] ?? false); } + + /** + * Is a force installation configured + * + * @return bool + */ + private function isForceInstall(): bool + { + $extra = $this->composer->getPackage()->getExtra(); + return (bool) ($extra['captainhook']['force-install'] ?? false); + } }