Skip to content

Commit

Permalink
Cleanup temp files after compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster committed Feb 10, 2024
1 parent 6646e5e commit 7146d66
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions src/Workspaces/Concerns/CompilesWorkspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Stillat\BladeParser\Workspaces\Concerns;

use Exception;
use Illuminate\Support\Str;
use Stillat\BladeParser\Compiler\AppendState;
use Stillat\BladeParser\Contracts\PathFormatter;
Expand Down Expand Up @@ -135,29 +136,40 @@ public function compile(string $outputDirectory): void
{
$outputDirectory = Paths::normalizePathWithTrailingSlash($outputDirectory);
$this->lastTempDirectory = $outputDirectory;

/** @var Document $doc */
foreach ($this->getDocuments() as $doc) {
$compilePath = $outputDirectory.$this->pathFormatter->getPath($doc);
$options = $this->getCompilerOptions();

$this->docSourceMaps[$compilePath] = [];
$options->appendCallbacks[] = function (AppendState $state) use ($compilePath) {
for ($i = $state->beforeLineNumber; $i <= $state->afterLineNumber; $i++) {
$this->docSourceMaps[$compilePath][$i] = $state->node->position->startLine;
$createdDirs = [];

try {
/** @var Document $doc */
foreach ($this->getDocuments() as $doc) {
$compilePath = $outputDirectory.$this->pathFormatter->getPath($doc);
$options = $this->getCompilerOptions();

$this->docSourceMaps[$compilePath] = [];
$options->appendCallbacks[] = function (AppendState $state) use ($compilePath) {
for ($i = $state->beforeLineNumber; $i <= $state->afterLineNumber; $i++) {
$this->docSourceMaps[$compilePath][$i] = $state->node->position->startLine;
}
};
$result = $doc->compile($options);

$dir = Paths::normalizePathWithTrailingSlash(Str::afterLast(Paths::normalizePath($compilePath), '/'));
$createdDirs[] = $dir;

if (! file_exists($dir)) {
@mkdir($dir, 0755, true);
}
};
$result = $doc->compile($options);

$dir = Paths::normalizePathWithTrailingSlash(Str::afterLast(Paths::normalizePath($compilePath), '/'));

if (! file_exists($dir)) {
@mkdir($dir, 0755, true);
file_put_contents($compilePath, $result);
$this->compiledFiles[$compilePath] = $doc->getFilePath();
$this->compiledDocuments[$compilePath] = $doc;
}
} catch (Exception $e) {
foreach ($createdDirs as $dir) {
@rmdir($dir);
}
$this->removeCompiledFiles();

file_put_contents($compilePath, $result);
$this->compiledFiles[$compilePath] = $doc->getFilePath();
$this->compiledDocuments[$compilePath] = $doc;
throw $e;
}
}

Expand Down

0 comments on commit 7146d66

Please sign in to comment.