Skip to content

Commit e82d4b5

Browse files
committed
fixed macos
1 parent d2b03f1 commit e82d4b5

1 file changed

Lines changed: 61 additions & 39 deletions

File tree

scripts/create_dmg.sh

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,13 @@ if [ -d "$MOUNT_POINT" ]; then
123123
sleep 1
124124
fi
125125

126-
# Mount with retry logic
126+
# Mount with retry logic (capture disk id for reliable unmount in CI)
127+
ATTACH_OUTPUT=""
127128
MAX_RETRIES=3
128129
RETRY_COUNT=0
129130
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
130-
if hdiutil attach "${TEMP_DIR}/${DMG_NAME}-temp.dmg" -mountpoint "$MOUNT_POINT" -readwrite -nobrowse 2>&1; then
131+
ATTACH_OUTPUT=$(hdiutil attach "${TEMP_DIR}/${DMG_NAME}-temp.dmg" -mountpoint "$MOUNT_POINT" -readwrite -nobrowse 2>&1) && true
132+
if [ -d "$MOUNT_POINT" ]; then
131133
echo "DMG mounted successfully"
132134
break
133135
else
@@ -137,12 +139,18 @@ while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
137139
sleep 2
138140
else
139141
echo "Error: Failed to mount DMG after $MAX_RETRIES attempts"
140-
echo "DMG file info:"
142+
echo "$ATTACH_OUTPUT"
141143
ls -lh "${TEMP_DIR}/${DMG_NAME}-temp.dmg" || true
142144
exit 1
143145
fi
144146
fi
145147
done
148+
# Capture disk identifier for unmount (e.g. disk6 from "/dev/disk6" or "disk6")
149+
MOUNTED_DISK=""
150+
if echo "$ATTACH_OUTPUT" | grep -qE '/dev/disk[0-9]+'; then
151+
MOUNTED_DISK=$(echo "$ATTACH_OUTPUT" | grep -oE 'disk[0-9]+' | head -1)
152+
echo "Mounted disk identifier: $MOUNTED_DISK"
153+
fi
146154

147155
# Configure layout with AppleScript
148156
echo "Configuring DMG layout..."
@@ -194,51 +202,57 @@ if [ -n "$CI" ]; then
194202
sleep 1
195203
fi
196204

