---
version: '3'

vars:
  COMMON_DOCS_URL: https://gitlab.com/megabyte-labs/documentation/shared/-/raw/master/common.json
  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.
    prompt:
      type: input
      message: Enter the build command
      default:
        sh: |
          DATA_SUBTYPE="$(curl -sSL '{{.PROJECT_SUBTYPE_VARS_URL}}')" || EXIT_CODE=$?
          if [ -n "$EXIT_CODE" ]; then
            DATA_TYPE="$(curl -sSL '{{.PROJECT_VARS_URL}}')" || EXIT_CODE_2=$?
            if [ -n "$EXIT_CODE_2" ]; then
              echo ''
            else
              if [ "$(echo "$DATA_TYPE" | jq -r '.scriptsBuild')" != 'null' ]; then
                echo "$DATA_TYPE" | jq -r '.scriptsBuild'
              else
                echo ''
              fi
            fi
          else
            if [ "$(echo "$DATA_SUBTYPE" | jq -r '.scriptsBuild')" != 'null' ]; then
              echo "$DATA_SUBTYPE" | jq -r '.scriptsBuild'
            else
              echo ''
            fi
          fi
      answer:
        cmds:
          - TMP="$(mktemp)" && jq --arg cmd '{{.ANSWER | replace "'" "\'"}}' '.scripts.build = $cmd' package.json && mv "$TMP" package.json
    status:
      - |
        [[ "$(jq -r '.blueprint.description' package.json)" != "null" ]]

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

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

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

  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.
    prompt:
      type: input
      message: Enter the test command
      default:
        sh: |
          DATA_SUBTYPE="$(curl -sSL '{{.PROJECT_SUBTYPE_VARS_URL}}')" || EXIT_CODE=$?
          if [ -n "$EXIT_CODE" ]; then
            DATA_TYPE="$(curl -sSL '{{.PROJECT_VARS_URL}}')" || EXIT_CODE_2=$?
            if [ -n "$EXIT_CODE_2" ]; then
              echo ''
            else
              if [ "$(echo "$DATA_TYPE" | jq -r '.scriptsTest')" != 'null' ]; then
                echo "$DATA_TYPE" | jq -r '.scriptsTest'
              else
                echo ''
              fi
            fi
          else
            if [ "$(echo "$DATA_SUBTYPE" | jq -r '.scriptsTest')" != 'null' ]; then
              echo "$DATA_SUBTYPE" | jq -r '.scriptsTest'
            else
              echo ''
            fi
          fi
      answer:
        cmds:
          - TMP="$(mktemp)" && jq --arg cmd '{{.ANSWER | replace "'" "\'"}}' '.scripts.test = $cmd' package.json && mv "$TMP" package.json
    status:
      - |
        [[ "$(jq -r '.scripts.test' package.json)" != "null" ]]

  title:
    type: input
    message: Enter the title of the README.md
    default:
      sh: jq -r '.blueprint.name' package.json
    answer:
      cmds:
        - TMP="$(mktemp)" && jq --arg title '{{.ANSWER | replace "'" "\'"}}' '.blueprint.title = $title' package.json && mv "$TMP" package.json
    status:
      - |
        [[ "$(jq -r '.blueprint.title' package.json)" != "null" ]]
