const fs = require('fs');
|
const path = require('path');
|
|
// 檔案路徑設定
|
const sourceFile = path.resolve(__dirname, 'CB_IMGPSScanImp.pas');
|
const outDir = path.resolve(__dirname, 'separate', 'scanImp');
|
|
if (!fs.existsSync(sourceFile)) {
|
console.error("找不到原始檔案:", sourceFile);
|
process.exit(1);
|
}
|
|
if (!fs.existsSync(outDir)) {
|
fs.mkdirSync(outDir, { recursive: true });
|
}
|
|
const content = fs.readFileSync(sourceFile, 'utf8');
|
|
const implMatch = content.match(/\bimplementation\b/i);
|
const initMatch = content.match(/\binitialization\b/i);
|
|
if (!implMatch || !initMatch) {
|
console.error("無法在檔案中找到 implementation 或 initialization 區段。");
|
process.exit(1);
|
}
|
|
const headerPart = content.substring(0, implMatch.index + implMatch[0].length);
|
let implPart = content.substring(implMatch.index + implMatch[0].length, initMatch.index);
|
const footerPart = content.substring(initMatch.index);
|
|
const usesMatch = implPart.match(/^\s*uses[\s\S]*?;/im);
|
let usesClause = "";
|
if (usesMatch) {
|
usesClause = usesMatch[0];
|
implPart = implPart.substring(usesMatch.index + usesMatch[0].length);
|
}
|
|
// 提取並保留不在方法內的實作開頭,例如 {$R *.DFM} 或是 { TCB_IMGPSScanX }
|
const methodRegex = /^(?:procedure|function)\s+TCB_IMGPSScanX\./im;
|
let otherImpl = "";
|
const firstMethodMatch = implPart.match(methodRegex);
|
|
if (firstMethodMatch) {
|
otherImpl = implPart.substring(0, firstMethodMatch.index);
|
implPart = implPart.substring(firstMethodMatch.index);
|
}
|
|
// 切割所有方法
|
const methodSplitRegex = /(?=^(?:procedure|function)\s+TCB_IMGPSScanX\.)/im;
|
const methods = implPart.split(methodSplitRegex).filter(m => m.trim().length > 0);
|
|
const uiMethods = [];
|
const scanMethods = [];
|
const dataMethods = [];
|
const utilMethods = [];
|
|
methods.forEach(m => {
|
const nameMatch = m.match(/^(?:procedure|function)\s+TCB_IMGPSScanX\.([A-Za-z0-9_]+)/i);
|
if (nameMatch) {
|
const name = nameMatch[1].toLowerCase();
|
if (/(click|mouse|key|drag|scroll|event|menu|btn|form|node|tree|view|activate|paint|resize)/.test(name)) {
|
uiMethods.push(m);
|
} else if (/(scan|acquire|image|graphic|twain|dpi|color|deskew|rotate|crop|smooth|point)/.test(name)) {
|
scanMethods.push(m);
|
} else if (/(ftp|http|trans|load|sql|server|setinf|data|ask|log|file|path|zip|dir|docno|formid|case)/.test(name)) {
|
dataMethods.push(m);
|
} else {
|
utilMethods.push(m);
|
}
|
} else {
|
utilMethods.push(m);
|
}
|
});
|
|
fs.writeFileSync(path.join(outDir, 'CB_IMGPSScanImp_UI.pas'), uiMethods.join(''), 'utf8');
|
fs.writeFileSync(path.join(outDir, 'CB_IMGPSScanImp_Scan.pas'), scanMethods.join(''), 'utf8');
|
fs.writeFileSync(path.join(outDir, 'CB_IMGPSScanImp_Data.pas'), dataMethods.join(''), 'utf8');
|
fs.writeFileSync(path.join(outDir, 'CB_IMGPSScanImp_Utils.pas'), utilMethods.join(''), 'utf8');
|
|
const mainFile = [
|
headerPart,
|
usesClause,
|
otherImpl,
|
"{$I CB_IMGPSScanImp_UI.pas}",
|
"{$I CB_IMGPSScanImp_Scan.pas}",
|
"{$I CB_IMGPSScanImp_Data.pas}",
|
"{$I CB_IMGPSScanImp_Utils.pas}",
|
footerPart
|
].join('');
|
|
fs.writeFileSync(path.join(outDir, 'CB_IMGPSScanImp_Main.pas'), mainFile, 'utf8');
|
|
console.log("拆分完成!產生的五個檔案如下:");
|
console.log("1. CB_IMGPSScanImp_Main.pas (主結構與引用)");
|
console.log("2. CB_IMGPSScanImp_UI.pas (UI事件相關:", uiMethods.length, "個方法)");
|
console.log("3. CB_IMGPSScanImp_Scan.pas (掃描影像相關:", scanMethods.length, "個方法)");
|
console.log("4. CB_IMGPSScanImp_Data.pas (資料存取與網路傳輸:", dataMethods.length, "個方法)");
|
console.log("5. CB_IMGPSScanImp_Utils.pas (工具與其他邏輯:", utilMethods.length, "個方法)");
|