Extra2D/scripts/update_includes_v2.js

150 lines
4.5 KiB
JavaScript
Raw Normal View History

/**
* @file update_includes_v2.js
* @brief 更新头文件包含路径完整版
* @description #include <extra2d/xxx/xxx.h> #include "extra2d/xxx/xxx.h"
* 替换为 #include <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'];
// 替换规则:处理尖括号和双引号两种情况
const PATTERNS = [
{ regex: /#include\s+<extra2d\//g, replacement: '#include <' },
{ regex: /#include\s+"extra2d\//g, replacement: '#include "' }
];
/**
* @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 totalChanges = 0;
// 应用所有替换规则
for (const pattern of PATTERNS) {
const matches = content.match(pattern.regex);
if (matches) {
totalChanges += matches.length;
}
content = content.replace(pattern.regex, pattern.replacement);
}
// 检查是否有修改
if (content !== originalContent) {
fs.writeFileSync(filePath, content, 'utf-8');
return { modified: true, changes: totalChanges };
}
return { modified: false, changes: 0 };
}
/**
* @brief 主函数
*/
function main() {
console.log('========================================');
console.log('🚀 开始更新头文件包含路径(完整版)');
console.log('========================================');
console.log('替换规则:');
console.log(' #include <extra2d/xxx.h> -> #include <xxx.h>');
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();