From afb07f899ed8362a7377012c3ed8bbb9975167b6 Mon Sep 17 00:00:00 2001 From: Kit Senior Date: Fri, 3 Dec 2021 18:07:54 +0000 Subject: [PATCH] update commands --- dev/Commands/NovaLangCleanup.php | 64 ++++++++++++++++++++++++++++++++ dev/Commands/NovaLangCountry.php | 4 +- dev/Commands/NovaLangReorder.php | 2 +- dev/Commands/NovaLangStats.php | 35 ++++++++++------- nova-lang | 1 + 5 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 dev/Commands/NovaLangCleanup.php diff --git a/dev/Commands/NovaLangCleanup.php b/dev/Commands/NovaLangCleanup.php new file mode 100644 index 0000000..9244a91 --- /dev/null +++ b/dev/Commands/NovaLangCleanup.php @@ -0,0 +1,64 @@ +'; + protected const REMOVED_MISSING_KEYS = '%d missing or blank translation keys for "%s" locale were removed.'; + protected const NO_MISSING_KEYS = '"%s" locale has no missing or blank translation keys.'; + + /** + * The name and signature of the console command. + * + * @var string + */ + protected $signature = 'cleanup + {locales? : Comma-separated list of languages} + {--all : Output all languages}'; + + /** + * The console command description. + * + * @var string + */ + protected $description = 'Remove missing or blank keys from Nova language files.'; + + /** + * Handle the command for a given locale. + * + * @param string $locale + * @return void + */ + protected function handleLocale(string $locale): void + { + $inputFile = $this->directoryFrom("$locale.json"); + + if (! $this->availableLocales->contains($locale)) { + $this->warn(sprintf(static::LOCALE_FILE_DOES_NOT_EXIST, $locale)); + + return; + } + + $inputKeys = $this->loadJson($inputFile); + + $outputKeys = array_filter($inputKeys, fn ($text) => ! empty(trim($text)) && $text !== static::MISSING_TEXT); + + $missingKeys = count($inputKeys) - count($outputKeys); + + $outputFile = $inputFile; + + if ($missingKeys > 0) { + $this->saveJson($outputFile, $outputKeys); + + $this->info(sprintf(static::REMOVED_MISSING_KEYS, $missingKeys, $locale, $outputFile)); + } else { + $this->info(sprintf(static::NO_MISSING_KEYS, $locale)); + } + } + + protected function afterHandle() + { + // + } +} diff --git a/dev/Commands/NovaLangCountry.php b/dev/Commands/NovaLangCountry.php index ab737f8..467e686 100644 --- a/dev/Commands/NovaLangCountry.php +++ b/dev/Commands/NovaLangCountry.php @@ -8,7 +8,7 @@ class NovaLangCountry extends AbstractDevCommand { protected const SAVED_COUNTRY_NAMES = 'Country names for "%s" locale have been added to [%s].'; - protected const RUN_MISSING_COMMAND = 'Country names were not found for %d keys. These were not added to the file. Run the command `php nova-lang missing` to add them.'; + protected const RUN_MISSING_COMMAND = 'Country names were not found for %d keys, which were not added to the file. Run the command `php nova-lang missing %s` to add them.'; protected const COUNTRY_NAMES_NOT_FOUND = 'Country names could not be found for "%s" locale. The file was not updated.'; protected const CLDR_URL = 'https://github.com/unicode-org/cldr-json/raw/main/cldr-json/cldr-localenames-modern/main/%s/territories.json'; @@ -69,7 +69,7 @@ protected function handleLocale(string $locale): void $this->info(sprintf(static::SAVED_COUNTRY_NAMES, $locale, $outputFile)); if ($untranslated > 0) { - $this->warn(' ' . sprintf(static::RUN_MISSING_COMMAND, $untranslated, $outputFile)); + $this->warn(' ' . sprintf(static::RUN_MISSING_COMMAND, $untranslated, $locale)); $this->newLine(); } } else { diff --git a/dev/Commands/NovaLangReorder.php b/dev/Commands/NovaLangReorder.php index 7ecfb47..0de39d6 100644 --- a/dev/Commands/NovaLangReorder.php +++ b/dev/Commands/NovaLangReorder.php @@ -8,7 +8,7 @@ class NovaLangReorder extends AbstractDevCommand { protected const KEYS_OUT_OF_ORDER = '%d translation keys for "%s" locale were out of order. The updated file has been output to [%s].'; protected const NO_KEYS_OUT_OF_ORDER = '"%s" locale has no translation keys out of order.'; - protected const RUN_MISSING_COMMAND = '%d translation keys for "%s" locale were missing. Run the command `php nova-lang missing` to add them.'; + protected const RUN_MISSING_COMMAND = '%d translation keys for "%s" locale were missing. Run the command `php nova-lang missing %2$s` to add them.'; /** * The name and signature of the console command. diff --git a/dev/Commands/NovaLangStats.php b/dev/Commands/NovaLangStats.php index 692b189..f600542 100644 --- a/dev/Commands/NovaLangStats.php +++ b/dev/Commands/NovaLangStats.php @@ -134,7 +134,7 @@ public function handle() $contributorsTable = $contributors->map(function ($localeStat, $locale) use ($sourceCount) { $percent = $this->getPercent($localeStat['complete'], $sourceCount); - $icon = $this->getPercentIcon($localeStat['complete'], $percent); + $icon = $this->getPercentBadge($localeStat['complete'], $percent); $contributors = implode(', ', array_map(function ($contributor) { if ($contributor == '(unknown)') { @@ -156,10 +156,17 @@ public function handle() $sourceComplete = $sourceCount * $languagesCount; $percent = $this->getPercent($translatedCount, $sourceComplete); - $countIcon = $this->getPercentIcon($languagesCount); - $icon = $this->getPercentIcon($translatedCount, $percent); + $countIcon = $this->getTextBadge($languagesCount); + $icon = $this->getPercentBadge($translatedCount, $percent); - $totals = sprintf('Total languages ![%s](%s) ', $languagesCount, $countIcon) . PHP_EOL . + $composer = $this->loadJson($this->basePath('composer.lock'))['packages-dev']; + $package = array_filter($composer, fn ($package) => $package['name'] == 'laravel/nova'); + $novaVersion = array_shift($package)['version']; + $versionIcon = $this->getTextBadge($novaVersion); + + $totals = + sprintf('Latest Nova version ![%s](%s) ', $novaVersion, $versionIcon) . PHP_EOL . + sprintf('Total languages ![%s](%s) ', $languagesCount, $countIcon) . PHP_EOL . sprintf('Total lines translated ![%s (%s%%)](%s)', number_format($translatedCount), $percent, $icon); $header = '## Available Languages' . PHP_EOL . PHP_EOL. @@ -186,11 +193,12 @@ public function handle() $contributorsList = $contributors->map(function ($localeStat, $locale) use ($sourceCount) { $percent = $this->getPercent($localeStat['complete'], $sourceCount); - return sprintf('* `%s` %s · **%d (%s%%)**', str_replace('-', '‑', $locale), $localeStat['name'], $localeStat['complete'], $percent); + return sprintf('* `%s` %s · **%d** (%s%%)', str_replace('-', '‑', $locale), $localeStat['name'], $localeStat['complete'], $percent); }); - $totals = sprintf('Total languages **%s** ', $languagesCount) . PHP_EOL . - sprintf('Total lines translated **%s (%s%%)**', number_format($translatedCount), $percent); + $totals = sprintf('Current Nova version **%s** ', $novaVersion) . PHP_EOL . + sprintf('Total languages **%s** ', $languagesCount) . PHP_EOL . + sprintf('Total lines translated **%s** (%s%%)', number_format($translatedCount), $percent); $header = '### Available Languages' . PHP_EOL . PHP_EOL . $totals . PHP_EOL; @@ -201,7 +209,7 @@ public function handle() $originalContents = $this->loadText($outputFile); - $contents = preg_replace('/(.+)## Available Languages.+/sm', '$1' . $contents, $originalContents); + $contents = preg_replace('/^#+ Available Languages.+/sm', '$1' . $contents, $originalContents); $this->saveText($outputFile, $contents); @@ -217,12 +225,8 @@ protected function getPercent(int $complete, int $total): float return $complete > $total ? 100 : round(($complete / $total) * 100, 1); } - protected function getPercentIcon($complete, $percent = null): string + protected function getPercentBadge(float $complete, float $percent): string { - if (is_null($percent)) { - return sprintf('https://img.shields.io/badge/%d-gray?style=flat-square', $complete); - } - $colors = [ 1 => 'red', 85 => 'orange', @@ -244,6 +248,11 @@ protected function getPercentIcon($complete, $percent = null): string return sprintf('https://img.shields.io/badge/%s-%s%%25-%s?style=flat-square', $complete, $percent, $color); } + protected function getTextBadge(string $text): string + { + return sprintf('https://img.shields.io/badge/%s-gray?style=flat-square', $text); + } + protected function getAvailableLocales(): Collection { $localesByDirectories = collect($this->filesystem->directories($this->directoryFrom())) diff --git a/nova-lang b/nova-lang index bbccd05..e38bc90 100644 --- a/nova-lang +++ b/nova-lang @@ -16,6 +16,7 @@ $artisan->setName('Laravel Nova Language'); $artisan->resolve(Commands\NovaLangCountry::class); $artisan->resolve(Commands\NovaLangMissing::class); +$artisan->resolve(Commands\NovaLangCleanup::class); $artisan->resolve(Commands\NovaLangReorder::class); $artisan->resolve(Commands\NovaLangStats::class);