Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add package clone command #24

Merged
merged 1 commit into from
Apr 16, 2024
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
121 changes: 121 additions & 0 deletions src/Commands/ClonePackageCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace AdminKit\Core\Commands;

use Exception;
use Illuminate\Console\Command;
use Illuminate\Process\Exceptions\ProcessFailedException;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;

class ClonePackageCommand extends Command
{
protected $signature = 'admin-kit:clone {url}';

protected $description = 'Clone AdminKit package into composer-packages';

public function handle(): void
{
$url = $this->argument('url');

try {
$packageName = $this->getPackageName($url);

$destination = "composer-packages/ibecsystems/{$packageName}";

if (! $this->hasPackage($destination)) {
$this->comment("Cloning package...\n");
$this->clonePackage($url, $destination);
} else {
$this->comment('Package already cloned. Skipping...');
}

if ($this->hasPHPStormConfig()) {
if (! $this->hasDirectoryMapping($destination)) {
$this->comment("[PHPStorm] Adding Directory Mapping...\n");
$this->addDirectoryMapping($destination);
} else {
$this->comment('[PHPStorm] Directory Mapping already added. Skipping...');
}
}

$this->info("Package ready for development: {$destination}");
} catch (Exception $exception) {
$this->error($exception->getMessage());
}
}

/**
* @throws Exception
*/
private function getPackageName(string $url): string
{
$repoName = explode('/', $url)[1] ?? null;

if (! $repoName) {
throw new Exception("Invalid repository url: {$url}");
}

if (str_contains($repoName, '.git')) {
$repoName = str_replace('.git', '', $repoName);
}

if (! is_string($repoName)) {
throw new Exception("Error while parsing repository url: {$url}");
}

return $repoName;
}

private function hasPackage(string $destination): bool
{
return File::isDirectory(base_path($destination));
}

/**
* @throws Exception
*/
private function clonePackage(string $url, string $destination): void
{
try {
Process::run("git clone {$url} {$destination}")
->throw();
} catch (ProcessFailedException $e) {
throw new Exception('Cannot clone package: '.$e->getMessage());
}
}

private function hasPHPStormConfig(): bool
{
return File::isDirectory(base_path('.idea'));
}

/**
* @throws Exception
*/
private function hasDirectoryMapping(string $destination): bool
{
$vcsPath = base_path('.idea/vcs.xml');

$xml = simplexml_load_file($vcsPath);

$mapping = $xml->xpath("//mapping[@directory='\$PROJECT_DIR\$/{$destination}']");

return (bool) $mapping;
}

private function addDirectoryMapping(string $destination): void
{
$vcsPath = base_path('.idea/vcs.xml');

$xml = simplexml_load_file($vcsPath);

$components = $xml->xpath('//component[@name="VcsDirectoryMappings"]');

$mapping = $components[0]->addChild('mapping');
$mapping->addAttribute('directory', "\$PROJECT_DIR\$/{$destination}");
$mapping->addAttribute('vcs', 'Git');

$xml->asXML($vcsPath);
}
}
6 changes: 5 additions & 1 deletion src/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace AdminKit\Core;

use AdminKit\Core\Commands\ClonePackageCommand;
use AdminKit\Core\Commands\InstallCommand;
use AdminKit\Core\Providers\FilamentServiceProvider;
use AdminKit\Core\Providers\MiddlewareServiceProvider;
Expand All @@ -24,7 +25,10 @@ public function configurePackage(Package $package): void
->hasMigration('create_admin_kit_users_table')
->hasTranslations()
->hasRoute('api')
->hasCommand(InstallCommand::class);
->hasCommands([
InstallCommand::class,
ClonePackageCommand::class,
]);
}

public function registeringPackage()
Expand Down