multiple commands

This commit is contained in:
2024-08-24 18:51:36 +03:00
parent d9e2c4954a
commit 1505aaa909
10 changed files with 100 additions and 50 deletions
+3 -1
View File
@@ -2,6 +2,7 @@
namespace Lucent\Channel;
use Lucent\Channel\Data\UserCommand;
use Lucent\Primitive\Collection;
use Lucent\Schema\Schema;
@@ -13,12 +14,13 @@ final class Channel
/**
* @param Collection<Schema> $schemas
* @param Collection<UserCommand> $commands
*/
function __construct(
public string $name,
public string $url,
public string $previewTarget,
public string $generateCommand,
public Collection $commands,
public Collection $schemas,
public array $imageFilters,
public array $roles,
+10 -5
View File
@@ -3,7 +3,7 @@
namespace Lucent\Channel;
use Lucent\File\FileService;
use Lucent\Channel\Data\UserCommand;
use Lucent\Primitive\Collection;
use Lucent\Schema\Schema;
use Lucent\Schema\SchemaService;
@@ -30,11 +30,16 @@ final class ChannelService
$schemaService = new SchemaService();
$schemasCollection = (new Collection($schemasArray["schemas"] ?? []))->map([$schemaService, 'fromArray']);
$userCommands = [];
foreach (config("lucent.commands") as $signature => $desc) {
$userCommands[] = new UserCommand($desc, $signature);
}
$channel = new Channel(
name: config("lucent.name") ?? "",
url: rtrim(config("lucent.url") ?? "", "/"),
previewTarget: rtrim(config("lucent.previewTarget") ?? "", "/"),
generateCommand: config("lucent.generateCommand") ?? "",
commands: Collection::make($userCommands),
schemas: $schemasCollection,
imageFilters: config("lucent.imageFilters") ?? [],
roles: $schemasArray["roles"] ?? []
@@ -64,7 +69,7 @@ final class ChannelService
*/
public function schemasReadableByRoles(array $roles): array
{
$schemasAllRead = $this->channel->schemas->filter(fn(Schema $schema) => empty($schema->read))->values()->pluck("name");
$schemasAllRead = $this->channel->schemas->filter(fn(Schema $schema) => empty($schema->read))->values()->pluck("name");
$schemasCanRead = $this->channel->schemas->filter(fn(Schema $schema) => count(array_intersect($schema->read ?? [], $roles)) > 0)->values()->pluck("name");
$schemasCanWrite = $this->channel->schemas->filter(fn(Schema $schema) => count(array_intersect($schema->write ?? [], $roles)) > 0)->values()->pluck("name");
return $schemasAllRead->merge($schemasCanRead)->merge($schemasCanWrite)->unique()->values()->toArray();
@@ -77,8 +82,8 @@ final class ChannelService
*/
public function schemasWritableByRoles(array $roles): array
{
$schemasAllRead = $this->channel->schemas->filter(fn(Schema $schema) => empty($schema->write ?? []))->values()->pluck("name");
$schemasCanWrite = $this->channel->schemas->filter(fn(Schema $schema) => count(array_intersect($schema->write ?? [], $roles)) > 0)->values()->pluck("name");
$schemasAllRead = $this->channel->schemas->filter(fn(Schema $schema) => empty($schema->write ?? []))->values()->pluck("name");
$schemasCanWrite = $this->channel->schemas->filter(fn(Schema $schema) => count(array_intersect($schema->write ?? [], $roles)) > 0)->values()->pluck("name");
return $schemasAllRead->merge($schemasCanWrite)->unique()->values()->toArray();
}
+15
View File
@@ -0,0 +1,15 @@
<?php
namespace Lucent\Channel\Data;
class UserCommand
{
public function __construct(
public string $name,
public string $signature,
)
{
}
}
+6 -6
View File
@@ -1,13 +1,13 @@
<?php
return [
"env" => env("LUCENT_ENV", "production"),
"schemas_path" => env("LUCENT_SCHEMAS_PATH", "app/Lucent"),
"database" => env('LUCENT_DB_CONNECTION', env('DB_CONNECTION',"sqlite")),
"name" => env("LUCENT_NAME", "Lucent"),
"env" => env("LUCENT_ENV", "production"),
"schemas_path" => env("LUCENT_SCHEMAS_PATH", "app/Lucent"),
"database" => env('LUCENT_DB_CONNECTION', env('DB_CONNECTION', "sqlite")),
"name" => env("LUCENT_NAME", "Lucent"),
"url" => env("LUCENT_URL", env('APP_URL')),
"previewTarget" => env("LUCENT_PREVIEW_TARGET", "previewTarget"),
"generateCommand" => env("LUCENT_GENERATE_COMMAND", "generate:static"),
"previewTarget" => env("LUCENT_PREVIEW_TARGET", "previewTarget"),
"commands" => [],
"imageFilters" => [],
"canInvite" => ["admin"],
"canBuild" => ["admin"],
+40 -18
View File
@@ -5,11 +5,8 @@ namespace Lucent\Http\Controller;
use App\Http\Controllers\Controller;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Artisan;
use Lucent\Channel\ChannelService;
use Lucent\Svelte\Svelte;
use function Lucent\Response\ok;
class BuildController extends Controller
{
@@ -20,49 +17,66 @@ class BuildController extends Controller
{
}
public function build()
public function build(Request $request)
{
$buildLogFile = storage_path("lucent/build.log");
if(file_exists($buildLogFile)){
$commandSignature = $request->route("signature");
$buildLogFile = $this->getLogFile($commandSignature);
$pidFile = $this->getPidFile($commandSignature);
if (file_exists($buildLogFile)) {
unlink($buildLogFile);
}
if (file_exists($pidFile)) {
unlink($pidFile);
}
exec("cd " . base_path() . " && php8.3 artisan {$this->channelService->channel->generateCommand} > " . $buildLogFile . " 2>&1 & echo $!", $op);
exec("cd " . base_path() . " && php8.3 artisan {$commandSignature} > " . $buildLogFile . " 2>&1 & echo $!", $op);
$pid = (int)$op[0];
return redirect($this->channelService->channel->lucentUrl . "/build-report");
file_put_contents($pidFile, $pid);
return redirect($this->channelService->channel->lucentUrl . "/command-report/" . $commandSignature);
}
public function report(): View
public function report(Request $request): View
{
$commandSignature = $request->route("signature");
$command = $this->channelService->channel->commands->firstWhere("signature", $commandSignature);
return $this->svelte->render(
layout: "channel",
view: "buildReport",
title: "Build Report",
title: $command->name,
data: [
"command" => $command,
]
);
}
public function reportSource()
public function reportSource(Request $request)
{
return response()->stream(function () {
$commandSignature = $request->route("signature");
return response()->stream(function () use ($commandSignature) {
while (true) {
// sleep(1); // 50ms
$data["date"] = date("Y-m-d H:i:s");
$data["logs"] = file_get_contents(storage_path("lucent/build.log"));
$data["logs"] = file_get_contents($this->getLogFile($commandSignature));
// $lines = explode("\n",$data["logs"]);
echo 'data: ' .json_encode($data);
echo 'data: ' . json_encode($data);
echo "\n\n";
ob_flush();
flush();
if(str_contains($data["logs"],"Finito")){
$pidFile = $this->getPidFile($commandSignature);
if (file_exists($pidFile)) {
$pid = file_get_contents($pidFile);
} else {
break;
}
if(str_contains($data["logs"],"Exception")){
if (empty($pid)) {
break;
}
if (!file_exists("/proc/$pid")) {
break;
}
@@ -79,5 +93,13 @@ class BuildController extends Controller
]);
}
private function getLogFile(string $signature): string
{
return storage_path("lucent/$signature.log");
}
private function getPidFile(string $signature): string
{
return storage_path("lucent/$signature.pid.log");
}
}
+3 -3
View File
@@ -33,9 +33,9 @@ Route::group([
Route::get('/profile', [AccountController::class, 'profile']);
Route::post('/account/update-name', [AccountController::class, 'updateName']);
Route::post('/account/update-email', [AccountController::class, 'updateEmail']);
Route::get('/build-report', [BuildController::class, 'report']);
Route::get('/build-report-source', [BuildController::class, 'reportSource']);
Route::post('/build', [BuildController::class, 'build']);
Route::get('/command-report/{signature}', [BuildController::class, 'report']);
Route::get('/command-report-source/{signature}', [BuildController::class, 'reportSource']);
Route::post('/command/{signature}', [BuildController::class, 'build']);
});
+2 -2
View File
@@ -27,11 +27,11 @@ class StaticGenerator
try {
$callback($this->writer);
}catch (Throwable $th){
echo "Finito with errors".Carbon::now()->format("Y-m-d H:i:s")." ".$th->getMessage().PHP_EOL;
echo "Finished with errors".Carbon::now()->format("Y-m-d H:i:s")." ".$th->getMessage().PHP_EOL;
}
$this->copyBuildDirectory();
echo "Finito ".Carbon::now()->format("Y-m-d H:i:s").PHP_EOL;
echo "Finished ".Carbon::now()->format("Y-m-d H:i:s").PHP_EOL;
}
private function removeBuildDirectory() :void{