<?php
/**
* http://welfaredelivery.wpygo.com/tool.php
*
* 开发小工具:提取fastadmin项目中属于某个插件的文件并拷贝到 addons/[插件名] 目录,便于打包插件。
*
* 功能说明:
* - 只检查 application 和 public 目录
* - 只根据文件路径判断是否包含插件名称
* - 特殊处理 /public/assets/addons/[插件名] 目录中的文件,将其复制到插件的 assets 目录
* - 支持 --dry-run 模式,只列出将要拷贝的文件
*
* 打包文档:https://doc.fastadmin.net/developer/78.html#toc-3
* 下面是打包命令
* cd D:\www\welfaredelivery.wpygo.com
* D:\php-7.4.33-Win32-vc15-x64\php think addon -a welfaredelivery -c package
*/
$plugin = "welfaredelivery"; // 默认插件名
$root = "D:\www\welfaredelivery.wpygo.com"; // 项目根目录
$destBase = $root . DIRECTORY_SEPARATOR . 'addons' . DIRECTORY_SEPARATOR . $plugin;
// 只检查下面的目录
$checkDirs = [
'application',
'public'
];
$copied = 0;
$candidates = [];
echo "正在项目中搜索插件 '{$plugin}' ...\n";
foreach ($checkDirs as $checkDirName) {
$checkDir = $root . DIRECTORY_SEPARATOR . $checkDirName;
if (!is_dir($checkDir)) {
echo "跳过 {$checkDir} (目录不存在)\n";
continue;
}
echo "正在扫描 {$checkDir}...\n";
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($checkDir, RecursiveDirectoryIterator::SKIP_DOTS));
foreach ($rii as $file) {
$filePath = $file->getPathname();
// 只处理文件
if (!$file->isFile()) continue;
$relPath = ltrim(str_replace($root, '', $filePath), DIRECTORY_SEPARATOR);
// 只根据路径判断是否包含插件名
if (stripos($relPath, $plugin) !== false) {
$candidates[] = $relPath;
}
}
}
// 特殊处理 /public/assets/addons/[插件名] 目录
$assetsAddonDir = 'public' . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'addons' . DIRECTORY_SEPARATOR . $plugin;
$assetsAddonPath = $root . DIRECTORY_SEPARATOR . $assetsAddonDir;
if (is_dir($assetsAddonPath)) {
echo "正在扫描 {$assetsAddonPath}...\n";
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($assetsAddonPath, RecursiveDirectoryIterator::SKIP_DOTS));
foreach ($rii as $file) {
$filePath = $file->getPathname();
// 只处理文件
if (!$file->isFile()) continue;
$relPath = ltrim(str_replace($root, '', $filePath), DIRECTORY_SEPARATOR);
// 将文件添加到候选列表中
$candidates[] = $relPath;
}
}
if (empty($candidates)) {
echo "未找到与插件 '{$plugin}' 相关的文件。\n";
exit(0);
}
echo "找到 " . count($candidates) . " 个候选文件需要复制。\n";
// 创建目标基础目录
if (!$dryRun && !is_dir($destBase)) {
mkdir($destBase, 0777, true);
echo "创建目录: {$destBase}\n";
}
foreach ($candidates as $relPath) {
$source = $root . DIRECTORY_SEPARATOR . $relPath;
// 特殊处理 /public/assets/addons/[插件名] 路径
if (strpos($relPath, 'public' . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'addons' . DIRECTORY_SEPARATOR . $plugin) === 0) {
// 将 /public/assets/addons/[插件名]/* 映射到 /addons/[插件名]/assets/*
$relativeAssetPath = substr($relPath, strlen('public' . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'addons' . DIRECTORY_SEPARATOR . $plugin));
$target = $destBase . DIRECTORY_SEPARATOR . 'assets' . $relativeAssetPath;
} else {
$target = $destBase . DIRECTORY_SEPARATOR . $relPath;
}
$targetDir = dirname($target);
if (!is_dir($targetDir)) {
if ($dryRun) {
echo "[预览模式] 将创建目录: {$targetDir}\n";
} else {
@mkdir($targetDir, 0777, true);
echo "创建目录: {$targetDir}\n";
}
}
if ($dryRun) {
echo "[预览模式] 将复制: {$source} -> {$target}\n";
} else {
if (@copy($source, $target)) {
echo "已复制: {$source} -> {$target}\n";
$copied++;
} else {
echo "复制失败: {$source} -> {$target}\n";
}
}
}
if ($dryRun) {
echo "预览模式完成。" . count($candidates) . " 个文件将被复制。\n";
} else {
echo "完成。共复制 {$copied} 个文件到 {$destBase}。\n";
}

被折叠的 条评论
为什么被折叠?



