# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

name: Build wasm demos

on:
    workflow_dispatch:
    workflow_call:
        inputs:
            build_artifacts:
                type: boolean
                required: true
                description: True if we should build all demos and produce artefacts

jobs:
    wasm_demo:
        env:
            CARGO_PROFILE_RELEASE_OPT_LEVEL: s
            CARGO_INCREMENTAL: false
        runs-on: ubuntu-22.04
        steps:
            - uses: actions/checkout@v6
            - uses: ./.github/actions/install-linux-dependencies
            - uses: ./.github/actions/setup-rust
              with:
                  target: wasm32-unknown-unknown
            - name: Install wasm-pack
              run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
            - name: Printerdemo WASM build
              run: |
                  sed -i "s/#wasm# //" Cargo.toml
                  wasm-pack build --release --target web
              working-directory:  demos/printerdemo/rust
            - name: Gallery WASM build
              if: ${{ inputs.build_artifacts }}
              run: |
                  sed -i "s/#wasm# //" Cargo.toml
                  export SLINT_STYLE=fluent && wasm-pack build --release  --out-dir pkg/fluent --target web
                  export SLINT_STYLE=material && wasm-pack build --release --out-dir pkg/material --target web
                  export SLINT_STYLE=cupertino && wasm-pack build --release --out-dir pkg/cupertino --target web
                  export SLINT_STYLE=cosmic && wasm-pack build --release --out-dir pkg/cosmic --target web
              working-directory: examples/gallery
            - name: Remaining wasm demos
              if: ${{ inputs.build_artifacts }}
              run: |
                  for demo in demos/printerdemo/rust demos/usecases/rust examples/todo/rust examples/todo-mvc/rust examples/carousel/rust examples/slide_puzzle examples/memory examples/imagefilter/rust examples/plotter examples/opengl_underlay demos/home-automation/rust examples/speedometer/rust; do
                      pushd $demo
                      sed -i "s/#wasm# //" Cargo.toml
                      wasm-pack build --release --target web
                      popd
                  done
            - name: Energy Monitor example WASM build
              if: ${{ inputs.build_artifacts }}
              run: |
                  sed -i "s/#wasm# //" Cargo.toml
                  wasm-pack build --release --target web --no-default-features --features slint/default,chrono
              working-directory: demos/energy-monitor
            - name: Weather Demo example WASM build
              run: |
                  sed -i "s/#wasm# //" Cargo.toml
                  wasm-pack build --release --target web --no-default-features --features slint/default,chrono
              working-directory: demos/weather-demo
            - name: "Upload Demo Artifacts"
              if: ${{ inputs.build_artifacts }}
              uses: actions/upload-artifact@v7
              with:
                  name: wasm_demo
                  path: |
                      examples/gallery/
                      demos/printerdemo/rust/
                      examples/todo/
                      examples/todo-mvc/rust/
                      examples/carousel/rust/
                      examples/memory/
                      examples/slide_puzzle/
                      examples/speedometer/rust/
                      examples/imagefilter/rust
                      examples/plotter/
                      examples/opengl_underlay/
                      demos/energy-monitor/
                      demos/home-automation/rust
                      demos/weather-demo/
                      demos/usecases/rust
                      !/**/.gitignore
            - name: Clean cache # Otherwise the cache is much too big
              run: |
                  du -hs target
                  rm -rf target/*/incremental
                  rm -rf target/*/*/*slint*
                  du -hs target
