hvigorfile.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. import { appTasks } from '@ohos/hvigor-ohos-plugin';
  2. import { HvigorPlugin, HvigorNode } from '@ohos/hvigor';
  3. import fs from 'fs';
  4. import path from 'path';
  5. import { execSync } from 'child_process';
  6. function customPlugin(): HvigorPlugin {
  7. return {
  8. pluginId: 'customPlugin',
  9. apply(node: HvigorNode) {
  10. try {
  11. // console.log('node', JSON.stringify(node))
  12. // 根据node对象结构获取项目路径
  13. const projectPath = node.nodeDir.filePath;
  14. console.log('目标路径:', projectPath);
  15. // 获取所有子模块
  16. const modules = node.allSubNodes.filter(subNode => subNode.node.classKind === 'module');
  17. console.log('模块数量:', modules.length);
  18. // 打印每个模块的路径
  19. const modulePaths = [];
  20. modules.forEach((module, index) => {
  21. try {
  22. // 从module.node._nodePath获取模块路径
  23. const modulePath = module.node._nodePath;
  24. if (modulePath) {
  25. console.log(`模块 ${index + 1} 路径:`, modulePath);
  26. modulePaths.push(modulePath);
  27. } else {
  28. console.log(`模块 ${index + 1} 路径: 无法获取路径`);
  29. }
  30. } catch (error) {
  31. console.error(`获取模块 ${index + 1} 路径时出错:`, error);
  32. }
  33. });
  34. // 遍历并处理所有模块
  35. modulePaths.forEach((modulePath, index) => {
  36. console.log(`处理模块 ${index + 1}:`, modulePath);
  37. processModule(modulePath, projectPath);
  38. });
  39. } catch (error) {
  40. console.error('Error in custom plugin:', error);
  41. }
  42. }
  43. }
  44. function processModule(modulePath: string, projectPath: string) {
  45. try {
  46. // 构建pages目录路径 (基于模块路径)
  47. const pagesPath = path.join(modulePath, 'src', 'main', 'ets', 'pages');
  48. console.log('pages路径:', pagesPath);
  49. // 构建profile目录路径 (基于模块路径)
  50. const rawfilePath = path.join(modulePath, 'src', 'main', 'resources', 'base', 'profile');
  51. console.log('profile路径:', rawfilePath);
  52. // 确保profile目录存在
  53. if (!fs.existsSync(rawfilePath)) {
  54. fs.mkdirSync(rawfilePath, { recursive: true });
  55. }
  56. // router_map.json文件路径
  57. const routerMapPath = path.join(rawfilePath, 'router_map.json');
  58. // module.json5文件路径
  59. const moduleJsonPath = path.join(modulePath, 'src', 'main', 'module.json5');
  60. // 初始化routerMap
  61. let routerMap = {
  62. routerMap: []
  63. };
  64. // 如果router_map.json文件已存在,则读取现有内容
  65. if (fs.existsSync(routerMapPath)) {
  66. try {
  67. const existingContent = fs.readFileSync(routerMapPath, 'utf8');
  68. routerMap = JSON.parse(existingContent);
  69. console.log('读取现有的router_map.json文件');
  70. } catch (error) {
  71. console.error('读取router_map.json文件出错:', error);
  72. }
  73. }
  74. // 检查pages目录是否存在
  75. if (fs.existsSync(pagesPath)) {
  76. // 读取目录内容
  77. const files = fs.readdirSync(pagesPath);
  78. console.log(`Found ${files.length} files/directories in pages folder:`);
  79. // 遍历并处理每个文件
  80. files.forEach((file, index) => {
  81. const fullPath = path.join(pagesPath, file);
  82. const stat = fs.statSync(fullPath);
  83. const type = stat.isDirectory() ? '[DIR]' : '[FILE]';
  84. console.log(`${index + 1}. ${type} ${file}`);
  85. // 如果是文件,读取文件内容
  86. if (!stat.isDirectory() && file.endsWith('.ets')) {
  87. try {
  88. const content = fs.readFileSync(fullPath, 'utf8');
  89. // console.log(`Content of ${file}:`);
  90. // console.log(content);
  91. // 检查是否有@RouterPage修饰器
  92. if (content.includes('@RouterPage')) {
  93. // 生成Builder函数名(去掉Page后缀,加上Builder后缀)
  94. const fileNameWithoutExtension = file.replace('.ets', '');
  95. const builderName = fileNameWithoutExtension.replace(/Page$/, '') + 'Builder';
  96. // 检查是否已经存在Builder函数
  97. const builderRegex = new RegExp(`@Builder\\s+function\\s+${builderName}\\s*\\(\\)`, 'g');
  98. if (!builderRegex.test(content)) {
  99. // 构造新的内容,在文件末尾添加Builder函数
  100. const structName = fileNameWithoutExtension; // 假设struct名称与文件名相同
  101. const builderFunction = `\n@Builder\nfunction ${builderName}() {\n ${structName}()\n}\n`;
  102. const newContent = content + builderFunction;
  103. // 写入文件
  104. fs.writeFileSync(fullPath, newContent, 'utf8');
  105. console.log(`Added ${builderName} function to ${file}`);
  106. } else {
  107. console.log(`Builder function ${builderName} already exists in ${file}`);
  108. }
  109. // 添加到routerMap中
  110. const pageEntry = {
  111. name: fileNameWithoutExtension,
  112. pageSourceFile: `src/main/ets/pages/${file}`,
  113. buildFunction: builderName
  114. };
  115. // 检查是否已存在该条目
  116. const existingIndex = routerMap.routerMap.findIndex(item => item.name === fileNameWithoutExtension);
  117. if (existingIndex >= 0) {
  118. // 更新现有条目
  119. routerMap.routerMap[existingIndex] = pageEntry;
  120. } else {
  121. // 添加新条目
  122. routerMap.routerMap.push(pageEntry);
  123. }
  124. }
  125. console.log('----------------------------------------');
  126. } catch (readError) {
  127. console.error(`Error reading file ${file}:`, readError);
  128. }
  129. }
  130. });
  131. // 将routerMap写入router_map.json文件
  132. try {
  133. fs.writeFileSync(routerMapPath, JSON.stringify(routerMap, null, 2), 'utf8');
  134. console.log('router_map.json文件已更新');
  135. } catch (writeError) {
  136. console.error('写入router_map.json文件出错:', writeError);
  137. }
  138. // 更新module.json5文件,添加routerMap字段
  139. try {
  140. if (fs.existsSync(moduleJsonPath)) {
  141. const moduleJsonContent = fs.readFileSync(moduleJsonPath, 'utf8');
  142. // 使用正则表达式在"module": {后添加routerMap字段
  143. if (moduleJsonContent.includes('"module"')) {
  144. // 检查是否已经存在routerMap字段
  145. if (!moduleJsonContent.includes('"routerMap"')) {
  146. // 在"module": {后添加routerMap字段
  147. const updatedContent = moduleJsonContent.replace(
  148. /("module"\s*:\s*{)/,
  149. '$1\n "routerMap": "$profile:router_map",'
  150. );
  151. fs.writeFileSync(moduleJsonPath, updatedContent, 'utf8');
  152. console.log('module.json5文件已更新,添加了routerMap字段');
  153. } else {
  154. console.log('module.json5中已存在routerMap字段');
  155. }
  156. } else {
  157. console.log('module.json5中未找到"module"字段');
  158. }
  159. } else {
  160. console.log('module.json5文件不存在');
  161. }
  162. } catch (error) {
  163. console.error('更新module.json5文件出错:', error);
  164. }
  165. } else {
  166. console.log('Pages directory not found');
  167. }
  168. } catch (error) {
  169. console.error(`处理模块 ${modulePath} 时出错:`, error);
  170. }
  171. }
  172. }
  173. function rDBPlugin(): HvigorPlugin {
  174. return {
  175. pluginId: 'customPlugin',
  176. apply(node: HvigorNode) {
  177. try {
  178. // 根据node对象结构获取项目路径
  179. const projectPath = node.nodeDir.filePath;
  180. console.log('目标路径:', projectPath);
  181. // 获取所有子模块
  182. const modules = node.allSubNodes.filter(subNode => subNode.node.classKind === 'module');
  183. console.log('模块数量:', modules.length);
  184. // 打印每个模块的路径
  185. const modulePaths = [];
  186. modules.forEach((module, index) => {
  187. try {
  188. // 从module.node._nodePath获取模块路径
  189. const modulePath = module.node._nodePath;
  190. if (modulePath) {
  191. console.log(`模块 ${index + 1} 路径:`, modulePath);
  192. modulePaths.push(modulePath);
  193. if (modulePath.includes('basic')) {
  194. processTables(modulePath)
  195. }
  196. } else {
  197. console.log(`模块 ${index + 1} 路径: 无法获取路径`);
  198. }
  199. } catch (error) {
  200. console.error(`获取模块 ${index + 1} 路径时出错:`, error);
  201. }
  202. });
  203. // 遍历并处理所有模块
  204. // modulePaths.forEach((modulePath, index) => {
  205. // console.log(`处理模块 ${index + 1}:`, modulePath);
  206. // processTables(); // 处理tables目录的逻辑
  207. // });
  208. // processTables(modulePaths[])
  209. } catch (error) {
  210. console.error('Error in custom plugin:', error);
  211. }
  212. }
  213. }
  214. function processTables(currentDir:string) {
  215. try {
  216. // 构建tables目录路径
  217. const tablesPath = path.join(currentDir, 'src', 'main', 'ets', 'rdb', 'tables');
  218. console.log('tables路径:', tablesPath);
  219. // 检查tables目录是否存在
  220. if (fs.existsSync(tablesPath)) {
  221. // 读取目录内容
  222. const files = fs.readdirSync(tablesPath);
  223. console.log(`Found ${files.length} files/directories in tables folder:`);
  224. const tableNames: string[] = [];
  225. const sqlStatements: string[] = [];
  226. // 替换原有的字段处理逻辑
  227. files.forEach((file, index) => {
  228. const fullPath = path.join(tablesPath, file);
  229. const stat = fs.statSync(fullPath);
  230. const type = stat.isDirectory() ? '[DIR]' : '[FILE]';
  231. console.log(`${index + 1}. ${type} ${file}`);
  232. // 如果是文件,读取文件内容
  233. if (!stat.isDirectory() && file.endsWith('.ets')) {
  234. try {
  235. const content = fs.readFileSync(fullPath, 'utf8');
  236. // 使用正则表达式匹配interface名称
  237. const typeRegex = /export\s+(interface|class)\s+(\w+)/g;
  238. let match;
  239. while ((match = typeRegex.exec(content)) !== null) {
  240. const typeKind = match[1]; // 'interface' 或 'class'
  241. const typeName = match[2]; // 类型名称
  242. tableNames.push(typeName);
  243. // 解析字段定义
  244. const fieldDefinitions = parseTypeFields(content, typeName);
  245. // 生成CREATE TABLE SQL语句
  246. const sqlStatement =
  247. `static readonly CREATE_${typeName.toUpperCase()}_TABLE = 'CREATE TABLE IF NOT EXISTS ${typeName} (${fieldDefinitions})';`;
  248. sqlStatements.push(sqlStatement);
  249. console.log(`Found ${typeKind}: ${typeName}`);
  250. }
  251. } catch (readError) {
  252. console.error(`Error reading file ${file}:`, readError);
  253. }
  254. }
  255. });
  256. // 添加解析接口字段的辅助函数
  257. function parseTypeFields(content: string, typeName: string): string {
  258. // 使用正则表达式查找 interface 或 class 定义,允许中间有多个空格
  259. const classRegex = new RegExp(`export\\s+class\\s+${typeName}`, 'g');
  260. const interfaceRegex = new RegExp(`export\\s+interface\\s+${typeName}`, 'g');
  261. const classMatch = classRegex.exec(content);
  262. const interfaceMatch = interfaceRegex.exec(content);
  263. let typeStart = -1;
  264. if (classMatch) {
  265. typeStart = classMatch.index;
  266. } else if (interfaceMatch) {
  267. typeStart = interfaceMatch.index;
  268. }
  269. console.log('开始解析类型体内容:');
  270. console.log('typeStart内容:', typeStart);
  271. if (typeStart === -1) {
  272. return 'ID INTEGER PRIMARY KEY AUTOINCREMENT';
  273. }
  274. // 后续逻辑保持不变...
  275. const openBrace = content.indexOf('{', typeStart);
  276. const closeBrace = content.indexOf('}', openBrace);
  277. if (openBrace === -1 || closeBrace === -1) {
  278. console.log('openBrace和closeBrace内容:', openBrace,closeBrace);
  279. return 'ID INTEGER PRIMARY KEY AUTOINCREMENT';
  280. }
  281. const typeBody = content.substring(openBrace + 1, closeBrace);
  282. const lines = typeBody.split('\n');
  283. const fields: string[] = [];
  284. let hasPrimaryKey = false;
  285. // 改进字段解析逻辑
  286. lines.forEach(line => {
  287. const trimmedLine = line.trim();
  288. // 使用更宽松的正则表达式,支持多种格式
  289. // 匹配格式: fieldName?: type; 或 fieldName: type; (允许前后空格)
  290. const fieldMatch = trimmedLine.match(/^(\w+)(\??)\s*:\s*([^;]+?)\s*(?:;|$)/);
  291. if (fieldMatch) {
  292. const fieldName = fieldMatch[1];
  293. const isOptional = fieldMatch[2] === '?';
  294. const fieldType = fieldMatch[3].trim();
  295. console.log(`解析字段: ${fieldName}, 可选: ${isOptional}, 类型: ${fieldType}`); // 调试信息
  296. // 根据TypeScript类型映射到SQLite类型
  297. let sqliteType = 'TEXT';
  298. const normalizedFieldType = fieldType.toLowerCase();
  299. if (normalizedFieldType.includes('number') || normalizedFieldType.includes('int') || normalizedFieldType.includes('float')) {
  300. sqliteType = 'INTEGER';
  301. } else if (normalizedFieldType.includes('boolean')) {
  302. sqliteType = 'INTEGER';
  303. } else if (normalizedFieldType.includes('date')) {
  304. sqliteType = 'TEXT';
  305. }
  306. // 检查是否为主键字段
  307. if (fieldName.toLowerCase() === 'id') {
  308. fields.push(`${fieldName} INTEGER PRIMARY KEY AUTOINCREMENT`);
  309. hasPrimaryKey = true;
  310. } else {
  311. // 处理可选字段
  312. const nullable = isOptional ? '' : ' NOT NULL';
  313. fields.push(`${fieldName} ${sqliteType}${nullable}`);
  314. }
  315. }
  316. });
  317. // 如果没有定义主键,则添加默认主键
  318. if (!hasPrimaryKey) {
  319. fields.unshift('ID INTEGER PRIMARY KEY AUTOINCREMENT');
  320. }
  321. return fields.join(', ');
  322. }
  323. // 更新RelationalStoreUtils.ets文件中的tables数组
  324. const relationalStorePath =
  325. path.join(currentDir, 'src', 'main', 'ets', 'rdb', 'utils', 'RelationalStoreUtis.ets');
  326. if (fs.existsSync(relationalStorePath)) {
  327. let content = fs.readFileSync(relationalStorePath, 'utf8');
  328. // 更新tables数组
  329. if (tableNames.length > 0) {
  330. const tablesArray = `private static tables: string[] = [${tableNames.map(name => `'${name}'`).join(', ')}]`;
  331. const updatedContent = content.replace(/private static tables: string\[\] = \[[^\]]*\]/, tablesArray);
  332. fs.writeFileSync(relationalStorePath, updatedContent, 'utf8');
  333. console.log('RelationalStoreUtis.ets文件已更新');
  334. }
  335. }
  336. // 替换原有的更新RDBSql.ets文件的代码块
  337. // 更新RDBSql.ts文件
  338. // 更新RDBSql.ts文件的改进逻辑
  339. const rdbSqlPath = path.join(currentDir, 'src', 'main', 'ets', 'rdb', 'sqls', 'RDBSql.ts');
  340. if (fs.existsSync(rdbSqlPath)) {
  341. let content = fs.readFileSync(rdbSqlPath, 'utf8');
  342. // 解析现有rDbSql对象中的属性
  343. const existingProperties: Map<string, string> = new Map();
  344. const propertyRegex = /(\w+):\s*'([^']+)'/g;
  345. let match;
  346. while ((match = propertyRegex.exec(content)) !== null) {
  347. existingProperties.set(match[1], match[2]);
  348. }
  349. // 构建SQL语句对象属性
  350. if (sqlStatements.length > 0) {
  351. const sqlProperties: string[] = [];
  352. // 首先保留现有的属性(用于手动添加的SQL)
  353. existingProperties.forEach((value, key) => {
  354. // 检查是否是自动生成的CREATE_*_TABLE属性
  355. if (!key.startsWith('CREATE_') || !key.endsWith('_TABLE')) {
  356. sqlProperties.push(` ${key}: '${value}'`);
  357. }
  358. });
  359. // 添加或更新基于类定义生成的SQL属性
  360. sqlStatements.forEach(statement => {
  361. const propertyName = statement.split('=')[0].trim().replace('static readonly ', '');
  362. const propertyValue = statement.split('=')[1].trim().slice(1, -2);
  363. sqlProperties.push(` ${propertyName}: '${propertyValue}'`);
  364. });
  365. // 构建新的对象内容
  366. const newContent = `export const rDbSql = {\n${sqlProperties.join(',\n')}\n}`;
  367. fs.writeFileSync(rdbSqlPath, newContent, 'utf8');
  368. console.log('RDBSql.ts文件已更新,保留了手动添加的SQL语句');
  369. }
  370. } else {
  371. // 如果文件不存在,创建新文件(原有逻辑保持不变)
  372. if (sqlStatements.length > 0) {
  373. const sqlProperties: string[] = [];
  374. sqlStatements.forEach(statement => {
  375. const propertyName = statement.split('=')[0].trim().replace('static readonly ', '');
  376. const propertyValue = statement.split('=')[1].trim().slice(1, -2);
  377. sqlProperties.push(` ${propertyName}: '${propertyValue}'`);
  378. });
  379. const newContent = `export const rDbSql = {\n${sqlProperties.join(',\n')}\n}`;
  380. // 确保目录存在
  381. const dirPath = path.dirname(rdbSqlPath);
  382. if (!fs.existsSync(dirPath)) {
  383. fs.mkdirSync(dirPath, { recursive: true });
  384. }
  385. fs.writeFileSync(rdbSqlPath, newContent, 'utf8');
  386. console.log('RDBSql.ts文件已创建并初始化为对象形式');
  387. }
  388. }
  389. console.log('----------------------------------------');
  390. } else {
  391. console.log('Tables directory not found');
  392. }
  393. } catch (error) {
  394. console.error(`处理tables目录时出错:`, error);
  395. }
  396. }
  397. }
  398. function forwardingPort(){
  399. return {
  400. pluginId: 'customPlugin',
  401. apply(node: HvigorNode) {
  402. try {
  403. var connectKeys = execSync('hdc list targets', { encoding: 'utf-8' }).split('\n');
  404. var targetKey = connectKeys[0].trim();
  405. var operation = `hdc -t ${targetKey} fport rm tcp:8080 tcp:8080`
  406. var result = execSync(operation, { encoding: 'utf-8' })
  407. console.log(`执行命令 ${operation}, 删除端口结果:${result.trim()}`);
  408. operation = `hdc -t ${targetKey} fport tcp:8080 tcp:8080`
  409. result = execSync(operation, { encoding: 'utf-8' })
  410. console.log(`执行命令 ${operation}, 转发端口结果:${result.trim()}`);
  411. } catch (error) {
  412. console.error(`执行失败: ${error.message}`);
  413. }
  414. }
  415. }
  416. }
  417. export default {
  418. system: appTasks, /* Built-in plugin of Hvigor. It cannot be modified. */
  419. plugins: [
  420. customPlugin(), // 应用自定义Plugin
  421. rDBPlugin(),
  422. forwardingPort()
  423. ] /* Custom plugin to extend the functionality of Hvigor. */
  424. }