Skip to content

Commit

Permalink
DRY up shell process code.
Browse files Browse the repository at this point in the history
  • Loading branch information
franzliedke committed Mar 17, 2015
1 parent da992ed commit 2e75d9a
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 94 deletions.
9 changes: 3 additions & 6 deletions bin/studio
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@ use Studio\Config\FileStorage;
use Studio\Console\CreateCommand;
use Studio\Console\LoadCommand;
use Studio\Console\ScrapCommand;
use Studio\Shell\TaskRunner;
use Symfony\Component\Console\Application;

$studioFile = getcwd().'/studio.json';
$config = new Config(new FileStorage($studioFile));

$shell = new TaskRunner();

$application = new Application();
$application->add(new CreateCommand($config, $shell));
$application->add(new LoadCommand($config, $shell));
$application->add(new ScrapCommand($config, $shell));
$application->add(new CreateCommand($config));
$application->add(new LoadCommand($config));
$application->add(new ScrapCommand($config));
$application->run();
26 changes: 5 additions & 21 deletions src/Console/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Studio\Console;

use Studio\Parts\ConsoleInput;
use Studio\Shell\TaskRunner;
use Studio\Shell\Shell;
use Studio\Config\Config;
use Studio\Creator\CreatorInterface;
use Studio\Creator\GitRepoCreator;
Expand All @@ -20,8 +20,6 @@ class CreateCommand extends Command

protected $config;

protected $shell;

protected $partClasses = [
'Studio\Parts\Base\Part',
'Studio\Parts\Composer\Part',
Expand All @@ -36,12 +34,11 @@ class CreateCommand extends Command
protected $partInput;


public function __construct(Config $config, TaskRunner $shell)
public function __construct(Config $config)
{
parent::__construct();

$this->config = $config;
$this->shell = $shell;
}

protected function configure()
Expand Down Expand Up @@ -75,27 +72,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("<info>Package directory $path created.</info>");

$output->writeln("<comment>Running composer install for new package...</comment>");
$this->runOnShell('composer install --prefer-dist', $package->getPath());
Shell::run('composer install --prefer-dist', $package->getPath());
$output->writeln("<info>Package successfully created.</info>");

$output->writeln("<comment>Dumping autoloads...</comment>");
$this->runOnShell('composer dump-autoload');
Shell::run('composer dump-autoload');
$output->writeln("<info>Autoloads successfully generated.</info>");
}

protected function runOnShell($task, $workDir = null)
{
$process = $this->shell->process($task, $workDir);
$process->run();

if (! $process->isSuccessful()) {
$error = $process->getErrorOutput();
throw new \RuntimeException("Error while running Composer: $error");
}

return $process->getOutput();
}

/**
* Build a package creator from the given input options.
*
Expand All @@ -107,7 +91,7 @@ protected function makeCreator(InputInterface $input)
$path = $input->getArgument('path');

if ($input->getOption('git')) {
return new GitRepoCreator($input->getOption('git'), $path, $this->shell);
return new GitRepoCreator($input->getOption('git'), $path);
} else {
$creator = new SkeletonCreator($path);
$this->installParts($creator);
Expand Down
23 changes: 3 additions & 20 deletions src/Console/LoadCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Studio\Console;

use Studio\Package;
use Studio\Shell\TaskRunner;
use Studio\Config\Config;
use Studio\Shell\Shell;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -17,15 +17,12 @@ class LoadCommand extends Command

protected $config;

protected $shell;


public function __construct(Config $config, TaskRunner $shell)
public function __construct(Config $config)
{
parent::__construct();

$this->config = $config;
$this->shell = $shell;
}

protected function configure()
Expand All @@ -48,22 +45,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("<info>Package loaded successfully.</info>");

$output->writeln("<comment>Dumping autoloads...</comment>");
$this->runOnShell('composer dump-autoload');
Shell::run('composer dump-autoload');
$output->writeln("<info>Autoloads successfully generated.</info>");
}

protected function runOnShell($task, $workDir = null)
{
$process = $this->shell->process($task, $workDir);
$process->run();

if (! $process->isSuccessful()) {
$command = collect(explode(' ', $task))->first();
$error = $process->getErrorOutput();
throw new \RuntimeException("Error while running $command: $error");
}

return $process->getOutput();
}

}
22 changes: 3 additions & 19 deletions src/Console/ScrapCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use League\Flysystem\Filesystem;
use Studio\Package;
use Studio\Config\Config;
use Studio\Shell\TaskRunner;
use Studio\Shell\Shell;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -19,15 +19,12 @@ class ScrapCommand extends Command

protected $config;

protected $shell;


public function __construct(Config $config, TaskRunner $shell)
public function __construct(Config $config)
{
parent::__construct();

$this->config = $config;
$this->shell = $shell;
}

protected function configure()
Expand Down Expand Up @@ -60,23 +57,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$output->writeln("<info>Package successfully removed.</info>");

$output->writeln("<comment>Dumping autoloads...</comment>");
$this->runOnShell('composer dump-autoload');
Shell::run('composer dump-autoload');
$output->writeln("<info>Autoloads successfully generated.</info>");
}

protected function runOnShell($task, $workDir = null)
{
$process = $this->shell->process($task, $workDir);
$process->run();

if (! $process->isSuccessful()) {
$error = $process->getErrorOutput();
throw new \RuntimeException("Error while running Composer: $error");
}

return $process->getOutput();
}

protected function abortDeletion($path, OutputInterface $output)
{
$dialog = $this->getHelper('dialog');
Expand Down
13 changes: 3 additions & 10 deletions src/Creator/GitRepoCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Studio\Creator;

use Studio\Package;
use Studio\Shell\TaskRunner;
use Studio\Shell\Shell;

class GitRepoCreator implements CreatorInterface
{
Expand All @@ -12,17 +12,11 @@ class GitRepoCreator implements CreatorInterface

protected $path;

/**
* @var TaskRunner
*/
protected $shell;


public function __construct($repo, $path, TaskRunner $shell)
public function __construct($repo, $path)
{
$this->repo = $repo;
$this->path = $path;
$this->shell = $shell;
}

/**
Expand All @@ -39,8 +33,7 @@ public function create()

protected function cloneRepository()
{
$this->shell->process("git clone $this->repo $this->path")
->run();
Shell::run("git clone $this->repo $this->path");
}

}
26 changes: 26 additions & 0 deletions src/Shell/Shell.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Studio\Shell;

use Symfony\Component\Process\Process;

class Shell
{

public static function run($task, $directory = null)
{
$process = new Process("$task", $directory);
$process->setTimeout(3600);

$process->run();

if (! $process->isSuccessful()) {
$command = collect(explode(' ', $task))->first();
$error = $process->getErrorOutput();
throw new \RuntimeException("Error while running $command: $error");
}

return $process->getOutput();
}

}
18 changes: 0 additions & 18 deletions src/Shell/TaskRunner.php

This file was deleted.

0 comments on commit 2e75d9a

Please sign in to comment.