name: Build Executables

on:
  workflow_dispatch:
    inputs:
      version:
        description: 'Version number (e.g. 1.0.9)'
        required: true
        default: '1.7.06'

permissions:
  contents: write
  actions: write
  packages: write

jobs:
  create-tag:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0  # 获取所有标签
          
      - name: Delete existing tag if exists
        run: |
          if git ls-remote --tags origin | grep -q "refs/tags/v${{ github.event.inputs.version }}"; then
            git push origin --delete "v${{ github.event.inputs.version }}" || true
            git tag -d "v${{ github.event.inputs.version }}" || true
          fi
          
      - name: Create Tag
        run: |
          git tag "v${{ github.event.inputs.version }}"
          git push origin "v${{ github.event.inputs.version }}"

  build-windows:
    needs: create-tag
    runs-on: windows-latest
    
    steps:
    - uses: actions/checkout@v2
    
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.x'
    
    - name: Set version
      shell: bash
      run: echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
    
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install pyinstaller
        pip install -r requirements.txt
        
    - name: Build EXE
      run: |
        pyinstaller build.spec
         
    - name: Upload Windows artifact
      uses: actions/upload-artifact@v4
      with:
        name: CursorFreeVIP_${{ env.VERSION }}_windows.exe
        path: dist/CursorFreeVIP_${{ env.VERSION }}_windows.exe

  build-macos-arm64:
    needs: create-tag
    runs-on: macos-latest
    
    steps:
    - uses: actions/checkout@v2
    
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.x'
    
    - name: Set version
      shell: bash
      run: echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
    
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install pyinstaller
        pip install -r requirements.txt
        
    - name: Build MacOS ARM executable
      run: |
        pyinstaller build.spec
        mv "dist/CursorFreeVIP_${{ env.VERSION }}_mac" "dist/CursorFreeVIP_${{ env.VERSION }}_mac_arm64"

    - name: Upload MacOS ARM artifact
      uses: actions/upload-artifact@v4
      with:
        name: CursorFreeVIP_${{ env.VERSION }}_mac_arm64
        path: dist/CursorFreeVIP_${{ env.VERSION }}_mac_arm64

  build-linux-x64:
    needs: create-tag
    runs-on: ubuntu-22.04
    
    steps:
    - uses: actions/checkout@v2
    
    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.x'
    
    - name: Set version
      shell: bash
      run: echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
    
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install pyinstaller
        pip install -r requirements.txt
        
    - name: Build Linux x64 executable
      env:
        VERSION: ${{ env.VERSION }}
      run: |
        pyinstaller build.spec
        mv "dist/CursorFreeVIP_${{ env.VERSION }}_linux" "dist/CursorFreeVIP_${{ env.VERSION }}_linux_x64"
        echo "Contents of dist directory:"
        ls -la dist/
        
    - name: Upload Linux x64 artifact
      uses: actions/upload-artifact@v4
      with:
        name: CursorFreeVIP_${{ env.VERSION }}_linux_x64
        path: dist/CursorFreeVIP_${{ env.VERSION }}_linux_x64

  build-linux-arm64:
    needs: create-tag
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v2
    
    - name: Set up QEMU
      uses: docker/setup-qemu-action@v2
      with:
        platforms: arm64
    
    - name: Set version
      shell: bash
      run: echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
    
    - name: Build in ARM64 Docker container
      run: |
        docker run --rm --platform linux/arm64 -v ${{ github.workspace }}:/app -w /app arm64v8/python:3.9-slim bash -c "
          apt-get update && apt-get install -y build-essential
          pip install --upgrade pip
          pip install pyinstaller
          pip install -r requirements.txt
          python -m PyInstaller build.spec
          mv /app/dist/CursorFreeVIP_${{ env.VERSION }}_linux /app/dist/CursorFreeVIP_${{ env.VERSION }}_linux_arm64
        "
        echo "Contents of dist directory:"
        ls -la dist/
        
    - name: Upload Linux ARM64 artifact
      uses: actions/upload-artifact@v4
      with:
        name: CursorFreeVIP_${{ env.VERSION }}_linux_arm64
        path: dist/CursorFreeVIP_${{ env.VERSION }}_linux_arm64

  build-macos-intel:
    needs: create-tag
    runs-on: macos-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up Python
      uses: actions/setup-python@v2
      with:
        python-version: '3.x'

    - name: Set version
      shell: bash
      run: echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV

    - name: Install dependencies
      run: |
        arch -x86_64 pip3 install --upgrade pip
        arch -x86_64 pip3 install pyinstaller
        arch -x86_64 pip3 install -r requirements.txt

    - name: Build MacOS Intel executable
      env:
        TARGET_ARCH: 'x86_64'
        VERSION: ${{ env.VERSION }}
      run: |
        arch -x86_64 python3 -m PyInstaller build.spec
        mv "dist/CursorFreeVIP_${{ env.VERSION }}_mac" "dist/CursorFreeVIP_${{ env.VERSION }}_mac_intel"
        
    - name: Upload MacOS Intel artifact
      uses: actions/upload-artifact@v4
      with:
        name: CursorFreeVIP_${{ env.VERSION }}_mac_intel
        path: dist/CursorFreeVIP_${{ env.VERSION }}_mac_intel

  create-release:
    needs: [build-windows, build-macos-arm64, build-linux-x64, build-linux-arm64, build-macos-intel]
    runs-on: ubuntu-22.04
    
    steps:
      - name: Get version
        shell: bash
        run: echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
          
      - name: Download all artifacts
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: Prepare release files
        run: |
          cd artifacts
          echo "Contents of artifacts directory:"
          ls -la
          echo "Contents of subdirectories:"
          ls -la */
          
      - name: Create Release
        uses: softprops/action-gh-release@v1
        with:
          tag_name: v${{ env.VERSION }}
          files: |
            artifacts/CursorFreeVIP_${{ env.VERSION }}_windows.exe/CursorFreeVIP_${{ env.VERSION }}_windows.exe
            artifacts/CursorFreeVIP_${{ env.VERSION }}_mac_arm64/CursorFreeVIP_${{ env.VERSION }}_mac_arm64
            artifacts/CursorFreeVIP_${{ env.VERSION }}_linux_x64/CursorFreeVIP_${{ env.VERSION }}_linux_x64
            artifacts/CursorFreeVIP_${{ env.VERSION }}_linux_arm64/CursorFreeVIP_${{ env.VERSION }}_linux_arm64
            artifacts/CursorFreeVIP_${{ env.VERSION }}_mac_intel/CursorFreeVIP_${{ env.VERSION }}_mac_intel
          draft: false
          prerelease: false
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
