Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Issue #2
Browse files Browse the repository at this point in the history
- Added test for generate-tools command and refactored the command to make it cleaner.
- Added test for init-command.
  • Loading branch information
percymamedy committed Mar 7, 2019
1 parent 5b95b16 commit 00908b9
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 195 deletions.
30 changes: 7 additions & 23 deletions app/Commands/GenerateCommandLineTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace App\Commands;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Storage;
use App\Support\Artifacts\Containers;
use LaravelZero\Framework\Commands\Command;
use App\Support\Concerns\CommandResponsable;

class GenerateCommandLineTools extends Command
{
use CommandResponsable;

/**
* The signature of the command.
*
Expand All @@ -29,26 +31,8 @@ class GenerateCommandLineTools extends Command
*/
public function handle()
{
// Copy file to Working directory.
Storage::disk('work_dir')->put('containers', $this->getContainersFileContents());

// Make file executed.
app(Filesystem::class)->chmod(
Storage::disk('work_dir')->path('containers'), 0755
);

$this->info('containers script generated!');

return true;
}

/**
* Get the command line tools content.
*
* @return string
*/
protected function getContainersFileContents(): string
{
return file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'containers.stub');
return Containers::make()->save() ?
$this->sendSuccessResponse('containers script generated!') :
$this->sendErrorResponse('Could generate containers script!');
}
}
18 changes: 18 additions & 0 deletions app/Support/Artifacts/AbstractArtifact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\Support\Artifacts;

abstract class AbstractArtifact implements Artifact
{
/**
* Create a new instance of the Artifact.
*
* @param array $args
*
* @return Artifact
*/
public static function make(...$args): Artifact
{
return new static(...$args);
}
}
23 changes: 23 additions & 0 deletions app/Support/Artifacts/Artifact.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Support\Artifacts;

interface Artifact
{
/**
* Create a new instance of the Artifact.
*
* @param array $args
*
* @return Artifact
*/
public static function make(...$args): Artifact;

/**
* Saves the artifact and return true
* if saved was successful.
*
* @return bool
*/
public function save(): bool;
}
66 changes: 66 additions & 0 deletions app/Support/Artifacts/Containers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Support\Artifacts;

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Storage;

class Containers extends AbstractArtifact
{
/**
* The path of the stub file.
*
* @var string
*/
protected $stub;

/**
* Containers constructor.
*
* @param string $stubPath
*/
public function __construct(string $stubPath = null)
{
$this->stub = $stubPath ?? $this->stubDefaultPath();
}

/**
* Saves the artifact and return true
* if saved was successful.
*
* @return bool
*/
public function save(): bool
{
// Copy file to Working directory.
$wasSaved = Storage::disk('work_dir')->put('containers', $this->stubContents());

// Make file executed.
app(Filesystem::class)->chmod(
Storage::disk('work_dir')->path('containers'), 0755
);

return $wasSaved;
}

/**
* Get the command line tools content.
*
* @return string
*/
protected function stubContents(): string
{
return file_get_contents($this->stub);
}

/**
* Get the default path where containers stub is
* stored.
*
* @return string
*/
protected function stubDefaultPath(): string
{
return app_path('Commands' . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'containers.stub');
}
}
38 changes: 38 additions & 0 deletions app/Support/Concerns/CommandResponsable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Support\Concerns;

trait CommandResponsable
{
/**
* Show a sucess message and the exit the program.
*
* @param string $message
*
* @return bool
*/
protected function sendSuccessResponse(string $message = null): bool
{
$message = $message ?? 'Command successfully ran!';

$this->info($message);

return true;
}

/**
* Show an error message and then exits the program.
*
* @param string $message
*
* @return bool
*/
protected function sendErrorResponse(string $message = null): bool
{
$message = $message ?? 'Command failed !';

$this->error($message);

return false;
}
}
27 changes: 27 additions & 0 deletions app/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,33 @@ function home_dir()
}
}

if (!function_exists('work_dir')) {
/**
* Return the project directory.
*
* @return string
*/
function work_dir()
{
return getenv('APP_ENV') === 'testing' ?
realpath(__DIR__ . '/../../tests/fixtures/work_dir') :
getcwd();
}
}

if (!function_exists('docker_dir')) {
/**
* Return the directory in which all
* docker assets will be saved.
*
* @return string
*/
function docker_dir()
{
return work_dir() . DIRECTORY_SEPARATOR . '.docker';
}
}

if (!function_exists('ddd')) {
/**
* Advanced dumped and die.
Expand Down
Loading

0 comments on commit 00908b9

Please sign in to comment.