generated from librarianphp/command-help
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from WendellAdriel/feature/command
Command to Create Commands
- Loading branch information
Showing
13 changed files
with
375 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
trim_trailing_whitespace = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.{yml,yaml,json}] | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace librarianphp\Create; | ||
|
||
use Minicli\Command\CommandController as MiniCliCommandController; | ||
use Minicli\Input; | ||
|
||
class CommandController extends MiniCliCommandController | ||
{ | ||
public function handle(): void | ||
{ | ||
$input = new Input(' '); | ||
|
||
$this->info('Command: '); | ||
$command = $input->read(); | ||
|
||
$this->createCommandFile($this->buildCommandPath($command), $command); | ||
} | ||
|
||
private function buildCommandPath(string $command): array | ||
{ | ||
$commandsPath = realpath($this->config->app_path[0]); | ||
$commandArray = explode(' ', $command); | ||
$commandPartsCount = count($commandArray); | ||
if ($commandPartsCount > 2) { | ||
$this->error('Command name must be one or two words.'); | ||
|
||
return []; | ||
} | ||
|
||
$commandPath = []; | ||
|
||
do { | ||
$commandPart = array_shift($commandArray); | ||
$commandPart = ucfirst(strtolower($commandPart)); | ||
$commandPath[] = $commandPart; | ||
|
||
if (count($commandArray) === 0 && $commandPartsCount > 1) { | ||
break; | ||
} | ||
|
||
$dir = "{$commandsPath}/" . implode('/', $commandPath); | ||
if (! is_dir($dir)) { | ||
mkdir($dir); | ||
} | ||
} while (count($commandArray) > 0); | ||
|
||
return array_map(fn ($item) => ucfirst(strtolower($item)), $commandPath); | ||
} | ||
|
||
private function createCommandFile(array $commandPath, string $command): void | ||
{ | ||
if ($commandPath === []) { | ||
return; | ||
} | ||
$commandsPath = realpath($this->config->app_path[0]); | ||
$commandName = count($commandPath) > 1 ? array_pop($commandPath) : 'Default'; | ||
$commandClass = "{$commandName}Controller"; | ||
$commandFilePath = realpath("{$commandsPath}/" . implode('/', $commandPath)) . "/{$commandClass}.php"; | ||
|
||
if (file_exists($commandFilePath)) { | ||
$this->error("Command file already exists at {$commandFilePath}"); | ||
|
||
return; | ||
} | ||
|
||
$commandNamespace = 'namespace App\Command' . '\\' . implode('\\', $commandPath); | ||
$commandFileContent = file_get_contents(__DIR__ . '/../../stubs/command.stub'); | ||
$commandFileContent = str_replace( | ||
['{{command_namespace}}', '{{command_class}}', '{{command_name}}'], | ||
[$commandNamespace, $commandClass, $command], | ||
$commandFileContent | ||
); | ||
|
||
file_put_contents($commandFilePath, $commandFileContent); | ||
|
||
$this->success("{$command} command created!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace librarianphp\Create; | ||
|
||
use Minicli\App; | ||
use Minicli\Command\CommandController; | ||
|
||
class DefaultController extends CommandController | ||
{ | ||
public function handle(): void | ||
{ | ||
$this->info("./librarian create [subcommand]", true); | ||
$this->info("Run \"./librarian create content\" to create a content file based on a template."); | ||
$this->info('./librarian create [subcommand]', true); | ||
$this->info('Run "./librarian create content" to create a content file based on a template.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
# command-create | ||
<div align="center"> | ||
<h1>Command Create</h1> | ||
<h4>Librarian's built-in create command.</h4> | ||
</div> | ||
|
||
Librarian's built-in create command. | ||
## Commands Available | ||
|
||
### Create Content | ||
|
||
```shell | ||
./librarian create content [template] | ||
``` | ||
|
||
### Create Command | ||
|
||
```shell | ||
./librarian create command | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,41 @@ | ||
{ | ||
"name": "librarianphp/command-create", | ||
"type": "library", | ||
"description": "Librarian's built-in command to create new content", | ||
"license": "MIT", | ||
"homepage": "https://github.com/librarianphp/command-demo", | ||
"keywords": ["cli","command-line", "markdown"], | ||
"autoload": { | ||
"psr-4": { | ||
"librarianphp\\": "Command/" | ||
} | ||
}, | ||
"require": { | ||
"minicli/minicli": "^4.0", | ||
"minicli/stencil": "^0.1.1" | ||
}, | ||
"require-dev": { | ||
"pestphp/pest": "^2.4", | ||
"friendsofphp/php-cs-fixer": "^3.16" | ||
}, | ||
"scripts": { | ||
"test" : ["pest"], | ||
"csfix": ["php-cs-fixer fix"] | ||
}, | ||
"config": { | ||
"allow-plugins": { | ||
"pestphp/pest-plugin": true | ||
} | ||
"name": "librarianphp/command-create", | ||
"type": "library", | ||
"description": "Librarian's built-in command to create new content", | ||
"license": "MIT", | ||
"homepage": "https://github.com/librarianphp/command-demo", | ||
"keywords": [ | ||
"cli", | ||
"command-line", | ||
"markdown" | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"librarianphp\\": "Command/" | ||
} | ||
}, | ||
"require": { | ||
"php": ">=8.1", | ||
"minicli/minicli": "^4.0", | ||
"minicli/stencil": "^0.1.1" | ||
}, | ||
"require-dev": { | ||
"pestphp/pest": "^2.6", | ||
"friendsofphp/php-cs-fixer": "^3.17", | ||
"laravel/pint": "^1.10" | ||
}, | ||
"scripts": { | ||
"lint": ["pint"], | ||
"test:lint": ["pint --test"], | ||
"test:unit": ["pest"], | ||
"test": [ | ||
"@test:lint", | ||
"@test:unit" | ||
] | ||
}, | ||
"config": { | ||
"allow-plugins": { | ||
"pestphp/pest-plugin": true | ||
} | ||
} | ||
} |
Oops, something went wrong.