|
@@ -0,0 +1,110 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace FD3\Command;
|
|
|
+
|
|
|
+use Symfony\Component\Console\Command\Command;
|
|
|
+use Symfony\Component\Console\Input\InputInterface;
|
|
|
+use Symfony\Component\Console\Input\InputArgument;
|
|
|
+use Symfony\Component\Console\Input\InputOption;
|
|
|
+use Symfony\Component\Console\Output\OutputInterface;
|
|
|
+
|
|
|
+use PHPGit\Git;
|
|
|
+use PHPGit\Exception\GitException;
|
|
|
+
|
|
|
+use WriteiniFile\WriteiniFile;
|
|
|
+
|
|
|
+class TagVendorsCommand extends Command
|
|
|
+{
|
|
|
+ protected function configure()
|
|
|
+ {
|
|
|
+ $this
|
|
|
+ ->setName('make:tag:vendors')
|
|
|
+ ->setDescription('Tags all vendor from bitbucket')
|
|
|
+ ->setHelp('Tag all vendors from bitbucket')
|
|
|
+ ->addArgument('ini_file', InputArgument::REQUIRED,
|
|
|
+ 'The ini file from where to get the source code config.')
|
|
|
+ ->addArgument('tag', InputArgument::REQUIRED, 'Create tag e.g. vX.Y.Z and branch X.Y')
|
|
|
+ ->addOption('branch', null, InputOption::VALUE_OPTIONAL,
|
|
|
+ 'Indicates if push branch or tag. If true, then push branch, else push tag', false);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
+ {
|
|
|
+ $file = $input->getArgument("ini_file");
|
|
|
+ $tag = $input->getArgument("tag");
|
|
|
+ $optionBranch = $input->getOption('branch');
|
|
|
+ if (strtolower(substr($tag, 0, 1)) == 'v') {
|
|
|
+ $version = explode(".", $tag);
|
|
|
+ if (count($version) == 3) {
|
|
|
+ unset($version[count($version) - 1]);
|
|
|
+ }
|
|
|
+ $version = implode(".", $version);
|
|
|
+
|
|
|
+ $realpath = realpath($file);
|
|
|
+ $dirname = dirname($realpath);
|
|
|
+ if (!chdir($dirname)) {
|
|
|
+ throw new \Exception("Can't change working directory to " . $dirname);
|
|
|
+ }
|
|
|
+
|
|
|
+ $content = parse_ini_file($realpath, true);
|
|
|
+ foreach ($content as $sec => $conf) {
|
|
|
+ $output->writeln($sec . " - " . $content[$sec]["branch"] . " - " . $version . " - " . $tag . " - " . $content[$sec]["repo"]);
|
|
|
+ $content[$sec]["branch"] = $version;
|
|
|
+ try {
|
|
|
+ $git = new Git();
|
|
|
+ $git_path = $dirname . '/' . $sec;
|
|
|
+ // agrego datos del usuario
|
|
|
+ $git->config->add('user.email', 'drone@flowdat.com', ["global" => true]);
|
|
|
+ $git->config->add('user.name', 'drone', ["global" => true]);
|
|
|
+ // clono el repositorio
|
|
|
+ $git->clone($content[$sec]["repo"], $git_path);
|
|
|
+ // agrego el path del repositorio
|
|
|
+ $git->setRepository($git_path);
|
|
|
+ $git->fetch('origin');
|
|
|
+ // comando a ejecutar
|
|
|
+ $cmd = "cd " . $git_path . "; git push origin ";
|
|
|
+ // necesito traer todos los branch's para ver si ya existe
|
|
|
+ $branchs = $git->branch(['all' => true]);
|
|
|
+ $findBranch = false;
|
|
|
+ $branchRemote = null;
|
|
|
+ // verifico si el branch a crear ya existe
|
|
|
+ foreach ($branchs as $branch) {
|
|
|
+ if (strpos($branch['name'], $version) !== false) {
|
|
|
+ $findBranch = true;
|
|
|
+ $branchRemote = $branch['name'];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!$findBranch && $optionBranch) {
|
|
|
+ // si no existe el branch y estoy creando un branch nuevo
|
|
|
+ $git->branch->create($version);
|
|
|
+ $cmd .= "$version ";
|
|
|
+ }
|
|
|
+ $git->checkout($version);
|
|
|
+ if (!$optionBranch) {
|
|
|
+ // estoy creando un tag
|
|
|
+ $tags = $git->tag();
|
|
|
+ if (!in_array($tag, $tags)) {
|
|
|
+ // el tag no existe
|
|
|
+ if ($branchRemote == null) {
|
|
|
+ // el branch remote no existe
|
|
|
+ $git->tag->create($tag, $version);
|
|
|
+ } else {
|
|
|
+ // utilizo el branch remoto
|
|
|
+ $git->tag->create($tag, $branchRemote);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $cmd .= " --tags";
|
|
|
+ }
|
|
|
+ // ejecuto el comando git
|
|
|
+ shell_exec($cmd);
|
|
|
+ } catch (GitException $e) {
|
|
|
+ $output->writeln($e->getTraceAsString());
|
|
|
+ $output->writeln($sec . ": " . $e->getMessage());
|
|
|
+ return $e->getCode();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+}
|