Skip to content

Commit

Permalink
Merge pull request #1393 from hydephp/improve-source-file-creator-act…
Browse files Browse the repository at this point in the history
…ions

Improve source file creator actions
  • Loading branch information
caendesilva committed Oct 21, 2023
2 parents 0dd4cf1 + 3454d57 commit 2726876
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 9 deletions.
3 changes: 2 additions & 1 deletion RELEASE_NOTES.md
Expand Up @@ -10,7 +10,8 @@ This serves two purposes:
2. At release time, you can move the Unreleased section changes into a new release version section.

### Added
- for new features.
- Added support for setting custom content when calling source file creator actions directly in https://github.com/hydephp/develop/pull/1393
- Added support for setting a custom post date when calling post file creator action directly in https://github.com/hydephp/develop/pull/1393

### Changed
- for changes in existing functionality.
Expand Down
Expand Up @@ -7,10 +7,9 @@
use Hyde\Framework\Exceptions\FileConflictException;
use Hyde\Facades\Filesystem;
use Hyde\Pages\MarkdownPost;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;

use function date;

/**
* Offloads logic for the make:post command.
*
Expand All @@ -28,6 +27,7 @@ class CreatesNewMarkdownPostFile
protected string $author;
protected string $date;
protected string $identifier;
protected ?string $customContent;

/**
* Construct the class.
Expand All @@ -36,15 +36,18 @@ class CreatesNewMarkdownPostFile
* @param string|null $description The Post Meta Description.
* @param string|null $category The Primary Post Category.
* @param string|null $author The Username of the Author.
* @param string|null $date Optionally specify a custom date.
* @param string|null $customContent Optionally specify custom post content.
*/
public function __construct(string $title, ?string $description, ?string $category, ?string $author)
public function __construct(string $title, ?string $description, ?string $category, ?string $author, ?string $date = null, ?string $customContent = null)
{
$this->title = $title;
$this->description = $description ?? 'A short description used in previews and SEO';
$this->category = $category ?? 'blog';
$this->author = $author ?? 'default';
$this->customContent = $customContent;

$this->date = date('Y-m-d H:i');
$this->date = Carbon::make($date ?? Carbon::now())->format('Y-m-d H:i');
$this->identifier = Str::slug($title);
}

Expand All @@ -58,7 +61,7 @@ public function __construct(string $title, ?string $description, ?string $catego
*/
public function save(bool $force = false): string
{
$page = new MarkdownPost($this->identifier, $this->toArray(), '## Write something awesome.');
$page = new MarkdownPost($this->identifier, $this->toArray(), $this->customContent ?? '## Write something awesome.');

if ($force !== true && Filesystem::exists($page->getSourcePath())) {
throw new FileConflictException($page->getSourcePath());
Expand Down
Expand Up @@ -13,6 +13,8 @@
use Hyde\Framework\Concerns\InteractsWithDirectories;
use Hyde\Framework\Exceptions\UnsupportedPageTypeException;

use function trim;
use function sprintf;
use function file_put_contents;
use function file_exists;
use function basename;
Expand All @@ -37,14 +39,17 @@ class CreatesNewPageSourceFile
protected string $subDir = '';
protected bool $force;

public function __construct(string $title, string $pageClass = MarkdownPage::class, bool $force = false)
protected ?string $customContent;

public function __construct(string $title, string $pageClass = MarkdownPage::class, bool $force = false, ?string $customContent = null)
{
$this->validateType($pageClass);
$this->pageClass = $pageClass;

$this->title = $this->parseTitle($title);
$this->filename = $this->fileName($title);
$this->force = $force;
$this->customContent = $customContent;

$this->outputPath = $this->makeOutputPath($pageClass);
}
Expand Down Expand Up @@ -100,7 +105,7 @@ protected function createBladeFile(): void
@php(\$title = "$this->title")
<main class="mx-auto max-w-7xl py-16 px-8">
<h1 class="text-center text-3xl font-bold">$this->title</h1>
{$this->getBladePageContent()}
</main>
@endsection
Expand All @@ -111,7 +116,7 @@ protected function createBladeFile(): void

protected function createMarkdownFile(): void
{
(new MarkdownPage($this->formatIdentifier(), ['title' => $this->title], "# $this->title"))->save();
(new MarkdownPage($this->formatIdentifier(), ['title' => $this->title], $this->getMarkdownPageContent()))->save();
}

protected function createDocumentationFile(): void
Expand All @@ -137,4 +142,18 @@ protected function failIfFileCannotBeSaved(string $path): void
throw new FileConflictException($path);
}
}

protected function getBladePageContent(): string
{
$baseContent = "<h1 class=\"text-center text-3xl font-bold\">$this->title</h1>";

return $this->customContent
? trim(sprintf("%s\n\n <div>\n %s\n </div>", $baseContent, $this->customContent))
: $baseContent;
}

protected function getMarkdownPageContent(): string
{
return trim(sprintf("# $this->title\n\n%s", $this->customContent ?? ''));
}
}
Expand Up @@ -133,6 +133,57 @@ public function test_that_a_documentation_file_can_be_created_and_contains_expec
Filesystem::unlink('_docs/test-page.md');
}

