1
1
# /bin/bash
2
2
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
+
4
38
5
39
appName=" MDCx"
6
40
@@ -19,7 +53,83 @@ pyi-makespec \
19
53
20
54
rm -rf ./dist
21
55
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
22
97
pyinstaller MDCx.spec
23
98
99
+ # Remove unnecessary files
24
100
rm -rf ./build
25
101
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