|
1 | | -name: Windows PyInstaller Build |
2 | | - |
| 1 | +name: Windows Build |
| 2 | + |
3 | 3 | on: |
4 | 4 | workflow_dispatch: |
5 | 5 | inputs: |
6 | 6 | version: |
7 | | - description: "Release version (e.g. v1.0.0)" |
| 7 | + description: "Release version tag (e.g. 1.2.0.2 or 1.2.0.2-beta1)" |
8 | 8 | required: true |
9 | 9 | type: string |
10 | | - |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + |
11 | 14 | jobs: |
12 | 15 | build-windows: |
13 | 16 | runs-on: windows-latest |
14 | | - |
| 17 | + |
15 | 18 | steps: |
16 | 19 | - name: Checkout code |
17 | | - uses: actions/checkout@v3 |
18 | | - |
| 20 | + uses: actions/checkout@v4 |
| 21 | + |
19 | 22 | - name: Set up Python |
20 | | - uses: actions/setup-python@v4 |
| 23 | + uses: actions/setup-python@v5 |
21 | 24 | with: |
22 | | - python-version: '3.10' |
23 | | - |
| 25 | + python-version: "3.10" |
| 26 | + |
24 | 27 | - name: Install dependencies |
25 | 28 | run: | |
26 | 29 | python -m pip install --upgrade pip |
27 | | - pip install pyinstaller |
| 30 | + pip install pyinstaller PyQt5 |
28 | 31 | if (Test-Path "requirements.txt") { pip install -r requirements.txt } |
29 | | - |
30 | | - - name: Create saves folder placeholder |
| 32 | +
|
| 33 | + - name: Prepare saves folder |
31 | 34 | run: | |
32 | 35 | if (!(Test-Path "saves")) { |
33 | | - New-Item -ItemType Directory -Path "saves" -Force |
| 36 | + New-Item -ItemType Directory -Path "saves" -Force | Out-Null |
34 | 37 | } |
35 | | - # 创建空的配置文件确保saves文件夹被包含 |
36 | 38 | if (!(Test-Path "saves\config.json")) { |
37 | 39 | '{}' | Out-File -FilePath "saves\config.json" -Encoding UTF8 |
38 | 40 | } |
39 | | - |
40 | | - - name: Add saves folder auto-creation code |
| 41 | +
|
| 42 | + - name: Build with PyInstaller (onedir) |
41 | 43 | run: | |
42 | | - # 在main.py开头添加saves文件夹自动创建代码 |
43 | | - $savesCode = @' |
44 | | -import os |
45 | | -import sys |
46 | | -import json |
47 | | - |
48 | | -def ensure_saves_folder(): |
49 | | - """确保saves文件夹存在,如果不存在则创建""" |
50 | | - if getattr(sys, 'frozen', False): |
51 | | - # 如果是打包后的可执行文件 |
52 | | - base_path = os.path.dirname(sys.executable) |
53 | | - else: |
54 | | - # 如果是开发环境 |
55 | | - base_path = os.path.dirname(os.path.abspath(__file__)) |
56 | | - |
57 | | - saves_path = os.path.join(base_path, 'saves') |
58 | | - |
59 | | - if not os.path.exists(saves_path): |
60 | | - try: |
61 | | - os.makedirs(saves_path) |
62 | | - print(f"Created saves folder at: {saves_path}") |
63 | | - |
64 | | - # 创建默认配置文件 |
65 | | - config_path = os.path.join(saves_path, 'config.json') |
66 | | - default_config = { |
67 | | - "passwordHash": "", |
68 | | - "passwordUpdatedAt": "", |
69 | | - "appFontFamily": "HarmonyOS Sans SC", |
70 | | - "fontUpdatedAt": "", |
71 | | - "onboardingDone": false, |
72 | | - "onboardingUpdatedAt": "" |
73 | | - } |
74 | | - |
75 | | - with open(config_path, 'w', encoding='utf-8') as f: |
76 | | - json.dump(default_config, f, indent=2, ensure_ascii=False) |
77 | | - |
78 | | - print(f"Created default config file at: {config_path}") |
79 | | - |
80 | | - except Exception as e: |
81 | | - print(f"Error creating saves folder: {e}") |
82 | | - |
83 | | -# 程序启动时自动创建saves文件夹 |
84 | | -ensure_saves_folder() |
85 | | - |
86 | | -'@ |
87 | | - |
88 | | - # 读取现有的main.py内容 |
89 | | - $originalContent = Get-Content "main.py" -Raw |
90 | | - |
91 | | - # 将新代码添加到文件开头 |
92 | | - $newContent = $savesCode + $originalContent |
93 | | - |
94 | | - # 写回文件 |
95 | | - $newContent | Set-Content "main.py" -Encoding UTF8 |
96 | | - |
97 | | - Write-Host "Added saves folder auto-creation code to main.py" |
98 | | - |
99 | | - - name: Build with PyInstaller |
100 | | - run: | |
101 | | - pyinstaller --onefile ` |
102 | | - --name=HomeworkIsland ` |
103 | | - --add-data="saves;saves" ` |
104 | | - --add-data=".;." ` |
105 | | - --exclude-module=matplotlib ` |
106 | | - --exclude-module=PIL ` |
107 | | - --exclude-module=pygame ` |
108 | | - --windowed ` |
109 | | - --icon=icon.ico ` |
110 | | - --clean ` |
| 44 | + pyinstaller ` |
111 | 45 | --noconfirm ` |
| 46 | + --clean ` |
| 47 | + --windowed ` |
| 48 | + --name "Homeworklsland" ` |
| 49 | + --icon "icon.ico" ` |
| 50 | + --add-data "qml;qml" ` |
| 51 | + --add-data "view;view" ` |
| 52 | + --add-data "about.html;." ` |
| 53 | + --add-data "icon.png;." ` |
| 54 | + --add-data "font.ttf;." ` |
| 55 | + --add-data "dev.txt;." ` |
112 | 56 | main.py |
113 | | - |
114 | | - - name: Create distribution package |
| 57 | +
|
| 58 | + - name: Create ZIP package (软件-版本-架构-系统) |
115 | 59 | run: | |
116 | | - New-Item -ItemType Directory -Path "dist\release" -Force |
117 | | - Copy-Item "dist\HomeworkIsland.exe" "dist\release\" |
118 | | - Copy-Item "saves" "dist\release\" -Recurse -Force |
119 | | - Copy-Item "icon.ico" "dist\release\" -ErrorAction SilentlyContinue |
120 | | - |
121 | | - # 创建README文件 |
122 | | - @" |
123 | | -HomeworkIsland - Windows Executable |
124 | | -=================================== |
125 | | - |
126 | | -首次运行程序时,会自动在程序所在目录创建saves文件夹和默认配置文件。 |
127 | | - |
128 | | -使用方法: |
129 | | -1. 双击 HomeworkIsland.exe 运行程序 |
130 | | -2. 程序会自动创建saves文件夹 |
131 | | -3. 配置文件将保存在 saves\config.json |
132 | | - |
133 | | -版本: ${{ github.event.inputs.version }} |
134 | | -构建时间: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss") |
135 | | -"@ | Out-File -FilePath "dist\release\README.txt" -Encoding UTF8 |
136 | | - |
137 | | - - name: Create ZIP package |
138 | | - run: | |
139 | | - Compress-Archive -Path "dist\release\*" -DestinationPath "HomeworkIsland-Windows-${{ github.event.inputs.version }}.zip" -Force |
140 | | - |
| 60 | + $tag = "${{ inputs.version }}" |
| 61 | + $arch = "${{ runner.arch }}".ToLower() |
| 62 | + if ($arch -eq "x64") { $arch = "x64" } |
| 63 | + elseif ($arch -eq "arm64") { $arch = "arm64" } |
| 64 | + elseif ($arch -eq "x86") { $arch = "x86" } |
| 65 | +
|
| 66 | + $packageDirName = "Homeworklsland-$tag-$arch-windows" |
| 67 | + $zipName = "$packageDirName.zip" |
| 68 | +
|
| 69 | + if (Test-Path "dist\$packageDirName") { Remove-Item "dist\$packageDirName" -Recurse -Force } |
| 70 | +
|
| 71 | + Move-Item "dist\Homeworklsland" "dist\$packageDirName" |
| 72 | +
|
| 73 | + if (!(Test-Path "dist\$packageDirName\saves")) { |
| 74 | + New-Item -ItemType Directory -Path "dist\$packageDirName\saves" -Force | Out-Null |
| 75 | + } |
| 76 | + Copy-Item "saves\*" "dist\$packageDirName\saves\" -Recurse -Force |
| 77 | +
|
| 78 | + if (Test-Path $zipName) { Remove-Item $zipName -Force } |
| 79 | + Compress-Archive -Path "dist\$packageDirName\*" -DestinationPath $zipName -Force |
| 80 | +
|
| 81 | + Write-Host "Created $zipName" |
| 82 | +
|
141 | 83 | - name: Upload artifact |
142 | | - uses: actions/upload-artifact@v3 |
143 | | - with: |
144 | | - name: HomeworkIsland-Windows |
145 | | - path: HomeworkIsland-Windows-${{ github.event.inputs.version }}.zip |
146 | | - |
147 | | - create-release: |
148 | | - needs: build-windows |
149 | | - runs-on: ubuntu-latest |
150 | | - permissions: |
151 | | - contents: write |
152 | | - |
153 | | - steps: |
154 | | - - name: Download artifact |
155 | | - uses: actions/download-artifact@v3 |
| 84 | + uses: actions/upload-artifact@v4 |
156 | 85 | with: |
157 | | - name: HomeworkIsland-Windows |
158 | | - path: release |
159 | | - |
160 | | - - name: Create Release |
| 86 | + name: Homeworklsland-windows-zip |
| 87 | + path: Homeworklsland-*-*-windows.zip |
| 88 | + |
| 89 | + - name: Create GitHub Release |
161 | 90 | uses: softprops/action-gh-release@v1 |
162 | 91 | with: |
163 | | - tag_name: ${{ github.event.inputs.version }} |
164 | | - name: HomeworkIsland Windows ${{ github.event.inputs.version }} |
165 | | - body: | |
166 | | - ## HomeworkIsland Windows 版本 |
167 | | - |
168 | | - 此版本包含: |
169 | | - - 完整的Windows可执行文件 |
170 | | - - 自动创建saves文件夹功能 |
171 | | - - 默认配置文件 |
172 | | - |
173 | | - ### 使用方法 |
174 | | - 1. 下载并解压ZIP文件 |
175 | | - 2. 双击 `HomeworkIsland.exe` 运行程序 |
176 | | - 3. 程序会自动创建saves文件夹和配置文件 |
177 | | - |
178 | | - ### 版本信息 |
179 | | - - 版本:${{ github.event.inputs.version }} |
180 | | - - 构建时间:${{ github.event.head_commit.timestamp }} |
181 | | - - 提交:${{ github.sha }} |
182 | | - files: | |
183 | | - release/HomeworkIsland-Windows-${{ github.event.inputs.version }}.zip |
184 | | - draft: false |
185 | | - prerelease: false |
| 92 | + tag_name: ${{ inputs.version }} |
| 93 | + name: Homeworklsland ${{ inputs.version }} |
| 94 | + files: Homeworklsland-*-*-windows.zip |
| 95 | + prerelease: ${{ contains(inputs.version, '-beta') }} |
0 commit comments