Skip to content

Commit

Permalink
Added sidekick:remove and allow for installation of Tests via prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
PapaRascal2020 committed Sep 28, 2024
1 parent 07d2ab3 commit 5fa07be
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,39 @@ class InstallCommand extends Command
public function handle()
{
$fileSystem = new Filesystem();

// Install resources
$this->components->info('Installing Views');
$fileSystem->ensureDirectoryExists(resource_path('views'));
$fileSystem->copyDirectory(__DIR__.'/../../stubs/default/resources/views', resource_path('views'));

// Install Routes
$this->components->info('Installing Routes');
$fileSystem->ensureDirectoryExists(base_path('routes'));
$fileSystem->copyDirectory(__DIR__.'/../../stubs/default/routes', base_path('routes'));

// Install Tests
if($this->confirm('Install Sidekick tests?')) {
$this->components->info('Installing Tests');
$packageTestDirectory = __DIR__.'/../../tests';

$fileSystem->ensureDirectoryExists(base_path('tests/Unit'));
$fileSystem->ensureDirectoryExists(base_path('tests/Feature'));

$testFiles = $fileSystem->allFiles($packageTestDirectory);

foreach ($testFiles as $testFile) {
$path = $testFile->getRelativePath();
$file = $testFile->getRelativePathName();
$this->components->info('Installing '. $file . ' to ' . base_path('tests/' . $path));
$fileSystem->copy($testFile, base_path('tests/' . $file));
}
}

$fileSystem->copyDirectory(__DIR__.'/../../stubs/default/routes', base_path('routes'));

// Make link to routes in web.php
$this->components->info('Linking Routes');
$webRoutesPath = base_path('routes/web.php');
$routeLink = "\nrequire base_path('routes/web.sidekick.php');\n";
$fileSystem->append($webRoutesPath, $routeLink);
Expand Down
98 changes: 98 additions & 0 deletions src/Console/RemoveCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace PapaRascalDev\Sidekick\Console;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'sidekick:remove', description: 'Uninstalls Sidekick Playground')]
class RemoveCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sidekick:remove';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Uninstalls Sidekick Playground';

/**
* Execute the console command.
*
* @return int|null
*/
public function handle()
{
$fileSystem = new Filesystem();

//Remove specific resources
$viewsPath = resource_path('views');
$stubViewsPath = __DIR__.'/../../stubs/default/resources/views';

if ($fileSystem->exists($viewsPath)) {
$this->removeSpecificFiles($fileSystem, $stubViewsPath, $viewsPath);
}

// Remove existing routes
$this->components->info('Removing web.sidekick.php');
$routesPath = base_path('routes/web.sidekick.php');
if ($fileSystem->exists($routesPath)) {
$fileSystem->delete($routesPath);
}

// Remove Tests
$this->components->info('Checking for tests and removing');
$packageTestDirectory = __DIR__.'/../../tests';

$fileSystem->ensureDirectoryExists(base_path('tests/Unit'));
$fileSystem->ensureDirectoryExists(base_path('tests/Feature'));

$testFiles = $fileSystem->allFiles($packageTestDirectory);

foreach ($testFiles as $testFile) {
$path = $testFile->getRelativePath();
$file = $testFile->getRelativePathName();
$this->components->info('Removing '. $file . ' from ' . base_path('tests/' . $path));
$fileSystem->delete(base_path('tests/' . $file));
}

// Unlink routes in web.php
$this->components->info('Unlinking Routes');
$webRoutesPath = base_path('routes/web.php');
$routeLink = "\nrequire base_path('routes/web.sidekick.php');\n";
$fileSystem->replaceInFile($routeLink, "", $webRoutesPath);

$this->components->warn('As we can\'t be sure to remove any user files, the directories remain in resources, please delete if not needed.' );

$this->components->success("Successfully removed Sidekick Playground");
}

/**
* Remove specific files and directories that were added by the application.
*
* @param Filesystem $fileSystem
* @param string $stubViewsPath
* @param string $viewsPath
* @return void
*/
protected function removeSpecificFiles(Filesystem $fileSystem, string $stubViewsPath, string $viewsPath)
{
$stubFiles = $fileSystem->allFiles($stubViewsPath);

foreach ($stubFiles as $stubFile) {
$relativePath = $stubFile->getRelativePathname();
$targetFile = $viewsPath . DIRECTORY_SEPARATOR . $relativePath;
if ($fileSystem->exists($targetFile)) {
$this->components->info('Removing ' . $relativePath);
$fileSystem->delete($targetFile);
}
}
}
}
2 changes: 2 additions & 0 deletions src/SidekickServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use PapaRascalDev\Sidekick\Console\InstallCommand;
use PapaRascalDev\Sidekick\Console\RemoveCommand;
use PapaRascalDev\Sidekick\Models\SidekickConversation as SidekickConversationModel;

/**
Expand All @@ -21,6 +22,7 @@ public function boot(): void

$this->commands([
InstallCommand::class,
RemoveCommand::class,
]);

$this->initializeMigrations();
Expand Down

0 comments on commit 5fa07be

Please sign in to comment.