public function test_that_a_markdown_file_can_be_created_with_custom_content()
{
(new CreatesNewPageSourceFile('Test Page', customContent: 'Hello World!'))->save();

$this->assertFileExists(Hyde::path('_pages/test-page.md'));

$this->assertSame(
<<<'MARKDOWN'
---
title: 'Test Page'
---
# Test Page
Hello World!
MARKDOWN
,
file_get_contents(Hyde::path('_pages/test-page.md'))
);
Filesystem::unlink('_pages/test-page.md');
}

public function test_that_a_blade_file_can_be_created_with_custom_content()
{
(new CreatesNewPageSourceFile('Test Page', BladePage::class, customContent: 'Hello World!'))->save();

$this->assertFileExists(Hyde::path('_pages/test-page.blade.php'));

$this->assertEquals(
<<<'BLADE'
@extends('hyde::layouts.app')
@section('content')
@php($title = "Test Page")
<main class="mx-auto max-w-7xl py-16 px-8">
<h1 class="text-center text-3xl font-bold">Test Page</h1>
<div>
Hello World!
</div>
</main>
@endsection
BLADE, file_get_contents(Hyde::path('_pages/test-page.blade.php'))
);

Filesystem::unlink('_pages/test-page.blade.php');
}

public function test_that_the_file_path_can_be_returned()
{
$this->assertSame(
Expand Down
Expand Up @@ -12,6 +12,7 @@

/**
* @covers \Hyde\Console\Commands\MakePageCommand
* @covers \Hyde\Framework\Actions\CreatesNewPageSourceFile
*/
class MakePageCommandTest extends TestCase
{
Expand Down
Expand Up @@ -8,6 +8,10 @@
use Hyde\Hyde;
use Hyde\Testing\TestCase;

/**
* @covers \Hyde\Console\Commands\MakePostCommand
* @covers \Hyde\Framework\Actions\CreatesNewMarkdownPostFile
*/
class MakePostCommandTest extends TestCase
{
public function test_command_has_expected_output_and_creates_valid_file()
Expand Down
104 changes: 104 additions & 0 deletions packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing\Unit;

use Hyde\Hyde;
use Hyde\Testing\TestCase;
use Illuminate\Support\Carbon;
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;

/**
* @covers \Hyde\Framework\Actions\CreatesNewMarkdownPostFile
*
* @see \Hyde\Framework\Testing\Feature\Commands\MakePostCommandTest
*/
class CreatesNewMarkdownPostFileTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

Carbon::setTestNow(Carbon::create(2024));
}

public function testWithDefaultData()
{
$action = new CreatesNewMarkdownPostFile('Example Title', null, null, null);

$this->assertSame([
'title' => 'Example Title',
'description' => 'A short description used in previews and SEO',
'category' => 'blog',
'author' => 'default',
'date' => '2024-01-01 00:00',
], $action->toArray());
}

public function testWithCustomData()
{
$action = new CreatesNewMarkdownPostFile('foo', 'bar', 'baz', 'qux', '2024-06-01 12:20');

$this->assertSame([
'title' => 'foo',
'description' => 'bar',
'category' => 'baz',
'author' => 'qux',
'date' => '2024-06-01 12:20',
], $action->toArray());
}

public function testSave()
{
$action = new CreatesNewMarkdownPostFile('Example Post', null, null, null);
$action->save();

$path = Hyde::path('_posts/example-post.md');

$this->assertSame(<<<'MARKDOWN'
---
title: 'Example Post'
description: 'A short description used in previews and SEO'
category: blog
author: default
date: '2024-01-01 00:00'
---
## Write something awesome.
MARKDOWN, file_get_contents($path));

unlink($path);
}

public function testSaveWithCustomContent()
{
$action = new CreatesNewMarkdownPostFile('Example Post', null, null, null, null, 'Hello World!');
$action->save();

$path = Hyde::path('_posts/example-post.md');

$this->assertSame(<<<'MARKDOWN'
---
title: 'Example Post'
description: 'A short description used in previews and SEO'
category: blog
author: default
date: '2024-01-01 00:00'
---
Hello World!
MARKDOWN, file_get_contents($path));

unlink($path);
}

public function testCustomDateNormalisation()
{
$action = new CreatesNewMarkdownPostFile('Example Post', null, null, null, 'Jan 1 2024 8am');

$this->assertSame('2024-01-01 08:00', $action->toArray()['date']);
}
}

0 comments on commit 2726876

Please sign in to comment.