|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Articstudio\PhpBin\Commands\Git\Subtree; |
| 6 | + |
| 7 | +use Articstudio\PhpBin\Commands\Command; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Input\InputOption; |
| 11 | +use Symfony\Component\Console\Output\OutputInterface; |
| 12 | + |
| 13 | +class Version extends Command |
| 14 | +{ |
| 15 | + |
| 16 | + use Concerns\HasSubtreesConfig; |
| 17 | + use Concerns\HasSubtreeBehaviour; |
| 18 | + |
| 19 | + protected $io; |
| 20 | + |
| 21 | + /** |
| 22 | + * Command name |
| 23 | + * |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected static $defaultName = 'git:subtree:version'; |
| 27 | + |
| 28 | + protected function configure() |
| 29 | + { |
| 30 | + $this->addOption('tag', 't', InputOption::VALUE_REQUIRED, 'Versió:'); |
| 31 | + $this->addArgument('package_name', InputArgument::IS_ARRAY, 'Nom del package:'); |
| 32 | + } |
| 33 | + |
| 34 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 35 | + { |
| 36 | + $repositories = $this->getSubtrees(); |
| 37 | + $versions_groups = $this->getVersionsGroups(); |
| 38 | + $this->io = $this->getStyle($output, $input); |
| 39 | + |
| 40 | + $package_names = $input->getArgument('package_name') ?: []; |
| 41 | + $version = $input->getOption('tag') ?: null; |
| 42 | + |
| 43 | + if (count($package_names) < 1) { |
| 44 | + $menu_options = array_keys($repositories) + [ |
| 45 | + 'all' => 'All subtrees', |
| 46 | + ]; |
| 47 | + foreach (array_keys($versions_groups) as $group_name) { |
| 48 | + $menu_options['group:'.$group_name] = 'Group: ' . $group_name; |
| 49 | + } |
| 50 | + $option = $this->selectPackageMenu('Version subtrees', $menu_options); |
| 51 | + |
| 52 | + if ($option === 'back') { |
| 53 | + return $this->callCommandByName('git', [], $output); |
| 54 | + } |
| 55 | + |
| 56 | + if ($option === null) { |
| 57 | + return 1; |
| 58 | + } |
| 59 | + |
| 60 | + if ($option === 'all') { |
| 61 | + $package_names = array_keys($repositories); |
| 62 | + } elseif (substr($option, 0, 6) === 'group:') { |
| 63 | + $group_name = substr($option, 6); |
| 64 | + $package_names = $versions_groups[$group_name] ?? []; |
| 65 | + } else { |
| 66 | + $package_names = is_int($option) |
| 67 | + ? [array_keys($repositories)[$option]] |
| 68 | + : []; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (! $version) { |
| 73 | + $version = $this->io->ask('Please enter the new version', $this->getPackageVersion()); |
| 74 | + } |
| 75 | + |
| 76 | + $result = $this->versionSubtrees($repositories, $package_names, $version); |
| 77 | + $this->showResume($result, $this->io); |
| 78 | + |
| 79 | + return $this->exit($output, 0); |
| 80 | + } |
| 81 | + |
| 82 | + private function versionSubtrees(array $repositories, $package_names, $version) |
| 83 | + { |
| 84 | + $result = [ |
| 85 | + 'skipped' => [], |
| 86 | + 'done' => [], |
| 87 | + 'error' => [], |
| 88 | + 'not_found' => [], |
| 89 | + ]; |
| 90 | + |
| 91 | + foreach ($repositories as $repo_package => $repo_url) { |
| 92 | + if (count($package_names) < 1 || ! in_array($repo_package, $package_names)) { |
| 93 | + $result['skipped'][] = $repo_package; |
| 94 | + continue; |
| 95 | + } |
| 96 | + if (! $this->subtreeExists($repo_package)) { |
| 97 | + $result['not_found'][] = $repo_package; |
| 98 | + unset($repositories[$repo_package]); |
| 99 | + continue; |
| 100 | + } |
| 101 | + $key = $this->versionSubtree($repo_package, $repo_url, $version) |
| 102 | + ? 'done' : 'error'; |
| 103 | + $result[$key][] = $repo_package; |
| 104 | + } |
| 105 | + |
| 106 | + return $result; |
| 107 | + } |
| 108 | + |
| 109 | + private function versionSubtree($package_name, $repository, $version): bool |
| 110 | + { |
| 111 | + $tmp = '/tmp/phpbin-release'; |
| 112 | + $cmds = [ |
| 113 | + "rm -rf {$tmp} && mkdir {$tmp}", |
| 114 | + "cd {$tmp} && git clone {$repository} .", |
| 115 | + "cd {$tmp} && git checkout master", |
| 116 | + "cd {$tmp} && git tag -a {$version} -m \"{$version}\"", |
| 117 | + "cd {$tmp} && git push origin --tags", |
| 118 | + ]; |
| 119 | + $exit_code = 0; |
| 120 | + foreach ($cmds as $cmd) { |
| 121 | + [$exit_code] = $this->callShell($cmd, false); |
| 122 | + if ($exit_code !== 0) { |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + $this->callShell("rm -rf {$tmp}", false); |
| 127 | + return $exit_code === 0; |
| 128 | + } |
| 129 | + |
| 130 | +} |
0 commit comments