ComposerRequireCommand.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace FD3;
  3. use Symfony\Component\Console\Command\Command;
  4. use Symfony\Component\Console\Input\InputInterface;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. class ComposerRequireCommand extends Command
  9. {
  10. protected function configure()
  11. {
  12. $this
  13. ->setName('composer:require')
  14. ->setDescription('Tag all composer software using a ini file.')
  15. ->setHelp('This command allows you to tag and change the required code on the composer.json file...')
  16. ->addArgument('ini_file', InputArgument::REQUIRED, 'The ini file from where to get the source code config.')
  17. ->addArgument('packages', InputArgument::REQUIRED, 'Tag the required packages (suggested value : ik/*).')
  18. ->addArgument('version', InputArgument::REQUIRED, 'Use the version expression.')
  19. ->addOption('composer_update', null, InputOption::VALUE_OPTIONAL, 'If true, run "docker-compose run ... bin/composer-update.sh" to generate composer.lock', 'true')
  20. ->addOption('pull', null, InputOption::VALUE_OPTIONAL, 'Indicates branch to pull for every modules', '');
  21. }
  22. protected function execute(InputInterface $input, OutputInterface $output)
  23. {
  24. $dockerCompose = $input->getOption("composer_update");
  25. $pull = $input->getOption("pull");
  26. $file = $input->getArgument("ini_file");
  27. $packages = $input->getArgument("packages");
  28. $new_version = $input->getArgument("version");
  29. $realpath = realpath($file);
  30. $dirname = dirname($realpath);
  31. if (!chdir($dirname)) {
  32. throw new \Exception("Can't change working directory to " . $dirname);
  33. }
  34. $content = parse_ini_file($realpath, true);
  35. foreach ($content as $sec => $conf) {
  36. $composer_file = $sec . "/composer.json";
  37. if (file_exists($composer_file)) {
  38. $output->writeln("----------------------------------------------------------------------");
  39. $output->writeln($sec);
  40. $composer_data = json_decode(file_get_contents($composer_file), true);
  41. try {
  42. foreach (array("require", "require-dev") as $k) {
  43. $output->writeln('Ejecutando:');
  44. $command = 'cd ' . $sec . '; git stash; git fetch; git checkout master; git pull origin master';
  45. $output->writeln("\t" . $command);
  46. shell_exec($command);
  47. if (strlen($pull) > 0) {
  48. $command = 'cd ' . $sec . '; git pull origin ' . $pull;
  49. $output->writeln("\t" . $command);
  50. shell_exec($command);
  51. }
  52. $output->writeln("\t" . 'Reemplazo composer.json');
  53. foreach ($composer_data[$k] as $package => $version) {
  54. if (strpos($packages, "*") !== false) {
  55. $preg = "|^" . str_replace("*", "[^\b]*", $packages) . "$|";
  56. if (preg_match($preg, $package)) {
  57. $composer_data[$k][$package] = (string)$new_version;
  58. $output->writeln("\t\t" . $package . " = " . $new_version);
  59. }
  60. } else {
  61. if (strcmp($packages, $package) === 0) {
  62. $composer_data[$k][$package] = (string)$new_version;
  63. $output->writeln("\t\t" . $package . " = " . $new_version);
  64. }
  65. }
  66. }
  67. }
  68. file_put_contents($composer_file, json_encode($composer_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
  69. if ($dockerCompose) {
  70. $output->writeln('Composer update');
  71. $command = 'docker-compose exec ' . $sec . ' bin/composer-update.sh';
  72. $output->writeln("\t" . $command);
  73. $resp = shell_exec($command);
  74. if ($resp) {
  75. $output->writeln("\tSalida comando.");
  76. $output->writeln("\t\t" . $resp);
  77. }
  78. }
  79. } catch (Throwable $t) {
  80. $output->writeln('Se produjo un error. Reestableciendo datos.');
  81. $command = 'cd ' . $sec . '; git reset';
  82. $output->writeln("\t" . $command);
  83. $shell_exec($command);
  84. }
  85. } else {
  86. $output->writeln($sec . " no composer.json found");
  87. }
  88. }
  89. }
  90. }