Skip to content

Commit 77e2b2d

Browse files
Add file hash check to speed up processing
1 parent 6e579fa commit 77e2b2d

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ return [
4545
* extension to use for the enum files
4646
*/
4747
'enum_extension' => '.d.ts',
48+
49+
50+
/*
51+
* Enable File hash check
52+
* - adds a hash check to determine if a file has changed to speed up performance
53+
*/
54+
'enable_file_hash_check' => true,
4855
];
4956
```
5057

config/enum-convertor-laravel.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@
1313
* extension to use for the enum files
1414
*/
1515
'enum_extension' => '.d.ts',
16+
17+
/*
18+
* Enable File hash check
19+
* - adds a hash check to determine if a file has changed to speed up performance
20+
*/
21+
'enable_file_hash_check' => true,
1622
];

src/Commands/EnumConvertorCommand.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace GearboxSolutions\EnumConvertor\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Cache;
67
use Illuminate\Support\Facades\File;
78
use Illuminate\Support\Str;
89

@@ -19,6 +20,10 @@ public function handle(): int
1920
$files = File::allFiles(base_path($path));
2021

2122
collect($files)->each(function ($file) use ($outputPath) {
23+
if(!$this->hasFileChanged($file)) {
24+
return;
25+
}
26+
2227
$class = Str::of($file)->replace('.php', '')
2328
->replace(base_path(), '')
2429
->explode('/')
@@ -87,4 +92,24 @@ private function convertValue($item)
8792
default => $item,
8893
};
8994
}
95+
96+
private function hasFileChanged($file) {
97+
if(!config('enum-convertor-laravel.enable_file_hash_check', false)) {
98+
return true;
99+
}
100+
101+
$fileHash = hash_file('sha256', $file->getPathname());
102+
103+
$cacheKey = 'enum-convertor-' . hash('sha256', $file->getPathname());
104+
105+
$previousHash = Cache::get($cacheKey, '');
106+
107+
if($fileHash !== $previousHash) {
108+
Cache::put($cacheKey, $fileHash);
109+
110+
return true;
111+
}
112+
113+
return false;
114+
}
90115
}

0 commit comments

Comments
 (0)