Extra2D/scripts/update_includes.js

145 lines
4.1 KiB
JavaScript

/**
* @file update_includes.js
* @brief 更新头文件包含路径
* @description 将 #include <extra2d/xxx/xxx.h> 替换为 #include <xxx/xxx.h>
* 扫描所有 .h, .hpp, .cpp 文件进行替换
*/
const fs = require('fs');
const path = require('path');
// 配置
const TARGET_DIRS = [
path.join(__dirname, '..', 'Extra2D', 'include'),
path.join(__dirname, '..', 'Extra2D', 'src'),
path.join(__dirname, '..', 'examples')
];
const FILE_EXTENSIONS = ['.h', '.hpp', '.cpp'];
// 替换规则:将 <extra2d/ 替换为 <
const OLD_PREFIX = /<extra2d\//g;
const NEW_PREFIX = '<';
/**
* @brief 递归获取所有目标文件
* @param {string} dir - 要扫描的目录
* @param {string[]} files - 文件列表(用于递归)
* @returns {string[]} 文件路径数组
*/
function getTargetFiles(dir, files = []) {
if (!fs.existsSync(dir)) {
return files;
}
const items = fs.readdirSync(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
// 递归扫描子目录
getTargetFiles(fullPath, files);
} else if (stat.isFile()) {
// 检查是否为目标文件类型
const ext = path.extname(item).toLowerCase();
if (FILE_EXTENSIONS.includes(ext)) {
files.push(fullPath);
}
}
}
return files;
}
/**
* @brief 处理单个文件,替换包含路径
* @param {string} filePath - 文件路径
* @returns {object} 处理结果 { modified: boolean, changes: number }
*/
function processFile(filePath) {
let content = fs.readFileSync(filePath, 'utf-8');
const originalContent = content;
// 统计替换次数
let matchCount = 0;
const matches = content.match(OLD_PREFIX);
if (matches) {
matchCount = matches.length;
}
// 执行替换
content = content.replace(OLD_PREFIX, NEW_PREFIX);
// 检查是否有修改
if (content !== originalContent) {
fs.writeFileSync(filePath, content, 'utf-8');
return { modified: true, changes: matchCount };
}
return { modified: false, changes: 0 };
}
/**
* @brief 主函数
*/
function main() {
console.log('========================================');
console.log('🚀 开始更新头文件包含路径');
console.log('========================================');
console.log('替换规则: #include <extra2d/xxx.h> -> #include <xxx.h>');
console.log('');
// 收集所有目标文件
let allFiles = [];
for (const dir of TARGET_DIRS) {
const files = getTargetFiles(dir);
allFiles = allFiles.concat(files);
}
console.log(`📊 发现 ${allFiles.length} 个目标文件`);
console.log('');
if (allFiles.length === 0) {
console.log('⚠️ 没有找到目标文件');
return;
}
// 处理文件
let modifiedCount = 0;
let totalChanges = 0;
const modifiedFiles = [];
for (const filePath of allFiles) {
const result = processFile(filePath);
if (result.modified) {
modifiedCount++;
totalChanges += result.changes;
const relativePath = path.relative(path.join(__dirname, '..'), filePath);
modifiedFiles.push({ path: relativePath, changes: result.changes });
console.log(`✅ 已更新: ${relativePath} (${result.changes} 处替换)`);
}
}
console.log('');
console.log('========================================');
console.log('✨ 处理完成!');
console.log(` 扫描文件: ${allFiles.length}`);
console.log(` 修改文件: ${modifiedCount}`);
console.log(` 总替换数: ${totalChanges}`);
console.log('========================================');
// 显示修改的文件列表
if (modifiedFiles.length > 0) {
console.log('');
console.log('📋 修改的文件列表:');
modifiedFiles.forEach((file, index) => {
console.log(` ${index + 1}. ${file.path} (${file.changes} 处)`);
});
}
}
// 执行主函数
main();