예제 #1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if (!$input->getOption('command')) {
         $output->writeln("<error>Missing required option: --command</error>");
         return 1;
     }
     $statusCode = 0;
     if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
         $output->writeln("<info>[[ Finding repositories ]]</info>");
     }
     $scanner = new \GitScan\GitRepoScanner();
     $gitRepos = $scanner->scan($input->getArgument('path'));
     foreach ($gitRepos as $gitRepo) {
         /** @var \GitScan\GitRepo $gitRepo */
         if (!$gitRepo->matchesStatus($input->getOption('status'))) {
             continue;
         }
         $topLevel = $this->fs->findFirstParent($gitRepo->getPath(), $input->getArgument('path'));
         if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
             $output->writeln("<info>[[ {$gitRepo->getPath()} ]]</info>");
         }
         $process = new \Symfony\Component\Process\Process($input->getOption('command'));
         $process->setWorkingDirectory($gitRepo->getPath());
         // $process->setEnv(...); sucks in Debian/Ubuntu
         putenv("path=" . $this->fs->makePathRelative($gitRepo->getPath(), $topLevel));
         putenv("toplevel=" . $topLevel);
         $errorOutput = $output;
         if (is_callable($output, 'getErrorOutput') && $output->getErrorOutput()) {
             $errorOutput = $output->getErrorOutput();
         }
         $process->run(function ($type, $buffer) use($output, $errorOutput) {
             if (\Symfony\Component\Process\Process::ERR === $type) {
                 if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
                     $errorOutput->write("<error>STDERR</error> ");
                 }
                 $errorOutput->write($buffer, FALSE, OutputInterface::OUTPUT_RAW);
             } else {
                 if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
                     $output->write("<comment>STDOUT</comment> ");
                 }
                 $output->write($buffer, FALSE, OutputInterface::OUTPUT_RAW);
             }
         });
         if (!$process->isSuccessful()) {
             $errorOutput->writeln("<error>[[ {$gitRepo->getPath()}: exit code = {$process->getExitCode()} ]]</error>");
             $statusCode = 2;
         }
     }
     putenv("path");
     putenv("toplevel");
     return $statusCode;
 }