Skip to content

Commit

Permalink
Add option to install only enabled hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianfeldmann committed Mar 6, 2024
1 parent 2298b62 commit 006ce94
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ configure anything to make it work.
"config": "config/captainhook.json",
"exec": "tools/captainhook.phar",
"force-install": true,
"only-enabled": true,
"disable-plugin": false
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/ComposerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,20 +228,31 @@ private function isPluginDisabled(): bool
*
* @return bool
*/
private function isForceInstall(): bool
private function forceInstallConfig(): bool
{
$extra = $this->composer->getPackage()->getExtra();
return ($extra['captainhook']['force-install'] ?? false) || getenv('CAPTAINHOOK_FORCE_INSTALL') === 'true';
}

/**
* Is a force installation configured
*
* @return bool
*/
private function onlyEnabledConfig(): bool
{
$extra = $this->composer->getPackage()->getExtra();
return $extra['captainhook']['only-enabled'] ?? false;
}

/**
* Install hooks to your .git/hooks directory
*/
private function install(): void
{
try {
$installer = new Installer($this->executable, $this->configuration, $this->dotGit->gitDirectory());
$installer->install($this->io, $this->isForceInstall());
$installer->install($this->io, $this->forceInstallConfig(), $this->onlyEnabledConfig());
} catch (\Exception $e) {
throw new RuntimeException($this->pluginErrorMessage($e->getMessage()));
}
Expand Down
10 changes: 6 additions & 4 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ public function __construct(string $executable, string $configuration, string $g
/**
* Install the hooks by executing the Cap'n
*
* @param \Composer\IO\IOInterface $io
* @param bool $force
* @param \Composer\IO\IOInterface $io
* @param bool $force
* @param bool $enabled
* @return void
*/
public function install(IOInterface $io, bool $force): void
public function install(IOInterface $io, bool $force, bool $enabled): void
{
// Respect composer CLI settings
$ansi = $io->isDecorated() ? ' --ansi' : ' --no-ansi';
Expand All @@ -68,10 +69,11 @@ public function install(IOInterface $io, bool $force): void
$configuration = ' -c ' . escapeshellarg($this->configuration);
$repository = ' -g ' . escapeshellarg($this->gitDirectory);
$forceOrSkip = $force ? ' -f' : ' -s';
$onlyEnabled = $enabled ? ' --only-enabled' : '';

// sub process settings
$cmd = PHP_BINARY . ' ' . $executable . ' install'
. $ansi . ' --no-interaction' . $forceOrSkip
. $ansi . ' --no-interaction' . $forceOrSkip . $onlyEnabled
. $configuration . $repository;
$pipes = [];
$spec = [
Expand Down

0 comments on commit 006ce94

Please sign in to comment.