Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert GeneratesDocumentationSearchIndex Action into Service #457

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
f4ce481
Replace redundant 'static' in final class with self
caendesilva Aug 31, 2022
0c4c87e
Move GeneratesDocumentationSearchIndexFile into Services namespace
caendesilva Aug 31, 2022
9bed35a
Rename GeneratesDocumentationSearchIndexFile to DocumentationSearchSe…
caendesilva Aug 31, 2022
c84bb51
Move test into services namespace
caendesilva Aug 31, 2022
3b7674a
Rename test to match renamed class
caendesilva Aug 31, 2022
7a388b4
Apply fixes from StyleCI
StyleCIBot Aug 31, 2022
87ae3e1
Switch internal method names
caendesilva Aug 31, 2022
932d2c4
Change action to service in test
caendesilva Aug 31, 2022
95abee6
Update PHPDoc
caendesilva Aug 31, 2022
715efa2
Merge branch 'master' into 435-convert-generatesdocumentationsearchin…
caendesilva Aug 31, 2022
1c521da
Handle search page generation in service
caendesilva Aug 31, 2022
d6dd8d7
Apply fixes from StyleCI
StyleCIBot Aug 31, 2022
6cf77a9
Use helper instead of creating extra variable
caendesilva Aug 31, 2022
587061c
Convert 'sprintf()' call to string interpolation
caendesilva Aug 31, 2022
f35c8d6
Add comment and format code
caendesilva Aug 31, 2022
7789244
Collapse formatting logic
caendesilva Aug 31, 2022
ca7df72
Update estimate formatting to match new format
caendesilva Aug 31, 2022
36c3898
Normalize directory separators in output path message
caendesilva Aug 31, 2022
5e2b9ff
Use greater or equal than comparison
caendesilva Aug 31, 2022
41e3996
Update guesstimation message tests
caendesilva Aug 31, 2022
a33f1c2
Apply fixes from StyleCI
StyleCIBot Aug 31, 2022
72b577f
Create output message helper to reduce repeated code
caendesilva Aug 31, 2022
d4a01b2
Apply fixes from StyleCI
StyleCIBot Aug 31, 2022
2b5dd36
Convert concatenation to string interpolation
caendesilva Aug 31, 2022
b11b196
Add PHPDoc
caendesilva Aug 31, 2022
f9207a2
No need to convert an absolute path to relative just to make it absolute
caendesilva Aug 31, 2022
1dab93e
Format code
caendesilva Aug 31, 2022
6f412ab
Collapse method chainable method calls
caendesilva Aug 31, 2022
db88e44
Resolve todo
caendesilva Aug 31, 2022
0d31143
Apply fixes from StyleCI
StyleCIBot Aug 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
@php
$page = Hyde\Framework\Models\Pages\DocumentationPage::make('search', ['title' => 'Search']);
$currentPage = $page->getCurrentPagePath();
$currentRoute = $page->getRoute();
$markdown = '';
// Emulate a page object
$page = Hyde\Framework\Models\Pages\DocumentationPage::make('search', ['title' => 'Search']);
$currentPage = $page->getCurrentPagePath();
$currentRoute = $page->getRoute();
$markdown = '';
@endphp

@extends('hyde::layouts.docs')
Expand Down
30 changes: 13 additions & 17 deletions packages/framework/src/Actions/PostBuildTasks/GenerateSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Hyde\Framework\Actions\PostBuildTasks;

use Hyde\Framework\Actions\GeneratesDocumentationSearchIndexFile;
use Hyde\Framework\Concerns\InteractsWithDirectories;
use Hyde\Framework\Contracts\AbstractBuildTask;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Services\DiscoveryService;
use Hyde\Framework\Services\DocumentationSearchService;

