ruoyi.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2021/05/18
  3. # @Author : ricky
  4. # @File : ruoyi.py
  5. # @Software: vscode
  6. """
  7. 核心修改类 RuoYi(标准版)
  8. """
  9. import os
  10. from core import base
  11. from constant import ruoyiconstant as const
  12. class RuoYi(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.修改pom.xml文件
  26. self.messagehandle('正在修改pom.xml...')
  27. self.__alter_pom_xml()
  28. self.messagehandle('pom.xml修改完成!')
  29. # 4.修改目录结构
  30. self.messagehandle('正在修改目录结构...')
  31. self.__alter_project_dir()
  32. self.messagehandle('目录结构修改完成!')
  33. if len(self.exceptions) > 0:
  34. self.messagehandle('\r发现有异常信息')
  35. self.messagehandle('-------------------\n\r')
  36. for e in self.exceptions:
  37. self.messagehandle(e)
  38. self.messagehandle('\r----------------------')
  39. def __alter_site_name_and_title(self):
  40. """修改站点名称和网站标题"""
  41. ntuple = const.RUOYI_SITE_RESOURCES_PATH_TUPLE
  42. for item in ntuple:
  43. filepath = os.path.join(self.rootpath,
  44. item.replace('#', os.path.sep))
  45. if os.path.exists(filepath):
  46. try:
  47. with open(filepath, 'r',
  48. encoding='utf-8') as srcfile, open(
  49. '%s.bak' % filepath, 'w',
  50. encoding='utf-8') as desfile:
  51. for line in srcfile:
  52. if const.RUOYI_DEFAULT_SITE_NAME in line:
  53. line = line.replace(
  54. const.RUOYI_DEFAULT_SITE_NAME,
  55. self.sitename)
  56. if '若依后台管理系统' in line:
  57. line = line.replace('若依后台管理系统', self.sitename)
  58. if '若依 后台管理系统' in line:
  59. line = line.replace('若依 后台管理系统', self.sitename)
  60. if '登录若依系统' in line:
  61. line = line.replace('登录若依系统',
  62. '登录' + self.sitename)
  63. if '若依系统' in line:
  64. line = line.replace('若依系统', self.sitename)
  65. if '若依介绍' in line:
  66. line = line.replace('若依介绍',
  67. self.sitename + '介绍')
  68. if 'RuoYi -' in line:
  69. line = line.replace('RuoYi -', self.sitename)
  70. desfile.write(line)
  71. # 移除旧文件
  72. os.remove(filepath)
  73. # 重命名备份文件为新文件
  74. os.rename('%s.bak' % filepath, filepath)
  75. except Exception as err:
  76. self.exceptionhandle(
  77. '修改站点名称和网站标题异常\n修改文件:{}\n异常信息:{}'.format(
  78. filepath, err))
  79. def __alter_package_name_and_project_name(self, rootpath):
  80. """
  81. 修改包名和项目名称
  82. 参数:
  83. rootpath (str): 根路径
  84. """
  85. files = os.listdir(rootpath)
  86. for filename in files:
  87. filepath = os.path.join(rootpath, filename)
  88. if os.path.isdir(filepath):
  89. self.__alter_package_name_and_project_name(filepath)
  90. else:
  91. if filename.endswith('.java') or filename.endswith(
  92. '.yml'
  93. ) or filename.endswith('Mapper.xml') or filename.endswith(
  94. 'logback.xml') or filename.endswith(
  95. '.factories') or filename.endswith(
  96. '.vm') or filename.endswith(
  97. '.bat') or filename.endswith('.sh'):
  98. try:
  99. with open(filepath, 'r',
  100. encoding='utf-8') as srcfile, open(
  101. '%s.bak' % filepath,
  102. 'w',
  103. encoding='utf-8') as desfile:
  104. self.messagehandle('正在修改:' + filename)
  105. for line in srcfile:
  106. if const.RUOYI_DEFAULT_PACKAGE_NAME in line:
  107. line = line.replace(
  108. const.RUOYI_DEFAULT_PACKAGE_NAME,
  109. self.packagename)
  110. if const.RUOYI_DEFAULT_PROJECT_NAME + '-' in line:
  111. line = line.replace(
  112. const.RUOYI_DEFAULT_PROJECT_NAME + '-',
  113. self.projectname + '-')
  114. if self.configdict['config.enable'] == 'True':
  115. if filename.endswith('.yml'):
  116. line = self.__check_yml_config(
  117. line, filename)
  118. desfile.write(line)
  119. # 移除旧文件
  120. os.remove(filepath)
  121. # 重命名备份文件为新文件
  122. os.rename('%s.bak' % filepath, filepath)
  123. except Exception as err:
  124. self.exceptionhandle(
  125. '修改包名和项目名称异常\n修改文件:{}\n异常信息:{}'.format(
  126. filepath, err))
  127. def __check_yml_config(self, line, filename):
  128. """
  129. 检测yml配置文件
  130. 参数:
  131. line (str): 行
  132. filename (str): 文件名
  133. """
  134. if 'localhost:3306/ry' in line and filename == 'application-druid.yml':
  135. line = self.__alert_yml_config(line, 'mysql_ip_port_name')
  136. if 'username: root' in line and filename == 'application-druid.yml':
  137. line = self.__alert_yml_config(line, 'mysql_username')
  138. if 'password: password' in line and filename == 'application-druid.yml':
  139. line = self.__alert_yml_config(line, 'mysql_password')
  140. return line
  141. def __alert_yml_config(self, line, type_):
  142. """
  143. 修改yml配置文件
  144. 参数:
  145. line (str): 行
  146. type_ (str): 修改类型
  147. """
  148. if type_ == 'mysql_ip_port_name':
  149. mysql_ip = self.configdict['database.ip']
  150. mysql_port = self.configdict['database.port']
  151. mysql_name = self.configdict['database.name']
  152. return line.replace('localhost:3306/ry',
  153. mysql_ip + ':' + mysql_port + '/' + mysql_name)
  154. if type_ == 'mysql_username':
  155. mysql_username = self.configdict['database.username']
  156. return line.replace('username: root',
  157. 'username: ' + mysql_username)
  158. if type_ == 'mysql_password':
  159. mysql_password = self.configdict['database.password']
  160. return line.replace('password: password',
  161. 'password: ' + mysql_password)
  162. return line
  163. def __alter_pom_xml(self):
  164. """修改项目pom.xml文件"""
  165. # 将最外层的文件夹添加到新的元组中
  166. ttuple = (const.RUOYI_DEFAULT_PROJECT_DIR_NAME, )
  167. ntuple = ttuple + const.RUOYI_DEFAULT_MODULE_NAME_TUPLE
  168. for module_name in ntuple:
  169. pom_xml_file = ''
  170. # 如果元组内元素是项目名,文件路径需要特殊处理
  171. if module_name == const.RUOYI_DEFAULT_PROJECT_DIR_NAME:
  172. pom_xml_file = os.path.join(self.rootpath, 'pom.xml')
  173. else:
  174. pom_xml_file = os.path.join(self.rootpath, module_name,
  175. 'pom.xml')
  176. if not os.path.exists(pom_xml_file):
  177. continue
  178. try:
  179. with open(pom_xml_file, 'r',
  180. encoding='utf-8') as xml_file, open(
  181. '%s.bak' % pom_xml_file, 'w',
  182. encoding='utf-8') as target_file:
  183. self.messagehandle('正在修改:' + module_name + '/pom.xml')
  184. for line in xml_file:
  185. if const.RUOYI_DEFAULT_GROUP_ID in line and '<groupId>' in line:
  186. line = line.replace(const.RUOYI_DEFAULT_GROUP_ID,
  187. self.groupid)
  188. if const.RUOYI_DEFAULT_ARTIFACTID_PREFIX in line and '<artifactId>' in line:
  189. line = line.replace(
  190. const.RUOYI_DEFAULT_ARTIFACTID_PREFIX,
  191. self.artifactid)
  192. if module_name == const.RUOYI_DEFAULT_PROJECT_DIR_NAME:
  193. if '<name>' in line or '<module>' in line:
  194. line = line.replace(
  195. const.RUOYI_DEFAULT_PROJECT_NAME,
  196. self.projectname)
  197. if 'version>' in line:
  198. line = line.replace(
  199. const.RUOYI_DEFAULT_ARTIFACTID_PREFIX,
  200. self.artifactid)
  201. if 'description>' in line:
  202. line = line.replace(
  203. const.RUOYI_DEFAULT_SITE_NAME,
  204. self.sitename)
  205. target_file.write(line)
  206. # 移除旧文件
  207. os.remove(pom_xml_file)
  208. # 重命名备份文件为新文件
  209. os.rename('%s.bak' % pom_xml_file, pom_xml_file)
  210. except Exception as err:
  211. self.exceptionhandle(
  212. '修改项目pom.xml文件异常\n修改文件:{}\n异常信息:{}'.format(
  213. pom_xml_file, err))
  214. def __alter_project_dir(self):
  215. """修改目录名"""
  216. for module_name in const.RUOYI_DEFAULT_MODULE_NAME_TUPLE:
  217. src_main_java_dir = os.path.join(self.rootpath, module_name,
  218. 'src/main/java')
  219. # 如果没有找到源代码路径,结束本次循环
  220. if not os.path.exists(src_main_java_dir):
  221. continue
  222. source_dir = os.path.join(
  223. src_main_java_dir, self.packagename.replace('.', os.path.sep))
  224. if not os.path.exists(source_dir):
  225. os.makedirs(source_dir)
  226. self.move_dir(os.path.join(src_main_java_dir, 'com/ruoyi'),
  227. source_dir)
  228. # 拷贝完目录后会删除掉之前的文件,但是目录没有删除,再次删除目录
  229. for root, dirs, files in os.walk(os.path.join(
  230. src_main_java_dir, 'com/ruoyi'),
  231. topdown=False):
  232. if not os.listdir(root):
  233. os.rmdir(root)
  234. # 删除com目录
  235. if not os.listdir(os.path.join(src_main_java_dir, 'com')):
  236. os.rmdir(os.path.join(src_main_java_dir, 'com'))
  237. os.rename(
  238. os.path.join(self.rootpath, module_name),
  239. os.path.join(
  240. self.rootpath,
  241. self.projectname + '-' + module_name.split('-')[1]))
  242. self.messagehandle('正在修改:' + module_name)
  243. os.rename(self.rootpath,
  244. os.path.join(self.targetdir, self.projectdirname))
  245. self.messagehandle('正在修改:' + const.RUOYI_DEFAULT_PROJECT_DIR_NAME)