Skip to content

Instantly share code, notes, and snippets.

@prodriguezval
Last active June 14, 2016 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prodriguezval/e2dfe94300f24191b3ea3dea21e9854a to your computer and use it in GitHub Desktop.
Save prodriguezval/e2dfe94300f24191b3ea3dea21e9854a to your computer and use it in GitHub Desktop.
This command check if the files composer.json and bower.json was modified in the previous pull and execute composer install and bower install to update the system dependencies
<?php
namespace App\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class InstallOnPostMergeCommand extends ContainerAwareCommand
{
const COMPOSER_JSON_FILE = 'composer.json';
const BOWER_JSON_FILE = 'bower.json';
/**
* @inheritDoc
*/
protected function configure()
{
$this
->setName('app:post-merge:install')
;
}
/**
* @inheritDoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('<fg=yellow;options=bold>Post-Merge Hook</fg=yellow;options=bold>');
$output->writeln('<info>Check composer</info>');
$pushedFiles = $this->extractPushedFiles();
if ($this->checkInstall(self::COMPOSER_JSON_FILE, $pushedFiles)) {
$output->writeln('<info> Executing composer install </info>');
exec('composer install --prefer-dist --optimize-autoloader');
$output->writeln('<info> composer install finished </info>');
} else {
$output->writeln('<info>composer.json isn\'t updated </info>');
}
if ($this->checkInstall(self::BOWER_JSON_FILE, $pushedFiles)) {
$output->writeln('<info> Executing bower install </info>');
exec('bower install');
$output->writeln('<info> bower install finished </info>');
} else {
$output->writeln('<info>bower.json isn\'t updated </info>');
}
}
private function checkInstall($file, $pushedFiles)
{
if (in_array($file, $pushedFiles)) {
return true;
}
return false;
}
private function extractPushedFiles()
{
$pushedFiles = [];
exec('git diff-tree -r --name-only --no-commit-id HEAD', $pushedFiles);
return $pushedFiles;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment