/** * Runs a process and returns the output. * * @param Process $process * @return string */ public function run(Process $process, $returnOutput = true) { $process->mustRun(); if ($returnOutput) { return $this->getProcessOutput($process); } }
/** * @param Process $process * @param bool $mustRun * @param bool $quiet * * @return int|string * @throws \Exception */ protected function runProcess(Process $process, $mustRun = false, $quiet = true) { $this->output->writeln("Running command: <info>" . $process->getCommandLine() . "</info>", OutputInterface::VERBOSITY_VERBOSE); try { $process->mustRun($quiet ? null : function ($type, $buffer) { $this->output->write(preg_replace('/^/m', ' ', $buffer)); }); } catch (ProcessFailedException $e) { if (!$mustRun) { return $process->getExitCode(); } // The default for ProcessFailedException is to print the entire // STDOUT and STDERR. But if $quiet is disabled, then the user will // have already seen the command's output. So we need to re-throw // the exception with a much shorter message. $message = "The command failed with the exit code: " . $process->getExitCode(); $message .= "\n\nFull command: " . $process->getCommandLine(); if ($quiet) { $message .= "\n\nError output:\n" . $process->getErrorOutput(); } throw new \Exception($message); } $output = $process->getOutput(); return $output ? rtrim($output) : true; }
/** * Runs the given string as a command and returns the resulting output. * The CWD is set to the root project directory to simplify command paths. * * @param string $command * * @return string * * @throws ProcessFailedException in case the command execution is not successful */ private function runCommand($command, $workingDirectory = null) { $process = new Process($command); $process->setWorkingDirectory($workingDirectory ?: $this->rootDir); $process->mustRun(); return $process->getOutput(); }
/** * Execute the console command. * * @return mixed */ public function handle() { $this->config->load($this->argument('conversion')); $command = $this->commandStringBuilder->build($this->config); $outputPath = $this->config->outputPath(); if (file_exists($outputPath)) { @unlink($outputPath); } if ($this->config->debug()) { $this->output->block('[DEBUG] Running command: ' . $command); } if (empty(shell_exec("which mysqldump"))) { throw new MissingDependency('mysqldump is not available'); } if (is_executable($this->config->converterExecutable()) === false) { $process = new Process('chmod +x ' . $this->config->converterExecutable()); $process->run(); } $process = new Process($command); $outputFilter = $this->outputFilter; $process->mustRun(function ($type, $output) use($outputFilter) { // some things are expected, so we'll hide that // to avoid misleading warnings/errors $output = $outputFilter->filter($output); if ($output != null) { if (Process::ERR === $type) { echo 'ERR > ' . $output; } else { echo 'OUT > ' . $output; } } }); $this->output->success('Dump created at ' . $outputPath); }
/** * @param string $name * @param array $arguments * * @throws GulpBadTaskConfigurationException * @throws GulpUndefinedTaskException */ public static function __callStatic($name, $arguments) { $taskName = Inflector::camelCaseToDaseCase($name); /** @var Event $event */ list($event) = $arguments; $gulpTasks = self::getOption('gulp-tasks', $event); if (!isset($gulpTasks[$taskName])) { throw new GulpUndefinedTaskException(sprintf('Gulp %s task is not defined.', $taskName)); } // get gulp options $gulpBin = self::getOption('gulp-bin', $event); $logPrepend = self::getOption('gulp-log-prepend', $event); // get task options $taskOptions = $gulpTasks[$taskName]; if (empty($taskOptions['command'])) { throw new GulpBadTaskConfigurationException(sprintf('Gulp %s task command option must be defined.', $taskName)); } $command = [$gulpBin, $taskOptions['command'], isset($taskOptions['params']) ? $taskOptions['params'] : '']; $failOnError = empty($taskOptions['fail-on-error']) ? false : $taskOptions['fail-on-error']; if (!$failOnError) { $command[] = '|| true'; } // execute process $process = new Process(implode(' ', $command), null, null, null, null, []); $process->mustRun(function ($type, $buffer) use($event, $logPrepend) { self::writeProcessBuffer($type, $buffer, $event, $logPrepend); }); }
public function mustRun(array $commands, $timeout = 60) { $command_format = <<<SHELL echo {{ESCAPED_COMMAND}} {{COMMAND}} SHELL; $formatted_commands = $this->formatCommands($commands, $command_format); $shell = <<<SHELL set -e set -u {$formatted_commands} SHELL; $log_service = $this->log_service; $process = new Process($shell); $process->setTimeout($timeout); $process->setIdleTimeout($timeout); $process->mustRun(function ($type, $buffer) use($log_service) { if (Process::ERR === $type) { $this->log_service->error($buffer); } else { $this->log_service->info($buffer); } }); }
/** * @param Event $event */ public static function report(Event $event) { $bin = self::getOption('phpcpd-bin', $event); $include = self::getOption('phpcpd-include', $event); $exclude = self::getOption('phpcpd-exclude', $event); $xmlReportFile = self::getOption('phpcpd-xml-report-file', $event); $minLines = self::getOption('phpcpd-min-lines', $event); $minTokens = self::getOption('phpcpd-min-tokens', $event); $failOnError = self::getOption('phpcpd-fail-on-error', $event); $logPrepend = self::getOption('phpcpd-log-prepend', $event); $command = array($bin, '--no-interaction', '--min-lines ' . $minLines, '--min-tokens ' . $minTokens); if ($xmlReportFile) { $command[] = '--log-pmd ' . self::getReportResultsPath($event) . '/' . $xmlReportFile; } if ($exclude) { $command[] = '--exclude ' . implode(' ', $exclude); } $command[] = implode(' ', $include); if (!$failOnError) { $command[] = '|| true'; } $process = new Process(implode(' ', $command), null, null, null, null, []); $process->mustRun(function ($type, $buffer) use($event, $logPrepend) { self::writeProcessBuffer($type, $buffer, $event, $logPrepend); }); }
public function mustRun($callback = null) { parent::mustRun($callback); // Check for problems with output. $this->checkOutputForProblems(); return $this; }
/** * @param string $path * @param Event $event */ protected static function runFix($path, Event $event) { $bin = self::getOption('phpcsfixer-bin', $event); $failOnError = self::getOption('phpcsfixer-fail-on-error', $event); $logPrepend = self::getOption('phpcsfixer-log-prepend', $event); $level = self::getOption('phpcsfixer-level', $event); $fixers = self::getOption('phpcsfixer-fixers', $event); $config = self::getOption('phpcsfixer-config', $event); $dryRun = self::getOption('phpcsfixer-dry-run', $event); $command = array($bin, 'fix', $path); if ($level) { $command[] = '--level=' . $level; } if (!empty($fixers) && is_array($fixers)) { $command[] = '--fixers=' . implode(',', $fixers); } if ($config) { $command[] = '--config=' . $config; } if ($dryRun === true) { $command[] = '--dry-run'; } if (!$failOnError) { $command[] = '|| true'; } $process = new Process(implode(' ', $command), null, null, null, null, []); $process->mustRun(function ($type, $buffer) use($event, $logPrepend) { self::writeProcessBuffer($type, $buffer, $event, $logPrepend); }); }
/** * @param Event $event */ public static function report(Event $event) { $bin = self::getOption('phpcs-bin', $event); $include = self::getOption('phpcs-include', $event); $exclude = self::getOption('phpcs-exclude', $event); $report = self::getOption('phpcs-report', $event); $xmlReportFile = self::getOption('phpcs-xml-report-file', $event); $standard = self::getOption('phpcs-standard', $event); $failOnError = self::getOption('phpcs-fail-on-error', $event); $logPrepend = self::getOption('phpcs-log-prepend', $event); $command = array($bin, '--report=' . $report, '--standard=' . $standard); if ($xmlReportFile) { $command[] = '--report-file=' . self::getReportResultsPath($event) . '/' . $xmlReportFile; } if ($exclude) { $command[] = implode(' ', array_map(function ($element) { return '--ignore=' . $element; }, $exclude)); } $command[] = implode(' ', $include); if (!$failOnError) { $command[] = '|| true'; } $process = new Process(implode(' ', $command), null, null, null, null, []); $process->mustRun(function ($type, $buffer) use($event, $logPrepend) { self::writeProcessBuffer($type, $buffer, $event, $logPrepend); }); }
/** * * @param Project $project * @return string * @throws DeployException */ public function deploy(Project $project) { $path = sys_get_temp_dir() . '/AwesomeDeployer'; $this->filesystem->mkdir($path); chdir($path); $process = new Process('git clone ' . $project->getRepo() . ' project'); $process->mustRun(); $out = $process->getOutput(); $pathProject = $path . '/project'; chdir($pathProject); $script = $project->getScript(); if ($script !== null) { $script = new Process($script); $script->mustRun(); $out .= $script->getOutput(); } switch ($project->getStrategy()) { case Project::STRATEGY_FABRIC: $command = $project->getCommand(); break; default: throw new DeployException('Strategy not supported'); } $deployment = new Process($command); $deployment->mustRun(); $this->filesystem->remove($path); $out .= $deployment->getOutput(); return $out; }
/** * {@inheritdoc} * * @see PlatformCommand::getCurrentEnvironment() */ protected function execute(InputInterface $input, OutputInterface $output) { /** @var \Symfony\Component\Console\Helper\ProcessHelper $process */ $process = $this->getHelper('process'); $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); if (!is_dir(Platform::rootDir() . '/docker/fg')) { $process->mustRun($output, ['git', 'clone', 'https://github.com/brendangregg/FlameGraph.git', Platform::rootDir() . '/docker/fg']); } if (!is_dir(Platform::rootDir() . '/docker/xhpfg')) { $process->mustRun($output, ['git', 'clone', 'https://github.com/msonnabaum/xhprof-flamegraphs.git', Platform::rootDir() . '/docker/xhpfg']); } $stack = StacksFactory::getStack(Platform::webDir()); switch ($stack->type()) { case Stacks\Drupal::TYPE: $this->stdOut->writeln("<comment>Patching Drupal for xhprof</comment>"); $patchProcess = new Process('patch -p1 < ' . CLI_ROOT . '/resources/drupal-enable-profiling.patch', Platform::webDir()); break; case Stacks\WordPress::TYPE: $this->stdOut->writeln("<comment>Patching WordPress for xhprof</comment>"); $patchProcess = new Process('patch -p0 < ' . CLI_ROOT . '/resources/wordpress-enable-profiling.patch', Platform::webDir()); break; default: throw new \Exception('Stack type not supported yet.'); } $patchProcess->mustRun(); }
/** * @param Event $event */ public static function check(Event $event) { $logPrepend = self::getOption('security-checker-log-prepend', $event); $process = new Process(self::getOption('security-checker-bin', $event) . ' security:check composer.lock --ansi'); $process->mustRun(function ($type, $buffer) use($event, $logPrepend) { self::writeProcessBuffer($type, $buffer, $event, $logPrepend); }); }
/** * @return string */ public function zip($file = 'archive.zip') { $temporaryDir = $this->createTmpDirectory(); $this->write($temporaryDir); $command = sprintf('zip -r "%s" "%s"', $file, $temporaryDir); $process = new Process($command); $process->mustRun(); }
public function do() { $commands = $this->commands(); if (!empty($commands)) { $process = new Process(implode("\n", $commands)); $process->mustRun(); $this->io->writeln($process->getOutput()); } }
/** * @param Event $event */ public static function addAll(Event $event) { $logPrepend = self::getOption('git-log-prepend', $event); // execute process $process = new Process('git add . -A'); $process->mustRun(function ($type, $buffer) use($event, $logPrepend) { self::writeProcessBuffer($type, $buffer, $event, $logPrepend); }); }
public function runCommands() { $commands = implode('; ', $this->getCommands()); $process = new Process($commands, $this->getPath()); $process->setTimeout(600); $process->setIdleTimeout(null); $this->logger->info('Running "' . $process->getCommandLine() . '"'); $process->mustRun(); }
/** * @return bool */ public function run() { $command = 'vendor/bin/console setup:install'; $this->info('Run ' . $command); $process = new Process($command); $process->setTimeout(600); $process->mustRun(function ($type, $buffer) { $this->info($buffer); }); }
/** * {@inheritdoc} */ public function run($command, $mustSucceed = true) { $process = new Process($command); if ($mustSucceed) { $process->setTimeout(null); return $process->mustRun(); } $process->run(); return $process; }
private function realExec($cmd, $throw_exception, $cwd) { $process = new Process($cmd, $cwd); if ($throw_exception) { $process->mustRun(); } else { $process->run(); } return $process->getOutput(); }
/** * {@inheritdoc} */ public function run(Process $process, $mustSucceed = true) { $this->userInteraction->write('<info>RUN</info> ' . $process->getCommandLine()); if ($mustSucceed) { $process->setTimeout(null); return $process->mustRun($this->getRunningProcessCallback()); } $process->run($this->getRunningProcessCallback()); return $process; }
/** * Performs an obscure check with the given password * * @param string $password * @throws ProcessFailedException */ public function check($password) { if (empty($password)) { throw new InvalidArgumentException('The password cannot be empty'); } $process = new Process($this->command); $process->setInput($password); $process->mustRun(); return $this->verifyOutput($process->getOutput()); }
/** * @return void */ public function run() { $command = 'vendor/bin/console collector:storage:export'; $this->info('Run ' . $command); $process = new Process($command); $process->setTimeout(600); $process->mustRun(function ($type, $buffer) { $this->info($buffer); }); }
/** * Runs a ``Process``. * * @param Process $process * * @return string * * @throws ProcessFailedException */ protected function runProcess(Process $process) { try { $process->mustRun(); $message = $process->getOutput(); } catch (ProcessFailedException $e) { $message = '<error>' . $e->getMessage() . '</error>'; } return $message; }
/** * Execute the command. * * @param InputInterface $input * @param OutputInterface $output * * @return void */ public function execute(InputInterface $input, OutputInterface $output) { $process = new Process('docker-compose ps'); try { $process->mustRun(); $output->writeln($process->getOutput()); } catch (ProcessFailedException $e) { $output->writeln('<error>' . $e->getMessage() . '</error>'); return; } }
/** * Execute the convert command. * * @param string $size * @param string $path */ protected function convert($size, $path) { $path = $this->outputPath($path); $this->comment("Converting {$path}"); if (!$this->isDryRun()) { $command = "convert -geometry {$size} {$this->baseFavicon} {$path}"; $process = new Process($command); $process->mustRun(); $this->output->write($process->getOutput()); } }
public function getFacts() { $hostname = $fqdn = gethostname(); try { $process = new Process('hostname --fqdn'); $process->mustRun(); $fqdn = trim($process->getOutput()); } catch (ProcessFailedException $e) { } return array('fqdn' => $fqdn, 'hostname' => gethostname()); }
public function testCommit() { $fs = new Filesystem(); for ($i = 1; $i < 10; ++$i) { $fs->dumpFile('commit-file-' . $i, 'test commit file'); } $process = new Process('git add commit-file-1'); $process->disableOutput(); $process->mustRun(); static::assertTrue($this->object->commit('Commit file name commit-file-1')); }
/** * @param string $branch * * @return bool */ protected function displayOrExecute(InputInterface $input, OutputInterface $output, $branch) { $command = 'git branch -D %s'; if ($input->getOption('force')) { $output->write('<question>Executing</question>: '); $process = new Process(sprintf($command, $branch)); $process->mustRun(); } $output->writeln(sprintf($command, '<info>' . $branch . '</info>')); return true; }
/** * @param string $commandLine * @param int|null $timeout * * @throws RuntimeException * * @return bool */ private function runCommandSilently(string $commandLine, $timeout = 60) : bool { try { $process = new Process($commandLine); $process->setTimeout($timeout); $process->disableOutput(); $process->mustRun(); } catch (Exception $exception) { throw new RuntimeException($exception->getMessage(), 0, $exception); } return $process->isSuccessful(); }