197-
# Unmount with retry logic
205+
# Unmount with retry logic (try mount point first, then disk id in CI)
198206
MAX_RETRIES=5
199207
RETRY_COUNT=0
208+
UNMOUNTED=0
200209
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
201-
# Check if still mounted
202-
if [ ! -d "$MOUNT_POINT" ]; then
203-
echo "DMG already unmounted"
204-
break
205-
fi
206-
207-
# Try to detach
210+
# Try detach by mount point first
208211
if hdiutil detach "$MOUNT_POINT" -force 2>&1; then
209212
echo "DMG unmounted successfully"
213+
UNMOUNTED=1
210214
break
215+
fi
216+
# If mount point is gone but we have disk id, detach by disk (image may still be open)
217+
if [ ! -d "$MOUNT_POINT" ] && [ -n "$MOUNTED_DISK" ]; then
218+
echo "Mount point gone, detaching by disk: $MOUNTED_DISK"
219+
if hdiutil detach "$MOUNTED_DISK" -force 2>&1; then
220+
echo "DMG detached by disk id successfully"
221+
UNMOUNTED=1
222+
break
223+
fi
224+
diskutil unmountDisk force "$MOUNTED_DISK" 2>/dev/null || true
225+
sleep 1
226+
fi
227+
RETRY_COUNT=$((RETRY_COUNT + 1))
228+
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
229+
echo "DMG unmount failed, retrying in 2 seconds... (attempt $RETRY_COUNT/$MAX_RETRIES)"
230+
[ -n "$MOUNTED_DISK" ] && hdiutil detach "$MOUNTED_DISK" -force 2>/dev/null || true
231+
osascript -e 'tell application "Finder" to close every window' 2>/dev/null || true
232+
sleep 2
211233
else
212-
RETRY_COUNT=$((RETRY_COUNT + 1))
213-
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
214-
echo "DMG unmount failed, retrying in 2 seconds... (attempt $RETRY_COUNT/$MAX_RETRIES)"
215-
# Try closing Finder windows again
216-
osascript -e 'tell application "Finder" to close every window' 2>/dev/null || true
234+
echo "Trying alternative unmount methods..."
235+
DISK_NUM="${MOUNTED_DISK:-$(diskutil list | grep -A 1 "${APP_NAME}" | grep -oE 'disk[0-9]+' | head -1)}"
236+
if [ -n "$DISK_NUM" ]; then
237+
echo "Attempting diskutil unmountDisk force $DISK_NUM"
238+
diskutil unmountDisk force "$DISK_NUM" 2>/dev/null || true
239+
sleep 2
240+
echo "Attempting hdiutil detach $DISK_NUM -force"
241+
hdiutil detach "$DISK_NUM" -force 2>/dev/null || true
217242
sleep 2
218-
else
219-
echo "Warning: Failed to unmount DMG after $MAX_RETRIES attempts"
220-
# Try alternative unmount methods
221-
echo "Trying alternative unmount methods..."
222-
# Try by disk number
223-
DISK_NUM=$(diskutil list | grep -A 1 "${APP_NAME}" | grep -o 'disk[0-9]*' | head -1)
224-
if [ -n "$DISK_NUM" ]; then
225-
echo "Attempting to unmount disk: $DISK_NUM"
226-
diskutil unmountDisk force "$DISK_NUM" 2>/dev/null || true
227-
hdiutil detach "$DISK_NUM" -force 2>/dev/null || true
228-
fi
229-
# Final attempt
230-
hdiutil detach "$MOUNT_POINT" -force 2>/dev/null || true
231-
sleep 1
232-
# Check if still mounted
233-
if [ -d "$MOUNT_POINT" ]; then
234-
echo "Error: DMG is still mounted at $MOUNT_POINT"
235-
echo "This may cause the compression step to fail"
236-
else
237-
echo "DMG successfully unmounted using alternative method"
238-
fi
243+
fi
244+
hdiutil detach "$MOUNT_POINT" -force 2>/dev/null || true
245+
if [ ! -d "$MOUNT_POINT" ]; then
246+
echo "DMG unmounted using alternative method"
247+
UNMOUNTED=1
239248
fi
240249
fi
241250
done
251+
# In CI, give the system time to release the image before convert
252+
if [ -n "$CI" ]; then
253+
echo "CI: waiting 5s for image to be released before compression..."
254+
sleep 5
255+
fi
242256

243257
# Convert to compressed DMG
244258
echo "Compressing DMG..."
@@ -248,13 +262,21 @@ if [ -d "$MOUNT_POINT" ]; then
248262
echo "Error: DMG is still mounted at $MOUNT_POINT, cannot compress"
249263
echo "Attempting emergency unmount..."
250264
hdiutil detach "$MOUNT_POINT" -force 2>/dev/null || true
251-
diskutil unmountDisk force "$MOUNT_POINT" 2>/dev/null || true
252-
sleep 2
265+
[ -n "$MOUNTED_DISK" ] && hdiutil detach "$MOUNTED_DISK" -force 2>/dev/null || true
266+
[ -n "$MOUNTED_DISK" ] && diskutil unmountDisk force "$MOUNTED_DISK" 2>/dev/null || true
267+
sleep 3
253268
if [ -d "$MOUNT_POINT" ]; then
254269
echo "Error: Cannot unmount DMG, compression will fail"
255270
exit 1
256271
fi
257272
fi
273+
# If image is still attached (e.g. mount point gone but image open), detach by disk id
274+
if [ -n "$MOUNTED_DISK" ] && hdiutil info 2>/dev/null | grep -qF "${TEMP_DIR}/${DMG_NAME}-temp.dmg"; then
275+
echo "Image still attached, detaching disk $MOUNTED_DISK..."
276+
hdiutil detach "$MOUNTED_DISK" -force 2>/dev/null || true
277+
diskutil unmountDisk force "$MOUNTED_DISK" 2>/dev/null || true
278+
sleep 3
279+
fi
258280

259281
# Verify temp DMG exists
260282
if [ ! -f "${TEMP_DIR}/${DMG_NAME}-temp.dmg" ]; then

0 commit comments

Comments
 (0)