Skip to content
This repository has been archived by the owner on Sep 5, 2020. It is now read-only.

Commit

Permalink
Remove some duplicated code
Browse files Browse the repository at this point in the history
  • Loading branch information
Anahkiasen committed Mar 31, 2014
1 parent 1a4cb44 commit 02e2eac
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 56 deletions.
51 changes: 26 additions & 25 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

59 changes: 40 additions & 19 deletions src/Rocketeer/Traits/BashModules/Binaries.php
Expand Up @@ -34,40 +34,41 @@ public function php($command = null)
return trim($php. ' ' .$command);
}

// Artisan
////////////////////////////////////////////////////////////////////

/**
* Prefix a command with the right path to Composer
* Prefix a command with the right path to Artisan
*
* @param string $command
* @param array $flags
*
* @return string
*/
public function composer($command = null)
public function artisan($command = null, $flags = array())
{
$composer = $this->which('composer', $this->releasesManager->getCurrentReleasePath().'/composer.phar');

// Prepend PHP command
if (strpos($composer, 'composer.phar') !== false) {
$composer = $this->php($composer);
$artisan = $this->which('artisan') ?: 'artisan';
foreach ($flags as $name => $value) {
$command .= ' --'.$name;
$command .= $value ? '="' .$value. '"' : '';
}

return trim($composer. ' ' .$command);
return $this->php($artisan. ' ' .$command);
}

// Artisan
////////////////////////////////////////////////////////////////////

/**
* Prefix a command with the right path to Artisan
* Run an artisan command
*
* @param string $command
* @param array $flags
*
* @return string
*/
public function artisan($command = null)
public function runArtisan($command = null, $flags = array())
{
$artisan = $this->which('artisan') ?: 'artisan';
$command = $this->artisan($command, $flags);

return $this->php($artisan. ' ' .$command);
return $this->runForCurrentRelease($command);
}

/**
Expand All @@ -79,10 +80,10 @@ public function artisan($command = null)
*/
public function runMigrations($seed = false)
{
$seed = $seed ? ' --seed' : null;
$this->command->comment('Running outstanding migrations');
$flags = $seed ? array('seed' => '') : array();

return $this->runForCurrentRelease($this->artisan('migrate'.$seed));
return $this->runArtisan('migrate', $flags);
}

/**
Expand All @@ -94,9 +95,10 @@ public function runMigrations($seed = false)
*/
public function seed($class = null)
{
$class = $class ? ' --class="'.$class.'"' : null;
$this->command->comment('Seeding database');
$flags = $class ? array('class' => $class) : array();

return $this->runForCurrentRelease($this->artisan('db:seed'.$class));
return $this->runArtisan('db:seed', $flags);
}

// PHPUnit
Expand Down Expand Up @@ -129,6 +131,25 @@ public function runTests($arguments = null)
// Composer
////////////////////////////////////////////////////////////////////

/**
* Prefix a command with the right path to Composer
*
* @param string $command
*
* @return string
*/
public function composer($command = null)
{
$composer = $this->which('composer', $this->releasesManager->getCurrentReleasePath().'/composer.phar');

// Prepend PHP command
if (strpos($composer, 'composer.phar') !== false) {
$composer = $this->php($composer);
}

return trim($composer. ' ' .$command);
}

/**
* Run Composer on the folder
*
Expand Down
37 changes: 25 additions & 12 deletions src/Rocketeer/Traits/BashModules/Filesystem.php
Expand Up @@ -54,12 +54,7 @@ public function symlink($folder, $symlink)
*/
public function move($origin, $destination)
{
$folder = dirname($destination);
if (!$this->fileExists($folder)) {
$this->createFolder($folder, true);
}

return $this->run(sprintf('mv %s %s', $origin, $destination));
return $this->fromTo('mv', $origin, $destination);
}

/**
Expand All @@ -72,12 +67,7 @@ public function move($origin, $destination)
*/
public function copy($origin, $destination)
{
$folder = dirname($destination);
if (!$this->fileExists($folder)) {
$this->createFolder($folder, true);
}

return $this->run(sprintf('cp %s %s', $origin, $destination));
return $this->fromTo('cp', $origin, $destination);
}

/**
Expand Down Expand Up @@ -192,4 +182,27 @@ public function removeFolder($folder = null)
{
return $this->run('rm -rf '.$this->rocketeer->getFolder($folder));
}

////////////////////////////////////////////////////////////////////
/////////////////////////////// HELPERS ////////////////////////////
////////////////////////////////////////////////////////////////////

/**
* Execute a "from/to" style command
*
* @param string $command
* @param string $from
* @param string $to
*
* @return string
*/
protected function fromTo($command, $from, $to)
{
$folder = dirname($to);
if (!$this->fileExists($folder)) {
$this->createFolder($folder, true);
}

return $this->run(sprintf('%s %s %s', $command, $from, $to));
}
}

0 comments on commit 02e2eac

Please sign in to comment.