123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace FD3;
- 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;
- class ComposerRequireCommand extends Command
- {
- protected function configure()
- {
- $this
- ->setName('composer:require')
- ->setDescription('Tag all composer software using a ini file.')
- ->setHelp('This command allows you to tag and change the required code on the composer.json file...')
- ->addArgument('ini_file', InputArgument::REQUIRED, 'The ini file from where to get the source code config.')
- ->addArgument('packages', InputArgument::REQUIRED, 'Tag the required packages (suggested value : ik/*).')
- ->addArgument('version', InputArgument::REQUIRED, 'Use the version expression.')
- ->addOption('composer_update', null, InputOption::VALUE_OPTIONAL, 'If true, run "docker-compose run ... bin/composer-update.sh" to generate composer.lock', 'true')
- ->addOption('pull', null, InputOption::VALUE_OPTIONAL, 'Indicates branch to pull for every modules', '');
- }
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $dockerCompose = $input->getOption("composer_update");
- $pull = $input->getOption("pull");
- $file = $input->getArgument("ini_file");
- $packages = $input->getArgument("packages");
- $new_version = $input->getArgument("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) {
- $composer_file = $sec . "/composer.json";
- if (file_exists($composer_file)) {
- $output->writeln("----------------------------------------------------------------------");
- $output->writeln($sec);
- $composer_data = json_decode(file_get_contents($composer_file), true);
- try {
- foreach (array("require", "require-dev") as $k) {
- $output->writeln('Ejecutando:');
- $command = 'cd ' . $sec . '; git stash; git fetch; git checkout master; git pull origin master';
- $output->writeln("\t" . $command);
- shell_exec($command);
- if (strlen($pull) > 0) {
- $command = 'cd ' . $sec . '; git pull origin ' . $pull;
- $output->writeln("\t" . $command);
- shell_exec($command);
- }
- $output->writeln("\t" . 'Reemplazo composer.json');
- foreach ($composer_data[$k] as $package => $version) {
- if (strpos($packages, "*") !== false) {
- $preg = "|^" . str_replace("*", "[^\b]*", $packages) . "$|";
- if (preg_match($preg, $package)) {
- $composer_data[$k][$package] = (string)$new_version;
- $output->writeln("\t\t" . $package . " = " . $new_version);
- }
- } else {
- if (strcmp($packages, $package) === 0) {
- $composer_data[$k][$package] = (string)$new_version;
- $output->writeln("\t\t" . $package . " = " . $new_version);
- }
- }
- }
- }
- file_put_contents($composer_file, json_encode($composer_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
- if ($dockerCompose) {
- $output->writeln('Composer update');
- $command = 'docker-compose exec ' . $sec . ' bin/composer-update.sh';
- $output->writeln("\t" . $command);
- $resp = shell_exec($command);
- if ($resp) {
- $output->writeln("\tSalida comando.");
- $output->writeln("\t\t" . $resp);
- }
- }
- } catch (Throwable $t) {
- $output->writeln('Se produjo un error. Reestableciendo datos.');
- $command = 'cd ' . $sec . '; git reset';
- $output->writeln("\t" . $command);
- $shell_exec($command);
- }
- } else {
- $output->writeln($sec . " no composer.json found");
- }
- }
- }
- }
|