
name: "Release To HF Win"

on:
  push:
    branches:
      - dev_comfyui_v2
    paths:
      - ".github/workflows/package-and-upload-new-windows.yaml"
  # workflow_dispatch:
  #   inputs:
  #     git_tag:
  #       description: 'Git Branch Tag'
  #       required: true
  #       type: string
  #       default: "main"
      # version:
      #   description: 'Release Version Tag'
      #   required: true
      #   type: string
      #   default: "beta"

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{ inputs.git_tag }}

      - uses: pnpm/action-setup@v2
        with:
          version: 9
          run_install: false

      - uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: "pnpm"
          cache-dependency-path: "web/pnpm-lock.yaml"

      - working-directory: "./web"
        run: pnpm install

      - working-directory: "./web"
        run: pnpm turbo run build --filter=web
        env:
          NEXT_PUBLIC_API_URL: ${{ secrets.NEXT_PUBLIC_API_URL }}

      - uses: actions/upload-artifact@v3
        with:
          name: web-build
          path: web/apps/web/dist
    
  package_shellagent_windows:
    permissions:
      contents: "write"
      packages: "write"
      pull-requests: "read"
    needs: build 
    runs-on: windows-latest
    steps:
      - uses: actions/checkout@v4
        with:
          # ref: ${{ inputs.git_tag }}
          ref: dev_comfyui_v2
          fetch-depth: 0
          persist-credentials: false
          submodules: recursive
      - uses: actions/setup-python@v5
        with:
          python-version: 3.10.10

      - name: Download web-build artifact
        uses: actions/download-artifact@v3
        with:
          name: web-build
          path: ./servers/web-build

      - shell: bash
        run: |
          cd ..
          cp -r ShellAgent ShellAgent_copy

          curl -L https://huggingface.co/XuminYu/git_embed/resolve/main/git.7z?download=true -o git.7z
          "C:\Program Files\7-Zip\7z.exe" x git.7z -ogit

          curl https://www.python.org/ftp/python/3.10.10/python-3.10.10-embed-amd64.zip -o python_embeded.zip
          unzip python_embeded.zip -d python_embeded
          
          cd python_embeded

          echo 'import site' >> ./python310._pth
          curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
          ./python.exe get-pip.py
          cp -r ../ShellAgent/.ci/python_dependencies_wins/include ./include
          cp -r ../ShellAgent/.ci/python_dependencies_wins/Library ./Library
          cp -r ../ShellAgent/.ci/python_dependencies_wins/libs ./libs
          sed -i '1i../ShellAgent' ./python310._pth
          
          
          cd ..

          mkdir ShellAgent_windows_portable
          mv python_embeded ShellAgent_windows_portable
          mv git ShellAgent_windows_portable
          mv ShellAgent_copy ShellAgent_windows_portable/ShellAgent

          cd ShellAgent_windows_portable
          cp -r ShellAgent/.ci/windows_base_files/* ./

          cd ShellAgent

          ../python_embeded/python.exe -m pip install poetry
          ../python_embeded/python.exe -m pip install -e .

          cd ..
          cd ..

          "C:\Program Files\7-Zip\7z.exe" a -t7z -m0=lzma2 -mx=8 -mfb=64 -md=32m -ms=on -mf=BCJ2 ShellAgent_windows_portable.7z ShellAgent_windows_portable/*
          mv ShellAgent_windows_portable.7z ShellAgent/ShellAgent_windows_portable.7z

          ls

      - name: Install Hugging Face Hub SDK
        run: pip install huggingface_hub

      - name: Upload binaries to Hugging Face
        run: |
          from huggingface_hub import HfApi
          api = HfApi()

          # Set repository and file paths
          repo_id = "myshell-ai/ShellAgent"
          file_path = "ShellAgent_windows_portable.7z"

          # Upload the file to Hugging Face repository
          api.upload_file(
              path_or_fileobj=file_path,
              # path_in_repo="ShellAgent_${{ inputs.version }}.7z",
              path_in_repo="ShellAgent_win_v1.0.0.7z",
              repo_id=repo_id,
              repo_type="model",  # or 'dataset' depending on your use case
              token="${{ secrets.HF_TOKEN }}"
          )
        shell: python
