-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_dmg.sh
More file actions
196 lines (166 loc) · 6.02 KB
/
Copy pathbuild_dmg.sh
File metadata and controls
196 lines (166 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/bash
#
# build_dmg.sh — Build a distributable .dmg for Markdown Viewer
# Run from the project root: bash build_dmg.sh
#
# Produces: "Markdown Viewer 1.0.dmg" in the current directory
#
APP_NAME="Markdown Viewer"
DMG_NAME="Markdown Viewer 1.0"
VOL_NAME="Markdown Viewer"
APP_BUNDLE="Markdown Viewer.app"
STAGING_DIR=".dmg_staging"
DMG_TEMP="${DMG_NAME}_temp.dmg"
DMG_FINAL="${DMG_NAME}.dmg"
# Cleanup function for error handling
cleanup() {
echo "Cleaning up..."
# Unmount if still mounted
if [ -d "/Volumes/${VOL_NAME}" ]; then
hdiutil detach "/Volumes/${VOL_NAME}" -force 2>/dev/null || true
fi
rm -f "${DMG_TEMP}"
rm -rf "${STAGING_DIR}"
}
echo "=== Building ${DMG_FINAL} ==="
# -------------------------------------------------------
# Step 0: Ensure no previous volume is mounted
# -------------------------------------------------------
if [ -d "/Volumes/${VOL_NAME}" ]; then
echo "Ejecting previously mounted volume..."
hdiutil detach "/Volumes/${VOL_NAME}" -force 2>/dev/null || true
sleep 1
fi
# -------------------------------------------------------
# Step 1: Make sure the .app bundle is up to date
# -------------------------------------------------------
echo "[1/7] Syncing latest source into app bundle..."
cp -f viewer.py "${APP_BUNDLE}/Contents/Resources/viewer.py"
cp -f markdown_parser.py "${APP_BUNDLE}/Contents/Resources/markdown_parser.py"
# Make sure the launcher is executable
chmod +x "${APP_BUNDLE}/Contents/MacOS/MarkdownViewer"
# Ensure bundle bit is set
SetFile -a B "${APP_BUNDLE}"
# -------------------------------------------------------
# Step 2: Clean up any previous build artifacts
# -------------------------------------------------------
echo "[2/7] Cleaning previous build artifacts..."
rm -rf "${STAGING_DIR}"
rm -f "${DMG_TEMP}"
rm -f "${DMG_FINAL}"
# -------------------------------------------------------
# Step 3: Create staging directory with app + Applications link
# -------------------------------------------------------
echo "[3/7] Creating staging directory..."
mkdir -p "${STAGING_DIR}"
# Copy the .app bundle (preserving resource forks and metadata)
cp -pR "${APP_BUNDLE}" "${STAGING_DIR}/"
# Ensure the bundle bit is set on the copy
SetFile -a B "${STAGING_DIR}/${APP_BUNDLE}"
# Create symlink to /Applications for drag-install
ln -s /Applications "${STAGING_DIR}/Applications"
# -------------------------------------------------------
# Step 4: Create a read/write DMG from the staging dir
# -------------------------------------------------------
echo "[4/7] Creating temporary read/write DMG..."
# Calculate size needed (app size + 5MB padding)
APP_SIZE_KB=$(du -sk "${STAGING_DIR}" | awk '{print $1}')
DMG_SIZE_KB=$((APP_SIZE_KB + 5120))
hdiutil create \
-srcfolder "${STAGING_DIR}" \
-volname "${VOL_NAME}" \
-fs HFS+ \
-fsargs "-c c=64,a=16,e=16" \
-format UDRW \
-size ${DMG_SIZE_KB}k \
"${DMG_TEMP}"
if [ $? -ne 0 ]; then
echo "ERROR: Failed to create DMG"
cleanup
exit 1
fi
# -------------------------------------------------------
# Step 5: Mount the DMG and customize Finder appearance
# -------------------------------------------------------
echo "[5/7] Mounting DMG and setting Finder view options..."
# Mount the writable DMG
MOUNT_OUTPUT=$(hdiutil attach -readwrite -noverify -noautoopen "${DMG_TEMP}" 2>&1)
MOUNT_POINT="/Volumes/${VOL_NAME}"
if [ ! -d "${MOUNT_POINT}" ]; then
echo "ERROR: Failed to mount DMG"
echo "${MOUNT_OUTPUT}"
cleanup
exit 1
fi
echo " Mounted at: ${MOUNT_POINT}"
# Give Finder a moment to index
sleep 2
# Use AppleScript to set the Finder window appearance
osascript <<APPLESCRIPT
tell application "Finder"
tell disk "${VOL_NAME}"
open
set current view of container window to icon view
set toolbar visible of container window to false
set statusbar visible of container window to false
set bounds of container window to {200, 200, 680, 460}
set theViewOptions to the icon view options of container window
set arrangement of theViewOptions to not arranged
set icon size of theViewOptions to 80
set position of item "${APP_BUNDLE}" of container window to {120, 130}
set position of item "Applications" of container window to {360, 130}
close
open
update without registering applications
delay 2
close
end tell
end tell
APPLESCRIPT
# Make sure .DS_Store is flushed
sync
# -------------------------------------------------------
# Step 5b: Set custom volume icon (optional)
# -------------------------------------------------------
if [ -f "${APP_BUNDLE}/Contents/Resources/appicon.icns" ]; then
echo "[5b/7] Setting volume icon..."
cp "${APP_BUNDLE}/Contents/Resources/appicon.icns" "${MOUNT_POINT}/.VolumeIcon.icns" 2>/dev/null && {
SetFile -c icnC "${MOUNT_POINT}/.VolumeIcon.icns" 2>/dev/null || true
SetFile -a C "${MOUNT_POINT}" 2>/dev/null || true
} || echo " (skipped - could not write to volume)"
fi
# -------------------------------------------------------
# Step 6: Unmount
# -------------------------------------------------------
echo "[6/7] Unmounting..."
hdiutil detach "/Volumes/${VOL_NAME}" -force 2>/dev/null || true
sleep 1
# Make sure it's really gone
if [ -d "/Volumes/${VOL_NAME}" ]; then
echo " Retrying unmount..."
sleep 3
hdiutil detach "/Volumes/${VOL_NAME}" -force 2>/dev/null || true
fi
# -------------------------------------------------------
# Step 7: Convert to compressed read-only DMG
# -------------------------------------------------------
echo "[7/7] Converting to compressed read-only DMG..."
hdiutil convert \
"${DMG_TEMP}" \
-format UDZO \
-imagekey zlib-level=9 \
-o "${DMG_FINAL}"
if [ $? -ne 0 ]; then
echo "ERROR: Failed to convert DMG"
cleanup
exit 1
fi
# Clean up
rm -f "${DMG_TEMP}"
rm -rf "${STAGING_DIR}"
echo ""
echo "=== Done! ==="
FINAL_SIZE=$(du -sh "${DMG_FINAL}" | awk '{print $1}')
echo "Created: ${DMG_FINAL} (${FINAL_SIZE})"
echo ""
echo "To test: open \"${DMG_FINAL}\""