name: WebAssembly

on: 
  push:
  pull_request:
  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@v7
      with:
        version: 2.0.0
        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-master" RAYLIB_RELEASE_PATH="../build/${{ env.RELEASE_NAME }}/lib"
        cd ..
  
    - name: Generate Artifacts
      run: |
        copy /Y .\src\raylib.h .\build\${{ env.RELEASE_NAME }}\include\raylib.h
        cd build
        7z a ./${{ env.RELEASE_NAME }}.zip ./${{ env.RELEASE_NAME }}
        dir
      shell: cmd
        
    - name: Upload Artifacts
      uses: actions/upload-artifact@v2
      with:
        name: ${{ env.RELEASE_NAME }}.zip
        path: ./build/${{ env.RELEASE_NAME }}.zip
        
    - name: Upload Artifact to Release
      uses: actions/upload-release-asset@v1.0.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./build/${{ env.RELEASE_NAME }}.zip
          asset_name: ${{ env.RELEASE_NAME }}.zip
          asset_content_type: application/zip
      if: github.event_name == 'release' && github.event.action == 'published'
