Browse Source

Pull request #3371: KERNEL-14076 feat:es6脚本自动修复xtype

Merge in VISUAL/fineui from ~TREECAT/fineui:es6 to es6

* commit '065e44cf0cc555da677c532416b9b17a92a9c661':
  KERNEL-14076 feat:es6脚本自动修复xtype
es6
treecat-罗群 2 years ago
parent
commit
f3d0a11577
  1. 67
      es6.js
  2. 37
      es6.xtype.js

67
es6.js

@ -2,7 +2,7 @@ const fs = require("fs");
const path = require("path"); const path = require("path");
const prettier = require("prettier"); const prettier = require("prettier");
const { exec } = require("child_process"); const { exec } = require("child_process");
const { search, initDepts } = require("./es6.xtype"); const { search, initDepts, depts } = require("./es6.xtype");
async function fix(path) { async function fix(path) {
new Promise(res => { new Promise(res => {
@ -42,28 +42,42 @@ const target = [
"remove", "remove",
"createItems", "createItems",
"makeArrayByArray", "makeArrayByArray",
"VerticalAlign",
"transformItems",
]; ];
// 加载模块 // 加载模块
const loader = { const loader = {
G: { "@/core": { shortcut: true } }, G: { "@/core": { shortcut: true } },
async load(srcName, module) { load(srcName, module) {
const G = loader.G; const G = loader.G;
if (target.indexOf(module) >= 0) { if (target.indexOf(module) >= 0) {
G["@/core"][module] = true; G["@/core"][module] = true;
return true; return true;
} }
if (module.startsWith('"bi.')) {
const key = search(srcName, module);
if (key) {
if (!G[key]) {
G[key] = {};
}
const clzName = depts[module].clzName;
G[key][clzName] = true;
}
const key = search(srcName, module); return !!key;
if (key) { } else {
if (!G[key]) { const key = search(srcName, module);
G[key] = {}; if (key) {
if (!G[key]) {
G[key] = {};
}
G[key][module] = true;
} }
G[key][module] = true;
}
return !!key; return !!key;
}
}, },
}; };
@ -74,7 +88,24 @@ async function handleFile(srcName) {
const result = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode); const result = /BI\.(.*?)\s\=\sBI\.inherit\(/.exec(sourceCode);
if (!result) { if (!result) {
console.log(`可能是已经es6过了 ${srcName}`); console.log(`可能是已经es6过了 ${srcName}, 尝试替换 xtype`);
// 处理 xtype
// 尝试对 xtype 进行替换
const noXtypeCode = sourceCode.replace(/"bi\.(.*?)"/g, matchedSentence => {
const loadSuccess = loader.load(srcName, matchedSentence);
if (loadSuccess) {
const clzName = depts[matchedSentence].clzName;
return `${clzName}.xtype`;
} else {
console.log(`加载 ${matchedSentence}失败`);
return matchedSentence;
}
});
fs.writeFileSync(srcName, noXtypeCode);
return; return;
} }
@ -157,7 +188,21 @@ async function handleFile(srcName) {
if (loadSuccess) { if (loadSuccess) {
return target + end; return target + end;
} else { } else {
console.log(`加载 ${target}失败`); console.log(`BI.xxx 加载 ${target}失败`);
return matchedSentence;
}
});
// 尝试对 xtype 进行替换
f = f.replace(/"bi\.(.*?)"/g, matchedSentence => {
const loadSuccess = loader.load(srcName, matchedSentence);
if (loadSuccess) {
const clzName = depts[matchedSentence].clzName;
return `${clzName}.xtype`;
} else {
console.log(`加载 ${matchedSentence}失败`);
return matchedSentence; return matchedSentence;
} }

37
es6.xtype.js

@ -10,10 +10,24 @@ async function handle(filename) {
let clzName; let clzName;
if (inheritRegResult) { if (inheritRegResult) {
clzName = inheritRegResult[1]; clzName = inheritRegResult[1];
// 把
} else { } else {
const clzRegResult = /export\sclass\s(.*?)\sextend/.exec(code); const clzRegResult = /export\sclass\s(.*?)\sextend/.exec(code);
if (clzRegResult) { if (clzRegResult) {
clzName = clzRegResult[1]; clzName = clzRegResult[1];
} else {
return;
}
const xtypeResult = /static xtype = (.*?)(;|\s)/.exec(code);
// 找一下 xtype
if (xtypeResult) {
depts[xtypeResult[1]] = {
clzName,
clzPath: filename,
};
} else {
// console.log(`${filename} 没有 xtype`);
} }
} }
depts[clzName] = filename; depts[clzName] = filename;
@ -43,13 +57,21 @@ async function initDepts() {
} }
function search(src, clzName) { function search(src, module) {
let clzName = module;
let clzPath = depts[module];
if (!depts[clzName]) { if (!depts[clzName]) {
return ""; return "";
} }
const dstName = path.basename(depts[clzName]).replace(/.js$/g, ""); if (clzName.indexOf("\"") >= 0) {
const dstPath = path.normalize(depts[clzName]).split("src")[1].split("\\").join("/").split("/"); clzName = depts[module].clzName;
clzPath = depts[module].clzPath;
}
const dstName = path.basename(clzPath).replace(/.js$/g, "");
const dstPath = path.normalize(clzPath).split("src")[1].split("\\").join("/").split("/");
const srcPath = path.normalize(src).split("src")[1].split("\\").join("/").split("/"); const srcPath = path.normalize(src).split("src")[1].split("\\").join("/").split("/");
// console.log("src", src); // console.log("src", src);
@ -60,8 +82,6 @@ function search(src, clzName) {
srcPath.shift(); srcPath.shift();
srcPath.pop(); srcPath.pop();
const findDstIndexPath = (dstArr, startIndex) => { const findDstIndexPath = (dstArr, startIndex) => {
let i = startIndex; let i = startIndex;
@ -100,12 +120,6 @@ function search(src, clzName) {
// dstPath 也没有了 // dstPath 也没有了
if (i < dstPath.length) { if (i < dstPath.length) {
return result + findDstIndexPath(dstPath, i); return result + findDstIndexPath(dstPath, i);
// 还有好多没有匹配完
// while (i < srcPath) {
// // exists(srcPath.slice(0, i).join())
// result += srcPath[i];
// i++;
// }
} else if (i === dstPath.length) { } else if (i === dstPath.length) {
return `${result}${dstName}`; return `${result}${dstName}`;
} }
@ -124,6 +138,7 @@ function search(src, clzName) {
exports.initDepts = initDepts; exports.initDepts = initDepts;
exports.search = search; exports.search = search;
exports.depts = depts;

Loading…
Cancel
Save