Skip to content

Commit bd7e614

Browse files
authored
Delete useless model directories (#81)
1 parent 9827855 commit bd7e614

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

cos-sdk-v5/src/Composer.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,49 @@ public static function removeUnusedModels(Event $event)
1414

1515
if ($listedModels) {
1616
$vendorPath = $composer->getConfig()->get('vendor-dir');
17-
$dir = "{$vendorPath}/tencentcloud/monitor/src/TencentCloud/Monitor/V20180724/Models";
18-
$files = scandir($dir);
17+
$keepVersion = 'V20180724';
18+
$monitorPath = "{$vendorPath}/tencentcloud/monitor/src/TencentCloud/Monitor";
19+
20+
$items = scandir($monitorPath);
21+
foreach ($items as $item) {
22+
if ($item === '.' || $item === '..' || $item === $keepVersion) {
23+
continue;
24+
}
25+
26+
$path = "{$monitorPath}/{$item}";
27+
if (is_dir($path)) {
28+
self::deleteDir($path);
29+
}
30+
}
31+
32+
$v20180724Path = "{$monitorPath}/{$keepVersion}/Models";
33+
$files = scandir($v20180724Path);
1934
foreach ($files as $file) {
2035
if ($file === '.' || $file === '..') {
2136
continue;
2237
}
2338
if (!in_array($file, $listedModels)) {
24-
unlink("{$dir}/{$file}");
39+
unlink("{$v20180724Path}/{$file}");
2540
}
2641
}
2742

2843
$generator = $composer->getAutoloadGenerator();
2944
$generator->dump($composer->getConfig(), $composer->getRepositoryManager()->getLocalRepository(), $composer->getPackage(), $composer->getInstallationManager(), 'composer', true);
3045
}
3146
}
47+
48+
protected static function deleteDir(string $dir): void
49+
{
50+
$it = new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS);
51+
$ri = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
52+
53+
foreach ($ri as $file) {
54+
if ($file->isDir()) {
55+
rmdir($file->getPathname());
56+
} else {
57+
unlink($file->getPathname());
58+
}
59+
}
60+
rmdir($dir);
61+
}
3262
}

0 commit comments

Comments
 (0)