Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Commands/InstallComponentCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function handle()
$result = (new ComponentInstaller($this, $this->components, $installationConfig))->install($name);

if ($result === Command::SUCCESS) {
$this->info(" ✓ Updated sheaf.json and sheaf-lock.json");
$this->components->info("Full documentation: https://sheafui.dev/docs/components/{$name}");
}
}
Expand Down Expand Up @@ -91,6 +92,7 @@ public function getBannerTitle(string $title)
{
$formattedTitle = Str::of($title)->headline();


return match (true) {
$this->option('only-deps') => "Installing {$formattedTitle} Dependencies Only",
$this->option('dry-run') => "Preview: Installing {$formattedTitle} (Dry Run)",
Expand Down
76 changes: 76 additions & 0 deletions src/Commands/RemoveComponentCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Sheaf\Cli\Commands;

use Sheaf\Cli\Services\ComponentInstaller;
use Sheaf\Cli\Support\InstallationConfig;
use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Sheaf\Cli\Services\ComponentRemover;

use function Laravel\Prompts\text;

class RemoveComponentCommand extends Command
{

public function __construct(protected ComponentRemover $componentRemover) {
parent::__construct();
}
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sheaf:remove
{name?* : the name of the component.}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Removing a sheaf Component';

/**
* Execute the console command.
*/
public function handle()
{

$componentNames = $this->getComponentName();

foreach ($componentNames as $name) {

$this->banner("Removing all $name files");

$this->componentRemover->remove($name);
}
$this->info("+ updated sheaf-lock.json and sheaf.json files");
return Command::SUCCESS;
}

private function getComponentName()
{
$componentName = $this->argument('name');


if (!$componentName) {
$componentName = text(label: 'What are the component(s) you would like to remove?', placeholder: 'button', required: true);
}


return Arr::wrap($componentName);
}

public function banner(string $title): void
{
$length = strlen(" {$title}") + 4;

$this->newLine();
$this->line(str_repeat("═", $length));
$this->line(" {$title}");
$this->line(str_repeat("═", $length));
$this->newLine();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Console\Command;
use Sheaf\Cli\Services\ComponentUpdater;

class UpdateCommand extends Command
class UpdateComponentCommand extends Command
{
/**
* The name and signature of the console command.
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/WhoAmICommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class WhoAmICommand extends Command
*/
public function handle()
{
$currentUser = SheafConfig::getConfigFile();
$currentUser = SheafConfig::getCurrentUser();

if(!$currentUser) {
$this->components->warn("You're not log in. please login first with your sheaf account.");
Expand Down
6 changes: 4 additions & 2 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
use Sheaf\Cli\Commands\LogoutCommand;
use Sheaf\Cli\Commands\WhoAmICommand;
use Illuminate\Support\ServiceProvider as SupportServiceProvider;
use Sheaf\Cli\Commands\UpdateCommand;
use Sheaf\Cli\Commands\RemoveComponentCommand;
use Sheaf\Cli\Commands\UpdateComponentCommand;

class ServiceProvider extends SupportServiceProvider
{
Expand All @@ -22,7 +23,8 @@ public function boot()
$this->commands([InstallComponentCommand::class]);
$this->commands([LogoutCommand::class]);
$this->commands([WhoAmICommand::class]);
$this->commands([UpdateCommand::class]);
$this->commands([UpdateComponentCommand::class]);
$this->commands([RemoveComponentCommand::class]);
}

$this->publishes(
Expand Down
19 changes: 19 additions & 0 deletions src/Services/ComponentHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ public function __construct()
$this->baseUrl = config('sheaf.cli.server_url');
$this->token = SheafConfig::getUserToken();
}

public function fetchComponentFilesPath(string $name) {
$url = "{$this->baseUrl}/api/cli/components/$name/files";

$response = Http::asJson()->get($url);

if ($response->failed()) {
$component = Str::of($name)->headline();
$message = array_key_exists('message', $response->json() ?? []) ? $response->json()['message'] : "";

throw new Exception("Failed to install the component '$component'. \n $message");
}

return [
'success' => true,
'data' => $response->collect()
];

}
public function fetchResources(string $componentName)
{

Expand Down
141 changes: 141 additions & 0 deletions src/Services/ComponentRemover.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php


namespace Sheaf\Cli\Services;

use Illuminate\Support\Facades\File;
use Symfony\Component\Console\Output\ConsoleOutput;


class ComponentRemover
{
protected $output;
protected $componentName;

public function __construct()
{
$this->output = new ConsoleOutput();
}

public function remove($name)
{
$this->componentName = $name;

$this->deleteComponentFiles();

$this->cleaningSheafLock($name);
}

protected function deleteComponentFiles()
{
$componentDirectory = resource_path("views/components/ui/{$this->componentName}");

if (File::isDirectory($componentDirectory)) {
File::deleteDirectory($componentDirectory);
$this->message("+ Deleted directory: $componentDirectory");
}

$componentFile = resource_path("views/components/ui/{$this->componentName}.blade.php");

if (File::exists($componentFile)) {
File::delete($componentFile);
}
}

protected function cleaningSheafLock($name)
{
$sheafLock = SheafConfig::loadSheafLock();

$this->cleaningFiles($sheafLock);

$this->cleaningDependencies($sheafLock);

$this->cleaningHelpers($sheafLock);

$this->updateSheafFile();

SheafConfig::saveSheafLock($sheafLock);
}

protected function cleaningFiles(&$sheafLock)
{
foreach ($sheafLock['files'] as $path => $components) {
$remainingComponents = $this->removeComponentFromList($components);

if (empty($remainingComponents)) {
$this->deleteFile($path);
unset($sheafLock['files'][$path]);
$this->message("+ Removed File: $path");
} else {
$sheafLock['files'][$path] = $remainingComponents;
}
}
}

protected function cleaningDependencies(&$sheafLock)
{
foreach ($sheafLock['internalDependencies'] as $dep => $components) {
$remainingComponents = $this->removeComponentFromList($components);

if (empty($remainingComponents)) {
$remover = new self();

$remover->remove($dep);
unset($sheafLock['internalDependencies'][$dep]);
$this->message("+ Removed internal dependency: $dep (no longer used.)");
} else {
$sheafLock['internalDependencies'][$dep] = $remainingComponents;
}
}
}

protected function cleaningHelpers(&$sheafLock)
{
foreach ($sheafLock['helpers'] as $helper => $components) {
$remainingComponents = $this->removeComponentFromList($components);

if (empty($remainingComponents)) {
$this->deleteHelperFile($helper);
unset($sheafLock['helpers'][$helper]);
$this->message("+ Removed helper: $helper (no longer used.)");
} else {
$sheafLock['helpers'][$helper] = $remainingComponents;
}
}
}

protected function updateSheafFile()
{
$sheafFile = SheafConfig::getSheafFile();

unset($sheafFile['components'][$this->componentName]);

SheafConfig::saveSheafFile($sheafFile);
}

protected function removeComponentFromList($components)
{
return array_values(array_filter($components, fn($c) => $c !== $this->componentName));
}

protected function deleteFile($path)
{
if (File::exists($path)) {
File::delete($path);
}
}

protected function deleteHelperFile($helper)
{
$path = resource_path("views/components/ui/$helper.blade.php");

if (File::exists($path)) {
File::delete($path);
}
}

protected function message(string $message)
{
$this->output->writeln("<fg=green>$message</fg=green>");
}
}
8 changes: 4 additions & 4 deletions src/Services/ComponentUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ public function installComponent()

public function needsUpdate($lastModified)
{
$installedComponents = SheafConfig::getInstalledComponents();
$sheafFile = SheafConfig::getSheafFile();

if (!$installedComponents) {
if (!$sheafFile) {
return true;
}

if (!array_key_exists($this->component, $installedComponents['components'])) {
if (!array_key_exists($this->component, $sheafFile['components'])) {
return true;
}

return $installedComponents['components'][$this->component]['installationTime'] < $lastModified;
return $sheafFile['components'][$this->component]['installationTime'] < $lastModified;
}


Expand Down
23 changes: 23 additions & 0 deletions src/Services/JavaScriptAssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public function SetupLivewire()
protected function createUtilsFile(): bool
{
$path = resource_path("js/utils.js");
$this->updateSheafLock("resources/js/utils.js", "dark-theme");

if (File::exists($path) && !$this->forceOverwrite) {
$this->command->warn("File already exists: utils.js");
Expand Down Expand Up @@ -301,4 +302,26 @@ protected function hasLivewire3(): bool

return version_compare($version, 'v3.0.0', '>=');
}

protected function updateSheafLock($path, $name) {

$sheafLockPath = base_path('sheaf-lock.json');

$sheafLock = [];

if(File::exists($sheafLockPath)) {
$sheafLock = json_decode(File::get($sheafLockPath), true) ?: [];
}

$sheafLock['files'] ??= [];
$sheafLock['files'][$path] ??= [];

if(!in_array($name, $sheafLock['files'][$path], true)) {
$sheafLock['files'][$path][] = $name;
}

File::put($sheafLockPath, json_encode($sheafLock, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));

}

}
Loading