Skip to content

Commit

Permalink
Merge pull request #556 from hydephp/develop
Browse files Browse the repository at this point in the history
v0.43.0-beta - 2022-06-25 - File-based Collections
  • Loading branch information
caendesilva authored Jun 25, 2022
2 parents 7b26a79 + 62b547a commit d4ae8dc
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 83 deletions.
15 changes: 9 additions & 6 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,19 @@
| Site Output Directory (Experimental 🧪)
|--------------------------------------------------------------------------
|
| If you want to store your compiled website in a different directory than
| the default `_pages`, you can change the path here. The Hyde::path()
| helper ensures the path is relative to your Hyde project. While
| you can set the path to an absolute path outside the project,
| this is not officially supported and may be unstable.
| This setting specifies the output path for your site, useful to for
| example, store the site in the docs/ directory for GitHub Pages.
| The path is relative to the root of your project.
|
| To use an absolute path, or just to learn more:
| @see https://hydephp.com/docs/master/advanced-customization#customizing-the-output-directory-
|
*/

'output_directory' => '_site',

/**
* @deprecated will be handled in the service provider
* @deprecated use the 'output_directory' setting instead
*/
'site_output_path' => Hyde\Framework\Hyde::path('_site'),

Expand Down
6 changes: 1 addition & 5 deletions src/Commands/HydeBuildStaticSiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class HydeBuildStaticSiteCommand extends Command
protected $signature = 'build
{--run-dev : Run the NPM dev script after build}
{--run-prod : Run the NPM prod script after build}
{--pretty : Deprecated option, use --run-prettier instead}
{--run-prettier : Format the output using NPM Prettier}
{--pretty-urls : Should links in output use pretty URLs?}
{--no-api : Disable API calls, for example, Torchlight}';
Expand Down Expand Up @@ -121,10 +120,7 @@ public function runPostBuildActions(): void
{
$service = new BuildHookService($this->output);

if ($this->option('run-prettier') || $this->option('pretty')) {
if ($this->option('pretty')) {
$this->warn('<error>Warning:</> The --pretty option is deprecated, use --run-prettier instead');
}
if ($this->option('run-prettier')) {
$this->runNodeCommand(
'npx prettier '.Hyde::pathToRelative(Hyde::getSiteOutputPath()).'/**/*.html --write --bracket-same-line',
'Prettifying code!',
Expand Down
46 changes: 0 additions & 46 deletions src/Concerns/Internal/AssetManager.php

This file was deleted.

2 changes: 0 additions & 2 deletions src/Hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Hyde\Framework;

use Composer\InstalledVersions;
use Hyde\Framework\Concerns\Internal\AssetManager;
use Hyde\Framework\Concerns\Internal\FileHelpers;
use Hyde\Framework\Concerns\Internal\FluentPathHelpers;
use Hyde\Framework\Helpers\HydeHelperFacade;
Expand All @@ -21,7 +20,6 @@
class Hyde
{
use FileHelpers;
use AssetManager;
use FluentPathHelpers;
use HydeHelperFacade;

Expand Down
28 changes: 24 additions & 4 deletions src/HydeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,18 @@ function () {

$this->discoverBladeViewsIn('_pages');

$this->storeCompiledSiteIn(config(
'hyde.site_output_path',
Hyde::path('_site')
));
/** @deprecated v0.43.0-beta and is used here as a fallback for compatibility */
if (config('hyde.output_directory') === null) {
$this->storeCompiledSiteIn(config(
'hyde.site_output_path',
Hyde::path('_site')
));
} else {
// Newer version which is safer.
$this->storeCompiledSiteIn(Hyde::path(config(
trim('hyde.output_directory', '_site'), '/\\')
));
}

$this->commands([
Commands\HydePublishHomepageCommand::class,
Expand All @@ -80,6 +88,8 @@ function () {

Commands\HydePackageDiscoverCommand::class,
]);

$this->registerModuleServiceProviders();
}

/**
Expand Down Expand Up @@ -128,4 +138,14 @@ protected function storeCompiledSiteIn(string $directory): void
{
StaticPageBuilder::$outputPath = $directory;
}

/**
* Register module service providers.
*
* @todo Make modules configurable.
*/
protected function registerModuleServiceProviders(): void
{
$this->app->register(Modules\DataCollections\DataCollectionServiceProvider::class);
}
}
68 changes: 68 additions & 0 deletions src/Modules/DataCollections/DataCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Hyde\Framework\Modules\DataCollections;

use Hyde\Framework\Hyde;
use Hyde\Framework\Models\MarkdownDocument;
use Hyde\Framework\Services\MarkdownFileService;
use Illuminate\Support\Collection;

/**
* Generates Laravel Collections from static data files,
* such as Markdown components and YAML files.
*
* @see \Hyde\Framework\Testing\Modules\DataCollections\DataCollectionTest\DataCollectionTest
*/
class DataCollection extends Collection
{
public string $key;

protected float $timeStart;
public float $parseTimeInMs;

public static string $sourceDirectory = '_data';

public function __construct(string $key)
{
$this->timeStart = microtime(true);
$this->key = $key;

parent::__construct();
}

public function getCollection(): DataCollection
{
$this->parseTimeInMs = round((microtime(true) - $this->timeStart) * 1000, 2);
unset($this->timeStart);

return $this;
}

public function getMarkdownFiles(): array
{
return glob(Hyde::path(
static::$sourceDirectory.'/'.$this->key.'/*'.MarkdownDocument::$fileExtension
));
}

/**
* Get a collection of Markdown documents in the _data/<$key> directory.
* Each Markdown file will be parsed into a MarkdownDocument with front matter.
*
* @param string $key for a subdirectory of the _data directory
* @return DataCollection<\Hyde\Framework\Models\MarkdownDocument>
*/
public static function markdown(string $key): DataCollection
{
$collection = new DataCollection($key);
foreach ($collection->getMarkdownFiles() as $file) {
if (! str_starts_with(basename($file), '_')) {
$collection->push(
(new MarkdownFileService($file))->get()
);
}
}

return $collection->getCollection();
}
}
29 changes: 29 additions & 0 deletions src/Modules/DataCollections/DataCollectionServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Hyde\Framework\Modules\DataCollections;

use Hyde\Framework\Hyde;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Support\ServiceProvider;

/**
* @see \Hyde\Framework\Testing\Modules\DataCollections\DataCollectionTest\DataCollectionTest
*/
class DataCollectionServiceProvider extends ServiceProvider
{
public function register()
{
// Register the class alias
AliasLoader::getInstance()->alias(
'MarkdownCollection', Facades\MarkdownCollection::class
);
}

public function boot()
{
// Create the _data directory if it doesn't exist
if (! is_dir(Hyde::path('_data'))) {
mkdir(Hyde::path('_data'));
}
}
}
16 changes: 16 additions & 0 deletions src/Modules/DataCollections/Facades/MarkdownCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Hyde\Framework\Modules\DataCollections\Facades;

use Hyde\Framework\Modules\DataCollections\DataCollection;

/**
* @see \Hyde\Framework\Testing\Modules\DataCollections\DataCollectionTest\DataCollectionTest
*/
class MarkdownCollection
{
public static function get(string $collectionKey): DataCollection
{
return DataCollection::markdown($collectionKey);
}
}
10 changes: 10 additions & 0 deletions src/Modules/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Hyde Modules

This directory contains self-contained code modules,
that may eventually be extracted into packages.

They may also be merged into the Hyde core.

As these modules may be experimental,
the namespaces used may be changed without notice
as per the current 0.x semantic versioning range.
5 changes: 4 additions & 1 deletion src/Services/CollectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ public static function getDocumentationPageList(): array
*/
public static function getMediaAssetFiles(): array
{
return glob(Hyde::path('_media/*.{png,svg,jpg,jpeg,gif,ico,css,js}'), GLOB_BRACE);
return glob(Hyde::path('_media/*.{'.str_replace(' ', '',
config('hyde.media_extensions', 'png,svg,jpg,jpeg,gif,ico,css,js')
).'}'), GLOB_BRACE
);
}
}
12 changes: 0 additions & 12 deletions tests/Feature/Commands/BuildStaticSiteCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,4 @@ public function test_generates_search_files_when_conditions_are_met()
->expectsOutput('Generating search page...')
->assertExitCode(0);
}

/**
* Added for code coverage, deprecated as the pretty flag is deprecated.
*
* @deprecated
*/
public function test_command_warns_when_deprecated_pretty_flag_is_used()
{
$this->artisan('build --pretty')
->expectsOutput('Warning: The --pretty option is deprecated, use --run-prettier instead')
->assertExitCode(0);
}
}
37 changes: 30 additions & 7 deletions tests/Feature/Services/CollectionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,36 @@ public function test_get_media_asset_files()
$this->assertTrue(is_array(CollectionService::getMediaAssetFiles()));
}

public function test_get_media_asset_files_discovers_files()
{
$testFiles = [
'png',
'svg',
'jpg',
'jpeg',
'gif',
'ico',
'css',
'js',
];
foreach ($testFiles as $fileType) {
$path = Hyde::path('_media/test.'.$fileType);
touch($path);
$this->assertContains($path, CollectionService::getMediaAssetFiles());
unlink($path);
}
}

public function test_get_media_asset_files_discovers_custom_file_types()
{
$path = Hyde::path('_media/test.custom');
touch($path);
$this->assertNotContains($path, CollectionService::getMediaAssetFiles());
config(['hyde.media_extensions' => 'custom']);
$this->assertContains($path, CollectionService::getMediaAssetFiles());
unlink($path);
}

public function test_files_starting_with_underscore_are_ignored()
{
touch(Hyde::path('_posts/_foo.md'));
Expand All @@ -53,11 +83,4 @@ private function testListUnit(string $model, string $path)

unlink(Hyde::path($path));
}

public function tearDown(): void
{
// restoreDirectory(Hyde::path('_docs'));

parent::tearDown();
}
}

0 comments on commit d4ae8dc

Please sign in to comment.