---
version: '3'

vars:
  COMMON_FILES_URL: https://gitlab.com/megabyte-labs/common/shared.git
  COMMON_FOLDER: .common

tasks:
  clean:
    log:
      error: Error while running `rm -rf {{.COMMON_FOLDER}}`
      start: Running `rm -rf {{.COMMON_FOLDER}}`
      success: Successfully ran `rm -rf {{.COMMON_FOLDER}}`
    cmds:
      - rm -rf {{.COMMON_FOLDER}}

  clone:
    log:
      error: Error in cloning logic for {{.COMMON_FILES_URL}}
      start: Cloning {{.COMMON_FILES_URL}}
      success: Successfully cloned {{.COMMON_FILES_URL}}
    cmds:
      - rm -rf {{.COMMON_FOLDER}}
      - git clone --depth=1 {{.COMMON_FILES_URL}} {{.COMMON_FOLDER}}
      - rm -rf {{.COMMON_FOLDER}}/.git

  copy:
    deps:
      - :install:software:coreutils
      - :install:software:yq
    log:
      error: Error copying common files
      start: Copying common files
      success: Copied common files
    cmds:
      - mv .gitlab-ci.yml old.gitlab-ci.yml
      - |
        {{if (eq OS "darwin")}}PATH="$(brew --prefix)/opt/coreutils/libexec/gnubin:$PATH"{{end}}
        cp -rT ./{{.COMMON_FOLDER}}/common/ .
      - |
        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
      - task: :common:husky:permissions
      - mv {{.COMMON_FOLDER}}/.gitlab-ci.yml .gitlab-ci.yml

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