Skip to content

Commit

Permalink
Merge pull request #3 from WendellAdriel/feature/command
Browse files Browse the repository at this point in the history
Command to Create Commands
  • Loading branch information
erikaheidi authored Jun 10, 2023
2 parents cbdb617 + e4cce35 commit 0051503
Show file tree
Hide file tree
Showing 13 changed files with 375 additions and 101 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
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
10 changes: 5 additions & 5 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Validate composer.json and composer.lock
run: composer validate

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run test suite
run: composer test
- name: Check code style
run: composer test:lint

- name: Run php-cs-fixer
run: composer csfix .
- name: Run test suite
run: composer test:unit
81 changes: 81 additions & 0 deletions Command/Create/CommandController.php
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!");
}
}
31 changes: 18 additions & 13 deletions Command/Create/ContentController.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

declare(strict_types=1);

namespace librarianphp\Create;

use Minicli\FileNotFoundException;
use Minicli\Stencil;
use Minicli\Command\CommandController;
use Minicli\FileNotFoundException;
use Minicli\Input;
use Minicli\Stencil;

class ContentController extends CommandController
{
Expand All @@ -14,41 +16,44 @@ class ContentController extends CommandController
*/
public function handle(): void
{
if (!$this->getApp()->config->has('stencil_dir')) {
$this->error("You must define a stencil_dir config option.");
if (! $this->getApp()->config->has('stencil_dir')) {
$this->error('You must define a stencil_dir config option.');

return;
}

if (!$this->getApp()->config->has('stencil_locations')) {
$this->error("You must define a stencil_locations array config option.");
if (! $this->getApp()->config->has('stencil_locations')) {
$this->error('You must define a stencil_locations array config option.');

return;
}

$args = $this->getArgs();
$template_name = $args[3] ?? null;
if (!$template_name) {
if (! $template_name) {
$template_name = 'post';
}

$stencil = new Stencil($this->getApp()->config->stencil_dir);

$input = new Input(' ');

$this->info("Content Title: ");
$this->info('Content Title: ');
$title = $input->read();

$this->info("Content Description: ");
$this->info('Content Description: ');
$description = $input->read();

$content = $stencil->applyTemplate($template_name, [
'title' => $title,
'description' => $description
'description' => $description,
]);

$save_locations = $this->getApp()->config->stencil_locations;

if (!array_key_exists($template_name, $save_locations)) {
$this->error("Save location not found for template $template_name");
if (! array_key_exists($template_name, $save_locations)) {
$this->error("Save location not found for template {$template_name}");

return;
}

Expand All @@ -57,7 +62,7 @@ public function handle(): void
$file = fopen($path . '/' . $save_name, 'a+');

fwrite($file, $content);
$this->info("Content generated at " . $path . '/' . $save_name);
$this->info('Content generated at ' . $path . '/' . $save_name);
}

public function slugify($title)
Expand Down
7 changes: 4 additions & 3 deletions Command/Create/DefaultController.php
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.');
}
}
15 changes: 13 additions & 2 deletions README.md
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
```
65 changes: 38 additions & 27 deletions composer.json
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
}
}
}
Loading

0 comments on commit 0051503

Please sign in to comment.