name: csound_wasm
env:
  nix_path: nixpkgs=https://github.com/NixOS/nixpkgs/archive/3876561795a7111dac8fd4b71e0279e46b08eed8.tar.gz
on:
  pull_request:
    branches: ["**"]
  push:
    branches: ["develop"]
jobs:
  csound-wasm:
    env:
      wasm_bin_dir: ./wasm
    name: Compiling wasm bins
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@master
      - uses: cachix/install-nix-action@v16
        with:
          nix_path: ${{ env.nix_path }}
      - uses: cachix/cachix-action@v10
        with:
          name: csound
          authToken: ${{ secrets.CACHIX_TOKEN }}
      - name: Compile wasm binaries
        run: cd $wasm_bin_dir && yarn build
      - name: Zip the compilation output
        run: |
          cd $wasm_bin_dir
          nix-env -f '<nixpkgs>' -iA zip
          zip -r lib.zip lib
      - name: Archive Wasm Artifacts
        uses: actions/upload-artifact@master
        with:
          name: lib
          path: ${{ env.wasm_bin_dir }}/lib.zip
  browser-test:
    env:
      wasm_bin_dir: ./wasm
      wasm_browser_dir: ./wasm/browser
      NIXPKGS_ALLOW_UNFREE: 1
    name: Browser bundle build and test
    needs: csound-wasm
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@master
      - uses: cachix/install-nix-action@v16
        with:
          nix_path: ${{ env.nix_path }}
      - uses: cachix/cachix-action@v10
        with:
          name: csound
          authToken: "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJjJDIzMTgiLCJqdGkiOiJhNDQzZmMwMi1mMDBjLTQzNDUtYjA3YS1jYzJjYjIzYjE2OTAiLCJzY29wZXMiOiJjYWNoZSJ9.UOQEV1Hd1ZEQdNF1STxbt0wcCrIrihsv4KThD_wHk3E"
      - name: Download Artifact
        uses: actions/download-artifact@master
        with:
          name: lib
      - name: Uncompress freshly compiled wasm artifacts
        run: |
          nix-env -f '<nixpkgs>' -iA unzip
          unzip ./lib.zip -d $wasm_bin_dir
          cd $wasm_bin_dir
          yarn link
      - name: Build the production js module bundle
        run: |
          cd $wasm_browser_dir
          yarn
          yarn link @csound/wasm-bin
          yarn build:prod
      - name: Install test dependencies
        run: |
          cd $wasm_browser_dir
          nix-shell --run 'exit 0'
      - name: Check eslint
        continue-on-error: true # allow the reporter to report the failures
        run: |
          cd $wasm_browser_dir
          nix-shell --pure
          yarn lint:ci
      - name: Run the unit-tests
        continue-on-error: true # allow the reporter to report the failures
        run: |
          cd $wasm_browser_dir
          nix-shell --pure
          yarn test:ci
      - name: Fix the test report
        run: |
          cd $wasm_browser_dir
          nix-shell --pure
          mv tests/results.junit.xml tests/_results.junit.xml
          ex -s +'%s/<testsuite name=""[^>].\{-}>\n.*<\/testsuite>//ge' +%p +q! \
            tests/_results.junit.xml > tests/results.junit.xml
          rm tests/_results.junit.xml
      - name: Publish Test Report
        uses: mikepenz/action-junit-report@v2
        with:
          report_paths: "**/tests/*.junit.xml"
          fail_on_failure: true
          github_token: ${{ secrets.GITHUB_TOKEN }}
