Skip to content

Commit 672eab0

Browse files
committed
WIP
1 parent f9b88e2 commit 672eab0

12 files changed

+314
-15
lines changed

devstack

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,28 @@ if (file_exists($autoload = __DIR__ . '/vendor/autoload.php')) {
77
require dirname(__DIR__, 2) . '/autoload.php';
88
}
99

10-
use Symfony\Component\Console\Application;
10+
use Webteractive\Devstack\App;
1111
use Webteractive\Devstack\Commands\Config;
1212
use Webteractive\Devstack\Commands\InitStack;
13-
use Webteractive\Devstack\RegisterDockerCommands;
13+
use Webteractive\Devstack\RegisterDockerComposeCommands;
1414
use Webteractive\Devstack\Commands\DownloadRuntimes;
15+
use Webteractive\Devstack\Commands\MySql;
16+
use Webteractive\Devstack\Commands\RunComposerCommands;
17+
use Webteractive\Devstack\Commands\RunLaravelArtisanCommand;
18+
use Webteractive\Devstack\Commands\RunPHPCommand;
19+
use Webteractive\Devstack\Commands\Shell;
1520

16-
$app = new Application;
21+
$app = new App('Devstack', '1.0.0');
1722

1823
$app->add(new InitStack);
1924
$app->add(new Config);
2025
$app->add(new DownloadRuntimes);
26+
$app->add(new RunLaravelArtisanCommand);
27+
$app->add(new RunPHPCommand);
28+
$app->add(new RunComposerCommands);
29+
$app->add(new Shell);
30+
$app->add(new MySql);
2131

22-
RegisterDockerCommands::register($app);
32+
RegisterDockerComposeCommands::register($app);
2333

2434
$app->run();

src/App.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Webteractive\Devstack;
4+
5+
use Symfony\Component\Console\Application;
6+
7+
class App extends Application
8+
{
9+
private static $name = "MyApp";
10+
private static $logo = <<<LOGO
11+
DDDDDDDDDDDDD tttt kkkkkkkk
12+
D::::::::::::DDD ttt:::t k::::::k
13+
D:::::::::::::::DD t:::::t k::::::k
14+
DDD:::::DDDDD:::::D t:::::t k::::::k
15+
D:::::D D:::::D eeeeeeeeeeee vvvvvvv vvvvvvv ssssssssss ttttttt:::::ttttttt aaaaaaaaaaaaa cccccccccccccccc k:::::k kkkkkkk
16+
D:::::D D:::::D ee::::::::::::ee v:::::v v:::::v ss::::::::::s t:::::::::::::::::t a::::::::::::a cc:::::::::::::::c k:::::k k:::::k
17+
D:::::D D:::::D e::::::eeeee:::::eev:::::v v:::::vss:::::::::::::s t:::::::::::::::::t aaaaaaaaa:::::a c:::::::::::::::::c k:::::k k:::::k
18+
D:::::D D:::::De::::::e e:::::e v:::::v v:::::v s::::::ssss:::::stttttt:::::::tttttt a::::ac:::::::cccccc:::::c k:::::k k:::::k
19+
D:::::D D:::::De:::::::eeeee::::::e v:::::v v:::::v s:::::s ssssss t:::::t aaaaaaa:::::ac::::::c ccccccc k::::::k:::::k
20+
D:::::D D:::::De:::::::::::::::::e v:::::v v:::::v s::::::s t:::::t aa::::::::::::ac:::::c k:::::::::::k
21+
D:::::D D:::::De::::::eeeeeeeeeee v:::::v:::::v s::::::s t:::::t a::::aaaa::::::ac:::::c k:::::::::::k
22+
D:::::D D:::::D e:::::::e v:::::::::v ssssss s:::::s t:::::t tttttta::::a a:::::ac::::::c ccccccc k::::::k:::::k
23+
DDD:::::DDDDD:::::D e::::::::e v:::::::v s:::::ssss::::::s t::::::tttt:::::ta::::a a:::::ac:::::::cccccc:::::ck::::::k k:::::k
24+
D:::::::::::::::DD e::::::::eeeeeeee v:::::v s::::::::::::::s tt::::::::::::::ta:::::aaaa::::::a c:::::::::::::::::ck::::::k k:::::k
25+
D::::::::::::DDD ee:::::::::::::e v:::v s:::::::::::ss tt:::::::::::tt a::::::::::aa:::a cc:::::::::::::::ck::::::k k:::::k
26+
DDDDDDDDDDDDD eeeeeeeeeeeeee vvv sssssssssss ttttttttttt aaaaaaaaaa aaaa cccccccccccccccckkkkkkkk kkkkkkk
27+
LOGO;
28+
29+
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
30+
{
31+
$this->setName(static::$name);
32+
$this->setVersion($version);
33+
parent::__construct($name, $version);
34+
}
35+
36+
/**
37+
* @return string
38+
*/
39+
public function getHelp(): string
40+
{
41+
return static::$logo . "\n\n" . parent::getHelp();
42+
}
43+
}

