---
version: '3'

vars:
  COMMON_DOCS_URL: https://gitlab.com/megabyte-labs/documentation/shared/-/raw/master/common.json
  COMMON_URL: 'https://gitlab.com/megabyte-labs/common/'
  PROJECT_SUBTYPE_VARS_URL: '{{.COMMON_URL}}{{.REPOSITORY_TYPE}}/-/raw/master/project-{{.REPOSITORY_SUBTYPE}}/.config/variables.json'
  PROJECT_VARS_URL: '{{.COMMON_URL}}{{.REPOSITORY_TYPE}}/-/raw/master/project/.config/variables.json'

tasks:
  all:
    cmds:
      - task: name
      - task: :boilerplate:populate:group
      - task: group
      - task: :boilerplate:populate:subgroup
      - task: subgroup
      - task: title
      - task: description
      - task: overview
      - task: slug
      - task: project-specific
      - task: build
      - task: test

  build:
    summary: |
      This task prompts the user for the `build` command to place in `scripts.build` inside
      of `package.json`.

      For the default value, it looks at the corresponding common respoitory by first checking the
      `project-subtype` folder and then the `project` folder's value for `scriptsBuild` inside of the
      `.config/variables.json` file.
    cmds:
      - .config/log prompt 'Enter the build command placed in `scripts.build` inside of `package.json`'
      - |
        BUILD_ANSWER="$(.config/log input 'Enter build command..')"
        task boilerplate:prompt:build:continue -- "$BUILD_ANSWER"
    status:
      - |
        [[ "$(jq -r '.blueprint.description' package.json)" != "null" ]]

  build:continue:
    cmds:
      - TMP="$(mktemp)" && jq --arg cmd '{{.CLI_ARGS | replace "'" "\'"}}' '.scripts.build = $cmd' package.json && mv "$TMP" package.json

  description:
    cmds:
      - .config/log prompt 'Enter a description for the project'
      - |
        DESC_ANSWER="$(.config/log input 'Enter description..')"
        task boilerplate:prompt:description:continue -- "$DESC_ANSWER"
    status:
      - |
        [[ "$(jq -r '.blueprint.description' package.json)" != "null" ]]

  description:continue:
    cmds:
      - TMP="$(mktemp)" && jq --arg desc '{{.CLI_ARGS | replace "'" "\'"}}' '.blueprint.description = $desc' package.json && mv "$TMP" package.json

  group:
    prompt:
      type: select
      message: Select a group
      options:
        - angular
        - ansible
        - docker
        - go
        - npm
        - packer
        - python
      answer:
        cmds:
          - |
            TMP="$(mktemp)" && jq --arg group '{{.ANSWER}}' '.blueprint.group = $group' package.json > "$TMP" && mv "$TMP" package.json
          - |
            TYPE='{{.ANSWER}}' yq e -i '.vars.REPOSITORY_TYPE = env(TYPE)' Taskfile.yml
    status:
      - |
        [[ "$(jq -r '.blueprint.group' package.json)" != "null" ]] && [[ "$(yq e '.vars.REPOSITORY_TYPE' Taskfile.yml)" != "null" ]]

  name:
    cmds:
      - .config/log prompt 'Enter a name for the project'
      - |
        NAME_ANSWER="$(.config/log input 'Enter the project name..')"
        task boilerplate:prompt:name:continue -- "$NAME_ANSWER"
    status:
      - |
        [[ "$(jq -r '.blueprint.name' package.json)" != "null" ]]

  name:continue:
    cmds:
      - TMP="$(mktemp)" && jq --arg name '{{.CLI_ARGS}}' '.blueprint.name = $name' package.json > "$TMP" && mv "$TMP" package.json

  overview:
    prompt:
      type: input
      message: Enter an overview for the project
      answer:
        cmds:
          - TMP="$(mktemp)" && jq --arg overview '{{.ANSWER}}' '.blueprint.overview = $overview' package.json > "$TMP" && mv "$TMP" package.json
    status:
      - |
        [[ "$(jq -r '.blueprint.overview' package.json)" != "null" ]]

  project-specific: 'true'

  slug:
    cmds:
      - .config/log prompt 'Enter a slug for the project'
      - |
        SLUG_ANSWER="$(.config/log input 'Enter a slug..')"
        task boilerplate:prompt:slug:continue -- "$SLUG_ANSWER"
    status:
      - |
        [[ "$(jq -r '.blueprint.slug' package.json)" != "null" ]]

  slug:continue:
    cmds:
      - TMP="$(mktemp)" && jq --arg slug '{{.CLI_ARGS}}' '.blueprint.slug = $slug' package.json > "$TMP" && mv "$TMP" package.json

  subgroup:
    env:
      SUBGROUP_GROUP:
        sh: |
          TASK_GROUP="$(yq e '.vars.REPOSITORY_TYPE' Taskfile.yml)"
          if [ "$TASK_GROUP" == 'null' ]; then
            PKG_GROUP="$(jq -r '.blueprint.group' package.json)"
            if [ "$PKG_GROUP" == 'null' ]; then
              echo 'generic'
            else
              echo "$PKG_GROUP"
            fi
          else
            echo "$TASK_GROUP"
          fi
    prompt:
      type: select
      message: Select a subgroup
      options:
        sh: curl -sSL '{{.COMMON_DOCS_URL}}' | jq --arg type "$SUBGROUP_GROUP" '.groups[$type]'
      answer:
        cmds:
          - |
            TMP="$(mktemp)" && jq --arg subtype '{{.ANSWER}}' '.blueprint.subgroup = $subtype' package.json > "$TMP" && mv "$TMP" package.json
          - |
            SUBTYPE='{{.ANSWER}}' yq e -i '.vars.REPOSITORY_SUBTYPE = env(SUBTYPE)' Taskfile.yml
    status:
      - |
        [[ "$(jq -r '.blueprint.subgroup' package.json)" != "null" ]] && [[ "$(yq e '.vars.REPOSITORY_SUBTYPE' Taskfile.yml)" != "null" ]]

  test:
    summary: |
      This task prompts the user for the `test` command to place in `scripts.test` inside
      of `package.json`.

      For the default value, it looks at the corresponding common respoitory by first checking the
      `project-subtype` folder and then the `project` folder's value for `scriptsTest` inside of the
      `.config/variables.json` file.
    cmds:
      - .config/log prompt 'Enter the test command placed in `scripts.test` inside of `package.json`'
      - |
        TEST_ANSWER="$(.config/log input 'Enter test command..')"
        task boilerplate:prompt:test:continue -- "$TEST_ANSWER"
    status:
      - |
        [[ "$(jq -r '.scripts.test' package.json)" != "null" ]]

  test:continue:
    cmds:
      - TMP="$(mktemp)" && jq --arg cmd '{{.CLI_ARGS | replace "'" "\'"}}' '.scripts.test = $cmd' package.json && mv "$TMP" package.json

  title:
    cmds:
      - .config/log prompt 'Enter the title of the README.md'
      - |
        TITLE_ANSWER="$(.config/log input 'Enter README.md title..')"
        task boilerplate:prompt:title:continue -- "$TITLE_ANSWER"
    status:
      - |
        [[ "$(jq -r '.blueprint.title' package.json)" != "null" ]]

  title:continue:
    cmds:
      - TMP="$(mktemp)" && jq --arg title '{{.CLI_ARGS | replace "'" "\'"}}' '.blueprint.title = $title' package.json && mv "$TMP" package.json
