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

[2.x] Refactor media asset transferring to be in a build task #1536

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 17 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ 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 a new `\Hyde\Framework\Actions\PreBuildTasks\TransferMediaAssets` build task handle media assets transfers for site builds.

### Changed
- Changed how the documentation search is generated, to be an `InMemoryPage` instead of a post-build task.
- Media asset files are now copied using the new build task instead of the deprecated `BuildService::transferMediaAssets()` method.

### Deprecated
- for soon-to-be removed features.

### Removed
- Breaking: Removed the build task `\Hyde\Framework\Actions\PostBuildTasks\GenerateSearch` (see upgrade guide below)
- Breaking: Removed the deprecated `\Hyde\Framework\Services\BuildService::transferMediaAssets()` method (see upgrade guide below)

### Fixed
- for any bug fixes.
Expand Down Expand Up @@ -68,3 +70,17 @@ according to the information below in case you wrote custom code that interacted
- In the highly unlikely event your site customizes any of the search pages by replacing them in the kernel route collection,
you would now need to do that in the kernel page collection due to the search pages being generated earlier in the lifecycle.
https://github.com/hydephp/develop/commit/82dc71f4a0e7b6be7a9f8d822fbebe39d2289ced

### Media asset transfer implementation changes

The internals of how media asset files are copied during the build process have been changed. For most users, this change
has no impact. However, if you have previously extended this method, or called it directly from your custom code,
you will need to adapt your code to use the new `TransferMediaAssets` build task.

For example, if you triggered the media transfer with a build service method call, use the new build task instead:

```php
(new BuildService)->transferMediaAssets();

(new TransferMediaAssets())->run();
```
2 changes: 0 additions & 2 deletions packages/framework/src/Console/Commands/BuildSiteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public function handle(): int

$this->runPreBuildActions();

$this->service->transferMediaAssets();

$this->service->compileStaticPages();

$this->runPostBuildActions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Hyde\Foundation\Facades\Pages;
use Hyde\Framework\Actions\StaticPageBuilder;
use Hyde\Framework\Features\BuildTasks\BuildTask;
use Hyde\Framework\Services\BuildService;
use Hyde\Framework\Actions\PreBuildTasks\TransferMediaAssets;
use Hyde\Hyde;
use Hyde\Pages\BladePage;
use Hyde\Pages\DocumentationPage;
Expand Down Expand Up @@ -37,7 +37,7 @@ class RebuildPageCommand extends Command
public function handle(): int
{
if ($this->argument('path') === Hyde::getMediaDirectory()) {
(new BuildService($this->getOutput()))->transferMediaAssets();
return (new TransferMediaAssets())->run($this->output);

$this->info('All done!');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Actions\PreBuildTasks;

use Hyde\Hyde;
use Hyde\Support\Filesystem\MediaFile;
use Hyde\Framework\Features\BuildTasks\PreBuildTask;
use Hyde\Framework\Concerns\InteractsWithDirectories;

class TransferMediaAssets extends PreBuildTask
{
protected static string $message = 'Transferring Media Assets';

use InteractsWithDirectories;

public function handle(): void
{
$this->needsDirectory(Hyde::siteMediaPath());

$this->newLine();

$this->withProgressBar(MediaFile::files(), function (string $identifier): void {
$sitePath = Hyde::siteMediaPath($identifier);
$this->needsParentDirectory($sitePath);
copy(Hyde::mediaPath($identifier), $sitePath);
});

$this->newLine();
}

public function printFinishMessage(): void
{
// We don't need a finish message for this task.
}
}
18 changes: 0 additions & 18 deletions packages/framework/src/Framework/Services/BuildService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@
use Hyde\Foundation\Facades\Routes;
use Hyde\Foundation\Kernel\RouteCollection;
use Hyde\Framework\Actions\StaticPageBuilder;
use Hyde\Framework\Concerns\InteractsWithDirectories;
use Hyde\Pages\Concerns\HydePage;
use Hyde\Support\Filesystem\MediaFile;
use Hyde\Support\Models\Route;
use Illuminate\Console\Concerns\InteractsWithIO;
use Illuminate\Console\OutputStyle;

use function class_basename;
use function preg_replace;
use function collect;
use function copy;

/**
* Moves logic from the build command to a service.
Expand All @@ -30,7 +27,6 @@
class BuildService
{
use InteractsWithIO;
use InteractsWithDirectories;

protected RouteCollection $router;

Expand All @@ -48,20 +44,6 @@ public function compileStaticPages(): void
});
}

public function transferMediaAssets(): void
{
$this->needsDirectory(Hyde::siteMediaPath());

$this->comment('Transferring Media Assets...');
$this->withProgressBar(MediaFile::files(), function (string $identifier): void {
$sitePath = Hyde::siteMediaPath($identifier);
$this->needsParentDirectory($sitePath);
copy(Hyde::mediaPath($identifier), $sitePath);
});

$this->newLine(2);
}

/**
* @param class-string<\Hyde\Pages\Concerns\HydePage> $pageClass
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Hyde\Framework\Actions\PreBuildTasks\CleanSiteDirectory;
use Hyde\Framework\Actions\PostBuildTasks\GenerateRssFeed;
use Hyde\Framework\Actions\PostBuildTasks\GenerateSitemap;
use Hyde\Framework\Actions\PreBuildTasks\TransferMediaAssets;
use Hyde\Framework\Actions\PostBuildTasks\GenerateBuildManifest;
use Illuminate\Console\OutputStyle;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -132,6 +133,7 @@ protected function makeTaskIdentifier(BuildTask $class): string
private function registerFrameworkTasks(): void
{
$this->registerIf(CleanSiteDirectory::class, $this->canCleanSiteDirectory());
$this->registerIf(TransferMediaAssets::class, $this->canTransferMediaAssets());
$this->registerIf(GenerateBuildManifest::class, $this->canGenerateManifest());
$this->registerIf(GenerateSitemap::class, $this->canGenerateSitemap());
$this->registerIf(GenerateRssFeed::class, $this->canGenerateFeed());
Expand All @@ -142,6 +144,11 @@ private function canCleanSiteDirectory(): bool
return Config::getBool('hyde.empty_output_directory', true);
}

private function canTransferMediaAssets(): bool
{
return Config::getBool('hyde.transfer_media_assets', true);
}

private function canGenerateManifest(): bool
{
return Config::getBool('hyde.generate_build_manifest', true);
Expand Down
1 change: 1 addition & 0 deletions packages/framework/tests/Unit/BuildTaskServiceUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected function setUp(): void
self::mockConfig(['hyde' => [
'empty_output_directory' => false,
'generate_build_manifest' => false,
'transfer_media_assets' => false,
]]);
$this->createService();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Hyde\Framework\Services\BuildService;
use Illuminate\Console\OutputStyle;
use Mockery;
use Hyde\Framework\Actions\PreBuildTasks\TransferMediaAssets;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Helper\ProgressBar;

Expand Down Expand Up @@ -92,7 +93,7 @@ public function test_relative_links_across_pages_retains_integrity()
'write' => null,
]));

$service->transferMediaAssets();
(new TransferMediaAssets())->run();
$service->compileStaticPages();

$this->assertSee('root', [
Expand Down
Loading