Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/Commands/AppPluginZipCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (is_file($zipFilePath)) {
unlink($zipFilePath);
}
$this->zipDirectory($name, $sourceDir, $zipFilePath);

$excludePaths = ['node_modules', '.git', '.idea', '.vscode', '__pycache__'];

$this->zipDirectory($name, $sourceDir, $zipFilePath, $excludePaths);
return self::SUCCESS;
}

/**
* @param $name
* @param $sourceDir
* @param $zipFilePath
* @param array $excludePaths
* @return bool
* @throws Exception
*/
protected function zipDirectory($name, $sourceDir, $zipFilePath) {
protected function zipDirectory($name, $sourceDir, $zipFilePath, array $excludePaths = []): bool
{
$zip = new ZipArchive();

if ($zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) {
Expand All @@ -67,19 +72,34 @@ protected function zipDirectory($name, $sourceDir, $zipFilePath) {
$sourceDir = realpath($sourceDir);

$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($sourceDir),
new RecursiveDirectoryIterator($sourceDir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePath = $name . DIRECTORY_SEPARATOR . substr($filePath, strlen($sourceDir) + 1);

// 修正排除目录的判断逻辑,确保所有层级都能排除
$shouldExclude = false;
foreach ($excludePaths as $excludePath) {
// 统一路径分隔符为正斜杠,兼容 Windows
$normalizedRelativePath = str_replace('\\', '/', $relativePath);
$normalizedExcludePath = str_replace('\\', '/', $excludePath);
if (preg_match('#/(?:' . preg_quote($normalizedExcludePath, '#') . ')(/|$)#i', $normalizedRelativePath)) {
$shouldExclude = true;
break;
}
}
if ($shouldExclude) {
continue;
}

$zip->addFile($filePath, $relativePath);
}
}

return $zip->close();
}

}