name: Create Release

on:
  push:
    tags:
      - 'v*'

jobs:
  build-and-release:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      with:
        fetch-depth: 0
    
    - name: Get version from tag
      id: get_version
      run: |
        VERSION="${GITHUB_REF#refs/tags/}"
        echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
        echo "VERSION_NUMBER=${VERSION#v}" >> $GITHUB_OUTPUT
    
    - name: Prepare release files
      run: |
        mkdir -p dist
        rsync -av --exclude='.git' --exclude='.github' --exclude='node_modules' --exclude='dist' . dist/
    
    - name: Package release
      id: package  # 为步骤设置ID，以便后续引用其输出
      run: |
        cd dist
        ASSET_NAME="TTenYX-BPBooks-${{ steps.get_version.outputs.VERSION_NUMBER }}.zip"
        # 进入dist目录，创建压缩包
        zip -r "../${ASSET_NAME}" .
        
        cd ../
        
        ASSET_PATH="$(pwd)/${ASSET_NAME}"
        
        #将文件路径设置为步骤输出，供后续步骤使用
        echo "asset_path=${ASSET_PATH}" >> $GITHUB_OUTPUT
        
        # 验证（调试用）
        echo "✅ 打包完成。文件绝对路径为：${ASSET_PATH}"
        # 列出生成的文件，用于调试
        ls -la *.zip
    
    - name: Create Release
      uses: softprops/action-gh-release@v1
      with:
        name: '[TTenYX]全流程蓝图包-${{ steps.get_version.outputs.VERSION }}'
        draft: false
        prerelease: false
        generate_release_notes: true
        files: |
          ${{ steps.package.outputs.asset_path }}
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
