Skip to content

Commit edd43a4

Browse files
northsea4sqzw-x
northsea4
authored andcommitted
CI: refine macos build
1 parent 5af0b14 commit edd43a4

File tree

2 files changed

+114
-11
lines changed

2 files changed

+114
-11
lines changed

.github/workflows/release.yml

+3-10
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,7 @@ jobs:
8383
- name: Build macOS app - macOS
8484
if: ${{ matrix.build == 'macos' }}
8585
run: |
86-
chmod +x build-macos.sh
87-
./build-macos.sh
88-
# cd dist
89-
ls -al dist
90-
rm dist/MDCx
91-
# `upload-artifact`会使文件丢失可执行权限。解决思路,先把`dist`打包成zip,再上传
92-
zip -r MDCx.app.zip dist
93-
ls -al
86+
bash ./build-macos.sh --create-dmg --version "${{ github.ref_name }}"
9487
9588
- name: Build Windows app - Windows
9689
if: ${{ matrix.build == 'windows' }}
@@ -125,8 +118,8 @@ jobs:
125118
uses: svenstaro/upload-release-action@2.7.0
126119
if: ${{ matrix.build == 'macos' }}
127120
with:
128-
asset_name: MDCx-$tag-${{ matrix.build }}-${{ matrix.arch }}.app.zip
129-
file: MDCx.app.zip
121+
asset_name: MDCx-$tag-${{ matrix.build }}-${{ matrix.arch }}.dmg
122+
file: dist/MDCx.dmg
130123
prerelease: ${{ startsWith(github.ref, 'refs/tags/pre-') }}
131124
body: |
132125
${{ steps.get-changelog.outputs.CHANGELOG }}

build-macos.sh

+111-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,40 @@
11
#/bin/bash
22

3-
# Sample build script for pyinstaller
3+
set -e
4+
5+
while [[ $# -gt 0 ]]
6+
do
7+
key="$1"
8+
case $key in
9+
--version|-v)
10+
APP_VERSION="$2"
11+
shift
12+
shift
13+
;;
14+
--create-dmg|-dmg)
15+
CREATE_DMG=true
16+
shift
17+
;;
18+
--help|-h)
19+
echo "Usage: build-macos.sh [options]"
20+
echo "Options:"
21+
echo " --version, -v Specify the version number. Required!"
22+
echo " --create-dmg, -dmg Create DMG file. Default is false."
23+
exit 0
24+
;;
25+
*)
26+
shift
27+
;;
28+
esac
29+
done
30+
31+
32+
# Check if APP_VERSION is set
33+
if [ -z "$APP_VERSION" ]; then
34+
echo "❌ Please specify the version number using --version option!"
35+
exit 1
36+
fi
37+
438

539
appName="MDCx"
640

@@ -19,7 +53,83 @@ pyi-makespec \
1953

2054
rm -rf ./dist
2155

56+
# Find line number by keyword
57+
findLine() {
58+
local file="$1"
59+
local keyword="$2"
60+
local line=$(grep -n "$keyword" "$file" | cut -d: -f1)
61+
echo "$line"
62+
}
63+
64+
# Insert content after a specific line
65+
insertAfterLine() {
66+
local file="$1"
67+
local line="$2"
68+
local content="$3"
69+
local newContent=""
70+
local i=1
71+
while IFS= read -r lineContent; do
72+
if [ $i -eq $line ]; then
73+
newContent+="$lineContent\n$content\n"
74+
else
75+
newContent+="$lineContent\n"
76+
fi
77+
i=$((i+1))
78+
done < "$file"
79+
echo -e "$newContent"
80+
}
81+
82+
# Add `info_plist` to `MDCx.spec` file
83+
INFO_PLIST=$(cat <<EOF
84+
info_plist={
85+
'CFBundleShortVersionString': '$APP_VERSION',
86+
'CFBundleVersion': '$APP_VERSION',
87+
}
88+
EOF
89+
)
90+
91+
LINE=$(findLine "MDCx.spec" "bundle_identifier")
92+
NEW_CONTENT=$(insertAfterLine "MDCx.spec" $LINE "$INFO_PLIST")
93+
echo -e "$NEW_CONTENT" > MDCx.spec
94+
95+
96+
# Build the app
2297
pyinstaller MDCx.spec
2398

99+
# Remove unnecessary files
24100
rm -rf ./build
25101
rm *.spec
102+
103+
104+
# Install `create-dmg` if `CREATE_DMG` is true
105+
if [ "$CREATE_DMG" = true ] && ! command -v create-dmg &> /dev/null; then
106+
echo "Installing create-dmg..."
107+
brew install create-dmg
108+
if [ $? -ne 0 ]; then
109+
echo "❌ Failed to install create-dmg!"
110+
exit 1
111+
fi
112+
fi
113+
114+
115+
# Create DMG file
116+
if [ "$CREATE_DMG" = true ]; then
117+
echo "Creating DMG file..."
118+
# https://github.com/create-dmg/create-dmg?tab=readme-ov-file#usage
119+
create-dmg \
120+
--volname "$appName" \
121+
--volicon "resources/Img/MDCx.icns" \
122+
--window-pos 200 120 \
123+
--window-size 800 400 \
124+
--icon-size 80 \
125+
--icon "$appName.app" 300 36 \
126+
--hide-extension "$appName.app" \
127+
--app-drop-link 500 36 \
128+
"dist/$appName.dmg" \
129+
"dist/$appName.app"
130+
131+
if [ $? -ne 0 ]; then
132+
echo "❌ Failed to create DMG file!"
133+
exit 1
134+
fi
135+
fi

0 commit comments

Comments
 (0)