Skip to content

CodexBar 是一个 macOS 菜单栏应用,用于查看本机 Codex 账号状态、额度、Token 用量、详细统计信息

feat: 支持 DMG 布局模板生成与复用

Bobaeb4b4f571e7
diff --git a/Config/BuildNumber.xcconfig b/Config/BuildNumber.xcconfig
index 1185635..ac86891 100644
--- a/Config/BuildNumber.xcconfig
+++ b/Config/BuildNumber.xcconfig
@@ -1,3 +1,3 @@
// 请勿手动修改
// Do not modify manually
-CURRENT_PROJECT_VERSION = 314
\ No newline at end of file
+CURRENT_PROJECT_VERSION = 315
\ No newline at end of file
diff --git a/Scripts/dmg.sh b/Scripts/dmg.sh
index ece08ba..01b922f 100755
--- a/Scripts/dmg.sh
+++ b/Scripts/dmg.sh
@@ -4,18 +4,85 @@ set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
BUILD_DIR="${BUILD_DIR:-${PROJECT_DIR}/Build}"
+BACKGROUND_FILE="${SCRIPT_DIR}/resources/arrow.tiff"
+LAYOUT_FILE="${SCRIPT_DIR}/resources/layout"
+COPY_LAYOUT=0
-APP_PATH="${1:-}"
-OUTPUT_PATH="${2:-}"
+usage() {
+ cat <<USAGE
+usage: Scripts/dmg.sh [options] [App.app] [Output.dmg]
+
+Options:
+ --layout PATH Layout template to read, or save with --copy-layout
+ Defaults to Scripts/resources/layout
+ --copy-layout Configure the layout with Finder, save it, and build the DMG
+ -h, --help Show this help
+
+Without --copy-layout, the template is copied without calling Finder
+The template must match the App name, volume name, and .background/arrow.tiff path
+Relative paths are resolved from the project directory
+
+Examples:
+ Scripts/dmg.sh --copy-layout
+ Scripts/dmg.sh
+ Scripts/dmg.sh --layout path/to/layout
+USAGE
+}
+
+POSITIONAL_ARGS=()
+while [[ "$#" -gt 0 ]]; do
+ case "$1" in
+ --copy-layout)
+ COPY_LAYOUT=1
+ shift
+ ;;
+ --layout)
+ if [[ -z "${2:-}" || "${2}" == -* ]]; then
+ echo "error: --layout requires a path" >&2
+ exit 1
+ fi
+ LAYOUT_FILE="$2"
+ shift 2
+ ;;
+ -h|--help)
+ usage
+ exit 0
+ ;;
+ --)
+ shift
+ POSITIONAL_ARGS+=("$@")
+ break
+ ;;
+ -*)
+ echo "error: unknown option $1" >&2
+ exit 1
+ ;;
+ *)
+ POSITIONAL_ARGS+=("$1")
+ shift
+ ;;
+ esac
+done
+
+if [[ "${#POSITIONAL_ARGS[@]}" -gt 2 ]]; then
+ usage >&2
+ exit 1
+fi
+
+APP_PATH="${POSITIONAL_ARGS[0]:-}"
+OUTPUT_PATH="${POSITIONAL_ARGS[1]:-}"
if [[ "${BUILD_DIR}" != /* ]]; then
BUILD_DIR="${PROJECT_DIR}/${BUILD_DIR}"
fi
+if [[ "${LAYOUT_FILE}" != /* ]]; then
+ LAYOUT_FILE="${PROJECT_DIR}/${LAYOUT_FILE}"
+fi
if [[ -z "${APP_PATH}" ]]; then
if [[ ! -d "${BUILD_DIR}" ]]; then
echo "error: build directory does not exist: ${BUILD_DIR}" >&2
- echo "usage: Scripts/dmg.sh [App.app] [Output.dmg]" >&2
+ usage >&2
exit 1
fi
@@ -26,7 +93,7 @@ if [[ -z "${APP_PATH}" ]]; then
if [[ "${#apps[@]}" -eq 0 ]]; then
echo "error: no .app found in build directory: ${BUILD_DIR}" >&2
- echo "usage: Scripts/dmg.sh [App.app] [Output.dmg]" >&2
+ usage >&2
exit 1
fi
@@ -68,6 +135,7 @@ RW_DMG="${DIST_DIR}/${APP_NAME}-${VERSION}-layout-rw.dmg"
TEMP_OUTPUT_PATH="${OUTPUT_PATH%.dmg}.tmp.dmg"
STAGING_DIR="$(mktemp -d "/private/tmp/${APP_NAME}DMGStage.XXXXXX")"
MOUNT_POINT=""
+LAYOUT_TEMP=""
cleanup() {
if [[ -n "${MOUNT_POINT}" ]]; then
@@ -76,12 +144,15 @@ cleanup() {
rm -rf "${STAGING_DIR}"
rm -f "${RW_DMG}" "${TEMP_OUTPUT_PATH}"
+ if [[ -n "${LAYOUT_TEMP}" ]]; then
+ rm -f "${LAYOUT_TEMP}"
+ fi
}
-trap cleanup EXIT
+trap 'rm -rf "${STAGING_DIR}"' EXIT
require_command() {
if ! command -v "$1" >/dev/null 2>&1; then
- echo "error: missing command: $1" >&2
+ echo "error: missing command $1" >&2
exit 1
fi
}
@@ -94,26 +165,86 @@ run_quietly() {
fi
}
+validate_layout() {
+ if [[ ! -s "$1" || "$(od -An -tx1 -N8 "$1" | tr -d '[:space:]')" != "0000000142756431" ]]; then
+ echo "error: missing or invalid layout $1" >&2
+ echo " use --copy-layout to create a template" >&2
+ return 1
+ fi
+}
+
+unmount_app_dmgs() {
+ local image_info="${STAGING_DIR}/mounted-images.plist"
+ local image_count index image_path device
+
+ hdiutil info -plist > "${image_info}"
+ image_count="$(plutil -extract images raw -expect array "${image_info}")"
+
+ for ((index = 0; index < image_count; index++)); do
+ image_path="$(plutil -extract "images.${index}.image-path" raw -expect string "${image_info}")"
+ case "${image_path##*/}" in
+ "${APP_NAME}.dmg"|"${APP_NAME}-v"*.dmg|"${APP_NAME}-"*-layout-rw.dmg)
+ ;;
+ *)
+ continue
+ ;;
+ esac
+
+ device="$(plutil -extract "images.${index}.system-entities.0.dev-entry" raw -expect string "${image_info}")"
+ run_quietly diskutil eject "${device}"
+ done
+
+ rm -f "${image_info}"
+}
+
require_command diskutil
-require_command osascript
+require_command hdiutil
+require_command plutil
require_command ditto
+if [[ "${COPY_LAYOUT}" == "1" ]]; then
+ require_command osascript
+fi
+
+if [[ ! -f "${BACKGROUND_FILE}" ]]; then
+ echo "error: missing DMG background ${BACKGROUND_FILE}" >&2
+ exit 1
+fi
+
+if [[ "${COPY_LAYOUT}" == "1" ]]; then
+ mkdir -p "$(dirname "${LAYOUT_FILE}")"
+ if [[ -d "${LAYOUT_FILE}" || ! -w "$(dirname "${LAYOUT_FILE}")" ]]; then
+ echo "error: cannot save layout to ${LAYOUT_FILE}" >&2
+ exit 1
+ fi
+else
+ validate_layout "${LAYOUT_FILE}"
+ cp "${LAYOUT_FILE}" "${STAGING_DIR}/.DS_Store"
+fi
mkdir -p "${DIST_DIR}"
-rm -f "${RW_DMG}" "${TEMP_OUTPUT_PATH}"
+echo "==> Unmounting DMG"
+unmount_app_dmgs
+
+echo "==> Removing DMG"
+find "${DIST_DIR}" -maxdepth 1 -type f -name '*.dmg' -delete
+trap cleanup EXIT
-echo "==> Preparing DMG contents"
+echo "==> Preparing DMG"
ditto "${APP_PATH}" "${STAGING_DIR}/${APP_NAME}.app"
+mkdir -p "${STAGING_DIR}/.background"
+ditto "${BACKGROUND_FILE}" "${STAGING_DIR}/.background/arrow.tiff"
-echo "==> Creating writable DMG"
+echo "==> Creating DMG"
run_quietly diskutil image create from \
--format RAW \
--volumeName "${VOLUME_NAME}" \
"${STAGING_DIR}" \
"${RW_DMG}"
-echo "==> Mounting writable DMG"
+echo "==> Mounting DMG"
MOUNT_POINT="${STAGING_DIR}/mount"
run_quietly diskutil image attach \
+ --mountOptions nobrowse \
--mountPoint "${MOUNT_POINT}" \
"${RW_DMG}"
@@ -122,18 +253,16 @@ if [[ -z "${MOUNT_POINT}" || ! -d "${MOUNT_POINT}" ]]; then
exit 1
fi
-echo "==> Writing Finder layout"
-osascript <<APPLESCRIPT
+ln -s /Applications "${MOUNT_POINT}/Applications"
+
+if [[ "${COPY_LAYOUT}" == "1" ]]; then
+ echo "==> Writing Layout"
+ osascript >/dev/null <<APPLESCRIPT
set mountFolder to POSIX file "${MOUNT_POINT}" as alias
-set applicationsFolder to POSIX file "/Applications" as alias
tell application "Finder"
open mountFolder
delay 1
- if exists item "Applications" of mountFolder then
- delete item "Applications" of mountFolder
- end if
- make new alias file to applicationsFolder at mountFolder with properties {name:"Applications"}
set targetWindow to front Finder window
set current view of targetWindow to icon view
set toolbar visible of targetWindow to false
@@ -143,8 +272,10 @@ tell application "Finder"
set arrangement of theViewOptions to not arranged
set icon size of theViewOptions to 96
set text size of theViewOptions to 13
- set position of item "${APP_NAME}.app" of mountFolder to {170, 110}
- set position of item "Applications" of mountFolder to {350, 110}
+ set backgroundFile to get file ".background:arrow.tiff" of mountFolder
+ set background picture of theViewOptions to backgroundFile
+ set position of item "${APP_NAME}.app" of mountFolder to {160, 110}
+ set position of item "Applications" of mountFolder to {360, 110}
update mountFolder without registering applications
delay 2
try
@@ -152,19 +283,32 @@ tell application "Finder"
end try
end tell
APPLESCRIPT
-
-DS_STORE="${MOUNT_POINT}/.DS_Store"
-if [[ ! -s "${DS_STORE}" ]]; then
- echo "warning: .DS_Store was not written; the Finder window layout will not take effect" >&2
else
- echo "==> Finder layout saved"
+ echo "==> Applying Layout"
+ cp "${STAGING_DIR}/.DS_Store" "${MOUNT_POINT}/.DS_Store"
+ validate_layout "${MOUNT_POINT}/.DS_Store"
fi
sync
run_quietly diskutil eject "${MOUNT_POINT}"
MOUNT_POINT=""
-echo "==> Creating compressed DMG"
+if [[ "${COPY_LAYOUT}" == "1" ]]; then
+ # Finder may finish writing the layout while the volume is being ejected
+ MOUNT_POINT="${STAGING_DIR}/mount"
+ run_quietly diskutil image attach \
+ --readOnly \
+ --mountOptions nobrowse \
+ --mountPoint "${MOUNT_POINT}" \
+ "${RW_DMG}"
+ validate_layout "${MOUNT_POINT}/.DS_Store"
+ LAYOUT_TEMP="$(mktemp "${LAYOUT_FILE}.XXXXXX")"
+ cp "${MOUNT_POINT}/.DS_Store" "${LAYOUT_TEMP}"
+ run_quietly diskutil eject "${MOUNT_POINT}"
+ MOUNT_POINT=""
+fi
+
+echo "==> Compressed DMG"
run_quietly diskutil image create from \
--format UDZO \
"${RW_DMG}" \
@@ -172,6 +316,12 @@ run_quietly diskutil image create from \
mv -f "${TEMP_OUTPUT_PATH}" "${OUTPUT_PATH}"
+if [[ "${COPY_LAYOUT}" == "1" ]]; then
+ mv -f "${LAYOUT_TEMP}" "${LAYOUT_FILE}"
+ LAYOUT_TEMP=""
+ echo "==> Layout saved to ${LAYOUT_FILE}"
+fi
+
echo "==> Created ${OUTPUT_PATH}"
CHECKSUM="$(shasum -a 256 "${OUTPUT_PATH}" | awk '{print $1}')"
echo " Version: ${VERSION} (${BUILD})"
diff --git a/Scripts/resources/arrow.tiff b/Scripts/resources/arrow.tiff
new file mode 100644
index 0000000..ba2e662
Binary files /dev/null and b/Scripts/resources/arrow.tiff differ
diff --git a/Scripts/resources/layout b/Scripts/resources/layout
new file mode 100644
index 0000000..8d91b6f
Binary files /dev/null and b/Scripts/resources/layout differ