Skip to content

Commit

Permalink
Alert on non existent IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
aarondfrancis committed Sep 18, 2021
1 parent 8d9ca6f commit 4666db0
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 9 deletions.
54 changes: 53 additions & 1 deletion src/BlockManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use TightenCo\Jigsaw\Jigsaw;
use Torchlight\Blade\BladeManager;
use Torchlight\Block;
use Torchlight\Jigsaw\Exceptions\UnrenderedBlockException;
use Torchlight\Torchlight;

class BlockManager
Expand Down Expand Up @@ -55,6 +56,57 @@ public function render(Jigsaw $jigsaw)

$this->renderMarkdownCapturedBlocks();
$this->renderBladeDirectiveBlocks();
$this->scanForLeftovers();
}

protected function scanForLeftovers()
{
// Grep the directory to find files that have Torchlight blocks.
// No sense reg-exing through files that don't have blocks.
$files = $this->filesWithBlocks();

$leftovers = [];
$ignore = Torchlight::config('ignore_leftover_ids', []);

foreach ($files as $file) {
$contents = file_get_contents($file);

$ids = array_diff(Torchlight::findTorchlightIds($contents), $ignore);

if ($ids) {
$leftovers[$file] = $ids;
}
}

if (!$leftovers) {
return;
}

$output = <<<EOT
-----------------------------------------------------------------------------
Some Torchlight blocks were not replaced. They are listed below. If you're
aware of this and would like to ignore any of these blocks, you may add
their IDs to a "ignore_leftover_ids" array in your Torchlight configuration.
If you believe this is a bug, please let us know
at github.com/torchlight-api/torchlight-jigsaw.
-----------------------------------------------------------------------------
EOT;

foreach ($leftovers as $file => $ids) {
$output .= " File: $file \n IDs: \n";

foreach ($ids as $id) {
$output .= " - $id \n";
}
}

echo $output;

throw new UnrenderedBlockException('Unrendered Torchlight blocks found.');
}

protected function renderMarkdownCapturedBlocks()
Expand All @@ -68,7 +120,7 @@ protected function renderMarkdownCapturedBlocks()

// Gather up all of the <pre> elements.
$elements = array_merge(
// This one captures regular fenced code blocks.
// This one captures regular fenced code blocks.
$this->getCapturedGroup("/(<pre(?:.+)<\/pre>)/", $contents, $all = true),
// This one captures indented code blocks, which are weirdly different for some reason.
$this->getCapturedGroup("/(<pre><code>(?:.+)\n<\/code><\/pre>)/", $contents, $all = true)
Expand Down
14 changes: 14 additions & 0 deletions src/Exceptions/UnrenderedBlockException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* @author Aaron Francis <[email protected]|https://twitter.com/aarondfrancis>
*/

namespace Torchlight\Jigsaw\Exceptions;


use Exception;

class UnrenderedBlockException extends Exception
{

}
10 changes: 6 additions & 4 deletions src/TorchlightExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ protected function configureStandaloneTorchlight()
// See https://github.com/laravel/framework/blob/8.x/CHANGELOG-8.x.md#v8230-2021-01-19.
BladeManager::$affectedBySpacingBug = true;

// There is no `app()->environment` helper, so we need
// to tell Torchlight what environment we're in.
$this->events->beforeBuild(function ($jigsaw) {
Torchlight::overrideEnvironment($jigsaw->getEnvironment());
// There is no `app()->environment` helper, so we need to tell Torchlight what
// environment we're in. Hardcode the environment to a non "production" build
// because this is a build tool. If a request fails while building the
// production site, we need to notify the developer.
$this->events->beforeBuild(function () {
Torchlight::overrideEnvironment('build');
});

// Set an instantiated cache instance.
Expand Down
48 changes: 45 additions & 3 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\Console\Application;
use TightenCo\Jigsaw\Console\BuildCommand;
use TightenCo\Jigsaw\Jigsaw;
use Torchlight\Jigsaw\Exceptions\UnrenderedBlockException;
use Torchlight\Jigsaw\TorchlightExtension;
use Torchlight\Torchlight;

Expand Down Expand Up @@ -46,7 +47,8 @@ protected function prepareForBuilding()

$this->container->instance('cwd', $sitePath);
$this->container->buildPath = [
'source' => "$sitePath/source",
'source' => $sitePath,
'views' => $sitePath,
'destination' => "$sitePath/build_testing",
];

Expand All @@ -63,7 +65,7 @@ protected function prepareForBuilding()
include "$sitePath/bootstrap.php";
}

protected function build($source = 'source')
protected function build($source = 'source-1')
{
// Turn off the Jigsaw progress bars.
$this->container->consoleOutput->setup($verbosity = -1);
Expand Down Expand Up @@ -112,7 +114,6 @@ public function most_of_the_tests_are_here()
$this->assertSnapshotMatches('can-set-a-theme');
$this->assertSnapshotMatches('attributes-get-carried-over');
$this->assertSnapshotMatches('jigsaw-escapes-get-fixed');
$this->assertSnapshotMatches('non-existent-id');
$this->assertSnapshotMatches('one-component');
$this->assertSnapshotMatches('two-components');
$this->assertSnapshotMatches('code-indents-work');
Expand All @@ -136,6 +137,47 @@ public function no_blocks_doesnt_create_an_error()
$this->assertTrue(true);
}

/** @test */
public function non_existent_block_throws()
{
TorchlightExtension::macro('afterStandaloneConfiguration', function () {
Torchlight::getConfigUsing([
'blade_components' => true,
'token' => 'token'
]);
});

$this->prepareForBuilding();

try {
$this->build('source-3');
} catch (UnrenderedBlockException $e) {
return $this->assertTrue(true);
}

$this->assertTrue(false);
}

/** @test */
public function expected_non_existent_block_is_fine()
{
TorchlightExtension::macro('afterStandaloneConfiguration', function () {
Torchlight::getConfigUsing([
'blade_components' => true,
'token' => 'token',
'ignore_leftover_ids' => [
'fake_id'
]
]);
});

$this->prepareForBuilding();

$this->build('source-3');
$this->assertTrue(true);
}


/** @test */
public function test_publish_command()
{
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion tests/Site/source/_layouts/master.blade.php

This file was deleted.

0 comments on commit 4666db0

Please sign in to comment.