Skip to content
This repository has been archived by the owner on Feb 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #140 from dbpolito/reset-db_fwd-yaml
Browse files Browse the repository at this point in the history
Add fwd reset-db | Add fwd.yaml
  • Loading branch information
dbpolito authored Jun 25, 2020
2 parents e062964 + b358fb8 commit 8ed82e5
Show file tree
Hide file tree
Showing 14 changed files with 1,130 additions and 297 deletions.
11 changes: 11 additions & 0 deletions app/Builder/Fwd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Builder;

class Fwd extends Builder
{
public function getProgramName() : string
{
return 'fwd';
}
}
21 changes: 19 additions & 2 deletions app/Commands/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function handle()

$this->info('File ".fwd" copied.');
} else {
$this->warn('File ".fwd" already exists, skipping. (to override run again with --force)');
$this->warn('File ".fwd" already exists, skipping. (to override run again with --force).');
}

if ($this->option('force') || ! File::exists($this->environment->getContextDockerCompose())) {
Expand All @@ -70,7 +70,24 @@ public function handle()

$this->info('File "docker-compose.yml" copied.');
} else {
$this->warn('File "docker-compose.yml" already exists, skipping. (to override run again with --force)');
$this->warn('File "docker-compose.yml" already exists, skipping. (to override run again with --force).');
}

if ($this->option('force') || ! File::exists($this->environment->getContextFile('fwd.yaml'))) {
$copied = File::copy(
$this->environment->getDefaultFile('fwd.yaml'),
$this->environment->getContextFile('fwd.yaml')
);

if (false === $copied) {
$this->error('Failed to write local "fwd.yaml" file.');

return 1;
}

$this->info('File "fwd.yaml" copied.');
} else {
$this->warn('File "fwd.yaml" already exists, skipping. (to override run again with --force).');
}

$this->preset();
Expand Down
39 changes: 7 additions & 32 deletions app/Commands/Reset.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Builder\Composer;
use App\Builder\DockerComposeExec;
use App\Builder\Escaped;
use App\Builder\Mysql;
use App\Builder\Fwd;
use App\Builder\RedisCli;
use App\Builder\Yarn;

Expand Down Expand Up @@ -42,9 +42,7 @@ public function handle() : int
$commands = [
[$this, 'composerInstall'],
[$this, 'cacheFlushAll'],
[$this, 'datatabaseDropDatabase'],
[$this, 'databaseCreateDatabase'],
[$this, 'databaseGrantDatabase'],
[$this, 'databaseReset'],
[$this, 'artisanMigrateFresh'],
[$this, 'yarnInstall'],
[$this, 'yarnDev'],
Expand Down Expand Up @@ -79,35 +77,12 @@ protected function cacheFlushAll() : int
});
}

protected function datatabaseDropDatabase() : int
protected function databaseReset() : int
{
return $this->runTask('MySQL Drop Database', function () {
return $this->commandExecutor->runQuietly(Mysql::make(
'-e',
Escaped::make(sprintf('drop database if exists %s', env('DB_DATABASE')))
));
});
}

protected function databaseCreateDatabase() : int
{
return $this->runTask('MySQL Create Database', function () {
return $this->commandExecutor->runQuietly(Mysql::make(
'-e',
Escaped::make(sprintf('create database %s', env('DB_DATABASE')))
));
});
}

