# This file is a template, and might need editing before it works on your project.
# This is a sample GitLab CI/CD configuration file that should run without any modifications.
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
# it uses echo commands to simulate the pipeline execution.
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages
#
# You can copy and paste this template into a new `.gitlab-ci.yml` file.
# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword.
#
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml

stages:     
  - test
  - compile
  - package

run-tests:   
  image: ghcr.io/rust-lang/rust:nightly-slim
  stage: test
  script:
    - cargo test

compile-desktop:
  image: 
    name: r.lcr.gr/microhacks/rust-ci-builder:latest
  stage: compile
  before_script:
    - export CARGO_HOME="${CI_PROJECT_DIR}/.cargo"
    - export PATH="${CI_PROJECT_DIR}/.cargo/bin:$PATH"
  cache:
    key: build-cache-linux
    paths:
      - .cargo/registry/cache
      - .cargo/registry/index
      - .cargo/git/db
      - .cargo/bin/
      - target/
  script:
    - cargo build --release
  dependencies:
    - "run-tests"

compile-web:
  image: 
    name: r.lcr.gr/microhacks/rust-ci-builder:latest
  stage: compile
  before_script:
    - export CARGO_HOME="${CI_PROJECT_DIR}/.cargo"
    - export PATH="${CI_PROJECT_DIR}/.cargo/bin:$PATH"
  cache:
    key: build-cache-web
    paths:
      - .cargo/registry/cache
      - .cargo/registry/index
      - .cargo/git/db
      - .cargo/bin/
      - target/
  script:
    - rm -rf forge-script-web/out
    - mkdir -p forge-script-web/out
    - wasm-pack build forge-script-web --release --target web --out-dir out/pkg-web
    - wasm-pack build forge-script-web --release --target nodejs --out-dir out/pkg-npm
    - wasm-pack build forge-script-web --release --target bundler --out-dir out/pkg-bundle
    - cd "${CI_PROJECT_DIR}/forge-script-web/out/pkg-web" && zip -r pkg-web.zip ./* && mv pkg-web.zip "${CI_PROJECT_DIR}/"
    - cd "${CI_PROJECT_DIR}/forge-script-web/out/pkg-npm" && zip -r pkg-npm.zip ./* && mv pkg-npm.zip "${CI_PROJECT_DIR}/"
    - cd "${CI_PROJECT_DIR}/forge-script-web/out/pkg-bundle" && zip -r pkg-bundle.zip ./* && mv pkg-bundle.zip "${CI_PROJECT_DIR}/"
  dependencies:
    - "run-tests"
  artifacts:
    untracked: false
    when: on_success
    expire_in: 7 days
    paths:
      - pkg-web.zip
      - pkg-npm.zip
      - pkg-bundle.zip