src/Commands/Base.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,22 @@ public function setup()
4545
{
4646
[$name, $arguments, $options] = CommandSignature::parse($this->signature);
4747

48+
49+
if ($this->shouldIgnoreValidationErrors()) {
50+
$this->ignoreValidationErrors();
51+
}
52+
4853
parent::__construct($name);
4954

5055
$this->getDefinition()->addArguments($arguments);
5156
$this->getDefinition()->addOptions($options);
5257
}
5358

59+
public function shouldIgnoreValidationErrors(): bool
60+
{
61+
return false;
62+
}
63+
5464
protected function execute(InputInterface $input, OutputInterface $output)
5565
{
5666
return $this->setIO($input, $output)

src/Commands/MySql.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Webteractive\Devstack\Commands;
4+
5+
use Webteractive\Devstack\Process;
6+
use Webteractive\Devstack\WithSignalHandlers;
7+
8+
class MySql extends Base
9+
{
10+
use WithSignalHandlers;
11+
12+
protected $signature = 'mysql
13+
{--p|password=password : The password to use.}
14+
{--u|user=user : The user to use.}
15+
{--db|database= : The database to use.}';
16+
protected $description = 'Start a MySQL CLI session within the <comment>mysql</comment> container.';
17+
18+
public function handle(): int
19+
{
20+
$password = $this->output->
21+
22+
$this->handleTerminationSignals(
23+
$process = Process::prepareFromShell('docker compose exec -it mysql bash -c "MYSQL_PWD=password mysql -u user;"')
24+
);
25+
26+
$process->setTty(true)->run();
27+
28+
return static::SUCCESS;
29+
}
30+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Webteractive\Devstack\Commands;
4+
5+
use Webteractive\Devstack\Process;
6+
use Webteractive\Devstack\WithSignalHandlers;
7+
8+
class RunComposerCommands extends Base
9+
{
10+
use WithSignalHandlers;
11+
12+
protected $signature = 'composer';
13+
protected $description = 'Run composer commands.';
14+
15+
public function shouldIgnoreValidationErrors(): bool
16+
{
17+
return true;
18+
}
19+
20+
public function handle(): int
21+
{
22+
23+
$command = [
24+
'docker',
25+
'compose',
26+
'exec',
27+
'-u',
28+
'dev',
29+
'-T',
30+
'app',
31+
'composer',
32+
];
33+
34+
$this->handleTerminationSignals(
35+
$process = Process::prepare(array_merge($command, $this->fullCommandSignature))
36+
);
37+
38+
$process->setTty(true)->run();
39+
40+
return static::SUCCESS;
41+
}
42+
}

src/Commands/RunDockerCommands.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Webteractive\Devstack\Commands;
44

5-
use Symfony\Component\Process\Process;
5+
use Webteractive\Devstack\Process;
6+
use Webteractive\Devstack\WithSignalHandlers;
67

78
class RunDockerCommands extends Base
89
{
10+
use WithSignalHandlers;
11+
912
public function __construct($name, $description)
1013
{
1114
$this->signature = $name;
@@ -18,14 +21,16 @@ public function __construct($name, $description)
1821

1922
public function handle(): int
2023
{
21-
[, $name] = explode(':', $this->getName());
22-
$processSignature = ['docker', 'compose', $name];
23-
$process = new Process(array_merge($processSignature, $this->fullCommandSignature));
24-
$process->setTty(true);
25-
$process->run();
26-
$process->run(function ($type, $buffer) {
27-
$this->line($buffer);
28-
});
24+
$commandSignature = array_merge(
25+
['docker', 'compose', $this->getName()],
26+
$this->fullCommandSignature
27+
);
28+
29+
$this->handleTerminationSignals(
30+
$process = Process::prepare($commandSignature)
31+
);
32+
33+
$process->setTty(true)->run();
2934

3035
return static::SUCCESS;
3136
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Webteractive\Devstack\Commands;
4+
5+
use Webteractive\Devstack\Process;
6+
use Webteractive\Devstack\WithSignalHandlers;
7+
8+
class RunLaravelArtisanCommand extends Base
9+
{
10+
use WithSignalHandlers;
11+
12+
protected $signature = 'artisan';
13+
protected $description = 'Run Laravel\'s artisan command.';
14+
15+
public function shouldIgnoreValidationErrors(): bool
16+
{
17+
return true;
18+
}
19+
20+
public function handle(): int
21+
{
22+
$command = [
23+
'docker',
24+
'compose',
25+
'exec',
26+
'-u',
27+
'dev',
28+
'-T',
29+
'app',
30+
'php',
31+
'artisan',
32+
];
33+
34+
$this->handleTerminationSignals(
35+
$process = Process::prepare(array_merge($command, $this->fullCommandSignature))
36+
);
37+
38+
$process->setTty(true)->run();
39+
40+
return static::SUCCESS;
41+
}
42+
}

src/Commands/RunPhpCommand.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Webteractive\Devstack\Commands;
4+
5+
use Webteractive\Devstack\Process;
6+
use Webteractive\Devstack\WithSignalHandlers;
7+
8+
class RunPHPCommand extends Base
9+
{
10+
use WithSignalHandlers;
11+
12+
protected $signature = 'php';
13+
protected $description = 'Run PHP commands.';
14+
15+
public function shouldIgnoreValidationErrors(): bool
16+
{
17+
return true;
18+
}
19+
20+
public function handle(): int
21+
{
22+
$command = [
23+
'docker',
24+
'compose',
25+
'exec',
26+
'-u',
27+
'dev',
28+
'-T',
29+
'app',
30+
'php',
31+
];
32+
33+
$this->handleTerminationSignals(
34+
$process = Process::prepare(array_merge($command, $this->fullCommandSignature))
35+
);
36+
37+
$process->setTty(true)->run();
38+
39+
return static::SUCCESS;
40+
}
41+
}

src/Commands/Shell.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Webteractive\Devstack\Commands;
4+
5+
use Webteractive\Devstack\Process;
6+
use Webteractive\Devstack\WithSignalHandlers;
7+
8+
class Shell extends Base
9+
{
10+
use WithSignalHandlers;
11+
12+
protected $signature = 'shell {--r|root : As a root user.}';
13+
protected $description = 'Start a shell session within the application container';
14+
15+
public function handle(): int
16+
{
17+
$command = [
18+
'docker',
19+
'compose',
20+
'exec',
21+
];
22+
23+
if (!$this->input->getOption('root')) {
24+
$command[] = '-u';
25+
$command[] = 'dev';
26+
}
27+
28+
$this->handleTerminationSignals(
29+
$process = Process::prepare(array_merge($command, ['app', 'bash']))
30+
);
31+
32+
$process->setTty(true)->run();
33+
34+
return static::SUCCESS;
35+
}
36+
}

src/Process.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Webteractive\Devstack;
4+
5+
use Symfony\Component\Process\Process as SymfonyProcess;
6+
7+
class Process
8+
{
9+
public static function prepare(array $command)
10+
{
11+
return new SymfonyProcess($command);
12+
}
13+
14+
public static function prepareFromShell(string $command)
15+
{
16+
return SymfonyProcess::fromShellCommandline($command);
17+
}
18+
}

0 commit comments

Comments
 (0)