version: "3"

tasks:
  gen:
    cmds:
      - task gen-go
      - task gen-gorm
      - task gen-gql-enums
      - task gen-gql
      - task gen-protoc
      - task gen-mockery
      - task gen-classifier-schema
      - task gen-webui-graphql

  gen-go:
    cmds:
      - go generate ./...

  gen-gorm:
    cmds:
      - go run ./internal/dev gorm gen

  gen-gql-enums:
    cmds:
      - go run ./internal/gql/enums/gen/genenums.go

  gen-gql:
    cmds:
      - go run github.com/99designs/gqlgen generate --config ./internal/gql/gqlgen.yml

  gen-protoc:
    cmds:
      - protoc --go_out=. ./internal/protobuf/bitmagnet.proto

  gen-mockery:
    cmds:
      - go run github.com/vektra/mockery/v2

  gen-classifier-schema:
    cmds:
      - go run . classifier schema --format json > ./bitmagnet.io/schemas/classifier-0.1.json

  gen-webui-graphql:
    dir: ./webui
    cmds:
      - npm run graphql:codegen

  i18n-extract:
    dir: ./webui
    cmds:
      - npm run i18n:extract

  lint:
    cmds:
      # Removing golang-ci lint as the Nix package is currently broken
      # - task lint-golangci
      - task lint-webui
      - task lint-prettier

  lint-golangci:
    cmds:
      - golangci-lint run --timeout=10m

  lint-webui:
    dir: ./webui
    cmds:
      - npm run lint

  lint-prettier:
    cmds:
      - prettier --check .

  test:
    cmds:
      - task test-go
      - task test-webui

  test-go:
    cmds:
      - go test -v ./...

  test-webui:
    dir: ./webui
    cmds:
      - npm test
    env:
      CHROME_BIN: chromium

  migrate:
    cmds:
      - go run ./internal/dev migrate up

  build:
    cmds:
      - go build -ldflags "-X github.com/bitmagnet-io/bitmagnet/internal/version.GitTag=$(git describe --tags --always --dirty)"

  build-webui:
    dir: ./webui
    cmds:
      - npm run build

  build-docsite:
    dir: ./bitmagnet.io
    cmds:
      - bundle install
      - bundle exec jekyll build

  install-webui:
    dir: ./webui
    cmds:
      - npm ci

  serve-webui:
    dir: ./webui
    cmds:
      - npm start -- --host {{.HOST}} --port {{.PORT}}
    vars:
      HOST: localhost
      PORT: 3334

  serve-docsite:
    dir: ./bitmagnet.io
    cmds:
      - bundle exec jekyll serve --host {{.HOST}} --port {{.PORT}} --livereload
    vars:
      HOST: localhost
      PORT: 3335

  export-data:
    cmds:
      - |
        pg_dump \
        --column-inserts \
        --data-only \
        --on-conflict-do-nothing \
        --rows-per-insert=1000 \
        --table=metadata_sources \
        --table=content \
        --table=content_attributes \
        --table=content_collections \
        --table=content_collections_content \
        --table=torrent_sources \
        --table=torrents \
        --table=torrent_files \
        --table=torrent_hints \
        --table=torrent_contents \
        --table=torrent_tags \
        --table=torrents_torrent_sources \
        --table=key_values \
        bitmagnet \
        > export.sql

  create-migration:
    dir: ./migrations
    cmds:
      - goose -s create {{.NAME}} sql
    vars:
      NAME: migration
