ruoyivue.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2021/05/18
  3. # @Author : ricky
  4. # @File : ruoyivue.py
  5. # @Software: vscode
  6. """
  7. 核心修改类 RuoYi-Vue版本
  8. """
  9. import os
  10. from core import base
  11. from constant import ruoyivueconstant as const
  12. class RuoYiVue(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. files = os.listdir(rootpath)
  82. for filename in files:
  83. filepath = os.path.join(rootpath, filename)
  84. if os.path.isdir(filepath):
  85. self.__alter_package_name_and_project_name(filepath)
  86. else:
  87. if filename.endswith('.java') or filename.endswith(
  88. '.yml'
  89. ) or filename.endswith('Mapper.xml') or filename.endswith(
  90. 'logback.xml') or filename.endswith(
  91. '.factories') or filename.endswith(
  92. '.vm') or filename.endswith(
  93. '.bat') or filename.endswith('.sh'):
  94. try:
  95. with open(filepath, 'r',
  96. encoding='utf-8') as srcfile, open(
  97. '%s.bak' % filepath,
  98. 'w',
  99. encoding='utf-8') as desfile:
  100. self.messagehandle('正在修改:' + filename)
  101. for line in srcfile:
  102. if const.RUOYI_DEFAULT_PACKAGE_NAME in line:
  103. line = line.replace(
  104. const.RUOYI_DEFAULT_PACKAGE_NAME,
  105. self.packagename)
  106. if const.RUOYI_DEFAULT_PROJECT_NAME + '-' in line:
  107. line = line.replace(
  108. const.RUOYI_DEFAULT_PROJECT_NAME + '-',
  109. self.projectname + '-')
  110. if self.configdict['config.enable'] == 'True':
  111. if filename.endswith('.yml'):
  112. line = self.__check_yml_config(
  113. line, filename)
  114. desfile.write(line)
  115. # 移除旧文件
  116. os.remove(filepath)
  117. # 重命名备份文件为新文件
  118. os.rename('%s.bak' % filepath, filepath)
  119. except Exception as err:
  120. self.exceptionhandle(
  121. '修改包名和项目名称异常\n修改文件:{}\n异常信息:{}'.format(
  122. filepath, err))
  123. def __check_yml_config(self, line, filename):
  124. """
  125. 检测yml配置文件
  126. 参数:
  127. line (str): 行
  128. filename (str): 文件名
  129. """
  130. if 'localhost:3306/ry-vue' in line and filename == 'application-druid.yml':
  131. line = self.__alert_yml_config(line, 'mysql_ip_port_name')
  132. if 'username: root' in line and filename == 'application-druid.yml':
  133. line = self.__alert_yml_config(line, 'mysql_username')
  134. if 'password: password' in line and filename == 'application-druid.yml':
  135. line = self.__alert_yml_config(line, 'mysql_password')
  136. if 'host: localhost' in line and filename == 'application.yml':
  137. line = self.__alert_yml_config(line, 'redis_host')
  138. if 'port: 6379' in line and filename == 'application.yml':
  139. line = self.__alert_yml_config(line, 'redis_port')
  140. if 'password: \n' in line and filename == 'application.yml':
  141. line = self.__alert_yml_config(line, 'redis_password')
  142. return line
  143. def __alert_yml_config(self, line, type_):
  144. """
  145. 修改yml配置文件
  146. 参数:
  147. line (str): 行
  148. type_ (str): 修改类型
  149. """
  150. if type_ == 'mysql_ip_port_name':
  151. mysql_ip = self.configdict['database.ip']
  152. mysql_port = self.configdict['database.port']
  153. mysql_name = self.configdict['database.name']
  154. return line.replace('localhost:3306/ry-vue',
  155. mysql_ip + ':' + mysql_port + '/' + mysql_name)
  156. if type_ == 'mysql_username':
  157. mysql_username = self.configdict['database.username']
  158. return line.replace('username: root',
  159. 'username: ' + mysql_username)
  160. if type_ == 'mysql_password':
  161. mysql_password = self.configdict['database.password']
  162. return line.replace('password: password',
  163. 'password: ' + mysql_password)
  164. if type_ == 'redis_host':
  165. redis_ip = self.configdict['redis.ip']
  166. return line.replace('host: localhost', 'host: ' + redis_ip)
  167. if type_ == 'redis_port':
  168. redis_port = self.configdict['redis.port']
  169. return line.replace('port: 6379', 'port: ' + redis_port)
  170. if type_ == 'redis_password':
  171. redis_password = self.configdict['redis.password']
  172. return line.replace('password: \n',
  173. 'password: ' + redis_password + '\n')
  174. return line
  175. def __alter_pom_xml(self):
  176. """修改项目pom.xml文件"""
  177. # 将最外层的文件夹添加到新的元组中
  178. ttuple = (const.RUOYI_DEFAULT_PROJECT_DIR_NAME, )
  179. ntuple = ttuple + const.RUOYI_DEFAULT_MODULE_NAME_TUPLE
  180. for module_name in ntuple:
  181. pom_xml_file = ''
  182. # 如果元组内元素是项目名,文件路径需要特殊处理
  183. if module_name == const.RUOYI_DEFAULT_PROJECT_DIR_NAME:
  184. pom_xml_file = os.path.join(self.rootpath, 'pom.xml')
  185. else:
  186. pom_xml_file = os.path.join(self.rootpath, module_name,
  187. 'pom.xml')
  188. if not os.path.exists(pom_xml_file):
  189. continue
  190. try:
  191. with open(pom_xml_file, 'r',
  192. encoding='utf-8') as xml_file, open(
  193. '%s.bak' % pom_xml_file, 'w',
  194. encoding='utf-8') as target_file:
  195. self.messagehandle('正在修改:' + module_name + '/pom.xml')
  196. for line in xml_file:
  197. if const.RUOYI_DEFAULT_GROUP_ID in line and '<groupId>' in line:
  198. line = line.replace(const.RUOYI_DEFAULT_GROUP_ID,
  199. self.groupid)
  200. if const.RUOYI_DEFAULT_ARTIFACTID_PREFIX in line and '<artifactId>' in line:
  201. line = line.replace(
  202. const.RUOYI_DEFAULT_ARTIFACTID_PREFIX,
  203. self.artifactid)
  204. if module_name == const.RUOYI_DEFAULT_PROJECT_DIR_NAME:
  205. if '<name>' in line or '<module>' in line:
  206. line = line.replace(
  207. const.RUOYI_DEFAULT_PROJECT_NAME,
  208. self.projectname)
  209. if 'version>' in line:
  210. line = line.replace(
  211. const.RUOYI_DEFAULT_ARTIFACTID_PREFIX,
  212. self.artifactid)
  213. if 'description>' in line:
  214. line = line.replace(
  215. const.RUOYI_DEFAULT_SITE_NAME,
  216. self.sitename)
  217. target_file.write(line)
  218. # 移除旧文件
  219. os.remove(pom_xml_file)
  220. # 重命名备份文件为新文件
  221. os.rename('%s.bak' % pom_xml_file, pom_xml_file)
  222. except Exception as err:
  223. self.exceptionhandle(
  224. '修改项目pom.xml文件异常\n修改文件:{}\n异常信息:{}'.format(
  225. pom_xml_file, err))
  226. def __alter_project_dir(self):
  227. """修改目录名"""
  228. for module_name in const.RUOYI_DEFAULT_MODULE_NAME_TUPLE:
  229. src_main_java_dir = os.path.join(self.rootpath, module_name,
  230. 'src/main/java')
  231. # 如果没有找到源代码路径,结束本次循环
  232. if os.path.exists(src_main_java_dir):
  233. source_dir = os.path.join(
  234. src_main_java_dir,
  235. self.packagename.replace('.', os.path.sep))
  236. if not os.path.exists(source_dir):
  237. os.makedirs(source_dir)
  238. self.move_dir(os.path.join(src_main_java_dir, 'com/ruoyi'),
  239. source_dir)
  240. # 拷贝完目录后会删除掉之前的文件,但是目录没有删除,再次删除目录
  241. for root, dirs, files in os.walk(os.path.join(
  242. src_main_java_dir, 'com/ruoyi'),
  243. topdown=False):
  244. if not os.listdir(root):
  245. os.rmdir(root)
  246. # 删除com目录
  247. if not os.listdir(os.path.join(src_main_java_dir, 'com')):
  248. os.rmdir(os.path.join(src_main_java_dir, 'com'))
  249. os.rename(
  250. os.path.join(self.rootpath, module_name),
  251. os.path.join(
  252. self.rootpath,
  253. self.projectname + '-' + module_name.split('-')[1]))
  254. else:
  255. os.rename(
  256. os.path.join(self.rootpath, module_name),
  257. os.path.join(
  258. self.rootpath,
  259. self.projectname + '-' + module_name.split('-')[1]))
  260. self.messagehandle('正在修改:' + module_name)
  261. os.rename(self.rootpath,
  262. os.path.join(self.targetdir, self.projectdirname))
  263. self.messagehandle('正在修改:' + const.RUOYI_DEFAULT_PROJECT_DIR_NAME)