name: WebAssembly

on:
  workflow_dispatch:
  push:
    paths:
      - 'src/**'
      - 'examples/**'
      - '.github/workflows/webassembly.yml'
  pull_request:
    paths:
      - 'src/**'
      - 'examples/**'
      - '.github/workflows/webassembly.yml'
  release:
    types: [published]

jobs:
  build:
    runs-on: windows-latest
    
    env:
      RELEASE_NAME: raylib-dev_webassembly
    
    steps:
    - name: Checkout
      uses: actions/checkout@master
  
    - name: Setup emsdk
      uses: mymindstorm/setup-emsdk@v14
      with:
        version: 3.1.71
        actions-cache-folder: 'emsdk-cache'
        
    - name: Setup Release Version
      run: |
        echo "RELEASE_NAME=raylib-${{ github.event.release.tag_name }}_webassembly" >> $GITHUB_ENV
      shell: bash
      if: github.event_name == 'release' && github.event.action == 'published'

    - name: Setup Environment
      run: | 
        mkdir build
        cd build
        mkdir ${{ env.RELEASE_NAME }}
        cd ${{ env.RELEASE_NAME }}
        mkdir include
        mkdir lib
        cd ../..
   
    - name: Build Library
      run: |
        cd src
        emcc -v
        make PLATFORM=PLATFORM_WEB EMSDK_PATH="D:/a/raylib/raylib/emsdk-cache/emsdk-main" RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib" -B
        cd ..
  
    - name: Generate Artifacts
      run: |
        copy /Y .\src\raylib.h .\build\${{ env.RELEASE_NAME }}\include\raylib.h
        copy /Y .\src\raymath.h .\build\${{ env.RELEASE_NAME }}\include\raymath.h
        copy /Y .\src\rlgl.h .\build\${{ env.RELEASE_NAME }}\include\rlgl.h
        copy /Y .\CHANGELOG .\build/${{ env.RELEASE_NAME }}\CHANGELOG
        copy /Y .\README.md .\build\${{ env.RELEASE_NAME }}\README.md
        copy /Y .\LICENSE .\build\${{ env.RELEASE_NAME }}\LICENSE
        cd build
        7z a ./${{ env.RELEASE_NAME }}.zip ./${{ env.RELEASE_NAME }}
        dir
      shell: cmd
        
    - name: Upload Artifacts
      uses: actions/upload-artifact@v4
      with:
        name: ${{ env.RELEASE_NAME }}.zip
        path: ./build/${{ env.RELEASE_NAME }}.zip
        
    - name: Upload Artifact to Release
      uses: softprops/action-gh-release@v1
      with:
        files: ./build/${{ env.RELEASE_NAME }}.zip
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      if: github.event_name == 'release' && github.event.action == 'published'
