name: Alpha Build

on:
  workflow_dispatch:
  push:
    branches: [main]
    tags-ignore: [updater, alpha]
permissions: write-all
env:
  CARGO_INCREMENTAL: 0
  RUST_BACKTRACE: short
concurrency:
  # only allow per workflow per commit (and not pr) to run at a time
  group: "${{ github.workflow }} - ${{ github.head_ref || github.ref }}"
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
  alpha:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-latest
            target: x86_64-pc-windows-msvc
          - os: windows-latest
            target: aarch64-pc-windows-msvc
          - os: macos-latest
            target: aarch64-apple-darwin
          - os: macos-latest
            target: x86_64-apple-darwin
          - os: ubuntu-22.04
            target: x86_64-unknown-linux-gnu

    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Install Rust Stable
        uses: dtolnay/rust-toolchain@stable

      - name: Add Rust Target
        run: rustup target add ${{ matrix.target }}

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: src-tauri
          cache-all-crates: true
          cache-on-failure: true

      - name: Install dependencies (ubuntu only)
        if: matrix.os == 'ubuntu-22.04'
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.1-dev libayatana-appindicator3-dev librsvg2-dev patchelf

      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          run_install: false

      - name: Pnpm install and check
        run: |
          pnpm i
          pnpm check ${{ matrix.target }}

      - name: Tauri build
        uses: tauri-apps/tauri-action@v0
        env:
          NODE_OPTIONS: "--max_old_space_size=4096"
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
          APPLE_CERTIFICATE: ${{ secrets.APPLE_CERTIFICATE }}
          APPLE_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_CERTIFICATE_PASSWORD }}
          APPLE_SIGNING_IDENTITY: ${{ secrets.APPLE_SIGNING_IDENTITY }}
          APPLE_ID: ${{ secrets.APPLE_ID }}
          APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
          APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
        with:
          tagName: alpha
          releaseName: "Clash Verge Rev Alpha"
          releaseBody: "More new features are now supported."
          releaseDraft: false
          prerelease: true
          tauriScript: pnpm
          args: --target ${{ matrix.target }}

      - name: Portable Bundle
        if: matrix.os == 'windows-latest'
        run: pnpm portable ${{ matrix.target }} --alpha
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  alpha-for-linux-arm:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-22.04
            target: aarch64-unknown-linux-gnu
            arch: arm64
          - os: ubuntu-22.04
            target: armv7-unknown-linux-gnueabihf
            arch: armhf
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Install Rust Stable
        uses: dtolnay/rust-toolchain@stable

      - name: Add Rust Target
        run: rustup target add ${{ matrix.target }}

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: src-tauri
          cache-all-crates: true

      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - name: Install pnpm
        uses: pnpm/action-setup@v4
        with:
          run_install: false

      - name: Pnpm install and check
        run: |
          pnpm i
          pnpm check ${{ matrix.target }}

      - name: "Setup for linux"
        run: |-
          sudo ls -lR /etc/apt/

          cat > /tmp/sources.list << EOF
          deb [arch=amd64,i386] http://archive.ubuntu.com/ubuntu jammy main multiverse universe restricted
          deb [arch=amd64,i386] http://archive.ubuntu.com/ubuntu jammy-security main multiverse universe restricted
          deb [arch=amd64,i386] http://archive.ubuntu.com/ubuntu jammy-updates main multiverse universe restricted
          deb [arch=amd64,i386] http://archive.ubuntu.com/ubuntu jammy-backports main multiverse universe restricted

          deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports jammy main multiverse universe restricted
          deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports jammy-security main multiverse universe restricted
          deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports jammy-updates main multiverse universe restricted
          deb [arch=armhf,arm64] http://ports.ubuntu.com/ubuntu-ports jammy-backports main multiverse universe restricted
          EOF

          sudo mv /etc/apt/sources.list /etc/apt/sources.list.default
          sudo mv /tmp/sources.list /etc/apt/sources.list

          sudo dpkg --add-architecture ${{ matrix.arch }}
          sudo apt update

          sudo apt install -y \
            libwebkit2gtk-4.1-dev:${{ matrix.arch }} \
            libayatana-appindicator3-dev:${{ matrix.arch }} \
            libssl-dev:${{ matrix.arch }} \
            patchelf:${{ matrix.arch }} \
            librsvg2-dev:${{ matrix.arch }}

      - name: "Install aarch64 tools"
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          sudo apt install -y \
            gcc-aarch64-linux-gnu \
            g++-aarch64-linux-gnu

      - name: "Install armv7 tools"
        if: matrix.target == 'armv7-unknown-linux-gnueabihf'
        run: |
          sudo apt install -y \
            gcc-arm-linux-gnueabihf \
            g++-arm-linux-gnueabihf

      - name: Build for Linux
        run: |
          export PKG_CONFIG_ALLOW_CROSS=1
          if [ "${{ matrix.target }}" == "aarch64-unknown-linux-gnu" ]; then
            export PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig/:$PKG_CONFIG_PATH
            export PKG_CONFIG_SYSROOT_DIR=/usr/aarch64-linux-gnu/
          elif [ "${{ matrix.target }}" == "armv7-unknown-linux-gnueabihf" ]; then
            export PKG_CONFIG_PATH=/usr/lib/arm-linux-gnueabihf/pkgconfig/:$PKG_CONFIG_PATH
            export PKG_CONFIG_SYSROOT_DIR=/usr/arm-linux-gnueabihf/
          fi
          pnpm build --target ${{ matrix.target }}
        env:
          NODE_OPTIONS: "--max_old_space_size=4096"
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}

      - name: Get Version
        run: |
          sudo apt-get update
          sudo apt-get install jq 
          echo "VERSION=$(cat package.json | jq '.version' | tr -d '"')" >> $GITHUB_ENV
          echo "BUILDTIME=$(TZ=Asia/Shanghai date)" >> $GITHUB_ENV

      - name: Upload Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: alpha
          name: "Clash Verge Rev Alpha"
          body: "More new features are now supported."
          prerelease: true
          token: ${{ secrets.GITHUB_TOKEN }}
          files: |
            src-tauri/target/${{ matrix.target }}/release/bundle/deb/*.deb
            src-tauri/target/${{ matrix.target }}/release/bundle/rpm/*.rpm

  alpha-for-fixed-webview2:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            arch: x64
          - os: windows-latest
            target: aarch64-pc-windows-msvc
            arch: arm64
    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Add Rust Target
        run: rustup target add ${{ matrix.target }}

      - name: Rust Cache
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: src-tauri
          cache-all-crates: true
          cache-on-failure: true

      - name: Install Node
        uses: actions/setup-node@v4
        with:
          node-version: "20"

      - uses: pnpm/action-setup@v4
        name: Install pnpm
        with:
          run_install: false

      - name: Pnpm install and check
        run: |
          pnpm i
          pnpm check ${{ matrix.target }}

      - name: Download WebView2 Runtime
        run: |
          invoke-webrequest -uri https://github.com/westinyang/WebView2RuntimeArchive/releases/download/109.0.1518.78/Microsoft.WebView2.FixedVersionRuntime.109.0.1518.78.${{ matrix.arch }}.cab -outfile Microsoft.WebView2.FixedVersionRuntime.109.0.1518.78.${{ matrix.arch }}.cab
          Expand .\Microsoft.WebView2.FixedVersionRuntime.109.0.1518.78.${{ matrix.arch }}.cab -F:* ./src-tauri
          Remove-Item .\src-tauri\tauri.windows.conf.json
          Rename-Item .\src-tauri\webview2.${{ matrix.arch }}.json tauri.windows.conf.json

      - name: Tauri build
        id: build
        uses: tauri-apps/tauri-action@v0
        env:
          NODE_OPTIONS: "--max_old_space_size=4096"
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_KEY_PASSWORD }}
        with:
          tauriScript: pnpm
          args: --target ${{ matrix.target }}

      - name: Rename
        run: |
          Rename-Item '.\src-tauri\target\${{ matrix.target }}\release\bundle\nsis\Clash Verge_${{steps.build.outputs.appVersion}}_${{ matrix.arch }}-setup.exe' 'Clash Verge_${{steps.build.outputs.appVersion}}_${{ matrix.arch }}_fixed_webview2-setup.exe'
          Rename-Item '.\src-tauri\target\${{ matrix.target }}\release\bundle\nsis\Clash Verge_${{steps.build.outputs.appVersion}}_${{ matrix.arch }}-setup.nsis.zip' 'Clash Verge_${{steps.build.outputs.appVersion}}_${{ matrix.arch }}_fixed_webview2-setup.nsis.zip'
          Rename-Item '.\src-tauri\target\${{ matrix.target }}\release\bundle\nsis\Clash Verge_${{steps.build.outputs.appVersion}}_${{ matrix.arch }}-setup.nsis.zip.sig' 'Clash Verge_${{steps.build.outputs.appVersion}}_${{ matrix.arch }}_fixed_webview2-setup.nsis.zip.sig'

      - name: Upload Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: alpha
          name: "Clash Verge Rev Alpha"
          body: "More new features are now supported."
          prerelease: true
          token: ${{ secrets.GITHUB_TOKEN }}
          files: src-tauri/target/${{ matrix.target }}/release/bundle/nsis/*setup*

      - name: Portable Bundle
        run: pnpm portable-fixed-webview2 ${{ matrix.target }} --alpha
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  update_tag:
    name: Update tag
    runs-on: ubuntu-latest
    needs: [alpha, alpha-for-linux-arm, alpha-for-fixed-webview2]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Set Env
        run: |
          echo "BUILDTIME=$(TZ=Asia/Shanghai date)" >> $GITHUB_ENV
        shell: bash

      - name: Update Tag
        uses: richardsimko/update-tag@v1
        with:
          tag_name: alpha
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - run: |
          cat > release.txt << 'EOF'
          ## 我应该下载哪个版本？

          ### MacOS
          - MacOS intel芯片: x64.dmg
          - MacOS apple M芯片: aarch64.dmg

          ### Linux
          - Linux 64位: amd64.deb/amd64.rpm
          - Linux arm64 architecture: arm64.deb/aarch64.rpm
          - Linux armv7架构: armhf.deb/armhfp.rpm

          ### Windows (Win7 用户请查看下面FAQ中的解决方案)
          #### 正常版本(推荐)
          - 64位: x64-setup.exe
          - arm64架构: arm64-setup.exe
          #### 便携版问题很多不再提供
          #### 内置Webview2版(体积较大，仅在企业版系统或无法安装webview2时使用)
          - 64位: x64_fixed_webview2-setup.exe
          - arm64架构: arm64_fixed_webview2-setup.exe

          ### FAQ

          - [常见问题](https://clash-verge-rev.github.io/faq/windows.html)

          ### 稳定机场VPN推荐
          - [狗狗加速](https://verge.dginv.click/#/register?code=oaxsAGo6)

          Created at  ${{ env.BUILDTIME }}.
          EOF

      - name: Upload Release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: alpha
          name: "Clash Verge Rev Alpha"
          body_path: release.txt
          prerelease: true
          token: ${{ secrets.GITHUB_TOKEN }}
          generate_release_notes: true
