---
version: '3'

vars:
  PROJECT_COMMON_URL: https://gitlab.com/megabyte-labs/common/{{.REPOSITORY_TYPE}}.git
  SHARED_FOLDER: .shared

tasks:
  boilerplate:
    deps:
      - :install:software:coreutils
      - :install:software:jq
    log:
      error: There was an error in the boilerplate logic
      start: Checking if `.boilerplate/` code should be added
      success: Boilerplate logic completed successfully
    cmds:
      - |
        {{if (eq OS "darwin")}}PATH="$(brew --prefix)/opt/coreutils/libexec/gnubin:$PATH"{{end}}
        if [[ "$(jq -r '.blueprint.boilerplate' package.json)" == 'true' ]]; then
          for ENTITY in .boilerplate/*; do
            TARGET="$(echo $ENTITY | sed 's/^.boilerplate\///')"
            rm -rf "$TARGET"
          done
        fi
        GREP=""
        for ENTITY in .boilerplate/*; do
          TARGET="$(echo $ENTITY | sed 's/^.boilerplate\///')"
          GREP="$TARGET"\|"$GREP"
        done
        GREP="$(echo $GREP | sed 's/\\|$//')"
        if ! ls -la | grep "$GREP" &> /dev/null; then
          .config/log info "Adding boilerplate code since there are no overlapping files"
          cp -rT .boilerplate .
          .config/log success "Scaffolded the project with boilerplate code!"
        fi
      - rm -rf .boilerplate

  clean:
    log:
      error: Failed to run `rm -rf {{.SHARED_FOLDER}}`
      start: Running `rm -rf {{.SHARED_FOLDER}}`
      success: Successfully ran `rm -rf {{.SHARED_FOLDER}}`
    cmds:
      - rm -rf {{.SHARED_FOLDER}}

  clone:
    log:
      error: Error cloning {{.PROJECT_COMMON_URL}}
      start: Cloning {{.PROJECT_COMMON_URL}}
      success: Finished cloning {{.PROJECT_COMMON_URL}}
    cmds:
      - rm -rf {{.SHARED_FOLDER}}
      - git clone --depth=1 {{.PROJECT_COMMON_URL}} {{.SHARED_FOLDER}}
      - rm -rf ./{{.SHARED_FOLDER}}/.git

  copy:
    deps:
      - :install:software:coreutils
      - :install:software:yq
    log:
      error: Error copying pre-generated files
      start: Copying pre-generated files
      success: Copied pre-generated files
    cmds:
      - |
        if [ -f .gitlab-ci.yml ]; then
          mv .gitlab-ci.yml old.gitlab-ci.yml
        fi
      - |
        {{if (eq OS "darwin")}}PATH="$(brew --prefix)/opt/coreutils/libexec/gnubin:$PATH"{{end}}
        cp -rT {{.SHARED_FOLDER}}/_generated_/common .
        cp -rT {{.SHARED_FOLDER}}/_generated_/{{.REPOSITORY_SUBTYPE}} .
      - |
        if [ -f old.gitlab-ci.yml ]; then
          yq eval-all -i 'select(fileIndex == 0) * select(fileIndex == 1)' old.gitlab-ci.yml .gitlab-ci.yml
          mv old.gitlab-ci.yml .gitlab-ci.yml
        fi
      - task: :common:husky:permissions
      - task: save:common:keywords

  merge:package:overrides:
    deps:
      - :install:software:jq
    summary: |
      # Merge data from package.json's '.blueprint.jsonOverrides' key

      If you wanted to ensure that the project's package.json file always has its 'xyz' key
      equal to 'abc' then you would add the following to the package.json file:

      ```json
      {
        ...
        "blueprint": {
          "jsonOverrides": {
            "package.json": {
              "xyz": "abc"
            }
          }
        }
      }
      ```

      In the above example, you could also set the value of package.json equal to the path of some other
      JSON file in the project that you would like to override.

      This method is here in case there are any key/values that you would like to freeze.
    log:
      error: Error processing `.blueprint.jsonOverrides` in `package.json`
      start: Processing `.blueprint.jsonOverrides` in `package.json`
      success: Finished processing `.blueprint.jsonOverrides` in `package.json`
    cmds:
      - |
        if [ "$(jq -r '.blueprint.jsonOverrides' package.json)" != 'null' ]; then
          jq -r '.blueprint.jsonOverrides | keys[]' package.json | while read FILE_PATH; do
            VALUE="$(jq --arg filepath "$FILE_PATH" '.blueprint.jsonOverrides[$filepath]' "$FILE_PATH")"
            TMP="$(mktemp)"
            jq --arg value "$VALUE" -s '.[] + ($value | fromjson)' "$FILE_PATH" > "$TMP"
            mv "$TMP" "$FILE_PATH"
          done
        fi

  save:common:keywords:
    deps:
      - :install:software:jq
    log:
      error: Failed to save common keywords
      start: Saving common keywords
      success: Saved common keywords
    cmds:
      - |
        KEYWORDS="$(jq -r '.keywords' package.json.liquid)"
        jq -n --arg keywords "$KEYWORDS" '.keywords = ($keywords | fromjson | unique)' > .config/common-keywords.json.liquid

  template:
    cmds:
      - task: :upstream:variables
        vars:
          INPUT_FILE: .config/variables.json
          OUTPUT_FILE: .variables.json
      - task: :upstream:template