Skip to content

Commit

Permalink
Add ability to override stub location when generating
Browse files Browse the repository at this point in the history
  • Loading branch information
Riley19280 committed Jan 14, 2024
1 parent 6466344 commit ce93dc9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,15 @@ return Stencil::make()
->use('Illuminate\Database\Eloquent\Model')
->curlyStatement('class i_name extends Model', fn(Stencil $s) => $s
->line('use HasFactory;')
);
)
->overrideStubLocation(base_path('Domain/Other/Path/i_name.php'));
```

### Changing the stub output location

In larger projects, you may not be using the default locations where Laravel generates the file. If you would like to change this,
you can call the `overrideStubLocation` method on the Stencil, and provide a custom location where you would like the stub file to be written to.
Note that this method is implemented via a macro, and code completion will not be available for it.


If you have a formatter installed, such as Pint, PHP CS Fixer, or StyleCI, your stencil will be formatted as well!
22 changes: 16 additions & 6 deletions src/Laravel/LaravelCodeStencilServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace CodeStencil\Laravel;

use CodeStencil\Stencil;
use Illuminate\Console\Events\CommandFinished;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Support\Facades\App;
Expand Down Expand Up @@ -50,17 +51,26 @@ public function boot(): void

$prefixedArgs = [];

foreach($availableArgs as $arg => $val) {
foreach ($availableArgs as $arg => $val) {
$prefixedArgs['i_' . $arg] = $val;
}

foreach($newFiles as $newFile) {
foreach ($newFiles as $newFile) {
(new StencilFileProcessor($newFile, $prefixedArgs))();
}

App::forgetInstance('command-file-list');
});
}

Stencil::macro('overrideStubLocation', function(string $path) {
$newPath = $this->substituteVariables($path);
$newPath = $this->applyFunctions($newPath);

$this->variable('overrideStubLocation', $newPath);

return $this;
});
}

private function getFiles(): array
Expand All @@ -70,19 +80,19 @@ private function getFiles(): array
if ($file->isDir()) {
$baseDirectoryPath = trim(str_replace(base_path(), '', $file->getPathname()), '/');

foreach(config('code-stencil.ignore.directories') as $dir) {
foreach (config('code-stencil.ignore.directories') as $dir) {
if ($dir === $baseDirectoryPath) {
return false;
}
}
} else {
foreach(config('code-stencil.ignore.files') as $fileName) {
foreach (config('code-stencil.ignore.files') as $fileName) {
if ($file->getFilename() === $fileName) {
return false;
}
}

foreach(config('code-stencil.ignore.patterns') as $pattern) {
foreach (config('code-stencil.ignore.patterns') as $pattern) {
if (preg_match($pattern, $file->getPath())) {
return false;
}
Expand All @@ -96,7 +106,7 @@ private function getFiles(): array
$rii = new RecursiveIteratorIterator($filterIter);

$files = [];
foreach($rii as $file) {
foreach ($rii as $file) {
if (!$file->isDir()) {
$files[] = $file->getPathname();
}
Expand Down
23 changes: 22 additions & 1 deletion src/Laravel/StencilFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ public function __invoke()

$stencil->variable($this->variables);

$stencil->save($this->path);
$savePath = $this->resolveSavePath($stencil);

$stencil->save($savePath);

if ($savePath != $this->path) {
unlink($this->path);
}
}

private function resolveSavePath(Stencil $stencil): string
{
$reflectionClass = new \ReflectionClass($stencil);
$prop = $reflectionClass->getProperty('variables');
$prop->setAccessible(true);

$definedVariables = $prop->getValue($stencil);

if (array_key_exists('overrideStubLocation', $definedVariables)) {
return $definedVariables['overrideStubLocation'];
}

return $this->path;
}
}

0 comments on commit ce93dc9

Please sign in to comment.