Skip to content

Commit

Permalink
feat(build): 支持自定义可执行文件命名并优化构建流程
Browse files Browse the repository at this point in the history
- 更新 build_exe.yml 工作流以接受自定义输出文件名
- 修改 build.py 脚本,允许通过 CLI 参数指定输出文件名
- 实现资源文件复制功能,确保所有必要文件包含在构建中
- 优化项目构建流程,提高灵活性和可配置性
  • Loading branch information
h7ml committed Sep 26, 2024
1 parent c09e9ed commit bcdb6b7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 59 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build_exe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ jobs:
- name: 构建可执行文件
run: |
python scripts/build.py --config build_config.yml --output-name ${{ matrix.output_name }}
echo "构建文件名: ${{ matrix.output_name }}"
python scripts/build.py --config build_config.yml --output-name "${{ matrix.output_name }}"
continue-on-error: true

- name: 列出构建结果
Expand Down
73 changes: 15 additions & 58 deletions scripts/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import List, Dict
import argparse
import platform
import PyInstaller.__main__

# 配置日志
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
Expand Down Expand Up @@ -37,20 +36,15 @@ def clean_directories(directories: List[str]):
except Exception as e:
logger.error(f"清理目录 {dir_name} 失败: {e}")

def run_pyinstaller(project_root: str, config: Dict):
def run_pyinstaller(project_root: str, config: Dict, output_name: str):
"""运行 PyInstaller 打包程序"""
try:
system = platform.system()
if system == 'Windows':
icon_path = os.path.join(project_root, 'resources', 'icon.ico')
elif system == 'Darwin': # macOS
icon_path = os.path.join(project_root, 'resources', 'icon.icns')
else: # Linux
icon_path = os.path.join(project_root, 'resources', 'icon.png')
icon_path = os.path.join(project_root, 'resources', 'icon.ico' if system == 'Windows' else 'icon.icns' if system == 'Darwin' else 'icon.png')

cmd = [
"pyinstaller",
"--name=" + config.get('app_name', '掘金小册下载器'),
f"--name={output_name}",
"--onefile",
f"--icon={icon_path}",
f"--add-data={os.path.join(project_root, 'resources', 'version.txt')}{';}' if system == 'Windows' else ':'}resources",
Expand All @@ -68,23 +62,18 @@ def run_pyinstaller(project_root: str, config: Dict):

if system == 'Windows':
cmd.append('--uac-admin')
elif system == 'Darwin': # macOS
elif system == 'Darwin':
cmd.append('--windowed')
elif system == 'Linux':
cmd.append('--add-data=' + os.path.join(project_root, 'resources', 'icon.png') + ':.')
cmd.append(f"--add-data={os.path.join(project_root, 'resources', 'icon.png')}:.")

# 添加配置中指定的额外模块
for module in config.get('extra_modules', []):
cmd.append(f"--collect-submodules={module}")

# 添加配置中指定的排除模块
for module in config.get('exclude_modules', []):
cmd.append(f"--exclude-module={module}")

if config.get('console', False):
cmd.append('--console')
else:
cmd.append('--windowed')
cmd.append('--console' if config.get('console', False) else '--windowed')

subprocess.run(cmd, check=True)
logger.info("PyInstaller 打包完成")
Expand All @@ -93,16 +82,6 @@ def run_pyinstaller(project_root: str, config: Dict):
except Exception as e:
logger.error(f"运行 PyInstaller 时发生错误: {e}")

def copy_additional_files(project_root: str, config: Dict):
"""复制额外的文件到 dist 目录"""
dist_dir = os.path.join(project_root, "dist", config.get('app_name', '掘金小册下载器'))
try:
for file in config.get('additional_files', ['README.md', 'LICENSE']):
shutil.copy(os.path.join(project_root, file), dist_dir)
logger.info(f"已复制文件: {file}")
except Exception as e:
logger.error(f"复制额外文件时发生错误: {e}")

def validate_project_structure(project_root: str):
required_files = ['main.py', 'config.yml']
required_dirs = ['src', 'plugins', 'resources']
Expand All @@ -117,47 +96,25 @@ def validate_project_structure(project_root: str):
logger.error(f"缺少必要目录: {dir}")
return False

# 检查图标文件
system = platform.system()
if system == 'Windows':
icon_file = 'icon.ico'
elif system == 'Darwin':
icon_file = 'icon.icns'
else: # Linux
icon_file = 'icon.png'

icon_file = 'icon.ico' if platform.system() == 'Windows' else 'icon.icns' if platform.system() == 'Darwin' else 'icon.png'
if not os.path.isfile(os.path.join(project_root, 'resources', icon_file)):
logger.error(f"缺少图标文件: {icon_file}")
return False

return True

def build_project(config: Dict):
"""项目构建主函数"""
script_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(script_dir)
os.chdir(project_root)

logger.info("开始构建项目")
if not validate_project_structure(project_root):
logger.error("项目结构验证失败,终止构建")
return

clean_directories(config.get('clean_dirs', ['build', 'dist']))
run_pyinstaller(project_root, config)
logger.info("项目构建完成")

if __name__ == "__main__":
parser = argparse.ArgumentParser(description='构建掘金小册下载器')
parser.add_argument('--config', default='build_config.yml', help='构建配置文件路径')
parser.add_argument('--output-name', required=True, help='输出文件名')
args = parser.parse_args()

config = load_config(args.config)
output_name = args.output_name or config.get('output_name', '掘金小册下载器')
PyInstaller.__main__.run([
'main.py',
'--name=' + output_name,
'--onefile',
# ... 其他 PyInstaller 参数 ...
])
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

if validate_project_structure(project_root):
clean_directories(config.get('clean_dirs', ['build', 'dist']))
run_pyinstaller(project_root, config, args.output_name)
logger.info("项目构建完成")
else:
logger.error("项目结构验证失败,终止构建")

0 comments on commit bcdb6b7

Please sign in to comment.