Skip to content

Commit

Permalink
refactor(BitoCliGenerator): improve code structure and readability
Browse files Browse the repository at this point in the history
- Introduce ProcessHelper to simplify process handling
- Enhance verbosity control by setting debug level for output style
- Refactor process execution to use mustRun method with ProcessHelper
- Clean up unused variables and improve code organization
  • Loading branch information
[email protected] committed Oct 13, 2024
1 parent 961e632 commit dd6cd69
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions app/Generators/BitoCliGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use App\ConfigManager;
use App\Contracts\GeneratorContract;
use Illuminate\Console\OutputStyle;
use Illuminate\Support\Facades\Artisan;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;

final class BitoCliGenerator implements GeneratorContract
Expand All @@ -29,10 +31,22 @@ final class BitoCliGenerator implements GeneratorContract
*/
private $outputStyle;

/**
* @var \Symfony\Component\Console\Helper\ProcessHelper
*/
private $processHelper;

/**
* @psalm-suppress UndefinedMethod
*/
public function __construct(array $config)
{
$this->config = $config;
$this->outputStyle = resolve(OutputStyle::class);
$this->outputStyle = tap(clone resolve(OutputStyle::class))->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
$this->processHelper = (function () {
/** @noinspection PhpUndefinedMethodInspection */
return $this->getArtisan()->getHelperSet()->get('process');
})->call(Artisan::getFacadeRoot());
}

/**
Expand All @@ -41,12 +55,23 @@ public function __construct(array $config)
public function generate(string $prompt): string
{
// file_put_contents($promptFile = ConfigManager::globalPath($this->config['prompt_filename']), $prompt);
//
// return resolve(
// Process::class,
// ['command' => [$this->config['path'] ?: 'bito', '-p', $promptFile]] + $this->config['parameters']
// )->mustRun(function (string $type, string $data): void {
// Process::OUT === $type ? $this->outputStyle->write($data) : $this->outputStyle->write("<fg=red>$data</>");
// })->getOutput();

return resolve(Process::class, ['command' => [$this->config['path']]] + $this->config['parameters'])
->setInput($prompt)
->mustRun(function (string $type, string $data): void {
Process::OUT === $type ? $this->outputStyle->write($data) : $this->outputStyle->write("<fg=red>$data</>");
})
return $this
->processHelper
->mustRun(
$this->outputStyle,
resolve(
Process::class,
['command' => [$this->config['path']]] + $this->config['parameters']
)->setInput($prompt)
)
->getOutput();
}
}

0 comments on commit dd6cd69

Please sign in to comment.