Skip to content

Commit 02ee71a

Browse files
committed
improve stats output
1 parent b2a2c8a commit 02ee71a

File tree

1 file changed

+92
-10
lines changed

1 file changed

+92
-10
lines changed

src/Commands/NovaLangStats.php

Lines changed: 92 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,27 @@ public function handle()
6868
$contributorsFile = __DIR__.'/../../contributors.json';
6969
$contributors = collect(json_decode($this->filesystem->get($contributorsFile), true));
7070

71-
$sourceKeys = array_keys(json_decode($this->filesystem->get($sourceFile), true));
71+
$sourceKeys = $this->getJsonKeys($sourceFile);
72+
$sourcePhpKeys = $this->getPhpKeys($this->directoryNovaSource().'/en');
7273

73-
$sourceCount = count($sourceKeys);
74+
$sourceCount = count($sourceKeys) + count($sourcePhpKeys);
75+
$translatedCount = 0;
7476

7577
$availableLocales = $this->getAvailableLocales();
7678
$blame = collect($this->getBlame());
7779

78-
$availableLocales->each(function (string $locale) use ($contributors, $sourceKeys, $sourceCount, $blame) {
80+
$availableLocales->each(function (string $locale) use ($contributors, $sourceKeys, $sourceCount, $sourcePhpKeys, $blame, &$translatedCount) {
7981

8082
$inputDirectory = $this->directoryFrom().'/'.$locale;
8183

8284
$inputFile = $inputDirectory.'.json';
8385

84-
$localeKeys = array_keys(json_decode($this->filesystem->get($inputFile), true));
85-
86+
$localeKeys = $this->getJsonKeys($inputFile);
8687
$missingKeys = array_diff($sourceKeys, $localeKeys);
8788

89+
$localePhpKeys = $this->getPhpKeys($inputDirectory);
90+
$missingPhpKeys = array_diff($sourcePhpKeys, $localePhpKeys);
91+
8892
$localeStat = $contributors->get($locale, [
8993
'name' => class_exists('Locale') ? \Locale::getDisplayName($locale) : $locale,
9094
'contributors' => [],
@@ -105,7 +109,12 @@ public function handle()
105109
}
106110
}
107111

108-
$localeStat['complete'] = $sourceCount - count($missingKeys);
112+
$complete = $sourceCount - count($missingKeys) - count($missingPhpKeys);
113+
$translatedCount += $complete;
114+
115+
$localeStat['complete'] = $complete;
116+
$localeStat['json'] = count($localeKeys) > 0;
117+
$localeStat['php'] = count($localePhpKeys) > 0;
109118

110119
$localeStat['contributors'] = collect($localeStat['contributors'])
111120
->map(function($lines, $name) {
@@ -120,10 +129,14 @@ public function handle()
120129

121130
});
122131

132+
$en = $contributors->pull('en');
133+
123134
$contributors = $contributors->sort(function($a, $b) {
124135
return $a['complete'] === $b['complete'] ? $a['name'] <=> $b['name'] : 0 - ($a['complete'] <=> $b['complete']);
125136
});
126137

138+
$contributors->prepend($en, 'en');
139+
127140
$outputFile = $outputDirectory.'/contributors.json';
128141

129142
$this->filesystem->put($outputFile, json_encode($contributors, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
@@ -133,7 +146,8 @@ public function handle()
133146

134147
$contributors->transform(function($localeStat, $locale) use ($sourceCount) {
135148

136-
$percent = round(($localeStat['complete'] / $sourceCount) * 100, 1).'%';
149+
$percent = $this->getPercent($localeStat['complete'], $sourceCount);
150+
$icon = $this->getPercentIcon($localeStat['complete'], $percent);
137151

138152
$contributors = implode(', ', array_map(function($contributor) {
139153
if ($contributor == '(unknown)') {
@@ -142,19 +156,65 @@ public function handle()
142156
return sprintf('[%s](https://github.com/%s)', $contributor, $contributor);
143157
}, array_keys($localeStat['contributors'])));
144158

145-
return sprintf('| %s | [%s](resources/lang/%s.json) | %d (%s) | %s |', $localeStat['name'], $locale, $locale, $localeStat['complete'], $percent, $contributors);
159+
$hasPhp = $localeStat['php'] ? sprintf('[`json`](resources/lang/%s.json)', $locale) : '~~`php`~~';
160+
$hasJson = $localeStat['json'] ? sprintf('[`php`](resources/lang/%s)', $locale) : '~~`json`~~';
161+
162+
return sprintf('| `%s` | %s | %s %s | ![%d (%s%%)](%s) | %s |', $locale, $localeStat['name'], $hasJson, $hasPhp, $localeStat['complete'], $percent, $icon, $contributors);
146163
});
147164

148165
$outputFile = $outputDirectory.'/README.excerpt.md';
149166

150-
$header = '## Available Languages'.PHP_EOL.PHP_EOL.'| Language | Code | Lines translated | Thanks to |'.PHP_EOL.'| --- | --- | --- | --- |';
167+
$languagesCount = $contributors->count();
168+
169+
$sourceComplete = $sourceCount * $languagesCount;
170+
$percent = $this->getPercent($translatedCount, $sourceComplete);
171+
$countIcon = $this->getPercentIcon($languagesCount);
172+
$icon = $this->getPercentIcon($translatedCount, $percent);
173+
174+
$totals = sprintf('Total languages ![%s](%s) ', $languagesCount, $countIcon).PHP_EOL.
175+
sprintf('Total lines translated ![%d (%s%%)](%s)', $sourceComplete, $percent, $icon);
176+
177+
$header = '## Available Languages'.PHP_EOL.PHP_EOL.
178+
$totals.PHP_EOL.PHP_EOL.
179+
'| Code | Language | Translated files | Lines translated | Thanks to |'.PHP_EOL.
180+
'| --- | --- | --- | --- | --- |';
151181

152182
$this->filesystem->put($outputFile, $header.PHP_EOL.$contributors->join(PHP_EOL));
153183

154184
$this->info(sprintf('Updated "README.excerpt.md" has been output to [%s].', $outputFile));
155185
$this->warn('* Replace the Available Languages table in README.md in your fork of the repository with the contents of this file.');
156186
}
157187

188+
protected function getPercent(int $complete, int $total): float
189+
{
190+
return round(($complete / $total) * 100, 1);
191+
}
192+
193+
protected function getPercentIcon(int $complete, float $percent = null): string
194+
{
195+
if (is_null($percent)) {
196+
return sprintf('https://img.shields.io/badge/%d-gray?style=flat-square', $complete);
197+
}
198+
199+
$colors = [
200+
0 => 'red',
201+
85 => 'orange',
202+
90 => 'yellow',
203+
95 => 'green',
204+
100 => 'brightgreen',
205+
];
206+
207+
$percent = floor($percent);
208+
209+
$colors = array_filter($colors, function($color, $limit) use ($percent) {
210+
return $percent >= $limit;
211+
}, ARRAY_FILTER_USE_BOTH);
212+
213+
$color = array_pop($colors) ?: 'lightgray';
214+
215+
return sprintf('https://img.shields.io/badge/%d-%s%%25-%s?style=flat-square', $complete, $percent, $color);
216+
}
217+
158218
protected function getAvailableLocales(): Collection
159219
{
160220
$localesByDirectories = collect($this->filesystem->directories($this->directoryFrom()))
@@ -164,12 +224,34 @@ protected function getAvailableLocales(): Collection
164224

165225
$localesByFiles = collect($this->filesystem->files($this->directoryFrom()))
166226
->map(function (SplFileInfo $splFileInfo) {
167-
return str_replace('.'.$splFileInfo->getExtension(), '', $splFileInfo->getFilename());
227+
return $splFileInfo->getBasename('.'.$splFileInfo->getExtension());
168228
});
169229

170230
return $localesByDirectories->intersect($localesByFiles)->values();
171231
}
172232

233+
protected function getJsonKeys(string $path): array
234+
{
235+
if ($this->filesystem->exists($path)) {
236+
return array_keys(json_decode($this->filesystem->get($path), true));
237+
}
238+
239+
return [];
240+
}
241+
242+
protected function getPhpKeys(string $path): array
243+
{
244+
return collect($this->filesystem->glob($path.'/*.php'))
245+
->map(function (string $path) {
246+
$file = basename($this->filesystem->basename($path), '.php');
247+
$keys = collect(array_keys($this->filesystem->getRequire($path)))
248+
->map(function ($key) use ($file) {
249+
return "$file.$key";
250+
});
251+
return $keys;
252+
})->flatten()->all();
253+
}
254+
173255
protected function directoryFrom(): string
174256
{
175257
return base_path('vendor/coderello/laravel-nova-lang/resources/lang');

0 commit comments

Comments
 (0)