image: "r.lcr.gr/microhacks/bevy-builder:latest"

variables:
  BINARY_FOLDER: game_core
  BINARY_NAME: game_core

stages:
  - build
  - package

build-windows:
  stage: build
  before_script:
    - export CARGO_HOME="${CI_PROJECT_DIR}/.cargo"
    - export PATH="${CI_PROJECT_DIR}/.cargo/bin:$PATH"
  cache:
    key: build-cache-windows
    paths:
      - .cargo/registry/cache
      - .cargo/registry/index
      - .cargo/git/db
      - .cargo/bin/
      - target/
  script:
    - cargo build --release -p ${BINARY_FOLDER} --target x86_64-pc-windows-gnu
  artifacts:
    expire_in: 1 day
    paths:
      - target/x86_64-pc-windows-gnu/release/game_core.exe
  only:
    - trunk

build-linux:
  stage: build
  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 -p ${BINARY_FOLDER} --target x86_64-unknown-linux-gnu
  artifacts:
    expire_in: 1 day
    paths:
      - target/x86_64-unknown-linux-gnu/release/game_core
  only:
    - trunk

build-arm64:
  image: "r.lcr.gr/microhacks/bevy-builder:arm64"
  tags:
    - arm64
  stage: build
  before_script:
    - export CARGO_HOME="${CI_PROJECT_DIR}/.cargo"
    - export PATH="${CI_PROJECT_DIR}/.cargo/bin:$PATH"
  cache:
    key: build-cache-arm64
    paths:
      - .cargo/registry/cache
      - .cargo/registry/index
      - .cargo/git/db
      - .cargo/bin/
      - target/
  script:
    - cargo build --release -p ${BINARY_FOLDER} --target aarch64-unknown-linux-gnu
  artifacts:
    expire_in: 1 day
    paths:
      - target/aarch64-unknown-linux-gnu/release/game_core
  only:
    - trunk

build-web:
  stage: build
  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:
    - mkdir -p "${CI_PROJECT_DIR}/${BINARY_FOLDER}/fonts"
    - mkdir "${CI_PROJECT_DIR}/${BINARY_FOLDER}/dist"
    - make assets
    - sed -i 's#public_url = "/"#public_url = "./"#' "${CI_PROJECT_DIR}/${BINARY_FOLDER}/Trunk.toml"
    - cd "${CI_PROJECT_DIR}/${BINARY_FOLDER}" && trunk build --release
    - cd "${CI_PROJECT_DIR}"
  artifacts:
    expire_in: 1 day
    paths:
      - game_core/dist/
  only:
    - trunk

package-all:
  stage: package
  script:
    - mkdir -p dist/
    - make assets
    - cp -r assets dist/assets
    - cp target/x86_64-unknown-linux-gnu/release/${BINARY_NAME} "dist/${BINARY_NAME}"
    - cp target/x86_64-pc-windows-gnu/release/${BINARY_NAME}.exe "dist/${BINARY_NAME}.exe"
    - cp target/aarch64-unknown-linux-gnu/release/${BINARY_NAME} "dist/${BINARY_NAME}.arm64"
    - cd "${CI_PROJECT_DIR}/dist" && zip -r "windows.zip" "./${BINARY_NAME}.exe" ./assets
    - cd "${CI_PROJECT_DIR}/dist" && zip -r "linux.x86.zip" "./${BINARY_NAME}" ./assets
    - cd "${CI_PROJECT_DIR}/dist" && zip -r "linux.arm64.zip" "./${BINARY_NAME}.arm64" ./assets
    - cd "${CI_PROJECT_DIR}/${BINARY_FOLDER}/dist" && zip -r "web.zip" ./*
    - cd "${CI_PROJECT_DIR}" && mv "${CI_PROJECT_DIR}/game_core/dist/web.zip" "${CI_PROJECT_DIR}/dist/web.zip"
  dependencies:
    - build-windows
    - build-linux
    - build-arm64
    - build-web
  artifacts:
    expire_in: 7 days
    paths:
      - dist/web.zip
      - dist/windows.zip
      - dist/linux.x86.zip
      - dist/linux.arm64.zip
  only:
    - trunk

pages:
  stage: package
  script:
    - mkdir -p public/
    - cp -r ${BINARY_FOLDER}/dist/* public/
  artifacts:
    expire_in: 7 days
    paths:
      - public
  dependencies:
    - build-web
  only:
    - trunk