From f25f8f05ec41dd7bae97b713859c484b6aff89b9 Mon Sep 17 00:00:00 2001 From: Osahenrumwen Aigbogun Date: Tue, 19 Dec 2023 23:17:01 +0100 Subject: [PATCH 1/2] Rewrote Cmd/Trait/Make to the state it was before it was overwritten --- src/BobDBuilder/Cmd/Traits/Make/Domain.php | 77 +++++++++++++++++----- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/src/BobDBuilder/Cmd/Traits/Make/Domain.php b/src/BobDBuilder/Cmd/Traits/Make/Domain.php index 532fbe0..99bc82f 100644 --- a/src/BobDBuilder/Cmd/Traits/Make/Domain.php +++ b/src/BobDBuilder/Cmd/Traits/Make/Domain.php @@ -2,8 +2,10 @@ namespace BrickLayer\Lay\BobDBuilder\Cmd\Traits\Make; +use BrickLayer\Lay\Core\Exception; use BrickLayer\Lay\Libs\LayCopyDir; use BrickLayer\Lay\Libs\LayUnlinkDir; +use BrickLayer\Lay\Orm\SQL; use SplFileObject; @@ -45,7 +47,7 @@ public function domain(): void $domain_dir = $this->plug->server->domains . $domain; $exists = is_dir($domain_dir); - if ($this->plug->force && $exists) + if (!$this->plug->force && $exists) $this->plug->write_fail( "Domain directory *$domain_dir* exists already!\n" . "If you wish to force this action, pass the tag --force with the command\n" @@ -80,8 +82,7 @@ public function domain(): void public function domain_default_files(string $domain_name, string $domain_dir): void { file_put_contents( - $domain_dir . $this->plug->s . - "index.php", + $domain_dir . $this->plug->s . "index.php", <<create( id: "default", - builder: new \web\domains\{$domain_name}\Plaster(), + builder: new \web\domains\\$domain_name\Plaster(), ); FILE @@ -103,11 +104,11 @@ public function domain_default_files(string $domain_name, string $domain_dir): v "Plaster.php", <<plug->server->web . "index.php", 'w+'); + $main_file = $this->plug->server->web . "index.php"; + $lock_file = $this->plug->server->web . ".index.php.lock"; + + copy($main_file, $lock_file); + + $file = new SplFileObject($lock_file, 'r+'); $file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); $page = []; $domains = []; $key = 0; $storing_domain = false; + $existing_domain_key = null; + $page_index = 0; while (!$file->eof()) { $entry = $file->fgets(); + if($page_index > 0 && empty($entry)) + continue; + if (str_starts_with($entry, "Domain::new()")) $storing_domain = true; if ($storing_domain) { - if (empty($entry)) - continue; $domains[$key][] = $entry; + if( + $this->plug->force && + $existing_domain_key !== null + ) { + if( + str_starts_with(ltrim($entry), "id:") + ) + $existing_domain_key = trim( + rtrim( + explode("id:", $entry)[1], + "," + ), "'\"" + ) == $domain_id ? $key : null; + + if( + !$existing_domain_key && + str_starts_with(ltrim($entry), "builder:") + ) + $existing_domain_key = @explode("\\", $entry)[3] == $domain ? $key : null; + } + + if (str_ends_with($entry, ";")) { $storing_domain = false; $key++; @@ -184,9 +215,16 @@ public function update_general_domain_entry(string $domain, string $domain_id, s } $page[] = $entry; + $page_index++; } $default_domain = end($domains); + array_pop($domains); + + if($existing_domain_key) + unset($domains[$existing_domain_key]); + + $domains = SQL::new()->array_flatten($domains); $new_domain = [ 'Domain::new()->create(', @@ -194,12 +232,21 @@ public function update_general_domain_entry(string $domain, string $domain_id, s ' builder: new \web\domains\\' . $domain . '\\Plaster(),', ' patterns: [' . $pattern . '],', ');', - '', ]; - array_pop($domains); - array_push($page, $domains, $new_domain, $default_domain); + try{ + array_push($page, "", ...$domains, ...[""], ...$new_domain, ...[""], ...$default_domain); + + $file->seek(0); + $file->fwrite(implode("\n", $page)); + } catch (\Exception $e) { + Exception::throw_exception($e->getMessage(), "MakeDomain"); + + new LayUnlinkDir($domain_dir); + unlink($lock_file); + } - $file->fwrite(implode("\n", $page)); + copy($lock_file, $main_file); + unlink($lock_file); } } \ No newline at end of file From 9492ea9e1aca55799b5615d449505956765366c9 Mon Sep 17 00:00:00 2001 From: Osahenrumwen Aigbogun Date: Sat, 23 Dec 2023 18:34:21 +0100 Subject: [PATCH 2/2] Finally fixed previous issues that were not committed --- src/BobDBuilder/BobExec.php | 16 ++++++ src/BobDBuilder/Cmd/Make.php | 6 +- src/BobDBuilder/Cmd/Project.php | 23 +++++++- src/BobDBuilder/Cmd/Symlink.php | 2 +- src/BobDBuilder/Cmd/Traits/Make/Domain.php | 25 ++++---- src/BobDBuilder/Engine.php | 66 +++++++++++++--------- src/BobDBuilder/EnginePlug.php | 9 +-- src/Core/View/DomainResource.php | 1 - src/Core/View/ViewSrc.php | 4 +- src/static/js/constants.js | 11 ++-- src/static/js/constants.min.js | 2 +- 11 files changed, 106 insertions(+), 59 deletions(-) create mode 100644 src/BobDBuilder/BobExec.php diff --git a/src/BobDBuilder/BobExec.php b/src/BobDBuilder/BobExec.php new file mode 100644 index 0000000..80e1835 --- /dev/null +++ b/src/BobDBuilder/BobExec.php @@ -0,0 +1,16 @@ +plug = $plug; - $plug->add_arg($this, ["project:create"], 'project_create', true); + $plug->add_arg($this, ["project:create"], 'project_create', true, 0); } public function _spin(): void @@ -33,10 +34,13 @@ public function _spin(): void public function create(): void { $cmd = $this->tags['project_create'][0] ?? null; + $tag = $this->tags['project_create'][1] ?? ""; if (!$cmd) return; + $tag = trim($tag); + $server = $this->plug->server; // copy env file if it doesn't exist @@ -48,5 +52,18 @@ public function create(): void // copy helper js file to project lay folder new LayCopyDir($server->lay_static . "js", $server->shared . "lay"); + + if($tag == "--fresh-project") { + $this->plug->write_info("Fresh project detected!"); + + // create a default domain folder + new BobExec("make:domain Default * --force --silent"); + + // link lay folder to default folder + $s = DIRECTORY_SEPARATOR; + new BobExec("link:dir web{$s}shared{$s}lay web{$s}domains{$s}Default{$s}lay --silent"); + + $this->plug->write_info("Default domain folder created successfully!"); + } } } \ No newline at end of file diff --git a/src/BobDBuilder/Cmd/Symlink.php b/src/BobDBuilder/Cmd/Symlink.php index 8a653a5..2919f41 100644 --- a/src/BobDBuilder/Cmd/Symlink.php +++ b/src/BobDBuilder/Cmd/Symlink.php @@ -13,7 +13,7 @@ class Symlink implements CmdLayout { use IsSingleton; - private readonly EnginePlug $plug; + private EnginePlug $plug; public function _init(EnginePlug $plug): void { diff --git a/src/BobDBuilder/Cmd/Traits/Make/Domain.php b/src/BobDBuilder/Cmd/Traits/Make/Domain.php index 99bc82f..07fa96b 100644 --- a/src/BobDBuilder/Cmd/Traits/Make/Domain.php +++ b/src/BobDBuilder/Cmd/Traits/Make/Domain.php @@ -31,7 +31,7 @@ public function domain(): void . "Example: 'case-study'\n" ); - if (empty($pattern) || trim($pattern) == "*") + if (empty($pattern) || (!$this->plug->is_internal && trim($pattern) == "*")) $this->plug->write_warn( "Pattern cannot be an empty quote or '*'\n" . "\n" @@ -42,7 +42,7 @@ public function domain(): void $domain = explode(" ", ucwords($domain)); - $domain_id = implode("-", $domain) . "-id"; + $domain_id = strtolower(implode("-", $domain) . "-id"); $domain = implode("", $domain); $domain_dir = $this->plug->server->domains . $domain; $exists = is_dir($domain_dir); @@ -162,7 +162,7 @@ public function update_general_domain_entry(string $domain, string $domain_id, s copy($main_file, $lock_file); $file = new SplFileObject($lock_file, 'r+'); - $file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); + $file->setFlags(SplFileObject::DROP_NEW_LINE); $page = []; $domains = []; @@ -174,7 +174,7 @@ public function update_general_domain_entry(string $domain, string $domain_id, s while (!$file->eof()) { $entry = $file->fgets(); - if($page_index > 0 && empty($entry)) + if($page_index > 7 && empty($entry)) continue; if (str_starts_with($entry, "Domain::new()")) @@ -185,8 +185,8 @@ public function update_general_domain_entry(string $domain, string $domain_id, s $domains[$key][] = $entry; if( - $this->plug->force && - $existing_domain_key !== null + $existing_domain_key === null && + $this->plug->force ) { if( str_starts_with(ltrim($entry), "id:") @@ -218,8 +218,12 @@ public function update_general_domain_entry(string $domain, string $domain_id, s $page_index++; } - $default_domain = end($domains); - array_pop($domains); + $default_domain = []; + + if(!$this->plug->is_internal) { + $default_domain = end($domains); + array_pop($domains); + } if($existing_domain_key) unset($domains[$existing_domain_key]); @@ -235,9 +239,8 @@ public function update_general_domain_entry(string $domain, string $domain_id, s ]; try{ - array_push($page, "", ...$domains, ...[""], ...$new_domain, ...[""], ...$default_domain); - - $file->seek(0); + array_push($page, "", ...$domains, ...[""], ...$new_domain, ...[""], ...$default_domain, ...[""]); + $file->rewind(); $file->fwrite(implode("\n", $page)); } catch (\Exception $e) { Exception::throw_exception($e->getMessage(), "MakeDomain"); diff --git a/src/BobDBuilder/Engine.php b/src/BobDBuilder/Engine.php index 77a2b1a..8913034 100644 --- a/src/BobDBuilder/Engine.php +++ b/src/BobDBuilder/Engine.php @@ -11,29 +11,20 @@ class Engine public EnginePlug $plug; public function __construct( - private array $args + private array $args, + private readonly bool $is_internal = false ) { - $show_help = array_search("--help", $this->args, true); - $show_help = $show_help === false ? array_search("--h", $this->args, true) : $show_help; - - if ($show_help !== false) { - unset($this->args[$show_help]); - $show_help = true; - } - - $force_action = array_search("--force", $this->args, true); - $force_action = $force_action === false ? array_search("--f", $this->args, true) : $force_action; - - if ($force_action !== false) { - unset($this->args[$force_action]); - $force_action = true; - } + $force = $this->extract_global_tag("--force", "-f"); + $show_help = $this->extract_global_tag("--help", "-h"); + $silent = $this->extract_global_tag("--silent", "-s"); $this->plug = new EnginePlug($this->args); + $this->plug->is_internal = $this->is_internal; - $this->plug->force = $force_action ?? false; - $this->plug->show_help = $show_help ?? false; + $this->plug->force = $force; + $this->plug->show_help = $show_help; + $this->plug->silent = $silent; foreach ($this->args as $i => $arg) { if($this->plug->run($i, $arg) == CustomContinueBreak::BREAK) @@ -61,20 +52,41 @@ public function __construct( // End Bob execution if(empty($this->plug->active_cmd)) - $this->plug->write_info( + $this->plug->write_warn( "-- Bob has determined that the current command is invalid\n" . "-- Please use --help to see the list of available commands" , ["current_cmd" => $this->plug->typed_cmd] ); - $this->plug->write_success( - "\n" . ( - isset($this->plug->active_cmd_class) ? - "-- Operation completed!" : - "" - ), - ['close_talk' => true] - ); + if(!$this->plug->silent) + $this->plug->write_success( + "\n" . ( + isset($this->plug->active_cmd_class) ? + "-- Operation completed!" : + "" + ), + ['close_talk' => true] + ); + } + + public function extract_global_tag(string ...$tags) : bool + { + $out = false; + + foreach ($tags as $tag) { + if($out !== false) + break; + + $out = array_search($tag, $this->args, true); + + } + + if ($out !== false) { + unset($this->args[$out]); + $out = true; + } + + return $out; } public function intro(bool $close_talk = true): void diff --git a/src/BobDBuilder/EnginePlug.php b/src/BobDBuilder/EnginePlug.php index 6b921fb..8ed6cf4 100644 --- a/src/BobDBuilder/EnginePlug.php +++ b/src/BobDBuilder/EnginePlug.php @@ -21,7 +21,9 @@ class EnginePlug public bool $show_intro = true; public bool $show_help = false; public bool $force = false; + public bool $silent = false; public bool $cmd_found = false; + public bool $is_internal = false; public array $tags = []; public array $plugged_args = []; public string $typed_cmd = ""; @@ -200,7 +202,6 @@ public function write_fail(string $message, array $opts = []) : void { } public function write_talk(string $message, array $opts = []) : void { - $opts['color'] = "red"; $this->write($message, CmdOutType::TALK, $opts); } @@ -214,7 +215,7 @@ public function write_warn(string $message, array $opts = []) : void { public function write(string $message, ?CmdOutType $type = null, array $opts = []): void { $kill = $opts['kill'] ?? false; - $open_talk = $opts['open_talk'] ?? false; + $open_talk = $opts['open_talk'] ?? false; $close_talk = $opts['close_talk'] ?? false; $current_cmd = $this->active_cmd ?: ($opts['current_cmd'] ?? ""); $hide_cur_cmd = $opts['hide_current_cmd'] ?? true; @@ -236,7 +237,7 @@ public function write(string $message, ?CmdOutType $type = null, array $opts = [ "InvalidConsoleColor" ); - if ($open_talk) + if ($open_talk && !$this->silent) Console::log("(^_^) Bob is Building --::--", Foreground::light_gray); if (!$hide_cur_cmd && !empty($current_cmd)) { @@ -283,7 +284,7 @@ public function write(string $message, ?CmdOutType $type = null, array $opts = [ Console::log($m, $color); } - if ($close_talk) + if ($close_talk && !$this->silent) Console::log("(-_-) Bob is Done -----", Foreground::light_gray); Console::bell(); diff --git a/src/Core/View/DomainResource.php b/src/Core/View/DomainResource.php index dabcb4e..e11fdfe 100644 --- a/src/Core/View/DomainResource.php +++ b/src/Core/View/DomainResource.php @@ -91,7 +91,6 @@ public static function set_res(string $key, mixed $value) : void 'img' => 'string', 'js' => 'string', 'shared' => 'object [root, static, css, img, js, img_default [object [logo, favicon, icon, meta]]]', - 'server' => 'object', 'domain' => 'object', 'lay' => 'object [uri, root]', ])] diff --git a/src/Core/View/ViewSrc.php b/src/Core/View/ViewSrc.php index 5df8a7b..1b77ff2 100644 --- a/src/Core/View/ViewSrc.php +++ b/src/Core/View/ViewSrc.php @@ -2,6 +2,8 @@ declare(strict_types=1); namespace BrickLayer\Lay\Core\View; +use BrickLayer\Lay\Core\LayConfig; + final class ViewSrc { public static function gen(string $src) : string { @@ -28,7 +30,7 @@ public static function gen(string $src) : string if(!str_starts_with($src, $base)) return $src; - $local_file = str_replace($base, "", $src); + $local_file = str_replace($base, $client->domain->domain_root, $src); $src .= "?mt=" . @filemtime($local_file); diff --git a/src/static/js/constants.js b/src/static/js/constants.js index fb508a7..6a6febc 100644 --- a/src/static/js/constants.js +++ b/src/static/js/constants.js @@ -5,7 +5,6 @@ $lay.page = { title : $id("LAY-PAGE-TITLE").content, title_full : $id("LAY-PAGE-TITLE-FULL").innerHTML, desc : $attr($id("LAY-PAGE-DESC"),"content"), - type : $attr($id("LAY-PAGE-TYPE"),"content"), url : $attr($id("LAY-PAGE-URL"),"content"), urlFull : $attr($id("LAY-PAGE-FULL-URL"),"content"), img : $attr($id("LAY-PAGE-IMG"),"content"), @@ -16,12 +15,10 @@ $lay.src = { base : $id("LAY-PAGE-BASE").href, api : $id('LAY-API').value + "?c=", serve : $id('LAY-API').value, - custom_img : $id("LAY-CUSTOM-IMG").value, - back_img : $id("LAY-BACK-IMG").value, - front_img : $id("LAY-FRONT-IMG").value, - front_root : $id("LAY-FRONT-ROOT").value, - back_root : $id("LAY-BACK-ROOT").value, - custom_root : $id("LAY-CUSTOM-ROOT").value, + shared_root : $id('LAY-SHARED-ROOT').value, + shared_img : $id('LAY-SHARED-IMG').value, + domain_root : $id('LAY-DOMAIN-ROOT').value, + domain_img : $id('LAY-DOMAIN-IMG').value, uploads : $id("LAY-UPLOAD").value, } $lay.fn = { diff --git a/src/static/js/constants.min.js b/src/static/js/constants.min.js index 09f910a..5e463b2 100644 --- a/src/static/js/constants.min.js +++ b/src/static/js/constants.min.js @@ -1 +1 @@ -const $lay={};$lay.page={html:$id("LAY-HTML"),title:$id("LAY-PAGE-TITLE").content,title_full:$id("LAY-PAGE-TITLE-FULL").innerHTML,desc:$attr($id("LAY-PAGE-DESC"),"content"),type:$attr($id("LAY-PAGE-TYPE"),"content"),url:$attr($id("LAY-PAGE-URL"),"content"),urlFull:$attr($id("LAY-PAGE-FULL-URL"),"content"),img:$attr($id("LAY-PAGE-IMG"),"content"),site_name:$attr($id("LAY-SITE-NAME-SHORT"),"content"),site_name_full:$attr($id("LAY-SITE-NAME"),"content")},$lay.src={base:$id("LAY-PAGE-BASE").href,api:$id("LAY-API").value+"?c=",serve:$id("LAY-API").value,custom_img:$id("LAY-CUSTOM-IMG").value,back_img:$id("LAY-BACK-IMG").value,front_img:$id("LAY-FRONT-IMG").value,front_root:$id("LAY-FRONT-ROOT").value,back_root:$id("LAY-BACK-ROOT").value,custom_root:$id("LAY-CUSTOM-ROOT").value,uploads:$id("LAY-UPLOAD").value},$lay.fn={copy:(t,e="Link copied successfully")=>{if(navigator.clipboard)return navigator.clipboard.writeText(t).catch((()=>osNote("Cannot copy, enable clipboard permission from your setting and try again"))).then((t=>osNote(e,"success"))),!0;const n=document.createElement("textarea");return n.value=t,n.setAttribute("readonly",""),n.style.position="absolute",n.style.left="-9999px",document.body.appendChild(n),n.select(),document.execCommand("copy"),document.body.removeChild(n),osNote(e,"success"),!0},rowEntrySave:t=>``,rowEntryAction:t=>{$on(t.targetElement??$sel("table.has-table-action")??$sel("table.data-table")??$sel("table.dt-live-dom"),"click",(e=>{t.then&&t.then();let n,a=e.target;($class(a,"has","table-actions")||$in(a,".table-actions","top")||$class(a,"has","table-action")||$in(a,".table-action","top"))&&(n=a,e.preventDefault(),$loop(t,((t,e)=>{if($data(n,"action")===e){let e=n.closest(".table-actions-parent")??n.closest("td");t({id:$data(n,"id"),name:decodeURIComponent($data(n,"name")),item:n,params:$data(n,"params")?.split(","),fn:()=>{let t=$data(n,"fn")?.trim();if(!t)return null;let e=$data(n,"fn-args");return e?new Function("",`return ${t}(${e.split(",")})`).call(this):")"===t.substring(t.length-1,1)?new Function("",`return ${t}`).call(this):new Function("",`return ${t}()`).call(this)},info:$sel(".entry-row-info",e)?JSON.parse($html($sel(".entry-row-info",e))):""})}})))}))},currency:(t,e="USD",n="en-US")=>new Intl.NumberFormat(n,{style:"currency",currency:e}).format(t)}; \ No newline at end of file +const $lay={};$lay.page={html:$id("LAY-HTML"),title:$id("LAY-PAGE-TITLE").content,title_full:$id("LAY-PAGE-TITLE-FULL").innerHTML,desc:$attr($id("LAY-PAGE-DESC"),"content"),url:$attr($id("LAY-PAGE-URL"),"content"),urlFull:$attr($id("LAY-PAGE-FULL-URL"),"content"),img:$attr($id("LAY-PAGE-IMG"),"content"),site_name:$attr($id("LAY-SITE-NAME-SHORT"),"content"),site_name_full:$attr($id("LAY-SITE-NAME"),"content")},$lay.src={base:$id("LAY-PAGE-BASE").href,api:$id("LAY-API").value+"?c=",serve:$id("LAY-API").value,shared_root:$id("LAY-SHARED-ROOT").value,shared_img:$id("LAY-SHARED-IMG").value,domain_root:$id("LAY-DOMAIN-ROOT").value,domain_img:$id("LAY-DOMAIN-IMG").value,uploads:$id("LAY-UPLOAD").value},$lay.fn={copy:(t,e="Link copied successfully")=>{if(navigator.clipboard)return navigator.clipboard.writeText(t).catch((()=>osNote("Cannot copy, enable clipboard permission from your setting and try again"))).then((t=>osNote(e,"success"))),!0;const a=document.createElement("textarea");return a.value=t,a.setAttribute("readonly",""),a.style.position="absolute",a.style.left="-9999px",document.body.appendChild(a),a.select(),document.execCommand("copy"),document.body.removeChild(a),osNote(e,"success"),!0},rowEntrySave:t=>``,rowEntryAction:t=>{$on(t.targetElement??$sel("table.has-table-action")??$sel("table.data-table")??$sel("table.dt-live-dom"),"click",(e=>{t.then&&t.then();let a,n=e.target;($class(n,"has","table-actions")||$in(n,".table-actions","top")||$class(n,"has","table-action")||$in(n,".table-action","top"))&&(a=n,e.preventDefault(),$loop(t,((t,e)=>{if($data(a,"action")===e){let e=a.closest(".table-actions-parent")??a.closest("td");t({id:$data(a,"id"),name:decodeURIComponent($data(a,"name")),item:a,params:$data(a,"params")?.split(","),fn:()=>{let t=$data(a,"fn")?.trim();if(!t)return null;let e=$data(a,"fn-args");return e?new Function("",`return ${t}(${e.split(",")})`).call(this):")"===t.substring(t.length-1,1)?new Function("",`return ${t}`).call(this):new Function("",`return ${t}()`).call(this)},info:$sel(".entry-row-info",e)?JSON.parse($html($sel(".entry-row-info",e))):""})}})))}))},currency:(t,e="USD",a="en-US")=>new Intl.NumberFormat(a,{style:"currency",currency:e}).format(t)}; \ No newline at end of file