Skip to content
This repository has been archived by the owner on Feb 14, 2024. It is now read-only.

Commit

Permalink
cleanups/formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
surgiie committed Apr 1, 2023
1 parent 8af5128 commit cc6e975
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 52 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@
testing
storage/testing/mock
.compiled
composer.lock
composer.lock
*.rendered
*.rendered.*
2 changes: 1 addition & 1 deletion app/Commands/ClearCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ClearCommand extends BaseCommand
*
* @var string
*/
protected $signature = 'clear {--compile-path= : Custom directory for cached/compiled files. }';
protected $signature = 'clear';

/**
* The description of the command.
Expand Down
89 changes: 41 additions & 48 deletions app/Commands/RenderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,97 +117,87 @@ public function handle()
}

if (is_file($path)) {
$saveFilePath = $this->computeSavePath($path, $this->data->get('save-to'));

$this->renderFile($path, $variables, $saveFilePath);
$this->renderFile($path, $variables, $this->computeSavePath($path, $this->data->get('save-to')));

return 0;
}

if (is_dir($path)) {
$saveFilePath = $this->expandPath($this->data->get('save-to'));
if (! is_dir($path)) {
return 1;
}

$this->renderDirectoryFiles($path, $variables, $saveFilePath);
$saveFilePath = $this->expandPath($this->data->get('save-to'));

return 0;
if (! $saveFilePath) {
$this->exit('The --save-to directory option is required when rendering all files in a directory.');
}

return 1;
$this->renderDirectoryFiles($path, $variables, $saveFilePath);

return 0;
}

/**
* Render all files within a given directory.
*
* @return void
*/
protected function renderDirectoryFiles(string $path, array $variables, ?string $saveToPath)
protected function renderDirectoryFiles(string $path, array $variables, string $saveToPath)
{
if (rtrim($path, DIRECTORY_SEPARATOR) == rtrim($saveToPath, DIRECTORY_SEPARATOR)) {
// Ensure the path being processed isn't the same as the save directory
if (rtrim($path, DIRECTORY_SEPARATOR) === rtrim($saveToPath, DIRECTORY_SEPARATOR)) {
$this->exit('The path being processed is also the --save-to directory, use a different save directory.');
}

if (! $saveToPath) {
$this->exit('The --save-to directory option is required when rendering all files in a directory.');
}

if ($path == $saveToPath) {
// Ensure the path being processed isn't the same as the save directory
if ($path === $saveToPath) {
$this->exit('The path being processed is also the save directory, use a different save directory.');
}

if (is_dir($saveToPath) && $path != $saveToPath && ! $this->data->get('force', false)) {
// Check if save directory already exists and confirm overwrite
if (is_dir($saveToPath) && $path !== $saveToPath && ! $this->data->get('force', false)) {
$this->exit("The save to directory '$saveToPath' already exists, use --force to overwrite.");
}

$fs = (new Filesystem);
$fs->deleteDirectory($saveToPath, preserve: true);
// Delete the save directory
(new Filesystem)->deleteDirectory($saveToPath, preserve: true);

// Confirm rendering all files in the path directory
if (! $this->data->get('force', false) && ! $this->components->confirm("Are you sure you want to render ALL files in the '$path' directory?")) {
$this->exit('Canceled');
}

// validate save directory isnt the current directory being processed.
if ($saveToPath == $path) {
// Ensure the save directory isn't the same as the directory being processed
if ($saveToPath === $path) {
return $this->exit('The --save-to is the directory you are rendering, select different save directory.');
}

// Render each file within the directory
foreach ((new Finder())->in($path)->files() as $file) {
$fileName = $file->getFileName();
$pathName = $file->getPathName();

// compute a save as location that mirrors the current location of this file.
// Compute a save as location that mirrors the current location of this file
$computedDirectory = rtrim($saveToPath, DIRECTORY_SEPARATOR);

$relativePath = ltrim(Str::after($pathName, $path), DIRECTORY_SEPARATOR);
$saveDirectory = dirname(
normalize_path("$computedDirectory/$relativePath/$fileName")
);
$saveDirectory = dirname(normalize_path("$computedDirectory/$relativePath/$fileName"));

$this->renderFile($pathName, $variables, $saveDirectory);
}
}

/**
* Expand path if its a known path that can be expanded.
*
* @param ?string $path
*/
protected function expandPath(?string $path): string|null
{
// allow ~ syntax and expand accordingly
if (Str::startsWith($path, $alias = '~'.DIRECTORY_SEPARATOR)) {
$home = strncasecmp(PHP_OS, 'WIN', 3) == 0 ? getenv('USERPROFILE') : getenv('HOME');
$path = str_replace($alias, rtrim($home, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR, $path);
}
$env = strncasecmp(PHP_OS, 'WIN', 3) == 0 ? 'USERPROFILE' : 'HOME';

return $path;
return str_replace(['~/', '~/'], [getenv($env).'/', getenv($env).'/'], $path);
}

/**
* Render a file and save it's contents to the given path.
*
* @return void
* Render a file and save its contents to the given path.
*/
protected function renderFile(string $path, array $variables, string $saveTo)
protected function renderFile(string $path, array $variables, string $saveTo): void
{
$saveDirectory = dirname($saveTo);

Expand All @@ -228,20 +218,17 @@ protected function renderFile(string $path, array $variables, string $saveTo)
$contents = $this->blade()->compile($path, $variables);

return file_put_contents($saveTo, $contents) !== false;
} catch (\Exception $e) {
} catch (\Throwable $e) {
$task->remember(['exception' => $e]);

return false;
}

return file_put_contents($saveTo, $contents) !== false;
}, finishedText: "Rendered $saveTo");

$data = $task->data();

if (! $task->succeeded() && $data['exception'] ?? false) {
$this->components->error('Compile Error: '.$data['exception']->getMessage());
$this->exit();
if (! $task->succeeded() && isset($data['exception'])) {
$this->exit('Compile Error: '.$data['exception']->getMessage());
}
}

Expand All @@ -250,11 +237,17 @@ protected function renderFile(string $path, array $variables, string $saveTo)
*/
protected function getDefaultSaveFileName(string $path): string
{
$info = (new SplFileInfo($path));
$info = new SplFileInfo($path);

$basename = $info->getBasename('.'.$ext = $info->getExtension());

$basename = rtrim($info->getBasename($ext = $info->getExtension()), '.');
if (strpos($basename, '.') === 0 && ".$ext" == $basename) {
return $basename.'.rendered';
} else {
$basename .= '.rendered';
}

return $basename.'.rendered'.($ext ? ".$ext" : '');
return $basename.($ext ? '.'.$ext : '');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"guzzlehttp/guzzle": "^7.4",
"illuminate/http": "^9.0",
"intonate/tinker-zero": "^1.2",
"surgiie/console": "^3.0.0",
"surgiie/console": "^3.1.0",
"symfony/yaml": "^6.2"
},
"require-dev": {
Expand Down
4 changes: 3 additions & 1 deletion version.php
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
<?php return "v3.9.0";
<?php

return 'v3.9.0';

0 comments on commit cc6e975

Please sign in to comment.