Skip to content

Commit

Permalink
Merge branch 'master' into try-to-fix-codecov-again
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva authored Apr 14, 2024
2 parents 5e40afb + a37de19 commit b35fc70
Show file tree
Hide file tree
Showing 11 changed files with 434 additions and 4 deletions.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ This serves two purposes:
- Added a `Hyde::route()` helper to the `Hyde` facade in https://github.com/hydephp/develop/pull/1591
- Added new global helper functions (`asset()`, `route()`, `url()`) in https://github.com/hydephp/develop/pull/1592
- Added a new `Feature` enum to improve the `Features` facade in https://github.com/hydephp/develop/pull/1650
- Added a helper to `->skip()` build tasks in https://github.com/hydephp/develop/pull/1656

### Changed
- The `features` array in the `config/hyde.php` configuration file is now an array of `Feature` enums in https://github.com/hydephp/develop/pull/1650
- Sitemap generation will now be skipped if a base URL is not set, as Google now will not index sitemaps without a base URL in https://github.com/hydephp/develop/pull/1660

### Deprecated
- Deprecated the static `Features` flag methods used in the configuration files in https://github.com/hydephp/develop/pull/1650 and will be removed in HydePHP v2.0
Expand All @@ -26,6 +28,7 @@ This serves two purposes:

### Fixed
- Fixed a bug where the sitemap and RSS feed generator commands did not work when the `_site/` directory was not present in https://github.com/hydephp/develop/pull/1654
- Fixed extra newlines being written to console for failing build tasks in https://github.com/hydephp/develop/pull/1661
- Realtime Compiler: Fixed responsive dashboard table issue in https://github.com/hydephp/develop/pull/1595

