# https://taskfile.dev
version: '3'

tasks:
  default:
    - task: setup

  dev:
    deps: [check-dotenv, pnpm-install, generate-pwa-icons, grammar:compile]
    cmds:
      - turbo run build -F note3^...
      - cmd: turbo run dev -F note3 {{ .CLI_ARGS }}
        ignore_error: true # HACK: so it won't print "failed to run" when stopping via Ctrl-C
        # preconditions:
        #   - test -e .env
  dev-rs:
    cmds:
      - cmd: rsw watch
      #   ignore_error: true # don't print "failed to run" when stopping via Ctrl-C
  build:
    deps: [check-dotenv, generate-pwa-icons, grammar:compile] # pnpm-install covered by pwa-icons
    cmds:
      - turbo run build -F note3 {{ .CLI_ARGS }}
  build-deps-only:
    deps: [pnpm-install]
    cmds:
      - turbo run build -F note3^ {{ .CLI_ARGS }}
  build-preview:
    deps: [build]
    cmds:
      - vite preview
  build-preview-watch:
    deps: [build]
    cmds:
      - watchexec -r -w src/ --print-events -- task build-preview
  dev-deploy:
    deps: [pnpm-install]
    env:
      NODE_ENV: development
    cmds:
      - rm -rf dist/

    # ########
    # SETUP #
    # ########
  setup:
    cmds:
      - cmd: echo -e "Setting up dev environment...\n"
        silent: true
      - cmd: |
          test -n "$DIRENV_DIR" || test -n "$IN_NIX_SHELL" || echo -e "WARNING: direnv not loaded! Install dependencies yourself or run 'direnv allow' / 'nix develop'.\n"
        silent: true
      - task: copy-dotenv
      - task: pnpm-install
      - cmd: echo -e "\nYou're all set. Run 'task dev' to start dev server"
        silent: true
  copy-dotenv:
    cmds:
      - '! test -e .env'
      - cp .example.env .env
      - task: reset-dotenv-check
      - cmd: echo "Created .env from example, check if it needs adaptation"
        silent: true
    status:
      - test -e .env
      # - '! test -e .task/gen-sources/.env || diff .task/gen-sources/.env .example.env'
  check-dotenv:
    deps: [copy-dotenv]
    # preconditions:
    #     - '! diff .task/gen-sources/.env .example.env'
    cmds:
      - ls -al .example.env || true
      - ls -al .task/gen-sources/.env || true
      - cmd: diff -u .task/gen-sources/.env .example.env | delta -s --hunk-header-style=omit || diff .task/gen-sources/.env .example.env # --file-style=omit
        ignore_error: true
      - cmd: |
          echo -e "\nThe file .example.env has been updated since you've copied it!\nPlease update it & run either:\n  task reset-dotenv-check\nor:\n  rm .env\n"
        silent: true
      - cmd: exit 2
        silent: true
    status:
      - diff .task/gen-sources/.env .example.env
  reset-dotenv-check:
    cmds:
      - mkdir -p .task/gen-sources/ && cp .example.env .task/gen-sources/.env # for comparison check later

  pnpm-install:
    run: once
    dir: ../../
    cmds:
      - task pnpm-install # to leverage checksum caching of monorepo

  generate-pwa-icons:
    deps: [pnpm-install]
    cmds:
      - pnpm pwa-assets-generator
    sources:
      - pwa-assets.config.ts
      - assets/ipld-logo*

    # GRAMMAR #
  grammar:compile:
    deps: [pnpm-install]
    cmds:
      - pnpm nearleyc src/search/grammar.ne -o src/search/grammar-compiled.ts
    sources:
      - src/search/grammar.ne
  grammar:test:
    deps: [grammar:compile]
    cmds:
      - nearley-tester -r src/search/grammar.ne src/search/grammar-test/main.test -e src/search/grammar-test/expect/ --no-watch
  grammar:test-watch:
    deps: [grammar:compile]
    cmds:
      - nearley-tester -r src/search/grammar.ne src/search/grammar-test/main.test -e src/search/grammar-test/expect/
  grammar:reset-expected:
    cmds:
      - rm -rf src/search/grammar-test/expect/
      - task: grammar:test
  grammar:railroad:
    cmds:
      - nearley-railroad src/search/grammar.ne > /tmp/grammar.html
      - xdg-open /tmp/grammar.html
