Skip to content

Commit 043405e

Browse files
authored
Merge pull request #458 from hydephp/refactor-build-tasks
Automatically discover build task files in the app directory
2 parents 9cd7294 + d038129 commit 043405e

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

RELEASE_NOTES.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This release contains breaking changes regarding the PostBuildTasks that may req
66

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

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

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

3030
### Security
3131
- in case of vulnerabilities.

docs/digging-deeper/advanced-customization.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ class ExampleHook extends AbstractBuildTask
136136

137137
### Registering the hooks
138138

139-
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`.
139+
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`.
140140

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

149149
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.
150150

151+
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`.

packages/framework/src/Services/BuildHookService.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Hyde\Framework\Services;
44

55
use Hyde\Framework\Contracts\BuildTaskContract;
6+
use Hyde\Framework\Hyde;
67
use Illuminate\Console\OutputStyle;
78

89
/**
@@ -29,19 +30,32 @@ public function runPostBuildTasks(): void
2930
}
3031
}
3132

32-
/**
33-
* @todo #439 Automatically discover files in the app directory?
34-
*/
3533
public function getPostBuildTasks(): array
3634
{
3735
return array_unique(
3836
array_merge(
3937
config('hyde.post_build_tasks', []),
38+
static::findTasksInAppDirectory(),
4039
static::$postBuildTasks
4140
)
4241
);
4342
}
4443

44+
public static function findTasksInAppDirectory(): array
45+
{
46+
$tasks = [];
47+
48+
foreach (glob(Hyde::path('app/Actions/*BuildTask.php')) as $file) {
49+
$tasks[] = str_replace(
50+
[Hyde::path('app'), '.php', '/'],
51+
['App', '', '\\'],
52+
$file
53+
);
54+
}
55+
56+
return $tasks;
57+
}
58+
4559
public function run(string $task): static
4660
{
4761
$this->runTask(new $task($this->output));

packages/framework/tests/Feature/Services/BuildHookServiceTest.php

+31
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,37 @@ public function run(): void
178178
$this->assertEquals(1, $return);
179179
}
180180

181+
public function test_find_tasks_in_app_directory_method_discovers_tasks_in_app_directory()
182+
{
183+
File::makeDirectory(Hyde::path('app/Actions'));
184+
Hyde::touch('app/Actions/FooBuildTask.php');
185+
186+
$this->assertEquals(['App\Actions\FooBuildTask'], BuildHookService::findTasksInAppDirectory());
187+
File::deleteDirectory(Hyde::path('app/Actions'));
188+
}
189+
190+
public function test_automatically_discovered_tasks_can_be_executed()
191+
{
192+
File::makeDirectory(Hyde::path('app/Actions'));
193+
File::put(Hyde::path('app/Actions/FooBuildTask.php'), '<?php
194+
195+
namespace App\Actions;
196+
197+
use Hyde\Framework\Contracts\AbstractBuildTask;
198+
199+
class FooBuildTask extends AbstractBuildTask {
200+
public function run(): void {
201+
echo "FooBuildTask";
202+
}
203+
}');
204+
205+
$service = $this->makeService();
206+
$service->runPostBuildTasks();
207+
208+
$this->expectOutputString('FooBuildTask');
209+
File::deleteDirectory(Hyde::path('app/Actions'));
210+
}
211+
181212
protected function makeService(): BuildHookService
182213
{
183214
return new BuildHookService();

0 commit comments

Comments
 (0)