Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
erikaheidi committed Apr 12, 2023
1 parent bc55d60 commit 869792c
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 53 deletions.
66 changes: 66 additions & 0 deletions Command/Create/ContentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Librarian\Create;

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

class ContentController extends CommandController
{
public function handle(): void
{
if (!$this->getApp()->config->has('stencil_dir')) {
$this->getApp()->getPrinter()->error("You must define a stencil_dir config option.");
return;
}

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

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

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

$input = new Input(' ');

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

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

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

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

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

$path = $save_locations[$template_name];
$save_name = date('Ymd') . '_' . $this->slugify($title) . '.md';
$file = fopen($path . '/' . $save_name, 'a+');

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

public function slugify($title)
{
$slug = strtolower($title);
$slug = str_replace(' ', '-', $slug);

return preg_replace('/[^A-Za-z0-9\-]/', '', $slug);
}
}
15 changes: 15 additions & 0 deletions Command/Create/DefaultController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Librarian\Create;

use Minicli\App;
use Minicli\Command\CommandController;

class DefaultController extends CommandController
{
public function handle(): void
{
$this->getPrinter()->info("./librarian create [subcommand]", true);
$this->getPrinter()->info("Run \"./librarian create content\" to create a content file based on a template.");
}
}
46 changes: 0 additions & 46 deletions Command/Help/DefaultController.php

This file was deleted.

9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# command-demo
demo command repository
# command-create

Librarian's built-in create command.

```shell
./librarian create content [template]
```
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "librarianphp/command-help",
"name": "librarianphp/command-create",
"type": "library",
"description": "Librarian's built-in help command",
"description": "Librarian's built-in command to create new content",
"license": "MIT",
"homepage": "https://github.com/librarianphp/command-demo",
"keywords": ["cli","command-line", "markdown"],
Expand All @@ -11,6 +11,7 @@
}
},
"require": {
"minicli/minicli": "^3.0"
"minicli/minicli": "^3.0",
"minicli/stencil": "^0.1.1"
}
}
46 changes: 45 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion minicli
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ $app = new App([
'app_path' => [
__DIR__ . '/Command'
],
'stencil_dir' => __DIR__ . '/../../librarian/app/Resources/stencil',
'stencil_locations' => [
'post' => __DIR__ . '/../../librarian/app/Resources/data/_p'
],
'debug' => true
]);

$app->setSignature('./minicli help');
$app->setSignature('./minicli create');

try {
$app->runCommand($argv);
Expand Down

0 comments on commit 869792c

Please sign in to comment.