name: Development desktop builds

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
    paths-ignore: 
    - "Translations/*"
    - "installer/*.pot"
    - "installer/po/*"

concurrency:
  group: ci-${{github.actor}}-${{github.head_ref || github.run_number}}-${{github.ref}}-devdesktop
  cancel-in-progress: true

env:
  GODOT_VERSION: 4.3
  EXPORT_NAME: Pixelorama

jobs:
  export-windows:
    name: Windows Export 🗔
    runs-on: ubuntu-latest
    container:
      image: docker://barichello/godot-ci:4.3
    steps:
      - name: Setup WINE and rcedit 🍷
        run: |
          chown root:root -R ~
          godot --headless --quit
          echo 'export/windows/wine = "/usr/bin/wine64-stable"' >> ~/.config/godot/editor_settings-4.3.tres
          echo 'export/windows/rcedit = "/opt/rcedit.exe"' >> ~/.config/godot/editor_settings-4.3.tres
      - name: Checkout 🛎️
        uses: actions/checkout@v4
        with:
          submodules: true
      - name: Setup 💻
        run: |
          mkdir -v -p build/${EXPORT_NAME}-Windows-64bit  ~/.local/share/godot/export_templates
          mv /root/.local/share/godot/export_templates/${GODOT_VERSION}.stable ~/.local/share/godot/export_templates/${GODOT_VERSION}.stable
      - name: Import resources once 📦
        continue-on-error: true
        run: godot --headless -v --import
      - name: Windows Build 🔧
        run: godot --headless -v --export-release "Windows Desktop 64-bit" ./build/${EXPORT_NAME}-Windows-64bit/$EXPORT_NAME.exe
      - name: Copy pixelorama_data folder 📁
        run: |
          cp -R ./pixelorama_data ./build/${EXPORT_NAME}-Windows-64bit
          rm ./build/${EXPORT_NAME}-Windows-64bit/pixelorama_data/.gdignore
      - name: Upload Artifact 🚀
        uses: actions/upload-artifact@v4
        with:
          name: ${{env.EXPORT_NAME}}-Windows-64bit
          path: ./build/${{env.EXPORT_NAME}}-Windows-64bit/
          retention-days: 14

  export-linux:
    name: Linux Export 🐧
    runs-on: ubuntu-latest
    container:
      image: docker://barichello/godot-ci:4.3
    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@v4
        with:
          submodules: true
      - name: Setup 💻
        run: |
          mkdir -v -p build/${EXPORT_NAME}-Linux-64bit build/${EXPORT_NAME}-Linux-ARM64 ~/.local/share/godot/export_templates
          mv /root/.local/share/godot/export_templates/${GODOT_VERSION}.stable ~/.local/share/godot/export_templates/${GODOT_VERSION}.stable
      - name: Import resources once 📦
        continue-on-error: true
        run: godot --headless -v --import
      - name: Linux Build 🔧
        run: |
          godot --headless -v --export-release "Linux 64-bit" ./build/${EXPORT_NAME}-Linux-64bit/$EXPORT_NAME.x86_64
          godot --headless -v --export-release "Linux ARM64" ./build/${EXPORT_NAME}-Linux-ARM64/${EXPORT_NAME}.arm64
      - name: Give execute permission ☑️
        run: |
          chmod +x ./build/${EXPORT_NAME}-Linux-64bit/$EXPORT_NAME.x86_64
          chmod +x ./build/${EXPORT_NAME}-Linux-ARM64/${EXPORT_NAME}.arm64
      - name: Copy pixelorama_data folder 📁
        run: |
          rm ./pixelorama_data/.gdignore
          cp -R ./pixelorama_data ./build/${EXPORT_NAME}-Linux-64bit
          cp -R ./pixelorama_data ./build/${EXPORT_NAME}-Linux-ARM64
      - name: Create tar.gz archive 🗜️
        run: |
          cd build
          tar zcvf ${EXPORT_NAME}-Linux-64bit.tar.gz ${EXPORT_NAME}-Linux-64bit
          tar zcvf ${EXPORT_NAME}-Linux-ARM64.tar.gz ${EXPORT_NAME}-Linux-ARM64
      - name: Upload Linux x86_64 Artifact 🚀
        uses: actions/upload-artifact@v4
        with:
          name: ${{env.EXPORT_NAME}}-Linux-64bit
          path: ./build/${{env.EXPORT_NAME}}-Linux-64bit.tar.gz
          retention-days: 14
      - name: Upload Linux ARM64 Artifact 🚀
        uses: actions/upload-artifact@v4
        with:
          name: ${{env.EXPORT_NAME}}-Linux-ARM64
          path: ./build/${{env.EXPORT_NAME}}-Linux-ARM64.tar.gz
          retention-days: 14

  export-mac:
    name: Mac Export 🍎
    runs-on: macos-latest
    steps:
      - name: Checkout 🛎️
        uses: actions/checkout@v4
        with:
          submodules: true
      - name: Setup environment 🔧
        run: |
          export GODOT_VERSION=${GODOT_VERSION}
          export EXPORT_NAME=${EXPORT_NAME}
      - name: Download and extract export templates 💾
        run: |
          mkdir -v -p "/Users/runner/Library/Application Support/Godot/export_templates/${GODOT_VERSION}.stable"
          wget https://github.com/godotengine/godot-builds/releases/download/${GODOT_VERSION}-stable/Godot_v${GODOT_VERSION}-stable_export_templates.tpz
          unzip -a Godot_v${GODOT_VERSION}-stable_export_templates.tpz
          mv ./templates/* "/Users/runner/Library/Application Support/Godot/export_templates/${GODOT_VERSION}.stable"
      - name: Download Godot headless binary 🤖
        run: |
          wget https://github.com/godotengine/godot/releases/download/${GODOT_VERSION}-stable/Godot_v${GODOT_VERSION}-stable_macos.universal.zip
          unzip -a Godot_v${GODOT_VERSION}-stable_macos.universal.zip
      - name: Setup 💻
        run: mkdir -v -p ./build/mac
      - name: Import resources once 📦
        continue-on-error: true
        run: ./Godot.app/Contents/MacOS/Godot --headless -v --import
      - name: Mac Build 🔧
        run: |
          chown runner ./Godot.app/Contents/MacOS/Godot
          chmod +x ./Godot.app/Contents/MacOS/Godot
          ./Godot.app/Contents/MacOS/Godot --headless -v --export-release "macOS" ./build/mac/${EXPORT_NAME}.zip
      - name: Make application executable 🔧
        run: |
          unzip -a ./build/mac/${EXPORT_NAME}.zip -d ./build/mac
          chmod +x ./build/mac/${EXPORT_NAME}.app/Contents/MacOS/${EXPORT_NAME}
          rm ./build/mac/${EXPORT_NAME}.zip
      - name: Fix application icon 🖼
        run: sips -s format icns ./build/mac/${EXPORT_NAME}.app/Contents/Resources/icon.icns --out ./build/mac/${EXPORT_NAME}.app/Contents/Resources/icon.icns
      - name: Copy pixelorama_data folder 📁
        run: |
          cp -R ./pixelorama_data ./build/mac/${EXPORT_NAME}.app/Contents/Resources
          rm ./build/mac/${EXPORT_NAME}.app/Contents/Resources/pixelorama_data/.gdignore
      - name: Ad-hoc signing ✍️
        run: codesign -s - --force --deep ./build/mac/${EXPORT_NAME}.app
      - name: Create DMG archive 🔧
        run: hdiutil create -srcfolder ./build/mac -fs HFS+ -volname ${EXPORT_NAME} ./build/mac/${EXPORT_NAME}.dmg
      - name: Upload Artifact 🚀
        uses: actions/upload-artifact@v4
        with:
          name: ${{env.EXPORT_NAME}}-Mac
          path: ./build/mac/${{env.EXPORT_NAME}}.dmg
          retention-days: 14
