mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-16 06:04:03 +00:00
[4.x] Update commands CLI outputs (#968)
* Using laravel components * Ensure commands returns success * update tests * clean * bump EndBug version * Update ci.yml * Update ci.yml * Update ci.yml * revert CI changes * Update ci.yml * Update ci.yml * Update ci.yml * revert CI changes to it's original state * fix typos, improve code * improve Install & TenantList commands * php-cs-fixer * type GitHub properly Co-authored-by: Abrar Ahmad <abrar.dev99@gmail.com> Co-authored-by: Samuel Štancl <samuel@archte.ch> Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
parent
05f1b2d6f5
commit
e4f5b92485
15 changed files with 217 additions and 96 deletions
|
|
@ -4,50 +4,136 @@ declare(strict_types=1);
|
|||
|
||||
namespace Stancl\Tenancy\Commands;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class Install extends Command
|
||||
{
|
||||
protected $signature = 'tenancy:install';
|
||||
|
||||
protected $description = 'Install stancl/tenancy.';
|
||||
protected $description = 'Install Tenancy for Laravel.';
|
||||
|
||||
public function handle(): void
|
||||
public function handle(): int
|
||||
{
|
||||
$this->comment('Installing stancl/tenancy...');
|
||||
$this->callSilent('vendor:publish', [
|
||||
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
|
||||
'--tag' => 'config',
|
||||
]);
|
||||
$this->info('✔️ Created config/tenancy.php');
|
||||
$this->step(
|
||||
name: 'Publishing config file',
|
||||
tag: 'config',
|
||||
file: 'config/tenancy.php',
|
||||
newLineBefore: true,
|
||||
);
|
||||
|
||||
if (! file_exists(base_path('routes/tenant.php'))) {
|
||||
$this->callSilent('vendor:publish', [
|
||||
$this->step(
|
||||
name: 'Publishing routes [routes/tenant.php]',
|
||||
tag: 'routes',
|
||||
file: 'routes/tenant.php',
|
||||
);
|
||||
|
||||
$this->step(
|
||||
name: 'Publishing service provider',
|
||||
tag: 'providers',
|
||||
file: 'app/Providers/TenancyServiceProvider.php',
|
||||
);
|
||||
|
||||
$this->step(
|
||||
name: 'Publishing migrations',
|
||||
tag: 'migrations',
|
||||
files: [
|
||||
'database/migrations/2019_09_15_000010_create_tenants_table.php',
|
||||
'database/migrations/2019_09_15_000020_create_domains_table.php',
|
||||
],
|
||||
warning: 'Migrations already exist',
|
||||
);
|
||||
|
||||
$this->step(
|
||||
name: 'Creating [database/migrations/tenant] folder',
|
||||
task: fn () => mkdir(database_path('migrations/tenant')),
|
||||
unless: is_dir(database_path('migrations/tenant')),
|
||||
warning: 'Folder [database/migrations/tenant] already exists.',
|
||||
newLineAfter: true,
|
||||
);
|
||||
|
||||
$this->components->info('✨️ Tenancy for Laravel successfully installed.');
|
||||
|
||||
$this->askForSupport();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a step of the installation process.
|
||||
*
|
||||
* @param string $name The name of the step.
|
||||
* @param Closure|null $task The task code.
|
||||
* @param bool $unless Condition specifying when the task should NOT run.
|
||||
* @param string|null $warning Warning shown when the $unless condition is true.
|
||||
* @param string|null $file Name of the file being added.
|
||||
* @param string|null $tag The tag being published.
|
||||
* @param array|null $files Names of files being added.
|
||||
* @param bool $newLineBefore Should a new line be printed after the step.
|
||||
* @param bool $newLineAfter Should a new line be printed after the step.
|
||||
*/
|
||||
protected function step(
|
||||
string $name,
|
||||
Closure $task = null,
|
||||
bool $unless = false,
|
||||
string $warning = null,
|
||||
string $file = null,
|
||||
string $tag = null,
|
||||
array $files = null,
|
||||
bool $newLineBefore = false,
|
||||
bool $newLineAfter = false,
|
||||
): void {
|
||||
if ($file) {
|
||||
$name .= " [$file]"; // Append clickable path to the task name
|
||||
$unless = file_exists(base_path($file)); // Make the condition a check for the file's existence
|
||||
$warning = "File [$file] already exists."; // Make the warning a message about the file already existing
|
||||
}
|
||||
|
||||
if ($tag) {
|
||||
$task = fn () => $this->callSilent('vendor:publish', [
|
||||
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
|
||||
'--tag' => 'routes',
|
||||
'--tag' => $tag,
|
||||
]);
|
||||
$this->info('✔️ Created routes/tenant.php');
|
||||
}
|
||||
|
||||
if ($files) {
|
||||
// Show a warning if any of the files already exist
|
||||
$unless = count(array_filter($files, fn ($file) => file_exists(base_path($file)))) !== 0;
|
||||
}
|
||||
|
||||
if (! $unless) {
|
||||
if ($newLineBefore) {
|
||||
$this->newLine();
|
||||
}
|
||||
|
||||
$this->components->task($name, $task ?? fn () => null);
|
||||
|
||||
if ($files) {
|
||||
// Print out a clickable list of the added files
|
||||
$this->components->bulletList(array_map(fn (string $file) => "[$file]", $files));
|
||||
}
|
||||
|
||||
if ($newLineAfter) {
|
||||
$this->newLine();
|
||||
}
|
||||
} else {
|
||||
$this->info('Found routes/tenant.php.');
|
||||
$this->components->warn($warning);
|
||||
}
|
||||
}
|
||||
|
||||
$this->callSilent('vendor:publish', [
|
||||
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
|
||||
'--tag' => 'providers',
|
||||
]);
|
||||
$this->info('✔️ Created TenancyServiceProvider.php');
|
||||
|
||||
$this->callSilent('vendor:publish', [
|
||||
'--provider' => 'Stancl\Tenancy\TenancyServiceProvider',
|
||||
'--tag' => 'migrations',
|
||||
]);
|
||||
$this->info('✔️ Created migrations. Remember to run [php artisan migrate]!');
|
||||
|
||||
if (! is_dir(database_path('migrations/tenant'))) {
|
||||
mkdir(database_path('migrations/tenant'));
|
||||
$this->info('✔️ Created database/migrations/tenant folder.');
|
||||
/** If the user accepts, opens the GitHub project in the browser. */
|
||||
public function askForSupport(): void
|
||||
{
|
||||
if ($this->components->confirm('Would you like to show your support by starring the project on GitHub?', true)) {
|
||||
if (PHP_OS_FAMILY === 'Darwin') {
|
||||
exec('open https://github.com/archtechx/tenancy');
|
||||
}
|
||||
if (PHP_OS_FAMILY === 'Windows') {
|
||||
exec('start https://github.com/archtechx/tenancy');
|
||||
}
|
||||
if (PHP_OS_FAMILY === 'Linux') {
|
||||
exec('xdg-open https://github.com/archtechx/tenancy');
|
||||
}
|
||||
}
|
||||
|
||||
$this->comment('✨️ stancl/tenancy installed successfully.');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue