---
version: '3'

vars:
  DEVCONTAINER_CONFIG: .devcontainer/devcontainer.json
  EXTENSIONS_FILE: .vscode/extensions.json
  EXTENSIONS_URL: https://gitlab.com/megabyte-labs/gas-station/-/raw/master/environments/prod/group_vars/desktop/vscode-extensions.yml
  TASKS_FILE: .vscode/tasks.json

tasks:
  extensions:
    deps:
      - :install:npm:prettier
      - :install:software:jq
      - :install:software:yq
    summary: |
      # Generates the `{{.EXTENSIONS_FILE}}` file for VS Code auto plugin recommendations

      This task generates the `{{.EXTENSIONS_FILE}}` file which is used to automatically have
      VS Code show a popup with a link that leads to a list of plugins that the project recommends.
      This list is populated by converting the list of VS Code plugins installed by default by
      [Gas Station](https://gitlab.com/megabyte-labs/gas-station) into VS Code syntax.
    run: once
    log:
      error: Failed to generate `{{.EXTENSIONS_FILE}}`
      start: Generating `{{.EXTENSIONS_FILE}}`
      success: Generated `{{.EXTENSIONS_FILE}}`
    cmds:
      - mkdir -p .vscode
      - >-
        curl -s {{.EXTENSIONS_URL}} | yq e -o=j | jq '.[] = [(.[][] | select(.types | index("{{.REPOSITORY_TYPE}}") or index("all")))
        | .name] | . + {"recommendations": .vscode_extensions } | del(.vscode_extensions)' > {{.EXTENSIONS_FILE}}
      - task: :fix:prettier
        vars:
          CLI_ARGS: '{{.EXTENSIONS_FILE}}'

  extensions:devcontainer:
    deps:
      - :install:npm:prettier
      - :install:software:jq
    summary: |
      # Inject `devcontainer.json` with Extensions

      This task synchronizes the extensions from `.vscode/extensions.json` into the
      `.devcontainer/devcontainer.json` extensions field.
    run: once
    log:
      error: Failed to inject `{{.EXTENSIONS_FILE}}` `recommendations` into `{{.DEVCONTAINER_CONFIG}}`
      start: Injecting `{{.EXTENSIONS_FILE}}` `recommendations` into `{{.DEVCONTAINER_CONFIG}}`
      success: Successfully injected `{{.EXTENSIONS_FILE}}` `recommendations` into `{{.DEVCONTAINER_CONFIG}}`
    cmds:
      - |
        TMP="$(mktemp)"
        jq --arg recs "$(jq '.recommendations' {{.EXTENSIONS_FILE}})" '.extensions = ($recs | fromjson)' {{.DEVCONTAINER_CONFIG}} > "$TMP"
        mv "$TMP" {{.DEVCONTAINER_CONFIG}}
      - task: :fix:prettier
        vars:
          CLI_ARGS: '{{.DEVCONTAINER_CONFIG}}'

  generate:
    deps:
      - extensions
      - tasks
    run: once
    cmds:
      - task: extensions:devcontainer

  tasks:
    deps:
      - :install:npm:prettier
      - :install:software:jq
    summary: |
      # Populate the `.vscode/tasks.json` file

      This task populates the `.vscode/tasks.json` file with tasks that are viewable by
      running `task --list`. With the file generated using the output from `tasks --list`,
      the user can then easily run any of the tasks from within VS Code.

      This is a simple implementation. There are many features provided by VS Code that we
      can potentially leverage to make our `tasks.json` better and more useful to developers.
    run: when_changed
    log:
      error: Failed to generate `{{.EXTENSIONS_FILE}}`
      start: Generating `.vscode/tasks.json`
      success: Generated `{{.EXTENSIONS_FILE}}`
    cmds:
      - |
        TMP="$(mktemp)"
        echo '{"version": "2.0.0"}' > "$TMP"
        TASKS='['
        while read LINE; do
          if [[ "${LINE:0:1}" == '*' ]]; then
            TASK_NAME=$(echo "$LINE" | sed "s/'/ADD_QUOTE_BACK_IN/g" | sed 's/^\*.\([^\ ]*\):\(.*\)$/\1/g' | xargs)
            TASK_DESC=$(echo "$LINE" | sed "s/'/ADD_QUOTE_BACK_IN/g" | sed 's/^\*.\([^\ ]*\):\(.*\)$/\2/g' | xargs)
            LABEL="($TASK_NAME): $TASK_DESC"
            LABEL="$(echo "$LABEL" | sed "s/ADD_QUOTE_BACK_IN/'/g")"
            TYPE="shell"
            COMMAND="bash start.sh && task $TASK_NAME"
            TASKS+="{\"label\": \"$LABEL\", \"type\": \"$TYPE\", \"command\": \"$COMMAND\"},"
          fi
        done <<< "$(task --list)"
        jq --arg tasks "${TASKS%?}]" '.tasks = ($tasks | fromjson)' "$TMP" > '{{.TASKS_FILE}}'
      - task: :fix:prettier
        vars:
          CLI_ARGS: '{{.TASKS_FILE}}'
