ruoyicloud.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2021/05/18
  3. # @Author : ricky
  4. # @File : ruoyicloud.py
  5. # @Software: vscode
  6. """
  7. 核心修改类 RuoYi-Cloud版本
  8. """
  9. import os
  10. from core import base
  11. from constant import ruoyicloudconstant as const
  12. class RuoYiCloud(base.BaseCore):
  13. def start(self):
  14. # 拼接根路径
  15. self.rootpath = os.path.join(self.targetdir,
  16. const.RUOYI_DEFAULT_PROJECT_DIR_NAME)
  17. # 1.修改站点名称
  18. self.messagehandle('正在修改标题和修站点名称...')
  19. self.__alter_site_name_and_title()
  20. self.messagehandle('站点名称和标题修改完成!')
  21. # 2.修改包名和项目名
  22. self.messagehandle('正在修改包名和项目名...')
  23. self.__alter_package_name_and_project_name(self.rootpath)
  24. self.messagehandle('包名和项目名修改完成!')
  25. # 3.修改项目配置和日志配置
  26. self.messagehandle('正在修改项目配置和日志配置...')
  27. self.__alter_bootstrapyml_and_logbackxml(self.rootpath)
  28. self.messagehandle('项目配置和日志配置修改完成!')
  29. # 4.修改Nacos配置
  30. self.messagehandle('正在修改Nacos配置...')
  31. self.__alter_nacos_config(os.path.join(self.rootpath, 'sql'))
  32. self.messagehandle('Nacos配置修改完成!')
  33. # 5.修改pom.xml文件
  34. self.messagehandle('正在修改pom.xml...')
  35. self.__alter_pom_xml(self.rootpath)
  36. self.messagehandle('pom.xml修改完成!')
  37. # 6.修改目录结构
  38. self.messagehandle('正在修改目录结构...')
  39. self.__alter_project_dir()
  40. self.messagehandle('目录结构修改完成!')
  41. if len(self.exceptions) > 0:
  42. self.messagehandle('\r发现有异常信息')
  43. self.messagehandle('-------------------\n\r')
  44. for e in self.exceptions:
  45. self.messagehandle(e)
  46. self.messagehandle('\r----------------------')
  47. def __alter_site_name_and_title(self):
  48. """修改站点名称和网站标题"""
  49. ntuple = const.RUOYI_SITE_RESOURCES_PATH_TUPLE
  50. for item in ntuple:
  51. filepath = os.path.join(self.rootpath,
  52. item.replace('#', os.path.sep))
  53. if not os.path.exists(filepath):
  54. continue
  55. try:
  56. with open(filepath, 'r', encoding='utf-8') as srcfile, open(
  57. '%s.bak' % filepath, 'w', encoding='utf-8') as desfile:
  58. for line in srcfile:
  59. if const.RUOYI_DEFAULT_SITE_NAME in line:
  60. line = line.replace(const.RUOYI_DEFAULT_SITE_NAME,
  61. self.sitename)
  62. if '若依后台管理系统' in line:
  63. line = line.replace('若依后台管理系统', self.sitename)
  64. if '若依 后台管理系统' in line:
  65. line = line.replace('若依 后台管理系统', self.sitename)
  66. if '登录若依系统' in line:
  67. line = line.replace('登录若依系统', '登录' + self.sitename)
  68. if '若依系统' in line:
  69. line = line.replace('若依系统', self.sitename)
  70. if '若依介绍' in line:
  71. line = line.replace('若依介绍', self.sitename + '介绍')
  72. if 'RuoYi -' in line:
  73. line = line.replace('RuoYi -', self.sitename)
  74. desfile.write(line)
  75. # 移除旧文件
  76. os.remove(filepath)
  77. # 重命名备份文件为新文件
  78. os.rename('%s.bak' % filepath, filepath)
  79. except Exception as err:
  80. self.exceptionhandle('修改站点名称和网站标题异常\n修改文件:{}\n异常信息:{}'.format(
  81. filepath, err))
  82. def __alter_package_name_and_project_name(self, rootpath):
  83. """修改包名和项目名称"""
  84. files = os.listdir(rootpath)
  85. for filename in files:
  86. filepath = os.path.join(rootpath, filename)
  87. if os.path.isdir(filepath):
  88. self.__alter_package_name_and_project_name(filepath)
  89. else:
  90. if filename.endswith('.java') or filename.endswith(
  91. '.yml'
  92. ) or filename.endswith('Mapper.xml') or filename.endswith(
  93. 'logback.xml') or filename.endswith(
  94. '.factories') or filename.endswith(
  95. '.vm') or filename.endswith(
  96. '.bat') or filename.endswith('.sh'):
  97. try:
  98. with open(filepath, 'r',
  99. encoding='utf-8') as srcfile, open(
  100. '%s.bak' % filepath,
  101. 'w',
  102. encoding='utf-8') as desfile:
  103. self.messagehandle('正在修改:' + filename)
  104. for line in srcfile:
  105. if const.RUOYI_DEFAULT_PACKAGE_NAME in line:
  106. line = line.replace(
  107. const.RUOYI_DEFAULT_PACKAGE_NAME,
  108. self.packagename)
  109. if const.RUOYI_DEFAULT_PROJECT_NAME + '-' in line:
  110. line = line.replace(
  111. const.RUOYI_DEFAULT_PROJECT_NAME + '-',
  112. self.projectname + '-')
  113. if self.configdict['config.enable'] == 'True':
  114. if filename.endswith('.yml'):
  115. line = self.__check_yml_or_sql_config(
  116. line, filename)
  117. desfile.write(line)
  118. # 移除旧文件
  119. os.remove(filepath)
  120. # 重命名备份文件为新文件
  121. os.rename('%s.bak' % filepath, filepath)
  122. except Exception as err:
  123. self.exceptionhandle(
  124. '修改包名和项目名称异常\n修改文件:{}\n异常信息:{}'.format(
  125. filepath, err))
  126. def __alter_bootstrapyml_and_logbackxml(self, rootpath):
  127. """
  128. 修改项目bootstrap.yml和logback.xml中的模块名
  129. 参数:
  130. rootpath (str): 根路径
  131. """
  132. # 循环修改指定后缀名的文件内容
  133. files = os.listdir(rootpath)
  134. for filename in files:
  135. filepath = os.path.join(rootpath, filename)
  136. # 如果是目录继续递归
  137. if os.path.isdir(filepath):
  138. self.__alter_bootstrapyml_and_logbackxml(filepath)
  139. else:
  140. try:
  141. # 如果是文件才进行修改
  142. if filename.endswith('.yml') or filename.endswith(
  143. 'logback.xml'):
  144. with open(filepath, 'r',
  145. encoding='utf-8') as srcfile, open(
  146. '%s.bak' % filepath,
  147. 'w',
  148. encoding='utf-8') as desfile:
  149. self.messagehandle('正在修改:' + filename)
  150. for line in srcfile:
  151. if const.RUOYI_DEFAULT_PROJECT_NAME in line:
  152. line = line.replace(
  153. const.RUOYI_DEFAULT_PROJECT_NAME,
  154. self.projectname)
  155. desfile.write(line)
  156. # 移除旧文件
  157. os.remove(filepath)
  158. # 重命名备份文件为新文件
  159. os.rename('%s.bak' % filepath, filepath)
  160. except Exception as err:
  161. self.exceptionhandle(
  162. '修改项目bootstrap.yml和logback.xml中的模块名异常\n修改文件:{}\n异常信息:{}'
  163. .format(filepath, err))
  164. def __alter_nacos_config(self, sqldir):
  165. """
  166. 修改项目Nacos配置
  167. 参数:
  168. sqldir (str): sql目录
  169. """
  170. files = os.listdir(sqldir)
  171. for filename in files:
  172. filepath = os.path.join(sqldir, filename)
  173. # 如果是目录继续递归
  174. if os.path.isdir(filepath):
  175. self.__alter_nacos_config(filepath)
  176. else:
  177. try:
  178. # 如果是文件才进行修改
  179. if filename.startswith(
  180. const.RUOYI_DEFAULT_NACOS_CONFIG_SQL_PREFIX):
  181. with open(filepath, 'r',
  182. encoding='utf-8') as srcfile, open(
  183. '%s.bak' % filepath,
  184. 'w',
  185. encoding='utf-8') as desfile:
  186. self.messagehandle('正在修改:' + filename)
  187. for line in srcfile:
  188. if const.RUOYI_DEFAULT_PACKAGE_NAME in line:
  189. line = line.replace(
  190. const.RUOYI_DEFAULT_PACKAGE_NAME,
  191. self.packagename)
  192. if const.RUOYI_DEFAULT_PROJECT_NAME + '-' in line:
  193. line = line.replace(
  194. const.RUOYI_DEFAULT_PROJECT_NAME + '-',
  195. self.projectname + '-')
  196. if self.configdict['config.enable'] == 'True':
  197. line = self.__check_yml_or_sql_config(
  198. line, filename)
  199. desfile.write(line)
  200. # 移除旧文件
  201. os.remove(filepath)
  202. # 重命名备份文件为新文件
  203. os.rename('%s.bak' % filepath, filepath)
  204. except Exception as err:
  205. self.exceptionhandle(
  206. '修改项目Nacos配置异常\n修改文件:{}\n异常信息:{}'.format(
  207. filepath, err))
  208. def __check_yml_or_sql_config(self, line, filename):
  209. """
  210. 检测yml配置文件
  211. 参数:
  212. line (str): 行
  213. filename (str): 文件名
  214. """
  215. if 'localhost:3306/ry-cloud' in line and filename.endswith('.sql'):
  216. line = self.__alert_yml_or_sql_config(line, 'mysql_ip_port_name')
  217. if 'username: root' in line and filename.endswith('.sql'):
  218. line = self.__alert_yml_or_sql_config(line, 'mysql_username')
  219. if 'password: password' in line and filename.endswith('.sql'):
  220. line = self.__alert_yml_or_sql_config(line, 'mysql_password')
  221. if 'host: localhost' in line and filename.endswith('.sql'):
  222. line = self.__alert_yml_or_sql_config(line, 'redis_host')
  223. if 'port: 6379' in line and filename.endswith('.sql'):
  224. line = self.__alert_yml_or_sql_config(line, 'redis_port')
  225. if 'password: \\r\\n' in line and filename.endswith('.sql'):
  226. line = self.__alert_yml_or_sql_config(line, 'redis_password')
  227. return line
  228. def __alert_yml_or_sql_config(self, line, type_):
  229. """
  230. 修改yml配置文件
  231. 参数:
  232. line (str): 行
  233. type_ (str): 修改类型
  234. """
  235. if type_ == 'mysql_ip_port_name':
  236. mysql_ip = self.configdict['database.ip']
  237. mysql_port = self.configdict['database.port']
  238. mysql_name = self.configdict['database.name']
  239. return line.replace('localhost:3306/ry-cloud',
  240. mysql_ip + ':' + mysql_port + '/' + mysql_name)
  241. if type_ == 'mysql_username':
  242. mysql_username = self.configdict['database.username']
  243. return line.replace('username: root',
  244. 'username: ' + mysql_username)
  245. if type_ == 'mysql_password':
  246. mysql_password = self.configdict['database.password']
  247. return line.replace('password: password',
  248. 'password: ' + mysql_password)
  249. if type_ == 'redis_host':
  250. redis_ip = self.configdict['redis.ip']
  251. return line.replace('host: localhost', 'host: ' + redis_ip)
  252. if type_ == 'redis_port':
  253. redis_port = self.configdict['redis.port']
  254. return line.replace('port: 6379', 'port: ' + redis_port)
  255. if type_ == 'redis_password':
  256. redis_password = self.configdict['redis.password']
  257. return line.replace('password: \\r\\n',
  258. 'password: ' + redis_password + '\\r\\n')
  259. return line
  260. def __alter_pom_xml(self, rootpath):
  261. """
  262. 修改项目pom.xml文件
  263. 参数:
  264. rootpath (str): 根目录
  265. """
  266. files = os.listdir(rootpath)
  267. for filename in files:
  268. filepath = os.path.join(rootpath, filename)
  269. # 如果是目录继续递归
  270. if os.path.isdir(filepath):
  271. self.__alter_pom_xml(filepath)
  272. else:
  273. try:
  274. # 如果是文件才进行修改
  275. if filename.endswith('pom.xml'):
  276. with open(filepath, 'r',
  277. encoding='utf-8') as srcfile, open(
  278. '%s.bak' % filepath,
  279. 'w',
  280. encoding='utf-8') as desfile:
  281. self.messagehandle('正在修改:' + filename)
  282. for line in srcfile:
  283. if const.RUOYI_DEFAULT_GROUP_ID in line and '<groupId>' in line:
  284. line = line.replace(
  285. const.RUOYI_DEFAULT_GROUP_ID,
  286. self.groupid)
  287. if const.RUOYI_DEFAULT_ARTIFACTID_PREFIX in line and '<artifactId>' in line:
  288. line = line.replace(
  289. const.RUOYI_DEFAULT_ARTIFACTID_PREFIX,
  290. self.artifactid)
  291. if '<name>' in line or '<module>' in line:
  292. line = line.replace(
  293. const.RUOYI_DEFAULT_PROJECT_NAME,
  294. self.projectname)
  295. if 'version>' in line:
  296. line = line.replace(
  297. const.RUOYI_DEFAULT_ARTIFACTID_PREFIX,
  298. self.artifactid)
  299. if const.RUOYI_DEFAULT_SITE_NAME in line:
  300. line = line.replace(
  301. const.RUOYI_DEFAULT_SITE_NAME,
  302. self.sitename)
  303. line = line.replace(
  304. const.RUOYI_DEFAULT_ARTIFACTID_PREFIX,
  305. self.projectname)
  306. desfile.write(line)
  307. # 移除旧文件
  308. os.remove(filepath)
  309. # 重命名备份文件为新文件
  310. os.rename('%s.bak' % filepath, filepath)
  311. except Exception as err:
  312. self.exceptionhandle(
  313. '修改项目pom.xml文件异常\n修改文件:{}\n异常信息:{}'.format(
  314. filepath, err))
  315. def __alter_project_dir(self):
  316. """修改目录名"""
  317. for module_name in const.RUOYI_DEFAULT_MODULE_NAME_TUPLE:
  318. replace_module_name = module_name.replace('#', os.path.sep)
  319. src_main_java_dir = os.path.join(self.rootpath,
  320. replace_module_name,
  321. 'src/main/java')
  322. if os.path.exists(src_main_java_dir):
  323. source_dir = os.path.join(
  324. src_main_java_dir,
  325. self.packagename.replace('.', os.path.sep))
  326. if not os.path.exists(source_dir):
  327. print(source_dir)
  328. os.makedirs(source_dir)
  329. self.move_dir(os.path.join(src_main_java_dir, 'com/ruoyi'),
  330. source_dir)
  331. # 拷贝完目录后会删除掉之前的文件,但是目录没有删除,再次删除目录
  332. for root, dirs, files in os.walk(os.path.join(
  333. src_main_java_dir, 'com/ruoyi'),
  334. topdown=False):
  335. if not os.listdir(root):
  336. os.rmdir(root)
  337. # 删除com目录
  338. if not os.listdir(os.path.join(src_main_java_dir, 'com')):
  339. os.rmdir(os.path.join(src_main_java_dir, 'com'))
  340. if module_name.find('#') == -1:
  341. os.rename(
  342. os.path.join(self.rootpath, module_name),
  343. os.path.join(
  344. self.rootpath, self.projectname + '-' +
  345. module_name.split('-')[1]))
  346. else:
  347. tarpath = os.path.join(
  348. self.rootpath,
  349. module_name.split('#')[0], self.projectname + '-' +
  350. module_name.split('#')[1].replace('-', '$',
  351. 1).split('$')[1])
  352. os.rename(os.path.join(self.rootpath, replace_module_name),
  353. tarpath)
  354. else:
  355. os.rename(
  356. os.path.join(self.rootpath, module_name),
  357. os.path.join(
  358. self.rootpath,
  359. self.projectname + '-' + module_name.split('-')[1]))
  360. self.messagehandle('正在修改:' + replace_module_name)
  361. os.rename(self.rootpath,
  362. os.path.join(self.targetdir, self.projectdirname))
  363. self.messagehandle('正在修改:' + const.RUOYI_DEFAULT_PROJECT_DIR_NAME)