protected function databaseGrantDatabase() : int
{
return $this->runTask('MySQL Grant Privileges', function () {
return $this->commandExecutor->runQuietly(Mysql::make(
'-e',
Escaped::make(vsprintf('grant all on %s.* to %s@"%%"', [
env('DB_DATABASE'),
env('DB_USERNAME'),
]))
return $this->runTask('Database Reset', function () {
return $this->commandExecutor->runQuietly(Fwd::make(
'reset-db',
$this->argument('envFile')
));
});
}
Expand Down
78 changes: 78 additions & 0 deletions app/Commands/ResetDb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Commands;

use App\Builder\Escaped;
use App\Builder\Mysql;

class ResetDb extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'reset-db {envFile?}';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Reset DB.';

/**
* Execute the console command.
*
* @return int
*/
public function handle() : int
{
if ($envFile = $this->argument('envFile')) {
$this->environment->overloadEnv(
$this->environment->getContextEnv($envFile)
);
}

$commands = [
[$this, 'databaseDropDatabase'],
[$this, 'databaseCreateDatabase'],
[$this, 'databaseGrantDatabase'],
];

return $this->runCommands($commands);
}

protected function databaseDropDatabase() : int
{
return $this->runTask('MySQL Drop Database', function () {
return $this->commandExecutor->runQuietly(Mysql::make(
'-e',
Escaped::make(sprintf('drop database if exists %s', env('DB_DATABASE')))
));
});
}

protected function databaseCreateDatabase() : int
{
return $this->runTask('MySQL Create Database', function () {
return $this->commandExecutor->runQuietly(Mysql::make(
'-e',
Escaped::make(sprintf('create database %s', env('DB_DATABASE')))
));
});
}

protected function databaseGrantDatabase() : int
{
return $this->runTask('MySQL Grant Privileges', function () {
return $this->commandExecutor->runQuietly(Mysql::make(
'-e',
Escaped::make(vsprintf('grant all on %s.* to %s@"%%"', [
env('DB_DATABASE'),
env('DB_USERNAME'),
]))
));
});
}
}
61 changes: 61 additions & 0 deletions app/Commands/Script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Commands;

use App\Builder\Builder;
use Symfony\Component\Yaml\Yaml;

class Script extends Command
{
/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'script {name}';

/**
* The description of the command.
*
* @var string
*/
protected $description = 'Run fwd script.';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$fwdYaml = Yaml::parseFile(
$this->environment->getContextFile('fwd.yaml'),
Yaml::PARSE_OBJECT_FOR_MAP
);

if (! $script = object_get($fwdYaml, 'scripts.' . $this->argument('name'))) {
$this->error('Script not found.');

return 1;
}

try {
collect($script)
->each(function ($command) {
$exitCode = $this->runTask($command, function () use ($command) {
return $this->commandExecutor->runQuietly(
Builder::make($command)
);
});

if ($exitCode) {
throw new \Exception("Command '{$command}' failed.");
}
});
} catch (\Exception $exception) {
$this->error($exception->getMessage());

return 1;
}
}
}
11 changes: 9 additions & 2 deletions app/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ public function getContextPath(): string

public function getDefaultDockerCompose(string $version): string
{
return sprintf('%s/docker-compose-v%s.yml', $this->getDefaultPath(), $version);
return $this->getDefaultFile(
sprintf('docker-compose-v%s.yml', $version)
);
}

public function getContextDockerCompose(): string
Expand All @@ -122,7 +124,7 @@ public function getContextDockerCompose(): string

public function getDefaultFwd(): string
{
return sprintf('%s/.fwd', $this->getDefaultPath());
return $this->getDefaultFile('.fwd');
}

public function getHomeFwd(): string
Expand All @@ -135,6 +137,11 @@ public function getContextEnv(string $env = '.env'): string
return $this->getContextFile($env);
}

public function getDefaultFile(string $file): string
{
return sprintf('%s/%s', $this->getDefaultPath(), $file);
}

public function getContextFile(string $file): string
{
return sprintf('%s/%s', $this->getContextPath(), $file);
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"illuminate/http": "^7.0",
"illuminate/view": "^7.0",
"laravel-zero/framework": "^7.0",
"padraic/phar-updater": "^1.0.6"
"padraic/phar-updater": "^1.0.6",
"symfony/yaml": "^5.1"
},
"require-dev": {
"mockery/mockery": "1.3.*",
Expand Down
Loading

0 comments on commit 8ed82e5

Please sign in to comment.