diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 7d83b43..ed3974c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -83,14 +83,7 @@ jobs: - name: Build macOS app - macOS if: ${{ matrix.build == 'macos' }} run: | - chmod +x build-macos.sh - ./build-macos.sh - # cd dist - ls -al dist - rm dist/MDCx - # `upload-artifact`会使文件丢失可执行权限。解决思路,先把`dist`打包成zip,再上传 - zip -r MDCx.app.zip dist - ls -al + bash ./build-macos.sh --create-dmg --version "${{ github.ref_name }}" - name: Build Windows app - Windows if: ${{ matrix.build == 'windows' }} @@ -125,8 +118,8 @@ jobs: uses: svenstaro/upload-release-action@2.7.0 if: ${{ matrix.build == 'macos' }} with: - asset_name: MDCx-$tag-${{ matrix.build }}-${{ matrix.arch }}.app.zip - file: MDCx.app.zip + asset_name: MDCx-$tag-${{ matrix.build }}-${{ matrix.arch }}.dmg + file: dist/MDCx.dmg prerelease: ${{ startsWith(github.ref, 'refs/tags/pre-') }} body: | ${{ steps.get-changelog.outputs.CHANGELOG }} diff --git a/build-macos.sh b/build-macos.sh index 9bc718a..60b1e74 100644 --- a/build-macos.sh +++ b/build-macos.sh @@ -1,6 +1,40 @@ #/bin/bash -# Sample build script for pyinstaller +set -e + +while [[ $# -gt 0 ]] +do + key="$1" + case $key in + --version|-v) + APP_VERSION="$2" + shift + shift + ;; + --create-dmg|-dmg) + CREATE_DMG=true + shift + ;; + --help|-h) + echo "Usage: build-macos.sh [options]" + echo "Options:" + echo " --version, -v Specify the version number. Required!" + echo " --create-dmg, -dmg Create DMG file. Default is false." + exit 0 + ;; + *) + shift + ;; + esac +done + + +# Check if APP_VERSION is set +if [ -z "$APP_VERSION" ]; then + echo "❌ Please specify the version number using --version option!" + exit 1 +fi + appName="MDCx" @@ -19,7 +53,83 @@ pyi-makespec \ rm -rf ./dist +# Find line number by keyword +findLine() { + local file="$1" + local keyword="$2" + local line=$(grep -n "$keyword" "$file" | cut -d: -f1) + echo "$line" +} + +# Insert content after a specific line +insertAfterLine() { + local file="$1" + local line="$2" + local content="$3" + local newContent="" + local i=1 + while IFS= read -r lineContent; do + if [ $i -eq $line ]; then + newContent+="$lineContent\n$content\n" + else + newContent+="$lineContent\n" + fi + i=$((i+1)) + done < "$file" + echo -e "$newContent" +} + +# Add `info_plist` to `MDCx.spec` file +INFO_PLIST=$(cat < MDCx.spec + + +# Build the app pyinstaller MDCx.spec +# Remove unnecessary files rm -rf ./build rm *.spec + + +# Install `create-dmg` if `CREATE_DMG` is true +if [ "$CREATE_DMG" = true ] && ! command -v create-dmg &> /dev/null; then + echo "Installing create-dmg..." + brew install create-dmg + if [ $? -ne 0 ]; then + echo "❌ Failed to install create-dmg!" + exit 1 + fi +fi + + +# Create DMG file +if [ "$CREATE_DMG" = true ]; then + echo "Creating DMG file..." + # https://github.com/create-dmg/create-dmg?tab=readme-ov-file#usage + create-dmg \ + --volname "$appName" \ + --volicon "resources/Img/MDCx.icns" \ + --window-pos 200 120 \ + --window-size 800 400 \ + --icon-size 80 \ + --icon "$appName.app" 300 36 \ + --hide-extension "$appName.app" \ + --app-drop-link 500 36 \ + "dist/$appName.dmg" \ + "dist/$appName.app" + + if [ $? -ne 0 ]; then + echo "❌ Failed to create DMG file!" + exit 1 + fi +fi \ No newline at end of file