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

Automatically discover build task files in the app directory #458

Merged
merged 1 commit into from
Sep 1, 2022
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
4 changes: 2 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This release contains breaking changes regarding the PostBuildTasks that may req

### Added
- Added the option to define some site configuration settings in a `hyde.yml` file. See [#449](https://github.com/hydephp/develop/pull/449)
- Build tasks are now automatically registered when placed in the app/Actions directory and end with BuildTask.php

### Changed
- Renamed HydeSmartDocs.php to SemanticDocumentationArticle.php
Expand All @@ -24,8 +25,7 @@ This release contains breaking changes regarding the PostBuildTasks that may req
- for now removed features.

### Fixed
- Fixed [#443](https://github.com/hydephp/develop/issues/443): RSS feed meta link should not be added if there is not a feed

- Fixed [#443](https://github.com/hydephp/develop/issues/443): RSS feed meta link should not be added if there is not a feed

### Security
- in case of vulnerabilities.
3 changes: 2 additions & 1 deletion docs/digging-deeper/advanced-customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class ExampleHook extends AbstractBuildTask

### Registering the hooks

An autoloading feature is planned, but for now, you will need to register the hooks somewhere. There is a convenient place to do this, which is in the main configuration file, `config/hyde.php`.
There are a few ways to register these hooks so Hyde can find them. There is a convenient place to do this, which is in the main configuration file, `config/hyde.php`.

```php
// filepath config/hyde.php
Expand All @@ -148,3 +148,4 @@ An autoloading feature is planned, but for now, you will need to register the ho

If you are developing an extension, I recommend you do this in the `boot` method of a service provider so that it can be loaded automatically. Do this by adding the fully qualified class name to the `BuildHookService::$postBuildTasks` array.

Hyde can also autoload them if you store the files in the `app/Actions` directory and the names end in `BuildTask.php`. For example `app/Actions/ExampleBuildTask.php`.
20 changes: 17 additions & 3 deletions packages/framework/src/Services/BuildHookService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Hyde\Framework\Services;

use Hyde\Framework\Contracts\BuildTaskContract;
use Hyde\Framework\Hyde;
use Illuminate\Console\OutputStyle;

/**
Expand All @@ -29,19 +30,32 @@ public function runPostBuildTasks(): void
}
}

/**
* @todo #439 Automatically discover files in the app directory?
*/
public function getPostBuildTasks(): array
{
return array_unique(
array_merge(
config('hyde.post_build_tasks', []),
static::findTasksInAppDirectory(),
static::$postBuildTasks
)
);
}

public static function findTasksInAppDirectory(): array
{
$tasks = [];

foreach (glob(Hyde::path('app/Actions/*BuildTask.php')) as $file) {
$tasks[] = str_replace(
[Hyde::path('app'), '.php', '/'],
['App', '', '\\'],
$file
);
}

return $tasks;
}

public function run(string $task): static
{
$this->runTask(new $task($this->output));
Expand Down
31 changes: 31 additions & 0 deletions packages/framework/tests/Feature/Services/BuildHookServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,37 @@ public function run(): void
$this->assertEquals(1, $return);
}

public function test_find_tasks_in_app_directory_method_discovers_tasks_in_app_directory()
{
File::makeDirectory(Hyde::path('app/Actions'));
Hyde::touch('app/Actions/FooBuildTask.php');

$this->assertEquals(['App\Actions\FooBuildTask'], BuildHookService::findTasksInAppDirectory());
File::deleteDirectory(Hyde::path('app/Actions'));
}

public function test_automatically_discovered_tasks_can_be_executed()
{
File::makeDirectory(Hyde::path('app/Actions'));
File::put(Hyde::path('app/Actions/FooBuildTask.php'), '<?php

namespace App\Actions;

use Hyde\Framework\Contracts\AbstractBuildTask;

class FooBuildTask extends AbstractBuildTask {
public function run(): void {
echo "FooBuildTask";
}
}');

$service = $this->makeService();
$service->runPostBuildTasks();

$this->expectOutputString('FooBuildTask');
File::deleteDirectory(Hyde::path('app/Actions'));
}

protected function makeService(): BuildHookService
{
return new BuildHookService();
Expand Down