class GenerateSearch extends AbstractBuildTask
{
Expand All @@ -18,34 +16,32 @@ class GenerateSearch extends AbstractBuildTask
public function run(): void
{
$expected = $this->guesstimateGenerationTime();
if ($expected > 1) {
$this->line("<fg=gray> > This will take an estimated $expected seconds. Terminal may seem non-responsive.</>");
if ($expected >= 1) {
$this->line("<fg=gray>This will take an estimated $expected seconds. Terminal may seem non-responsive.</>");
}

GeneratesDocumentationSearchIndexFile::run();
DocumentationSearchService::generate();

if (config('docs.create_search_page', true)) {
$outputDirectory = Hyde::pathToRelative(Hyde::getSiteOutputPath(DocumentationPage::getOutputDirectory()));
$this->needsDirectory(Hyde::path($outputDirectory));
file_put_contents(
Hyde::path($outputDirectory.'/search.html'),
view('hyde::pages.documentation-search')->render()
);
$this->write(sprintf(
"\n > Created <info>_site/%s/search.html</info>",
config('docs.output_directory', 'docs')
));
$directory = DocumentationSearchService::generateSearchPage();

$this->createdSiteFile("$directory/search.html");
}
}

public function then(): void
{
$this->writeln(sprintf("\n > Created <info>%s</info> in %s",
GeneratesDocumentationSearchIndexFile::$filePath,
$this->normalizePath(DocumentationSearchService::$filePath),
$this->getExecutionTime()
));
}

protected function normalizePath(string $path): string
{
return str_replace('\\', '/', $path);
}

/** @internal Estimated processing time per file in ms */
public static float $guesstimationFactor = 52.5;

Expand Down
8 changes: 8 additions & 0 deletions packages/framework/src/Contracts/AbstractBuildTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Hyde\Framework\Contracts;

use Hyde\Framework\Hyde;
use Illuminate\Console\Concerns\InteractsWithIO;
use Illuminate\Console\OutputStyle;

Expand Down Expand Up @@ -67,4 +68,11 @@ public function writeln(string $message): void
{
$this->output?->writeln($message);
}

public function createdSiteFile(string $path): void
{
$this->write(sprintf("\n > Created <info>%s</info>",
str_replace('\\', '/', Hyde::pathToRelative($path))
));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

namespace Hyde\Framework\Actions;
namespace Hyde\Framework\Services;

use Hyde\Framework\Concerns\InteractsWithDirectories;
use Hyde\Framework\Contracts\ActionContract;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Illuminate\Support\Collection;
Expand All @@ -13,40 +12,47 @@
/**
* @internal Generate a JSON file that can be used as a search index for documentation pages.
*
* @todo #435 Convert into Service, and add more strategies, such as slug-only (no file parsing)
* search which while dumber, would be much faster to compile and take way less space.
*
* @see \Hyde\Framework\Testing\Feature\Actions\GeneratesDocumentationSearchIndexFileTest
* @see \Hyde\Framework\Testing\Feature\Services\DocumentationSearchServiceTest
*/
final class GeneratesDocumentationSearchIndexFile implements ActionContract
final class DocumentationSearchService
{
use InteractsWithDirectories;

public Collection $searchIndex;
public static string $filePath = '_site/docs/search.json';

public static function run(): self
public static function generate(): self
{
return (new self())->execute();
}

public static function generateSearchPage(): string
{
$outputDirectory = Hyde::getSiteOutputPath(DocumentationPage::getOutputDirectory());
self::needsDirectory(($outputDirectory));

file_put_contents(
"$outputDirectory/search.html",
view('hyde::pages.documentation-search')->render()
);

return $outputDirectory;
}

public function __construct()
{
$this->searchIndex = new Collection();
static::$filePath = Hyde::pathToRelative(Hyde::getSiteOutputPath(
DocumentationPage::getOutputDirectory().'/search.json')
);
self::$filePath = Hyde::pathToRelative(Hyde::getSiteOutputPath(
DocumentationPage::getOutputDirectory().'/search.json'
));
}

public function execute(): self
{
$this->generate();
$this->save();

return $this;
return $this->run()->save();
}

public function generate(): self
public function run(): self
{
/** @var DocumentationPage $page */
foreach (DocumentationPage::all() as $page) {
Expand All @@ -73,9 +79,9 @@ public function generatePageEntry(DocumentationPage $page): array

protected function save(): self
{
$this->needsDirectory(Hyde::path(str_replace('/search.json', '', static::$filePath)));
$this->needsDirectory(Hyde::path(str_replace('/search.json', '', self::$filePath)));

file_put_contents(Hyde::path(static::$filePath), $this->searchIndex->toJson());
file_put_contents(Hyde::path(self::$filePath), $this->searchIndex->toJson());

return $this;
}
Expand Down Expand Up @@ -112,11 +118,10 @@ protected function getSearchContentForDocument(DocumentationPage $page): string

public function getDestinationForSlug(string $slug): string
{
if ($slug === 'index' && config('site.pretty_urls', false)) {
$slug = '';
if (config('site.pretty_urls', false) === true) {
return $slug !== 'index' ? $slug : '';
}

return (config('site.pretty_urls', false) === true)
? $slug : $slug.'.html';
return $slug.'.html';
}
}
1 change: 1 addition & 0 deletions packages/framework/src/StaticPageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class StaticPageBuilder
{
use InteractsWithDirectories;

/** @var string The absolute path to the output directory */
public static string $outputPath;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public function test_it_does_not_display_the_estimation_message_when_it_is_less_
->assertExitCode(0);
}

public function test_it_displays_the_estimation_message_when_it_is_greater_than_1_second()
public function test_it_displays_the_estimation_message_when_it_is_greater_than_or_equal_to_1_second()
{
GenerateSearch::$guesstimationFactor = 1000000;
GenerateSearch::$guesstimationFactor = 1000;
Hyde::touch(('_docs/foo.md'));
$this->mockRoute();
$this->artisan('build:search')
->expectsOutputToContain('> This will take an estimated')
->expectsOutput('This will take an estimated 1 seconds. Terminal may seem non-responsive.')
->assertExitCode(0);
unlink(Hyde::path('_docs/foo.md'));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?php

namespace Hyde\Framework\Testing\Feature\Actions;
namespace Hyde\Framework\Testing\Feature\Services;

use Hyde\Framework\Actions\GeneratesDocumentationSearchIndexFile as Action;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Services\DocumentationSearchService as Service;
use Hyde\Testing\TestCase;

/**
* @covers \Hyde\Framework\Actions\GeneratesDocumentationSearchIndexFile
* @covers \Hyde\Framework\Services\DocumentationSearchService
*/
class GeneratesDocumentationSearchIndexFileTest extends TestCase
class DocumentationSearchServiceTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

unlinkIfExists(Action::$filePath);
unlinkIfExists(Service::$filePath);
}

protected function tearDown(): void
{
unlinkIfExists(Action::$filePath);
unlinkIfExists(Service::$filePath);

parent::tearDown();
}
Expand All @@ -41,10 +41,10 @@ public function test_it_generates_a_json_file_with_a_search_index()
],
];

Action::run();
Service::generate();

$this->assertEquals(
json_encode($expected), file_get_contents(Action::$filePath)
json_encode($expected), file_get_contents(Service::$filePath)
);

unlink(Hyde::path('_docs/foo.md'));
Expand All @@ -56,7 +56,7 @@ public function test_it_adds_all_files_to_search_index()
Hyde::touch(('_docs/bar.md'));
Hyde::touch(('_docs/baz.md'));

$this->assertCount(3, (new Action())->generate()->searchIndex);
$this->assertCount(3, (new Service())->run()->searchIndex);

unlink(Hyde::path('_docs/foo.md'));
unlink(Hyde::path('_docs/bar.md'));
Expand All @@ -65,16 +65,16 @@ public function test_it_adds_all_files_to_search_index()

public function test_it_handles_generation_even_when_there_are_no_pages()
{
Action::run();
Service::generate();

$this->assertEquals(
'[]', file_get_contents(Action::$filePath)
'[]', file_get_contents(Service::$filePath)
);
}

public function test_save_method_saves_the_file_to_the_correct_location()
{
Action::run();
Service::generate();

$this->assertFileExists('_site/docs/search.json');
}
Expand All @@ -91,7 +91,7 @@ public function test_generate_page_entry_method_generates_a_page_entry()
file_put_contents(Hyde::path('_docs/foo.md'), "# Bar\n\n Hello World");

$this->assertEquals(
$expected, (new Action())->generatePageEntry(DocumentationPage::parse('foo'))
$expected, (new Service())->generatePageEntry(DocumentationPage::parse('foo'))
);

unlink(Hyde::path('_docs/foo.md'));
Expand All @@ -102,7 +102,7 @@ public function test_it_generates_a_valid_JSON()
file_put_contents(Hyde::path('_docs/foo.md'), "# Bar\n\n Hello World");
file_put_contents(Hyde::path('_docs/bar.md'), "# Foo\n\n Hello World");

$generatesDocumentationSearchIndexFile = (new Action())->generate();
$generatesDocumentationSearchIndexFile = (new Service())->run();
$this->assertEquals(
'[{"slug":"bar","title":"Foo","content":"Foo \n Hello World","destination":"bar.html"},'.
'{"slug":"foo","title":"Bar","content":"Bar \n Hello World","destination":"foo.html"}]',
Expand All @@ -118,7 +118,7 @@ public function test_get_destination_for_slug_returns_empty_string_for_index_whe
config(['site.pretty_urls' => true]);

$this->assertEquals(
'', (new Action())->getDestinationForSlug('index')
'', (new Service())->getDestinationForSlug('index')
);
}

Expand All @@ -127,7 +127,7 @@ public function test_get_destination_for_slug_returns_pretty_url_when_enabled()
config(['site.pretty_urls' => true]);

$this->assertEquals(
'foo', (new Action())->getDestinationForSlug('foo')
'foo', (new Service())->getDestinationForSlug('foo')
);
}

Expand All @@ -136,7 +136,7 @@ public function test_excluded_pages_are_not_present_in_the_search_index()
Hyde::touch(('_docs/excluded.md'));
config(['docs.exclude_from_search' => ['excluded']]);

$generatesDocumentationSearchIndexFile = (new Action())->generate();
$generatesDocumentationSearchIndexFile = (new Service())->run();
$this->assertStringNotContainsString('excluded', json_encode($generatesDocumentationSearchIndexFile->searchIndex->toArray()));

unlink(Hyde::path('_docs/excluded.md'));
Expand All @@ -147,7 +147,7 @@ public function test_nested_source_files_do_not_retain_directory_name_in_search_
mkdir(Hyde::path('_docs/foo'));
touch(Hyde::path('_docs/foo/bar.md'));

$generatesDocumentationSearchIndexFile = (new Action())->generate();
$generatesDocumentationSearchIndexFile = (new Service())->run();
$this->assertStringNotContainsString('foo', json_encode($generatesDocumentationSearchIndexFile->searchIndex->toArray()));

unlink(Hyde::path('_docs/foo/bar.md'));
Expand Down