8000 Improving UX of Start and Reset by dbpolito · Pull Request #47 · fireworkweb/fwd · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Feb 15, 2021. It is now read-only.

Improving UX of Start and Reset #47

Merged
merged 4 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
10000
Diff view
105 changes: 67 additions & 38 deletions app/Commands/Reset.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use App\Process;
use App\Environment;
use App\Commands\Traits\RunTask;
use App\Commands\Traits\ArtisanCall;
use LaravelZero\Framework\Commands\Command;

class Reset extends Command
{
use ArtisanCall;
use ArtisanCall, RunTask;

/**
* The signature of the command.
Expand Down Expand Up @@ -74,94 +75,122 @@ public function handle(Environment $environment, Process $process)

protected function composerInstall()
{
return $this->artisanCall('composer', ['install']);
return $this->runTask('Composer Install', function () {
return $this->artisanCallNoOutput('composer', ['install']);
});
}

protected function mysqlDropDatabase()
{
return $this->artisanCall('mysql-raw', [
'-e',
sprintf('drop database if exists %s', env('DB_DATABASE')),
]);
return $this->runTask('MySQL Drop Database', function () {
return $this->artisanCallNoOutput('mysql-raw', [
'-e',
sprintf('drop database if exists %s', env('DB_DATABASE')),
]);
});
}

protected function mysqlCreateDatabase()
{
return $this->artisanCall('mysql-raw', [
'-e',
sprintf('create database %s', env('DB_DATABASE')),
]);
return $this->runTask('MySQL Create Database', function () {
return $this->artisanCallNoOutput('mysql-raw', [
'-e',
sprintf('create database %s', env('DB_DATABASE')),
]);
});
}

protected function mysqlGrantDatabase()
{
return $this->artisanCall('mysql-raw', ['-e', sprintf(
'grant all on %s.* to %s@"%%"',
env('DB_DATABASE'),
env('DB_USERNAME')
)]);
return $this->runTask('MySQL Grant Privileges', function () {
return $this->artisanCallNoOutput('mysql-raw', ['-e', sprintf(
'grant all on %s.* to %s@"%%"',
env('DB_DATABASE'),
env('DB_USERNAME')
)]);
});
}

protected function artisanMigrateFresh(Environment $environment, Process $process)
{
return $process->dockerComposeExec(
sprintf('-e DB_DATABASE=%s', env('DB_DATABASE')),
sprintf('-e DB_USERNAME=%s', env('DB_USERNAME')),
sprintf('-e DB_PASSWORD=%s', env('DB_PASSWORD')),
'app php artisan migrate:fresh'
);
return $this->runTask('Migrate Fresh', function () use ($process) {
return $process->dockerComposeExecNoOutput(
sprintf('-e DB_DATABASE=%s', env('DB_DATABASE')),
sprintf('-e DB_USERNAME=%s', env('DB_USERNAME')),
sprintf('-e DB_PASSWORD=%s', env('DB_PASSWORD')),
'app php artisan migrate:fresh'
);
});
}

protected function artisanMigrateFreshSeed(Environment $environment, Process $process)
{
return $process->dockerComposeExec(
sprintf('-e DB_DATABASE=%s', env('DB_DATABASE')),
sprintf('-e DB_USERNAME=%s', env('DB_USERNAME')),
sprintf('-e DB_PASSWORD=%s', env('DB_PASSWORD')),
'app php artisan migrate:fresh --seed'
);
return $this->runTask('Migrate Fresh Seed', function () use ($process) {
return $process->dockerComposeExecNoOutput(
sprintf('-e DB_DATABASE=%s', env('DB_DATABASE')),
sprintf('-e DB_USERNAME=%s', env('DB_USERNAME')),
sprintf('-e DB_PASSWORD=%s', env('DB_PASSWORD')),
'app php artisan migrate:fresh --seed'
);
});
}

protected function yarnInstall()
{
return $this->artisanCall('yarn', ['install']);
return $this->runTask('Yarn Install', function () {
return $this->artisanCallNoOutput('yarn', ['install']);
});
}

protected function yarnDev()
{
return $this->artisanCall('yarn', ['dev']);
return $this->runTask('Yarn Dev', function () {
return $this->artisanCallNoOutput('yarn', ['dev']);
});
}

protected function clearCompiled()
{
return $this->artisanCall('artisan', ['clear-compiled']);
return $this->runTask('Clear Compiled', function () {
return $this->artisanCallNoOutput('artisan', ['clear-compiled']);
});
}

protected function clearCache()
{
return $this->artisanCall('artisan', ['cache:clear']);
return $this->runTask('Clear Cache', function () {
return $this->artisanCallNoOutput('artisan', ['cache:clear']);
});
}

protected function clearConfig()
{
return $this->artisanCall('artisan', ['config:clear']);
return $this->runTask('Clear Config', function () {
return $this->artisanCallNoOutput('artisan', ['config:clear']);
});
}

protected function clearRoute()
{
return $this->artisanCall('artisan', ['route:clear']);
return $this->runTask('Clear Route', function () {
return $this->artisanCallNoOutput('artisan', ['route:clear']);
});
}

protected function clearView()
{
return $this->artisanCall('artisan', ['view:clear']);
return $this->runTask('Clear View', function () {
return $this->artisanCallNoOutput('artisan', ['view:clear']);
});
}

protected function clearLogs(Environment $environment, Process $process)
{
return $process->dockerComposeExec(
'app rm -f',
$environment->getContextFile('storage/logs/*.log')
);
return $this->runTask('Clear Logs', function () use ($environment, $process) {
return $process->dockerComposeExecNoOutput(
'app rm -f',
$environment->getContextFile('storage/logs/*.log')
);
});
}
}
73 changes: 69 additions & 4 deletions app/Commands/Start.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

namespace App\Commands;

use App\Process;
use App\Commands\Traits\RunTask;
use App\Commands\Traits\ArtisanCall;
use LaravelZero\Framework\Commands\Command;

class Start extends Command
{
use ArtisanCall, RunTask;

/**
* The signature of the command.
*
* @var string
*/
protected $signature = 'start';
protected $signature = 'start
{--no-wait : Do not wait for Docker and MySQL to become available}
{--timeout=60 : The number of seconds to wait}';

/**
* The description of the command.
Expand All @@ -21,13 +26,73 @@ class Start extends Command
*/
protected $description = 'Start fwd environment containers.';

protected $seconds = 0;

/**
* Execute the console command.
*
* @return mixed
*/
public function handle(Process $process)
public function handle()
{
$commands = [
[$this, 'dockerComposePs'],
[$this, 'dockerComposeUpD'],
[$this, 'mysql'],
];

// Run commands, first that isn't success (0) stops and return that exitCode
foreach ($commands as $command) {
if ($exitCode = call_user_func($command)) {
return $exitCode;
}
}
}

protected function dockerComposePs()
{
return $process->dockerCompose('up', '-d');
return $this->runTask('Checking Docker', function () {
return $this->runCommand(function () {
return $this->artisanCallNoOutput('ps');
});
});
}

protected function dockerComposeUpD()
{
return $this->runTask('Starting fwd', function () {
return $this->artisanCallNoOutput('up', ['-d']);
});
}

protected function mysql()
{
return $this->runTask('Checking MySQL', function () {
return $this->runCommand(function () {
return $this->artisanCallNoOutput('mysql-raw', ['-e', 'SELECT 1']);
});
});
}

protected function runCommand(\Closure $closure)
{
return ! $this->option('no-wait')
? $this->waitForCommand($closure)
: $closure();
}

protected function waitForCommand(\Closure $closure)
{
while ($exitCode = $closure()) {
if ($this->seconds++ > $this->option('timeout')) {
$this->error('Timed out waiting the command to finish');

return 1;
}

sleep(1);
}

return $exitCode;
}
}
11 changes: 11 additions & 0 deletions app/Commands/Traits/ArtisanCall.php
AE20
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Commands\Traits;

