version: '3'

##
# IMPORTANT: All tasks listed in this file should be executed from the root folder.
# https://taskfile.dev/usage/#task-directory
#
# NOTE: Examples of multiline strings in YAML.
# https://yaml-multiline.info/
#
# NOTE: Use `[ "${IN_DOCKER_CONTAINER}" == "true" ] && echo true || echo false` to debug ``[ "${IN_DOCKER_CONTAINER}" == "true" ]`.
#
# NOTE: Forwarding CLI arguments to commands ({{.CLI_ARGS}}).
# https://taskfile.dev/usage/#forwarding-cli-arguments-to-commands
#
tasks:
  default:
    cmds:
      - task: rspec

  coverage:lcov:merge:
    cmds:
      - npx --yes lcov-result-merger 'coverage/**/lcov.info' coverage/lcov.info

  coverage:open:
    cmds:
      - open coverage/index.html
    preconditions:
      - sh: '[ "${IN_DOCKER_CONTAINER}" != "true" ]'
        msg: This task can be invoked only from the host operating system (https://www.ibm.com/cloud/learn/containerization)

  deps:
    cmds:
      - task: deps:install

  deps:install:
    cmds:
      - bundle install
      - bundle exec appraisal install

  deps:clean:
    cmds:
      - "bundle exec rake confirm \"This task removes Gemfile*.lock and gemfiles/**/*. Are you sure?\""
      - rm Gemfile*.lock
      - rm -rf gemfiles

  docker:bash:ruby_2.7:
    cmds:
      - docker run --rm -it -v $(pwd):/gem convenient_service:2.7 bash
    interactive: true
    preconditions:
      - sh: '[ "${IN_DOCKER_CONTAINER}" != "true" ]'
        msg: This task can be invoked only from the host operating system (https://www.ibm.com/cloud/learn/containerization)

  docker:bash:ruby_3.0:
    cmds:
      - docker run --rm -it -v $(pwd):/gem convenient_service:3.0 bash
    interactive: true
    preconditions:
      - sh: '[ "${IN_DOCKER_CONTAINER}" != "true" ]'
        msg: This task can be invoked only from the host operating system (https://www.ibm.com/cloud/learn/containerization)

  docker:bash:ruby_3.1:
    cmds:
      - docker run --rm -it -v $(pwd):/gem convenient_service:3.1 bash
    interactive: true
    preconditions:
      - sh: '[ "${IN_DOCKER_CONTAINER}" != "true" ]'
        msg: This task can be invoked only from the host operating system (https://www.ibm.com/cloud/learn/containerization)

  docker:build:
    cmds:
      - task: docker:build:ruby_2.7
      - task: docker:build:ruby_3.0
      - task: docker:build:ruby_3.1
    preconditions:
      - sh: '[ "${IN_DOCKER_CONTAINER}" != "true" ]'
        msg: This task can be invoked only from the host operating system (https://www.ibm.com/cloud/learn/containerization)

  docker:build:ruby_2.7:
    cmds:
      - docker build . -f docker/2.7/Dockerfile -t convenient_service:2.7
    preconditions:
      - sh: '[ "${IN_DOCKER_CONTAINER}" != "true" ]'
        msg: This task can be invoked only from the host operating system (https://www.ibm.com/cloud/learn/containerization)

  docker:build:ruby_3.0:
    cmds:
      - docker build . -f docker/3.0/Dockerfile -t convenient_service:3.0
    preconditions:
      - sh: '[ "${IN_DOCKER_CONTAINER}" != "true" ]'
        msg: This task can be invoked only from the host operating system (https://www.ibm.com/cloud/learn/containerization)

  docker:build:ruby_3.1:
    cmds:
      - docker build . -f docker/3.1/Dockerfile -t convenient_service:3.1
    preconditions:
      - sh: '[ "${IN_DOCKER_CONTAINER}" != "true" ]'
        msg: This task can be invoked only from the host operating system (https://www.ibm.com/cloud/learn/containerization)

  ##
  # NOTE: Dev only command.
  # NOTE: macOS specific command.
  #
  docker:start:
    cmds:
      - open -a Docker

  ##
  # NOTE: Dev only command.
  #
  editor:open:
    cmds:
      - code .

  ##
  # NOTE: `sdoc` (wrapper for `rdoc`) is replaced by `yard`, since you have a feeling like every time is a first time with `rdoc`.
  # - https://kapeli.com/cheat_sheets/Yard.docset/Contents/Resources/Documents/index
  # - https://rubydoc.info/gems/yard/file/README.md
  # - `yardoc --help`
  #
  # NOTE: `commonmarker` supports GitHub Flavored Markdown.
  # - https://github.com/gjtorikian/commonmarker
  # - https://github.github.com/gfm/
  # - https://github.com/github/markup#markups
  # - https://github.com/lsegal/yard/pull/1388
  #
  # TODO: Contribute. Forward `--parse-option LIBERAL_HTML_TAG` to commonmarker.
  #
  # NOTE: options are specified in the `.yardopts` file.
  #
  docs:generate:
    cmds:
      - bundle exec yardoc

  docs:generate:open:
    cmds:
      - task: docs:generate
      - task: docs:open

  docs:lint:
    cmds:
      - mkdir -p docs
      - bundle exec yard-junk --text --html docs/junk-yard.html

  docs:missed:
    cmds:
      ##
      # NOTE: Options are taken from `.inch.yml`.
      #
      - bundle exec inch

  docs:open:
    cmds:
      - open docs/index.html
    preconditions:
      - sh: '[ "${IN_DOCKER_CONTAINER}" != "true" ]'
        msg: This task can be invoked only from the host operating system (https://www.ibm.com/cloud/learn/containerization)

  ##
  # NOTE: A trailing "/**" matches everything inside.
  # https://git-scm.com/docs/gitignore
  #
  git:staged:specs:
    cmds:
      - git diff --cached --name-only --diff-filter=d "spec/**"

  ##
  # NOTE: Dev only command.
  # NOTE: macOS specific command.
  #
  github:open:
    cmds:
      - open -na "Google Chrome" --args --new-window --incognito "https://github.com/marian13/convenient_service"

  lint:
    cmds:
      - task: rubocop
      - task: docs:lint

  playground:
    cmds:
      - bundle exec rake playground
    interactive: true

  playground:all:
    cmds:
      - bundle exec appraisal all rake playground
    interactive: true

  playground:dry:
    cmds:
      - bundle exec appraisal dry rake playground
    interactive: true

  playground:rails_5.2:
    cmds:
      - bundle exec appraisal rails_5.2 rake playground
    interactive: true

  playground:rails_6.0:
    cmds:
      - bundle exec appraisal rails_6.0 rake playground
    interactive: true

  playground:rails_6.1:
    cmds:
      - bundle exec appraisal rails_6.1 rake playground
    interactive: true

  playground:rails_7.0:
    cmds:
      - bundle exec appraisal rails_7.0 rake playground
    interactive: true

  playground:standard:
    cmds:
      - bundle exec rake playground
    interactive: true

  release:
    cmds:
      - gem release

  rspec:
    cmds:
      - task: rspec:standard
      - task: rspec:rails_7.0
      - task: rspec:rails_6.1
      - task: rspec:rails_6.0
      - task: rspec:rails_5.2
      - task: rspec:dry

  ##
  # NOTE: Run rspec only for git staged spec files.
  # https://githowto.com/staging_and_committing
  #
  rspec:staged:
    cmds:
      - task: rspec:standard:staged
      - task: rspec:rails_5.2:staged
      - task: rspec:rails_6.0:staged
      - task: rspec:rails_6.1:staged
      - task: rspec:rails_7.0:staged
      - task: rspec:dry:staged

  rspec:dry:
    cmds:
      - bundle exec appraisal dry rspec --format progress --require dry_helper {{.GIT_STAGED_SPEC_FILES}} {{.CLI_ARGS}}

  rspec:dry:staged:
    - task: rspec:dry
      vars:
        GIT_STAGED_SPEC_FILES:
          ##
          # NOTE: `task: git:staged:specs` does NOT work.
          #
          sh: task git:staged:specs --silent

  rspec:rails_5.2:
    cmds:
      - bundle exec appraisal rails_5.2 rspec --format progress --require rails_helper {{.GIT_STAGED_SPEC_FILES}} {{.CLI_ARGS}}

  rspec:rails_5.2:staged:
    - task: rspec:rails_5.2
      vars:
        GIT_STAGED_SPEC_FILES:
          ##
          # NOTE: `task: git:staged:specs` does NOT work.
          #
          sh: task git:staged:specs --silent

  rspec:rails_6.0:
    cmds:
      - bundle exec appraisal rails_6.0 rspec --format progress --require rails_helper {{.GIT_STAGED_SPEC_FILES}} {{.CLI_ARGS}}

  rspec:rails_6.0:staged:
    - task: rspec:rails_6.0
      vars:
        GIT_STAGED_SPEC_FILES:
          ##
          # NOTE: `task: git:staged:specs` does NOT work.
          #
          sh: task git:staged:specs --silent

  rspec:rails_6.1:
    cmds:
      - bundle exec appraisal rails_6.1 rspec --format progress --require rails_helper {{.GIT_STAGED_SPEC_FILES}} {{.CLI_ARGS}}

  rspec:rails_6.1:staged:
    - task: rspec:rails_6.1
      vars:
        GIT_STAGED_SPEC_FILES:
          ##
          # NOTE: `task: git:staged:specs` does NOT work.
          #
          sh: task git:staged:specs --silent

  rspec:rails_7.0:
    cmds:
      - bundle exec appraisal rails_7.0 rspec --format progress --require rails_helper {{.GIT_STAGED_SPEC_FILES}} {{.CLI_ARGS}}

  rspec:rails_7.0:staged:
    - task: rspec:rails_7.0
      vars:
        GIT_STAGED_SPEC_FILES:
          ##
          # NOTE: `task: git:staged:specs` does NOT work.
          #
          sh: task git:staged:specs --silent

  rspec:standard:
    cmds:
      - bundle exec rspec --format progress {{.GIT_STAGED_SPEC_FILES}} {{.CLI_ARGS}}

  rspec:standard:staged:
    cmds:
      - task: rspec:standard
        vars:
          GIT_STAGED_SPEC_FILES:
            ##
            # NOTE: `task: git:staged:specs` does NOT work.
            #
            sh: task git:staged:specs --silent

  rubocop:
    cmds:
      - bundle exec rubocop --config .rubocop.yml {{.CLI_ARGS}}

  rubocop:format:
    cmds:
      - bundle exec rubocop --config .rubocop.yml -A {{.CLI_ARGS}}

  ##
  # NOTE: Dev only command.
  #
  tmuxinator:start:
    cmds:
      - tmuxinator start convenient_service --project-config=.dev/.tmuxinator.yml