123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380 |
- # -*- coding: utf-8 -*-
- # @Time : 2021/05/18
- # @Author : ricky
- # @File : ruoyicloud.py
- # @Software: vscode
- """
- 核心修改类 RuoYi-Cloud版本
- """
- import os
- from core import base
- from constant import ruoyicloudconstant as const
- class RuoYiCloud(base.BaseCore):
- def start(self):
- # 拼接根路径
- self.rootpath = os.path.join(self.targetdir,
- const.RUOYI_DEFAULT_PROJECT_DIR_NAME)
- # 1.修改站点名称
- self.messagehandle('正在修改标题和修站点名称...')
- self.__alter_site_name_and_title()
- self.messagehandle('站点名称和标题修改完成!')
- # 2.修改包名和项目名
- self.messagehandle('正在修改包名和项目名...')
- self.__alter_package_name_and_project_name(self.rootpath)
- self.messagehandle('包名和项目名修改完成!')
- # 3.修改项目配置和日志配置
- self.messagehandle('正在修改项目配置和日志配置...')
- self.__alter_bootstrapyml_and_logbackxml(self.rootpath)
- self.messagehandle('项目配置和日志配置修改完成!')
- # 4.修改Nacos配置
- self.messagehandle('正在修改Nacos配置...')
- self.__alter_nacos_config(os.path.join(self.rootpath, 'sql'))
- self.messagehandle('Nacos配置修改完成!')
- # 5.修改pom.xml文件
- self.messagehandle('正在修改pom.xml...')
- self.__alter_pom_xml(self.rootpath)
- self.messagehandle('pom.xml修改完成!')
- # 6.修改目录结构
- self.messagehandle('正在修改目录结构...')
- self.__alter_project_dir()
- self.messagehandle('目录结构修改完成!')
- if len(self.exceptions) > 0:
- self.messagehandle('\r发现有异常信息')
- self.messagehandle('-------------------\n\r')
- for e in self.exceptions:
- self.messagehandle(e)
- self.messagehandle('\r----------------------')
- def __alter_site_name_and_title(self):
- """修改站点名称和网站标题"""
- ntuple = const.RUOYI_SITE_RESOURCES_PATH_TUPLE
- for item in ntuple:
- filepath = os.path.join(self.rootpath,
- item.replace('#', os.path.sep))
- if not os.path.exists(filepath):
- continue
- try:
- with open(filepath, 'r', encoding='utf-8') as srcfile, open(
- '%s.bak' % filepath, 'w', encoding='utf-8') as desfile:
- for line in srcfile:
- if const.RUOYI_DEFAULT_SITE_NAME in line:
- line = line.replace(const.RUOYI_DEFAULT_SITE_NAME,
- self.sitename)
- if '若依后台管理系统' in line:
- line = line.replace('若依后台管理系统', self.sitename)
- if '若依 后台管理系统' in line:
- line = line.replace('若依 后台管理系统', self.sitename)
- if '登录若依系统' in line:
- line = line.replace('登录若依系统', '登录' + self.sitename)
- if '若依系统' in line:
- line = line.replace('若依系统', self.sitename)
- if '若依介绍' in line:
- line = line.replace('若依介绍', self.sitename + '介绍')
- if 'RuoYi -' in line:
- line = line.replace('RuoYi -', self.sitename)
- desfile.write(line)
- # 移除旧文件
- os.remove(filepath)
- # 重命名备份文件为新文件
- os.rename('%s.bak' % filepath, filepath)
- except Exception as err:
- self.exceptionhandle('修改站点名称和网站标题异常\n修改文件:{}\n异常信息:{}'.format(
- filepath, err))
- def __alter_package_name_and_project_name(self, rootpath):
- """修改包名和项目名称"""
- files = os.listdir(rootpath)
- for filename in files:
- filepath = os.path.join(rootpath, filename)
- if os.path.isdir(filepath):
- self.__alter_package_name_and_project_name(filepath)
- else:
- if filename.endswith('.java') or filename.endswith(
- '.yml'
- ) or filename.endswith('Mapper.xml') or filename.endswith(
- 'logback.xml') or filename.endswith(
- '.factories') or filename.endswith(
- '.vm') or filename.endswith(
- '.bat') or filename.endswith('.sh'):
- try:
- with open(filepath, 'r',
- encoding='utf-8') as srcfile, open(
- '%s.bak' % filepath,
- 'w',
- encoding='utf-8') as desfile:
- self.messagehandle('正在修改:' + filename)
- for line in srcfile:
- if const.RUOYI_DEFAULT_PACKAGE_NAME in line:
- line = line.replace(
- const.RUOYI_DEFAULT_PACKAGE_NAME,
- self.packagename)
- if const.RUOYI_DEFAULT_PROJECT_NAME + '-' in line:
- line = line.replace(
- const.RUOYI_DEFAULT_PROJECT_NAME + '-',
- self.projectname + '-')
- if self.configdict['config.enable'] == 'True':
- if filename.endswith('.yml'):
- line = self.__check_yml_or_sql_config(
- line, filename)
- desfile.write(line)
- # 移除旧文件
- os.remove(filepath)
- # 重命名备份文件为新文件
- os.rename('%s.bak' % filepath, filepath)
- except Exception as err:
- self.exceptionhandle(
- '修改包名和项目名称异常\n修改文件:{}\n异常信息:{}'.format(
- filepath, err))
- def __alter_bootstrapyml_and_logbackxml(self, rootpath):
- """
- 修改项目bootstrap.yml和logback.xml中的模块名
- 参数:
- rootpath (str): 根路径
- """
- # 循环修改指定后缀名的文件内容
- files = os.listdir(rootpath)
- for filename in files:
- filepath = os.path.join(rootpath, filename)
- # 如果是目录继续递归
- if os.path.isdir(filepath):
- self.__alter_bootstrapyml_and_logbackxml(filepath)
- else:
- try:
- # 如果是文件才进行修改
- if filename.endswith('.yml') or filename.endswith(
- 'logback.xml'):
- with open(filepath, 'r',
- encoding='utf-8') as srcfile, open(
- '%s.bak' % filepath,
- 'w',
- encoding='utf-8') as desfile:
- self.messagehandle('正在修改:' + filename)
- for line in srcfile:
- if const.RUOYI_DEFAULT_PROJECT_NAME in line:
- line = line.replace(
- const.RUOYI_DEFAULT_PROJECT_NAME,
- self.projectname)
- desfile.write(line)
- # 移除旧文件
- os.remove(filepath)
- # 重命名备份文件为新文件
- os.rename('%s.bak' % filepath, filepath)
- except Exception as err:
- self.exceptionhandle(
- '修改项目bootstrap.yml和logback.xml中的模块名异常\n修改文件:{}\n异常信息:{}'
- .format(filepath, err))
- def __alter_nacos_config(self, sqldir):
- """
- 修改项目Nacos配置
- 参数:
- sqldir (str): sql目录
- """
- files = os.listdir(sqldir)
- for filename in files:
- filepath = os.path.join(sqldir, filename)
- # 如果是目录继续递归
- if os.path.isdir(filepath):
- self.__alter_nacos_config(filepath)
- else:
- try:
- # 如果是文件才进行修改
- if filename.startswith(
- const.RUOYI_DEFAULT_NACOS_CONFIG_SQL_PREFIX):
- with open(filepath, 'r',
- encoding='utf-8') as srcfile, open(
- '%s.bak' % filepath,
- 'w',
- encoding='utf-8') as desfile:
- self.messagehandle('正在修改:' + filename)
- for line in srcfile:
- if const.RUOYI_DEFAULT_PACKAGE_NAME in line:
- line = line.replace(
- const.RUOYI_DEFAULT_PACKAGE_NAME,
- self.packagename)
- if const.RUOYI_DEFAULT_PROJECT_NAME + '-' in line:
- line = line.replace(
- const.RUOYI_DEFAULT_PROJECT_NAME + '-',
- self.projectname + '-')
- if self.configdict['config.enable'] == 'True':
- line = self.__check_yml_or_sql_config(
- line, filename)
- desfile.write(line)
- # 移除旧文件
- os.remove(filepath)
- # 重命名备份文件为新文件
- os.rename('%s.bak' % filepath, filepath)
- except Exception as err:
- self.exceptionhandle(
- '修改项目Nacos配置异常\n修改文件:{}\n异常信息:{}'.format(
- filepath, err))
- def __check_yml_or_sql_config(self, line, filename):
- """
- 检测yml配置文件
- 参数:
- line (str): 行
- filename (str): 文件名
- """
- if 'localhost:3306/ry-cloud' in line and filename.endswith('.sql'):
- line = self.__alert_yml_or_sql_config(line, 'mysql_ip_port_name')
- if 'username: root' in line and filename.endswith('.sql'):
- line = self.__alert_yml_or_sql_config(line, 'mysql_username')
- if 'password: password' in line and filename.endswith('.sql'):
- line = self.__alert_yml_or_sql_config(line, 'mysql_password')
- if 'host: localhost' in line and filename.endswith('.sql'):
- line = self.__alert_yml_or_sql_config(line, 'redis_host')
- if 'port: 6379' in line and filename.endswith('.sql'):
- line = self.__alert_yml_or_sql_config(line, 'redis_port')
- if 'password: \\r\\n' in line and filename.endswith('.sql'):
- line = self.__alert_yml_or_sql_config(line, 'redis_password')
- return line
- def __alert_yml_or_sql_config(self, line, type_):
- """
- 修改yml配置文件
- 参数:
- line (str): 行
- type_ (str): 修改类型
- """
- if type_ == 'mysql_ip_port_name':
- mysql_ip = self.configdict['database.ip']
- mysql_port = self.configdict['database.port']
- mysql_name = self.configdict['database.name']
- return line.replace('localhost:3306/ry-cloud',
- mysql_ip + ':' + mysql_port + '/' + mysql_name)
- if type_ == 'mysql_username':
- mysql_username = self.configdict['database.username']
- return line.replace('username: root',
- 'username: ' + mysql_username)
- if type_ == 'mysql_password':
- mysql_password = self.configdict['database.password']
- return line.replace('password: password',
- 'password: ' + mysql_password)
- if type_ == 'redis_host':
- redis_ip = self.configdict['redis.ip']
- return line.replace('host: localhost', 'host: ' + redis_ip)
- if type_ == 'redis_port':
- redis_port = self.configdict['redis.port']
- return line.replace('port: 6379', 'port: ' + redis_port)
- if type_ == 'redis_password':
- redis_password = self.configdict['redis.password']
- return line.replace('password: \\r\\n',
- 'password: ' + redis_password + '\\r\\n')
- return line
- def __alter_pom_xml(self, rootpath):
- """
- 修改项目pom.xml文件
- 参数:
- rootpath (str): 根目录
- """
- files = os.listdir(rootpath)
- for filename in files:
- filepath = os.path.join(rootpath, filename)
- # 如果是目录继续递归
- if os.path.isdir(filepath):
- self.__alter_pom_xml(filepath)
- else:
- try:
- # 如果是文件才进行修改
- if filename.endswith('pom.xml'):
- with open(filepath, 'r',
- encoding='utf-8') as srcfile, open(
- '%s.bak' % filepath,
- 'w',
- encoding='utf-8') as desfile:
- self.messagehandle('正在修改:' + filename)
- for line in srcfile:
- if const.RUOYI_DEFAULT_GROUP_ID in line and '<groupId>' in line:
- line = line.replace(
- const.RUOYI_DEFAULT_GROUP_ID,
- self.groupid)
- if const.RUOYI_DEFAULT_ARTIFACTID_PREFIX in line and '<artifactId>' in line:
- line = line.replace(
- const.RUOYI_DEFAULT_ARTIFACTID_PREFIX,
- self.artifactid)
- if '<name>' in line or '<module>' in line:
- line = line.replace(
- const.RUOYI_DEFAULT_PROJECT_NAME,
- self.projectname)
- if 'version>' in line:
- line = line.replace(
- const.RUOYI_DEFAULT_ARTIFACTID_PREFIX,
- self.artifactid)
- if const.RUOYI_DEFAULT_SITE_NAME in line:
- line = line.replace(
- const.RUOYI_DEFAULT_SITE_NAME,
- self.sitename)
- line = line.replace(
- const.RUOYI_DEFAULT_ARTIFACTID_PREFIX,
- self.projectname)
- desfile.write(line)
- # 移除旧文件
- os.remove(filepath)
- # 重命名备份文件为新文件
- os.rename('%s.bak' % filepath, filepath)
- except Exception as err:
- self.exceptionhandle(
- '修改项目pom.xml文件异常\n修改文件:{}\n异常信息:{}'.format(
- filepath, err))
- def __alter_project_dir(self):
- """修改目录名"""
- for module_name in const.RUOYI_DEFAULT_MODULE_NAME_TUPLE:
- replace_module_name = module_name.replace('#', os.path.sep)
- src_main_java_dir = os.path.join(self.rootpath,
- replace_module_name,
- 'src/main/java')
- if os.path.exists(src_main_java_dir):
- source_dir = os.path.join(
- src_main_java_dir,
- self.packagename.replace('.', os.path.sep))
- if not os.path.exists(source_dir):
- print(source_dir)
- os.makedirs(source_dir)
- self.move_dir(os.path.join(src_main_java_dir, 'com/ruoyi'),
- source_dir)
- # 拷贝完目录后会删除掉之前的文件,但是目录没有删除,再次删除目录
- for root, dirs, files in os.walk(os.path.join(
- src_main_java_dir, 'com/ruoyi'),
- topdown=False):
- if not os.listdir(root):
- os.rmdir(root)
- # 删除com目录
- if not os.listdir(os.path.join(src_main_java_dir, 'com')):
- os.rmdir(os.path.join(src_main_java_dir, 'com'))
- if module_name.find('#') == -1:
- os.rename(
- os.path.join(self.rootpath, module_name),
- os.path.join(
- self.rootpath, self.projectname + '-' +
- module_name.split('-')[1]))
- else:
- tarpath = os.path.join(
- self.rootpath,
- module_name.split('#')[0], self.projectname + '-' +
- module_name.split('#')[1].replace('-', '$',
- 1).split('$')[1])
- os.rename(os.path.join(self.rootpath, replace_module_name),
- tarpath)
- else:
- os.rename(
- os.path.join(self.rootpath, module_name),
- os.path.join(
- self.rootpath,
- self.projectname + '-' + module_name.split('-')[1]))
- self.messagehandle('正在修改:' + replace_module_name)
- os.rename(self.rootpath,
- os.path.join(self.targetdir, self.projectdirname))
- self.messagehandle('正在修改:' + const.RUOYI_DEFAULT_PROJECT_DIR_NAME)
|