use App\Process;
use Illuminate\Support\Facades\Artisan;
use LaravelZero\Framework\Providers\CommandRecorder\CommandRecorderRepository;

Expand All @@ -15,4 +16,14 @@ public function artisanCall($command, array $arguments = [])

return Artisan::call($command, $arguments);
}

public function artisanCallNoOutput($command, array $arguments = [])
{
$process = app(Process::class);
$process->disableOutput();
$exitCode = $this->artisanCall($command, $arguments);
$process->enableOutput();

return $exitCode;
}
}
17 changes: 17 additions & 0 deletions app/Commands/Traits/RunTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Commands\Traits;

trait RunTask
{
public function runTask(string $title, \Closure $task)
{
$exitCode = null;

$this->task($title, function () use (&$exitCode, $task) {
return ! ($exitCode = $task());
});

return $exitCode;
}
}
44 changes: 31 additions & 13 deletions app/Process.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Process
protected $commands = [];
protected $cwd = null;
protected $asUser = null;
protected $output = true;

public function setAsUser($user)
{
Expand All @@ -20,6 +21,20 @@ public function asFWDUser()
return $this->setAsUser(env('FWD_ASUSER'));
}

public function enableOutput()
{
$this->output = true;

return $this;
}

public function disableOutput()
{
$this->output = false;

return $this;
}

public function dockerRun(...$command) : int
{
$commandPrefix = [
Expand Down Expand Up @@ -50,6 +65,15 @@ public function dockerComposeExec(...$command) : int
return $this->dockerCompose(...$params, ...$command);
}

public function dockerComposeExecNoOutput(...$command) : int
{
$this->disableOutput();
$exitCode = $this->dockerComposeExec(...$command);
$this->enableOutput();

return $exitCode;
}

public function docker(...$command) : int
{
$commandPrefix = [
Expand Down Expand Up @@ -119,7 +143,7 @@ protected function run(string $command) : int
$pipes = [];
$proc = proc_open(
$command,
[STDIN, STDOUT, STDERR],
$this->getDescriptors(),
$pipes,
$this->cwd,
null,
Expand All @@ -129,21 +153,15 @@ protected function run(string $command) : int
return proc_close($proc);
}

protected function getCallback()
protected function getDescriptors() : array
{
return $this->callback ?: function ($type, $buffer) {
$buffer = trim($buffer);
if ($this->output || env('FWD_VERBOSE')) {
return [STDIN, STDOUT, STDERR];
}

switch ($type) {
case 'err':
$this->print($buffer);
break;
$devNull = fopen('/dev/null', 'w');

case 'out':
$this->print($buffer);
break;
}
};
return [STDIN, $devNull, $devNull];
}

protected function print($line)
Expand Down
Loading
0