-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunCommand.php
More file actions
150 lines (131 loc) · 3.41 KB
/
RunCommand.php
File metadata and controls
150 lines (131 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace Gt\GtCommand\Command;
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;
use Gt\GtCommand\UI\Egg\RandomStartingMessage;
class RunCommand extends Command {
/**
* TODO: Simplify this function.
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
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", $bindValue);
$portValue = $arguments->get("port", "8080");
array_push($serveArgs, "--port", $portValue);
$threadsValue = $arguments->get("threads", "8");
array_push($serveArgs, "--threads", $threadsValue);
$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") && is_file("crontab")) {
$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";
}
$this->write("Starting WebEngine...");
usleep(500_000);
if($processList["serve"]->isRunning()) {
$this->writeLine(" ✅");
usleep(500_000);
$this->writeLine();
$this->writeLine("To view your application in your"
. " browser, visit: $localUrl");
$this->writeLine("To stop running, press [CTRL]+[C].");
$this->writeLine();
usleep(500_000);
}
else {
$this->writeLine(" ❌");
$this->write($processList["serve"]->getOutput(Process::PIPE_ERROR), Stream::ERROR);
return;
}
$this->write($processList["serve"]->getOutput());
$this->write($processList["serve"]->getOutput(Process::PIPE_ERROR), Stream::ERROR);
do {
$this->write($pool->read());
$this->write($pool->read(Process::PIPE_ERROR), Stream::ERROR);
usleep(100_000);
/** @var bool $isRunning
* @noinspection PhpRedundantVariableDocTypeInspection
* This is necessary to suppress "Do-while loop condition is always true" error from phpstan. Bug with stan?
*/
$isRunning = $processList["serve"]->isRunning();
}
while($isRunning);
$this->writeLine("The server process has ended.");
}
public function getName():string {
return "run";
}
public function getDescription():string {
return "Run all background scripts at once (serve, build, cron)";
}
public function getRequiredNamedParameterList():array {
return [];
}
public function getOptionalNamedParameterList():array {
return [];
}
public function getRequiredParameterList():array {
return [];
}
public function getOptionalParameterList():array {
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"
)
];
}
}