# https://taskfile.dev

version: "3"

tasks:
  default:
    cmds:
      - task: lint
      - task: test
      - task: build

  package:
    cmds:
      - task:clean
      - task:default
      - rm -fr dist
      - linuxdeploy-x86_64.AppImage --appdir dist
      - cp target/release/upend dist/usr/bin/upend
      - cp -r webui/dist dist/usr/bin/webui
      - cp assets/upend.png dist/usr/share/icons/upend.png
      - linuxdeploy-x86_64.AppImage --appdir dist -d upend.desktop --output appimage
    vars:
      VERSION:
        sh: grep '^version' Cargo.toml|grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')

  lint:
    deps: [lint:frontend, lint:backend, lint:webext]

  lint:frontend:
    dir: webui
    deps: [setup:frontend]
    sources:
      - ./**/*.ts
      - ./**/*.svelte
    cmds:
      - yarn check
      - yarn lint

  lint:backend:
    cmds:
      - cargo clippy --workspace

  lint:webext:
    dir: webext
    deps: [setup:webext]
    sources:
      - ./**/*.ts
      - ./**/*.svelte
    cmds:
      - yarn lint

  test:
    deps: [test:backend]

  test:backend:
    cmds:
      - cargo nextest run --workspace

  build:
    deps: [build:frontend, build:backend, build:webext]

  build:backend:
    dir: cli
    sources:
      - ./**/Cargo.toml
      - ./**/*.rs
    cmds:
      - cargo build --release

  build:frontend:
    dir: webui
    deps: [setup:frontend]
    sources:
      - ./**/*.ts
      - ./**/*.svelte
    cmds:
      - yarn build

  build:jslib:
    dir: tools/upend_js
    sources:
      - ./*.ts
      - ./package.lock
    cmds:
      - yarn install --immutable
      - yarn build

  setup:frontend:
    deps: [build:jslib]
    dir: webui
    sources:
      - ../tools/upend_js/*.js
      - package.lock
    cmds:
      - yarn add ../tools/upend_js
      - yarn install --immutable

  build:webext:
    deps: [setup:webext]
    dir: webext
    sources:
      - ./**/*.ts
      - ./**/*.svelte
    cmds:
      - yarn build

  setup:webext:
    deps: [build:jslib]
    dir: webext
    sources:
      - ../tools/upend_js/*.js
      - package.lock
    cmds:
      - yarn add ../tools/upend_js
      - yarn install --immutable

  dev:update_tool_deps:
    cmds:
      - task: build:jslib
      - cd webui && yarn add ../tools/upend_js && cd ..
      - cd webext && yarn add ../tools/upend_js && cd ..

  dev:
    deps: [dev:backend, dev:frontend]

  dev:backend:
    dir: cli
    cmds:
      - cargo run --release -- serve ../example_vault --clean --no-browser

  dev:frontend:
    dir: webui
    cmds:
      - yarn dev --open

  clean:
    deps:
      [clean:backend, clean:frontend, clean:webext, clean:tools, clean:vault]

  clean:backend:
    cmds:
      - cargo clean

  clean:frontend:
    cmds:
      - rm -rf webui/node_modules
      - rm -vrf webui/dist webui/public/vendor

  clean:webext:
    cmds:
      - rm -rf webext/node_modules
      - rm -vrf webext/web-ext-artifacts webext/dist

  clean:tools:
    cmds:
      - rm -vrf tools/upend_js/*.js

  clean:vault:
    cmds:
      - rm -vrf example_vault/.upend

  update-schema:
    cmds:
      - rm -f upend.sqlite3
      - diesel migration run --migration-dir migrations/upend/
      - diesel print-schema > src/database/inner/schema.rs
