Skip to content

Commit

Permalink
extract a few methods, add clarifying comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ragulka committed Sep 25, 2024
1 parent abc3905 commit 15da481
Showing 1 changed file with 37 additions and 14 deletions.
51 changes: 37 additions & 14 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Composer\Plugin\PluginInterface;
use Composer\Script\Event;
use Composer\Script\ScriptEvents;
use Exception;
use RuntimeException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
Expand Down Expand Up @@ -113,14 +114,8 @@ public function path( ...$parts ): array|string {

public function execute( Event $event ): void {

$extra = $event->getComposer()->getPackage()->getExtra();

// skip on install/update if autorun is disabled
if (
isset( $extra['aviary']['autorun'] ) &&
$extra['aviary']['autorun'] === false &&
( $event->getName() === ScriptEvents::POST_UPDATE_CMD || $event->getName() === ScriptEvents::POST_INSTALL_CMD )
) {
if ( $this->isAutorunDisabled($event) && $this->isPostInstallOrUpdateEvent($event) ) {
return;
}

Expand All @@ -129,8 +124,11 @@ public function execute( Event $event ): void {
return;
}

$source = $this->path( $this->tempDir, 'source' );
$destination = $this->path( $this->tempDir, 'destination' );
// temp dir for installing unscoped dependencies
$source = $this->path( $this->tempDir, 'source' );
// destination dir for outputting scoped dependencies (php-scoper output-dir & working-dir)
$destination = $this->path( $this->tempDir, 'destination' );

$phpScoperConfig = $this->createPhpScoperConfig( $this->tempDir, $source, $destination );
$composerJsonPath = $this->path( $source, 'composer.json' );
$composerLockPath = $this->path( $source, 'composer.lock' );
Expand Down Expand Up @@ -207,7 +205,8 @@ public function execute( Event $event ): void {
$this->runInstall( $source, $command, $useDevDependencies );
}

private function createPhpScoperConfig( string $path, string $source, string $destination ) {
private function createPhpScoperConfig( string $path, string $source, string $destination ): array|string
{

$inc_path = $this->createPath( [ 'config', 'scoper.inc.php' ] );
$config_path = $this->createPath( [ 'config', 'aviary.config.php' ] );
Expand All @@ -219,7 +218,6 @@ private function createPhpScoperConfig( string $path, string $source, string $de
$this->createFolder( $source );
$this->createFolder( $destination );


$config = [
'prefix' => $this->prefix,
'source' => $source,
Expand Down Expand Up @@ -277,7 +275,10 @@ private function createJson( string $path, $content ): void {
file_put_contents( $path, $json );
}

private function runInstall( string $path, string $command = 'install', bool $useDevDependencies = true ): int {
/**
* @throws Exception
*/
private function runInstall(string $path, string $command = 'install', bool $useDevDependencies = true ): int {

$output = new ConsoleOutput();
$application = new Application();
Expand All @@ -295,16 +296,38 @@ private function runInstall( string $path, string $command = 'install', bool $us
);
}

protected function getBinPath(string $packageName, string $binaryName): string {
protected function getBinPath( string $packageName, string $binaryName ): string {

$installationManager = $this->composer->getInstallationManager();
$package = $this->composer->getRepositoryManager()->getLocalRepository()->findPackage($packageName, '*');

if ($package) {
if ( $package ) {
$installPath = $installationManager->getInstallPath($package);
return $installPath . '/bin/' . $binaryName;
}

throw new RuntimeException("Package $packageName not found.");
}

/**
* Checks if autorun is disabled in composer config.
*
* @param Event $event
* @return bool
*/
protected function isAutorunDisabled( Event $event ): bool
{
$extra = $event->getComposer()->getPackage()->getExtra();

return isset( $extra['aviary']['autorun'] ) && $extra['aviary']['autorun'] === false;
}

/**
* @param Event $event
* @return bool
*/
protected function isPostInstallOrUpdateEvent( Event $event ): bool
{
return ( $event->getName() === ScriptEvents::POST_UPDATE_CMD || $event->getName() === ScriptEvents::POST_INSTALL_CMD );
}
}

0 comments on commit 15da481

Please sign in to comment.