Skip to content

Commit

Permalink
feature: gt run, needs parameters passing (#7)
Browse files Browse the repository at this point in the history
* feature: gt run, needs parameters passing
for #5

* feature: pass all arguments through to gt run
  • Loading branch information
g105b authored Oct 9, 2021
1 parent d07a99f commit 432f9c9
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 50 deletions.
6 changes: 3 additions & 3 deletions bin/gt
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ $app = new Application(
"PHP.Gt Command Line Interface",
new ArgumentList(...$argv),
new CreateCommand(),
new ServeCommand(),
new BuildCommand(),
new CronCommand(),
new RunCommand(),
new DeployCommand(),
new MigrateCommand(),
new ServeCommand(),
new BuildCommand(),
new CronCommand(),
);
$app->run();
66 changes: 33 additions & 33 deletions composer.lock

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

16 changes: 3 additions & 13 deletions src/Command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,9 @@ public function run(ArgumentValueList $arguments = null):void {
usleep(500_000);
chdir($name);

$process = new Process("gt", "run");
$process->exec();
do {
$this->write(
$process->getOutput()
);
$this->write(
$process->getErrorOutput(),
Stream::ERROR
);
usleep(1_000_000);
}
while($process->isRunning());
$runCommand = new RunCommand();
$runCommand->setStream($this->stream);
$runCommand->run($arguments);
}
else {
$this->writeLine("Your new application is in the '$name' directory.");
Expand Down
93 changes: 92 additions & 1 deletion src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,76 @@

use Gt\Cli\Argument\ArgumentValueList;
use Gt\Cli\Command\Command;
use Gt\Cli\Parameter\Parameter;
use Gt\Cli\Stream;
use Gt\Daemon\Pool;
use Gt\Daemon\Process;

class RunCommand extends Command {
public function run(ArgumentValueList $arguments = null):void {
global $argv;

$serveArgs = [];
if($arguments->contains("debug")) {
array_push($serveArgs, "--debug");
}
$bindValue = $arguments->get("bind", "0.0.0.0");
array_push($serveArgs, "--bind");
array_push($serveArgs, $bindValue);
$portValue = $arguments->get("port", "8080");
array_push($serveArgs, "--port");
array_push($serveArgs, $portValue);

$processList = [
"serve" => new Process(
$argv[0],
"serve",
...$serveArgs,
),
];

if(!$arguments->contains("no-build")) {
$processList["build"] = new Process($argv[0], "build", "--watch");
}

if(!$arguments->contains("no-cron")) {
$processList["cron"] = new Process($argv[0], "cron", "--watch");
}

$pool = new Pool();
foreach($processList as $name => $process) {
$pool->add($name, $process);
}

$pool->exec();

/** @noinspection HttpUrlsUsage */
$localUrl = "http://";
if($bindValue == "0.0.0.0" || $bindValue == "127.0.0.1") {
$localUrl .= "localhost";
}
else {
$localUrl .= $bindValue;
}

if($portValue != "80") {
$localUrl .= ":$portValue";
}

usleep(100_000);
if($processList["serve"]->isRunning()) {
$this->writeLine("To view your application in your browser, visit: $localUrl");
$this->writeLine("To stop running, press [CTRL]+[C].");
$this->writeLine();
}

do {
$this->write($pool->read());
$this->write($pool->read(Process::PIPE_ERROR), Stream::ERROR);
usleep(100_000);
}
while($processList["serve"]->isRunning());
$this->writeLine("The server process has ended.");
}

public function getName():string {
Expand All @@ -29,6 +96,30 @@ public function getRequiredParameterList():array {
}

public function getOptionalParameterList():array {
return [];
return [
new Parameter(
true,
"port",
"p"
),
new Parameter(
true,
"bind",
"b"
),
new Parameter(
false,
"debug",
"d"
),
new Parameter(
false,
"no-build"
),
new Parameter(
false,
"no-cron"
)
];
}
}

0 comments on commit 432f9c9

Please sign in to comment.