Skip to content

Commit

Permalink
add progress bar in missing command
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Dec 17, 2024
1 parent a246d60 commit d33f03e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
39 changes: 29 additions & 10 deletions src/Commands/MissingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Elegantly\Translator\Commands;

use Illuminate\Contracts\Console\PromptsForMissingInput;
use Laravel\Prompts\Progress;

use function Laravel\Prompts\info;
use function Laravel\Prompts\intro;
Expand All @@ -15,32 +16,50 @@ class MissingCommand extends TranslatorCommand implements PromptsForMissingInput

public $description = 'Display all the translation keys found in the codebase but not in the driver.';

public function formatPath(string $path): string
{
return (string) str($path)->after(base_path());
}

public function handle(): int
{
$locale = $this->argument('locale');
$sync = (bool) $this->option('sync');

/** @var null|Progress<int> $progress */
$progress = null;

$translator = $this->getTranslator();

$missing = $translator->getMissingTranslations($locale);
$missing = $translator->getMissingTranslations(
$locale,
start: function (int $total) use (&$progress) {
$progress = new Progress('Scanning your codebase', $total);
$progress->start();
},
progress: function (string $path) use (&$progress) {
$progress?->hint($this->formatPath($path));
$progress?->advance();
},
end: fn () => $progress?->finish(),
);

$count = count($missing);

intro('Using driver: '.$translator->driver::class);

note(count($missing).' missing keys detected.');
note("{$count} missing keys detected.");

table(
headers: ['Key', 'Count', 'Files'],
rows: collect($missing)
->map(function ($value, $key) {
return [
str($key)->limit(20)->value(),
(string) str($key)->limit(20),
(string) $value['count'],
implode("\n",
array_map(
fn ($file) => str($file)->after(base_path()),
$value['files'],
)
),
collect($value['files'])
->map(fn ($file) => $this->formatPath($file))
->join("\n"),
];
})->values()->all()
);
Expand All @@ -52,7 +71,7 @@ public function handle(): int
values: array_map(fn () => null, $missing)
);

info(count($missing).' missing keys added to the driver.');
info("{$count} missing keys added to the driver.");

}

Expand Down
7 changes: 3 additions & 4 deletions src/Services/SearchCode/PhpParserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ public function translationsByFiles(

$translations = collect($finder)
->map(function (SplFileInfo $file, string $path) use ($progress) {
if ($progress) {
$progress($path);
}

$lastModified = $file->getMTime();
$cachedResult = $this->cache?->get($path);
Expand All @@ -219,10 +222,6 @@ public function translationsByFiles(
$this->cache?->put($path, $translations);
}

if ($progress) {
$progress($path);
}

return $translations;
})
->filter()
Expand Down
17 changes: 14 additions & 3 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,28 @@ public function collect(): Translations
/**
* Scan the codebase to find keys not present in the driver
*
* @param null|(Closure(string $path):void) $progress
* @param null|(Closure(int $total):void) $start
* @param null|(Closure():void) $end
* @return array<string, array{ count: int, files: string[] }> The translations keys defined in the codebase but not defined in the driver
*/
public function getMissingTranslations(string $locale): array
{
public function getMissingTranslations(
string $locale,
?Closure $progress = null,
?Closure $start = null,
?Closure $end = null,
): array {
if (! $this->searchcodeService) {
throw TranslatorServiceException::missingSearchcodeService();
}

$translations = $this->getTranslations($locale);

$keys = $this->searchcodeService->filesByTranslations();
$keys = $this->searchcodeService->filesByTranslations(
progress: $progress,
start: $start,
end: $end
);

return collect($keys)
->filter(function ($value, $key) use ($translations) {
Expand Down

0 comments on commit d33f03e

Please sign in to comment.