forked from acquia/blt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…te updates.
- Loading branch information
Showing
4 changed files
with
188 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Provides a way to patch Composer packages after installation. | ||
*/ | ||
|
||
namespace Acquia\Blt\Composer; | ||
|
||
use Composer\Composer; | ||
use Composer\DependencyResolver\Operation\InstallOperation; | ||
use Composer\DependencyResolver\Operation\UninstallOperation; | ||
use Composer\DependencyResolver\Operation\UpdateOperation; | ||
use Composer\DependencyResolver\Operation\OperationInterface; | ||
use Composer\EventDispatcher\EventSubscriberInterface; | ||
use Composer\IO\IOInterface; | ||
use Composer\Package\AliasPackage; | ||
use Composer\Package\PackageInterface; | ||
use Composer\Plugin\PluginInterface; | ||
use Composer\Installer\PackageEvents; | ||
use Composer\Script\Event; | ||
use Composer\Script\ScriptEvents; | ||
use Composer\Script\PackageEvent; | ||
use Composer\Util\ProcessExecutor; | ||
use Composer\Util\RemoteFilesystem; | ||
use Symfony\Component\Process\Process; | ||
|
||
class BltPlugin implements PluginInterface, EventSubscriberInterface { | ||
|
||
/** | ||
* @var Composer $composer | ||
*/ | ||
protected $composer; | ||
/** | ||
* @var IOInterface $io | ||
*/ | ||
protected $io; | ||
/** | ||
* @var EventDispatcher $eventDispatcher | ||
*/ | ||
protected $eventDispatcher; | ||
/** | ||
* @var ProcessExecutor $executor | ||
*/ | ||
protected $executor; | ||
|
||
/** | ||
* @var \Composer\Package\PackageInterface | ||
*/ | ||
protected $bltPackage; | ||
|
||
/** | ||
* Apply plugin modifications to composer | ||
* | ||
* @param Composer $composer | ||
* @param IOInterface $io | ||
*/ | ||
public function activate(Composer $composer, IOInterface $io) { | ||
$this->composer = $composer; | ||
$this->io = $io; | ||
$this->eventDispatcher = $composer->getEventDispatcher(); | ||
$this->executor = new ProcessExecutor($this->io); | ||
} | ||
|
||
/** | ||
* Returns an array of event names this subscriber wants to listen to. | ||
*/ | ||
public static function getSubscribedEvents() { | ||
return array( | ||
PackageEvents::POST_PACKAGE_INSTALL => "onPostPackageEvent", | ||
PackageEvents::POST_PACKAGE_UPDATE => "onPostPackageEvent", | ||
ScriptEvents::POST_UPDATE_CMD => 'onPostCmdEvent', | ||
ScriptEvents::POST_INSTALL_CMD => 'onPostCmdEvent', | ||
); | ||
} | ||
|
||
/** | ||
* Marks blt to be processed after an install or update command. | ||
* | ||
* @param \Composer\Installer\PackageEvent $event | ||
*/ | ||
public function onPostPackageEvent(\Composer\Installer\PackageEvent $event){ | ||
$package = $this->getBltPackage($event->getOperation()); | ||
if ($package) { | ||
// By explicitly setting the blt package, the onPostCmdEvent() will | ||
// process the update automatically. | ||
$this->bltPackage = $package; | ||
} | ||
} | ||
|
||
/** | ||
* Post install command event to execute the blt update. | ||
* | ||
* @param \Composer\Script\Event $event | ||
*/ | ||
public function onPostCmdEvent(\Composer\Script\Event $event) { | ||
// Only install the scaffolding if acquia/blt was installed, | ||
if (isset($this->bltPackage)) { | ||
$this->executeBltUpdate(); | ||
} | ||
} | ||
|
||
/** | ||
* @param $operation | ||
* @return mixed | ||
*/ | ||
protected function getBltPackage($operation) { | ||
if ($operation instanceof InstallOperation) { | ||
$package = $operation->getPackage(); | ||
} | ||
elseif ($operation instanceof UpdateOperation) { | ||
$package = $operation->getTargetPackage(); | ||
} | ||
if (isset($package) && $package instanceof PackageInterface && $package->getName() == 'acquia/blt') { | ||
return $package; | ||
} | ||
return NULL; | ||
} | ||
|
||
protected function executeBltUpdate() { | ||
// @todo cd into project root from getVendorPath? | ||
$this->io->write('<info>Updating BLT templated files</info>'); | ||
$this->executeCommand('blt update'); | ||
$this->io->write('<info>BLT template files were updated</info>'); | ||
$this->io->write('<comment>This may have modified your composer.json and require a subsequent `composer update`</comment>'); | ||
} | ||
|
||
/** | ||
* Get the path to the 'vendor' directory. | ||
* | ||
* @return string | ||
*/ | ||
public function getVendorPath() { | ||
$config = $this->composer->getConfig(); | ||
$filesystem = new Filesystem(); | ||
$filesystem->ensureDirectoryExists($config->get('vendor-dir')); | ||
$vendorPath = $filesystem->normalizePath(realpath($config->get('vendor-dir'))); | ||
return $vendorPath; | ||
} | ||
|
||
/** | ||
* Executes a shell command with escaping. | ||
* | ||
* @param string $cmd | ||
* @return bool | ||
*/ | ||
protected function executeCommand($cmd) { | ||
// Shell-escape all arguments except the command. | ||
$args = func_get_args(); | ||
foreach ($args as $index => $arg) { | ||
if ($index !== 0) { | ||
$args[$index] = escapeshellarg($arg); | ||
} | ||
} | ||
// And replace the arguments. | ||
$command = call_user_func_array('sprintf', $args); | ||
$output = ''; | ||
if ($this->io->isVerbose()) { | ||
$this->io->write('<comment> > ' . $command . '</comment>'); | ||
$io = $this->io; | ||
$output = function ($type, $buffer) use ($io) { | ||
if ($type == Process::ERR) { | ||
$io->write('<error>' . $buffer . '</error>'); | ||
} | ||
else { | ||
// @todo Figure out how to preserve color! | ||
$io->write($buffer); | ||
} | ||
}; | ||
} | ||
return ($this->executor->execute($command, $output) == 0); | ||
} | ||
|
||
} |