// Generate reviewable apply_patch input; this script never writes project files. const fs = require('fs'); const path = require('path'); const root = path.resolve(__dirname, '../..'); const base = path.join(root, 'ruoyi-system/src/main/java/com/ruoyi/system'); const labels = { RentAccount: 'UserId', RentApp: 'Name', RentNode: 'Name', RentCredential: 'ApiKey', RentEvent: 'EventNo', RentLedger: 'BusinessKey', RentNonce: 'Nonce', RentOrder: 'OrderNo', RentPackage: 'Name', RentRefund: 'RefundNo', RentSettlement: 'SettlementNo', RentSettlementItem: 'OrderId', RentTask: 'TaskNo', RentTransaction: 'TxId', RentWallet: 'Address', RentWebhook: 'EventNo' }; const read = p => fs.readFileSync(p, 'utf8').replace(/\r\n/g, '\n'); function blockEnd(text, open) { let depth = 0; let quote = null; for (let i = open; i < text.length; i++) { const c = text[i]; if (quote) { if (c === '\\') i++; else if (c === quote) quote = null; } else if (c === '"' || c === "'") quote = c; else if (c === '{') depth++; else if (c === '}' && --depth === 0) return i + 1; } throw new Error('Unclosed generated block'); } function removeBlocks(text, pattern, includeDoc = false) { let match; while ((match = pattern.exec(text))) { let start = match.index; // The regex ends at the method/if brace. Mapping paths can contain {id}. const open = start + match[0].lastIndexOf('{'); const end = blockEnd(text, open); if (includeDoc) { const docStart = text.lastIndexOf('/**', start); const docEnd = text.lastIndexOf('*/', start); if (docStart >= 0 && docEnd > docStart && !text.slice(docEnd + 2, start).trim()) { start = text.lastIndexOf('\n', docStart) + 1; } } text = text.slice(0, start) + text.slice(end).replace(/^\n/, ''); pattern.lastIndex = 0; } return text; } const model = process.argv[2]; if (!(model in labels)) throw new Error('Explicit rental model required'); const plus = model + 'Plus'; const domain = read(path.join(base, 'domain', plus + '.java')); const hasUser = /private Long userId;/.test(domain); const label = labels[model]; if (!new RegExp('\\bget' + label + '\\(').test(domain)) throw new Error('Invalid display field'); let patch = '*** Begin Patch\n'; const changed = []; for (const [folder, name] of [ ['controller', model + 'Controller.java'], ['service', 'I' + model + 'Service.java'], ['service/impl', model + 'ServiceImpl.java'] ]) { const file = path.join(base, folder, name); const before = read(file); let after = before; if (!hasUser) { if (folder === 'controller') { after = removeBlocks(after, /^"\)\n[ \t]*public AjaxResult user\w+\([^\n]*\)\s*\{/m); after = removeBlocks(after, /^[ \t]*@PreAuthorize\("@ss\.hasPermi\('[^'\n]*:user-[^'\n]*'\)"\)[\s\S]*?public [^{]+\{/m, true); } if (folder === 'service/impl') { after = removeBlocks(after, /^[ \t]*if\s*\(\s*isUser\s*\)\s*\{/m); after = removeBlocks(after, /^[ \t]*private void checkUser(?:Plus)?\([^)]*\)\s*\{/m); after = after.replace('import com.ruoyi.common.utils.SecurityUtils;\n', ''); after = after.replaceAll(plus + '::getUserId,', ''); } after = after.replace(/\s*,\s*boolean isUser/g, ''); after = after.replace(/,\s*(?:false|true)(?=[,)])/g, ''); if (/\b(?:isUser|checkUser|checkUserPlus|getUserId|setUserId|userGetInfo|userRemove)\b/.test(after)) { throw new Error('Unexpected user-scope reference in ' + file); } } if (label !== 'Name') { after = after.replaceAll(plus + '::getName', plus + '::get' + label); after = after.replace('selectItem.setName(item.getName());', 'selectItem.setName(String.valueOf(item.get' + label + '()));'); } if (after === before) continue; const oldLines = before.trimEnd().split('\n'); const newLines = after.trimEnd().split('\n'); let start = 0; while (start < Math.min(oldLines.length, newLines.length) && oldLines[start] === newLines[start]) start++; let oldEnd = oldLines.length, newEnd = newLines.length; while (oldEnd > start && newEnd > start && oldLines[oldEnd - 1] === newLines[newEnd - 1]) { oldEnd--; newEnd--; } const contextStart = Math.max(0, start - 3); const contextEnd = Math.min(oldLines.length, oldEnd + 3); patch += '*** Update File: ' + file.replaceAll('\\', '/') + '\n@@\n'; patch += oldLines.slice(contextStart, start).map(l => ' ' + l + '\n').join(''); patch += oldLines.slice(start, oldEnd).map(l => '-' + l + '\n').join(''); patch += newLines.slice(start, newEnd).map(l => '+' + l + '\n').join(''); patch += oldLines.slice(oldEnd, contextEnd).map(l => ' ' + l + '\n').join(''); changed.push(path.relative(root, file)); } patch += '*** End Patch'; process.stdout.write(JSON.stringify({ model, hasUser, label, changed, patch: changed.length ? patch : null }));