### Security
Expand Down
19 changes: 19 additions & 0 deletions docs/advanced-features/build-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,22 @@ public function handle(): void
$this->output->writeln('This is a line of text');
}
```

### Skipping tasks

>info This feature was added in HydePHP v1.6.0
If you for some reason need to skip the task during its execution, you can call the `skip()` method.

```php
public function handle(): void
{
if ($this->someCondition() !== true) {
$this->skip('Some condition was not met');

// The task will not be executed past this point
}
}
```

This will then halt the execution of the task, and display a notice with the message you provided to the console.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@if(Features::hasDarkmode()))
@if(Features::hasDarkmode())
<button @click="toggleTheme" {{ $attributes->merge(['class' => 'theme-toggle-button flex items-center px-2 py-1 hover:text-gray-700 dark:text-gray-200']) }} title="Toggle theme">
<span class="sr-only">Toggle dark theme</span>
<svg width="1.25rem" height="1.25rem" class="w-5 h-5 hidden dark:block" fill="#FFFFFF" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M10 2a1 1 0 011 1v1a1 1 0 11-2 0V3a1 1 0 011-1zm4 8a4 4 0 11-8 0 4 4 0 018 0zm-.464 4.95l.707.707a1 1 0 001.414-1.414l-.707-.707a1 1 0 00-1.414 1.414zm2.12-10.607a1 1 0 010 1.414l-.706.707a1 1 0 11-1.414-1.414l.707-.707a1 1 0 011.414 0zM17 11a1 1 0 100-2h-1a1 1 0 100 2h1zm-7 4a1 1 0 011 1v1a1 1 0 11-2 0v-1a1 1 0 011-1zM5.05 6.464A1 1 0 106.465 5.05l-.708-.707a1 1 0 00-1.414 1.414l.707.707zm1.414 8.486l-.707.707a1 1 0 01-1.414-1.414l.707-.707a1 1 0 011.414 1.414zM4 11a1 1 0 100-2H3a1 1 0 000 2h1z" fill-rule="evenodd" clip-rule="evenodd"></path></svg>
Expand Down
2 changes: 1 addition & 1 deletion packages/framework/resources/views/layouts/head.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{{-- App Stylesheets --}}
@include('hyde::layouts.styles')

@if(Features::hasDarkmode()))
@if(Features::hasDarkmode())
{{-- Check the local storage for theme preference to avoid FOUC --}}
<meta id="meta-color-scheme" name="color-scheme" content="{{ config('hyde.default_color_scheme', 'light') }}">
<script>if (localStorage.getItem('color-theme') === 'dark' || (!('color-theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) { document.documentElement.classList.add('dark'); document.getElementById('meta-color-scheme').setAttribute('content', 'dark');} else { document.documentElement.classList.remove('dark') } </script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ class GenerateSitemap extends PostBuildTask

public function handle(): void
{
if (blank(Hyde::url()) || str_starts_with(Hyde::url(), 'http://localhost')) {
$this->skip('Cannot generate sitemap without a valid base URL');
}

$this->path = Hyde::sitePath('sitemap.xml');

$this->needsParentDirectory($this->path);
Expand Down
20 changes: 18 additions & 2 deletions packages/framework/src/Framework/Features/BuildTasks/BuildTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ public function run(?OutputStyle $output = null): int
$this->handle();
$this->printFinishMessage();
} catch (Throwable $exception) {
$this->writeln('<error>Failed</error>');
$this->writeln("<error>{$exception->getMessage()}</error>");
if ($exception instanceof BuildTaskSkippedException) {
$this->write("<bg=yellow>Skipped</>\n");
$this->write("<fg=gray> > {$exception->getMessage()}</>");
} else {
$this->write("<error>Failed</error>\n");
$this->write("<error>{$exception->getMessage()}</error>");
}

$this->exitCode = $exception->getCode();
}

Expand Down Expand Up @@ -84,6 +90,16 @@ public function writeln(string $message): void
$this->output?->writeln($message);
}

/**
* Write a fluent message to the output that the task is skipping and halt the execution.
*
* @throws \Hyde\Framework\Features\BuildTasks\BuildTaskSkippedException
*/
public function skip(string $reason = 'Task was skipped'): void
{
throw new BuildTaskSkippedException($reason);
}

/** Write a fluent message to the output that the task created the specified file. */
public function createdSiteFile(string $path): static
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Features\BuildTasks;

use RuntimeException;

class BuildTaskSkippedException extends RuntimeException
{
public function __construct(string $message = 'Task was skipped', int $code = 0)
{
parent::__construct($message, $code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class BuildTaskServiceTest extends TestCase
*/
public function testBuildCommandCanRunBuildTasks()
{
config(['hyde.url' => 'https://example.com']);

$this->artisan('build')
->expectsOutputToContain('Removing all files from build directory')
->expectsOutputToContain('Generating sitemap')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Testing\Feature;

use Illuminate\Support\Facades\File;
use Hyde\Framework\HydeServiceProvider;
use Hyde\Pages\BladePage;
use Hyde\Pages\DocumentationPage;
Expand All @@ -17,6 +18,13 @@
*/
class SourceDirectoriesCanBeChangedTest extends TestCase
{
public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();

File::deleteDirectory('_source');
}

public function testBaselines()
{
$this->assertEquals('_pages', HtmlPage::sourceDirectory());
Expand Down
51 changes: 51 additions & 0 deletions packages/framework/tests/Unit/BuildTaskSkippedExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing\Unit;

use Hyde\Framework\Features\BuildTasks\BuildTaskSkippedException;
use Hyde\Testing\UnitTestCase;

/**
* @covers \Hyde\Framework\Features\BuildTasks\BuildTaskSkippedException
*/
class BuildTaskSkippedExceptionTest extends UnitTestCase
{
public function testItCanBeInstantiated()
{
$exception = new BuildTaskSkippedException();

$this->assertInstanceOf(BuildTaskSkippedException::class, $exception);
}

public function testItThrowsAnExceptionWithDefaultMessage()
{
$this->expectException(BuildTaskSkippedException::class);
$this->expectExceptionMessage('Task was skipped');

throw new BuildTaskSkippedException();
}

public function testItThrowsAnExceptionWithCustomMessage()
{
$this->expectException(BuildTaskSkippedException::class);
$this->expectExceptionMessage('Custom message');

throw new BuildTaskSkippedException('Custom message');
}

public function testDefaultExceptionCode()
{
$exception = new BuildTaskSkippedException();

$this->assertSame(0, $exception->getCode());
}

public function testCustomExceptionCode()
{
$exception = new BuildTaskSkippedException('Custom message', 123);

$this->assertSame(123, $exception->getCode());
}
}
Loading

0 comments on commit b35fc70

Please sign in to comment.