Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsam726 authored and github-actions[bot] committed Apr 6, 2024
1 parent 178cb89 commit 22e7c41
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 34 deletions.
6 changes: 4 additions & 2 deletions src/Commands/ActivateModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Savannabits\Modular\Facades\Modular;

use function Laravel\Prompts\text;

class ActivateModuleCommand extends Command
{

public $signature = 'modular:activate {name?}';

public $description = 'Activate a module';

private string $moduleName;

public function handle(): void
Expand All @@ -24,7 +26,7 @@ public function handle(): void
private function activateModule(): void
{
$moduleName = $this->moduleName;
$repoName = config('modular.vendor','modular').'/'.$moduleName;
$repoName = config('modular.vendor', 'modular').'/'.$moduleName;
Modular::execCommand('composer require '.$repoName.':@dev');
Modular::execCommand("php artisan $moduleName:install");
}
Expand Down
6 changes: 4 additions & 2 deletions src/Commands/DeactivateModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Savannabits\Modular\Facades\Modular;

use function Laravel\Prompts\text;

class DeactivateModuleCommand extends Command
{

public $signature = 'modular:deactivate {name?}';

public $description = 'Deactivate a module';

private string $moduleName;

public function handle(): void
Expand All @@ -24,7 +26,7 @@ public function handle(): void
private function deactivateModule(): void
{
$moduleName = $this->moduleName;
$repoName = config('modular.vendor','modular').'/'.$moduleName;
$repoName = config('modular.vendor', 'modular').'/'.$moduleName;
Modular::execCommand("composer remove $repoName");
}
}
42 changes: 26 additions & 16 deletions src/Commands/MakeModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,25 @@
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Savannabits\Modular\Support\Concerns\CanManipulateFiles;

use function Laravel\Prompts\text;

class MakeModuleCommand extends Command
{
use CanManipulateFiles;

public $signature = 'modular:make {name?} {--F|force}';

public $description = 'Create a new module';

private string $moduleName;

private string $moduleTitle;

private string $moduleNamespace;

private string $modulePath;

private string $moduleStudlyName;

public function handle()
Expand All @@ -27,8 +35,8 @@ public function handle()
$this->moduleName = Str::of($name)->kebab()->toString();
$this->moduleStudlyName = Str::of($name)->studly()->toString();
$this->moduleTitle = Str::of($name)->kebab()->title()->replace('-', ' ')->toString();
$this->moduleNamespace = config('modular.namespace') . '\\' . Str::of($name)->studly()->toString();
$this->modulePath = config('modular.path') . '/' . $this->moduleName;
$this->moduleNamespace = config('modular.namespace').'\\'.Str::of($name)->studly()->toString();
$this->modulePath = config('modular.path').'/'.$this->moduleName;
$this->info("Creating module: $this->moduleName in $this->modulePath");

$this->generateModuleDirectories();
Expand All @@ -39,18 +47,20 @@ public function handle()
private function generateModuleDirectories(): bool
{
$directories = config('modular.directory_tree');
if (!count($directories)) {
if (! count($directories)) {
$this->error('No directories found in the configuration file');

return false;
}
foreach ($directories as $directory) {
$path = $this->modulePath . '/' . ltrim($directory,'/');
if (!is_dir($path)) {
$path = $this->modulePath.'/'.ltrim($directory, '/');
if (! is_dir($path)) {
mkdir($path, 0775, true);
$this->info("Created directory: $path");
}
}
$this->info('Module directories created successfully');

return true;
}

Expand All @@ -62,28 +72,28 @@ private function generateModuleFiles(): void
} catch (FileNotFoundException|NotFoundExceptionInterface|ContainerExceptionInterface $e) {
$this->error($e->getMessage());
}
// $this->generateModuleFacade();
// $this->generateModuleFacade();
}

private function generateModuleComposerFile(): void
{
$composerJson = [
'name' => config('modular.vendor') . '/' . $this->moduleName,
'name' => config('modular.vendor').'/'.$this->moduleName,
'type' => 'library',
'description' => $this->moduleTitle . ' module',
'description' => $this->moduleTitle.' module',
'require' => [
'php' => '^8.2',
],
'autoload' => [
'psr-4' => [
$this->moduleNamespace . '\\' => 'src/',
$this->moduleNamespace . '\\Database\\Factories\\' => 'database/factories/',
$this->moduleNamespace . '\\Database\\Seeders\\' => 'database/seeders/',
$this->moduleNamespace.'\\' => 'src/',
$this->moduleNamespace.'\\Database\\Factories\\' => 'database/factories/',
$this->moduleNamespace.'\\Database\\Seeders\\' => 'database/seeders/',
],
],
'autoload-dev' => [
'psr-4' => [
$this->moduleNamespace . '\\Tests\\' => 'tests/',
$this->moduleNamespace.'\\Tests\\' => 'tests/',
],
],
'config' => [
Expand All @@ -96,12 +106,12 @@ private function generateModuleComposerFile(): void
'extra' => [
'laravel' => [
'providers' => [
$this->moduleNamespace . '\\' . $this->moduleStudlyName . 'ServiceProvider',
$this->moduleNamespace.'\\'.$this->moduleStudlyName.'ServiceProvider',
],
],
],
];
$composerJsonPath = $this->modulePath . '/composer.json';
$composerJsonPath = $this->modulePath.'/composer.json';
file_put_contents($composerJsonPath, json_encode($composerJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
$this->info("Created composer.json file: $composerJsonPath");
}
Expand All @@ -113,9 +123,9 @@ private function generateModuleComposerFile(): void
*/
private function generateModuleServiceProvider(): void
{
$path = $this->modulePath . '/src/' . $this->moduleStudlyName . 'ServiceProvider.php';
$path = $this->modulePath.'/src/'.$this->moduleStudlyName.'ServiceProvider.php';
$namespace = $this->moduleNamespace;
$class = $this->moduleStudlyName . 'ServiceProvider';
$class = $this->moduleStudlyName.'ServiceProvider';
$this->copyStubToApp('module.provider', $path, [
'class' => $class,
'namespace' => $namespace,
Expand Down
17 changes: 10 additions & 7 deletions src/ModularServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
use Spatie\LaravelPackageTools\Commands\InstallCommand;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class ModularServiceProvider extends PackageServiceProvider
{
public static string $name = 'modular';

public static string $vendor = 'savannabits';

public static string $viewNamespace = 'modular';

public function configurePackage(Package $package): void
{
/*
Expand All @@ -30,8 +34,7 @@ public function configurePackage(Package $package): void
$command
->askToStarRepoOnGitHub($name)
->startWith(fn (InstallCommand $command) => $this->installationSteps($command));
})
;
});
$this->mergeConfigFrom($this->package->basePath('/../config/modular.php'), 'modular');
}

Expand All @@ -40,10 +43,10 @@ private function configureComposerMerge(InstallCommand $command): void
$command->comment('Configuring Composer merge plugin:');
$composerJson = json_decode(file_get_contents(base_path('composer.json')), true);
// Add the modules repositories into compose if they don't exist
if (!isset($composerJson['repositories'])){
if (! isset($composerJson['repositories'])) {
$composerJson['repositories'] = [];
}
if (!collect($composerJson['repositories'])->contains(fn ($repo) => $repo['type'] === 'path' && $repo['url'] === config('modular.path').'/*')) {
if (! collect($composerJson['repositories'])->contains(fn ($repo) => $repo['type'] === 'path' && $repo['url'] === config('modular.path').'/*')) {
$composerJson['repositories'][] = [
'type' => 'path',
'url' => config('modular.path').'/*',
Expand All @@ -52,7 +55,7 @@ private function configureComposerMerge(InstallCommand $command): void
],
];
}
if (!isset($composerJson['extra']['merge-plugin'])) {
if (! isset($composerJson['extra']['merge-plugin'])) {
$composerJson['extra']['merge-plugin'] = [
'include' => [
'modules/*/composer.json',
Expand All @@ -65,7 +68,7 @@ private function configureComposerMerge(InstallCommand $command): void
];

// Ensure the composer-merge-plugin is in the list of allowed plugins
if (!isset($composerJson['config']['allow-plugins'])) {
if (! isset($composerJson['config']['allow-plugins'])) {
$composerJson['config']['allow-plugins'] = [];
}
// If allowed-plugins is set to true, disregard
Expand Down Expand Up @@ -93,7 +96,7 @@ private function ensureModularPathExists(InstallCommand $command): void
{
$command->comment('Ensuring modular path exists:');
$path = config('modular.path');
if (!is_dir($path)) {
if (! is_dir($path)) {
mkdir($path, 0755, true);
$command->info("Directory $path created successfully");
} else {
Expand Down
14 changes: 7 additions & 7 deletions src/Support/Concerns/CanManipulateFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
trait CanManipulateFiles
{
/**
* @param array<string> $paths
* @param array<string> $paths
*/
protected function checkForCollision(array $paths): bool
{
foreach ($paths as $path) {
if (!$this->fileExists($path)) {
if (! $this->fileExists($path)) {
continue;
}

if (!confirm(basename($path) . ' already exists, do you want to overwrite it?')) {
if (! confirm(basename($path).' already exists, do you want to overwrite it?')) {
$this->components->error("{$path} already exists, aborting.");

return true;
Expand All @@ -39,7 +39,7 @@ protected function checkForCollision(array $paths): bool
}

/**
* @param array<string, string> $replacements
* @param array<string, string> $replacements
*
* @throws FileNotFoundException
* @throws ContainerExceptionInterface
Expand All @@ -50,15 +50,15 @@ protected function copyStubToApp(string $stub, string $targetPath, array $replac
$filesystem = app(Filesystem::class);

// Get the correct stub path whether published or not
$stubPath = $this->getDefaultStubPath() . "/{$stub}.stub";
$stubPath = $this->getDefaultStubPath()."/{$stub}.stub";

$stub = str($filesystem->get($stubPath));

foreach ($replacements as $key => $replacement) {
$stub = $stub->replace("{{ {$key} }}", $replacement);
}

$stub = (string)$stub;
$stub = (string) $stub;

$this->writeFile($targetPath, $stub);
}
Expand All @@ -85,7 +85,7 @@ protected function getDefaultStubPath(): string
{
$reflectionClass = new ReflectionClass($this);

return (string)str($reflectionClass->getFileName())
return (string) str($reflectionClass->getFileName())
->beforeLast('Commands')
->append('../stubs');
}
Expand Down

0 comments on commit 22e7c41

Please sign in to comment.