Skip to content

Commit

Permalink
Moving a pice of code to a better home
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jun 17, 2014
1 parent 583e918 commit d624cbb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
40 changes: 40 additions & 0 deletions src/Console/Command/Task/BakeTask.php
Expand Up @@ -101,6 +101,46 @@ public function main() {
}
}

/**
* Executes an external shell command and pipes its output to the stdout
*
* @param string $command the command to execute
* @return void
* @throws \RuntimeExeception if any errors occurred during the execution
*/
public function callProcess($command) {
$descriptorSpec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w']
];
$this->_io->verbose('Running ' . $command);
$process = proc_open(
$command,
$descriptorSpec,
$pipes
);
if (!is_resource($process)) {
$this->error(__d('cake_console', 'Could not start subprocess.'));
return false;
}
$output = $error = '';
fclose($pipes[0]);

$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);

$error = stream_get_contents($pipes[2]);
fclose($pipes[2]);
proc_close($process);

if ($error) {
throw new \RuntimeException($error);
}

$this->out($output);
}

/**
* Handles splitting up the plugin prefix and classname.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Command/Task/PluginTask.php
Expand Up @@ -249,7 +249,7 @@ protected function _modifyAutoloader($plugin, $path) {
try {
$command = 'cd ' . escapeshellarg($path) . '; ';
$command .= 'php ' . escapeshellarg($composer) . ' dump-autoload ';
$this->Project->callComposer($command);
$this->callProcess($command);
} catch (\RuntimeException $e) {
$error = $e->getMessage();
$this->error(__d('cake_console', 'Could not run `composer dump-autoload`: %s', $error));
Expand Down
35 changes: 1 addition & 34 deletions src/Console/Command/Task/ProjectTask.php
Expand Up @@ -147,7 +147,7 @@ public function bake($path) {
$command = 'php ' . escapeshellarg($composer) . ' create-project -s dev cakephp/app ' . escapeshellarg($path);

try {
$this->callComposer($command);
$this->callProcess($command);
} catch (\RuntimeException $e) {
$error = $e->getMessage();
$this->error(__d('cake_console', 'Installation from packagist.org failed with: %s', $error));
Expand All @@ -157,39 +157,6 @@ public function bake($path) {
return true;
}

public function callComposer($command) {
$descriptorSpec = [
0 => ['pipe', 'r'],
1 => ['pipe', 'w'],
2 => ['pipe', 'w']
];
$this->_io->verbose('Running ' . $command);
$process = proc_open(
$command,
$descriptorSpec,
$pipes
);
if (!is_resource($process)) {
$this->error(__d('cake_console', 'Could not start subprocess.'));
return false;
}
$output = $error = '';
fclose($pipes[0]);

$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);

$error = stream_get_contents($pipes[2]);
fclose($pipes[2]);
proc_close($process);

if ($error) {
throw new \RuntimeException($error);
}

$this->out($output);
}

/**
* get the option parser.
*
Expand Down

0 comments on commit d624cbb

Please sign in to comment.