Advertisement
Guest User

AdminBundle/Command/GitFlowCreateReleaseCommand.php

a guest
Aug 22nd, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.53 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AdminBundle\Command;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Console\Question\Question;
  9.  
  10. class GitFlowCreateReleaseCommand extends ContainerAwareCommand
  11. {
  12.     protected $em;
  13.  
  14.     protected function configure()
  15.     {
  16.         $this
  17.             ->setName('app:gitflow:create-release')
  18.             ->setHelp(<<<EOF
  19. The <info>%command.name%</info> creates a new GitFlow release (prompt for version number)
  20.  
  21.   <info>php app/console %command.name%</info>
  22.  
  23. EOF
  24.             );
  25.     }
  26.  
  27.     protected function execute(InputInterface $input, OutputInterface $output)
  28.     {
  29.         $lastTag = $this->getLatestGitTag();
  30.  
  31.         $output->writeln("== GitFlow Release Creator ==");
  32.         $output->writeln("");
  33.  
  34.         /**
  35.          * ask for next tag
  36.          */
  37.         $output->writeln(sprintf("Last created tag: %s", $lastTag));
  38.  
  39.         $nextReleaseTag = $this->askForNextReleaseTag($input, $output);
  40.         $output->writeln(sprintf("Next tag: %s", $nextReleaseTag));
  41.  
  42.         /**
  43.          * create release
  44.          */
  45.         $this->startAndFinishNewRelease($nextReleaseTag);
  46.     }
  47.  
  48.     /**
  49.      * Get the latest Git tag
  50.      *
  51.      * @return mixed
  52.      */
  53.     private function getLatestGitTag()
  54.     {
  55.         exec("git tag --sort=v:refname", $output);
  56.  
  57.         return array_pop($output);
  58.     }
  59.  
  60.     /**
  61.      * @param InputInterface $input
  62.      * @param OutputInterface $output
  63.      * @return mixed
  64.      */
  65.     private function askForNextReleaseTag(InputInterface $input, OutputInterface $output)
  66.     {
  67.         $helper = $this->getHelper('question');
  68.         $question = new Question('Insert the next Release tag to create: ', '');
  69.         $question->setValidator(function ($answer) {
  70.             if (empty($answer)) {
  71.                 throw new \RuntimeException(
  72.                     'You cannot create an empty Release tag'
  73.                 );
  74.             }
  75.  
  76.             return $answer;
  77.         });
  78.  
  79.         return $helper->ask($input, $output, $question);
  80.     }
  81.  
  82.     /**
  83.      * @param $nextReleaseTag
  84.      */
  85.     private function startAndFinishNewRelease($nextReleaseTag)
  86.     {
  87.         exec(sprintf(
  88.             "git flow release start %s",
  89.             $nextReleaseTag
  90.         ));
  91.  
  92.         exec(sprintf(
  93.             "git flow release finish -m \"%s\" %s",
  94.             $nextReleaseTag,
  95.             $nextReleaseTag
  96.         ));
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement