calver 1.1.6

Calver: A lightweight command-line tool for effortless Calendar Versioning increments.
Documentation
---
# https://taskfile.dev
#
# Do not edit this file, instead use Taskfile.project.yml.
#
version: '3'

dotenv: ['.env']

vars:
  DIR_TASKFILES: Taskfile.d
  FILE_TASK_START: "task-start-{{.TASK}}.txt"
  FILE_TASK_START1: "task-start-{{.TASK}}1.txt"
  TT_GIT_REPO: https://gitlab.com/op_so/task/task-templates/-/raw

includes:
  00:
    taskfile: Taskfile.project.yml
    optional: true
    # BEGIN-INCLUDES-TEMPLATES
  git:
    taskfile: Taskfile.d/git.yml
    optional: true
  lint:
    taskfile: Taskfile.d/lint.yml
    optional: true
    # END-INCLUDES-TEMPLATES

tasks:

  install-templates:
    desc: "[CORE] Download/update tasks templates. Arguments: [PROXY|P=http://proxy:8080] [PROXY_CREDENTIALS|PC=username:password] (*)"
    summary: |
      [CORE] Download/update tasks templates.
      Usage: task install-templates [PROXY|P=http://proxy_url:proxy_port] [PROXY_CREDENTIALS|PC=username:password]

      Arguments:
       PROXY | P               Proxy URL (optional)
       PROXY_CREDENTIALS | PC  Proxy credentials username:password (optional)

      Requirements:
        - wget or curl
    vars:
      PROXY: '{{default .P .PROXY}}'
      D_PROXY: '{{default .TASK_PROXY .PROXY}}'
      PROXY_CREDENTIALS: '{{default .PC .PROXY_CREDENTIALS}}'
      TASK_TEMPLATES:
        sh: task 00:00-get-list-templates
      FILE: '{{default .F .FILE}}'
      D_FILE: '{{default "Taskfile.yml" .FILE}}'
      BEGIN: "    # BEGIN-INCLUDES-TEMPLATES"
      PATTERN_BEGIN: "^{{.BEGIN}}"
      END: "# END-INCLUDES-TEMPLATES"
    cmds:
      - date > {{.FILE_TASK_START}}
      - defer: rm -f {{.FILE_TASK_START}}
      - mkdir -p "{{.DIR_TASKFILES}}"
      - if [ -z "{{.TASK_TEMPLATES}}" ]; then echo "Error TASK_TEMPLATES variable is empty in Taskfile.project.yml file" && exit 1; else echo "Download templates -> {{.TASK_TEMPLATES}}"; fi
      - |
        line_include_template_begin=$(grep -Fn '{{.BEGIN}}' "{{.D_FILE}}" | head -n 1 | cut -d ':' -f 1)
        line_delete_begin=$(expr $line_include_template_begin + 1)
        line_include_template_end=$(grep -Fn '{{.END}}' "{{.D_FILE}}" | head -n 1 | cut -d ':' -f 1)
        line_delete_end=$(expr $line_include_template_end - 1)
        if [ $line_delete_end -ge $line_delete_begin ]; then
          sed -i'.bu' "${line_delete_begin},${line_delete_end}d" "{{.D_FILE}}"
        fi
      - defer: rm -f "{{.D_FILE}}".bu
      - |
        includes="{{.BEGIN}}"
        for t in $(echo "{{.TASK_TEMPLATES}}" | tr "," " "); do
          if echo "$t" | grep -q "\[" 2>/dev/null; then
            task=$(echo "$t" | cut -d '[' -f 1)
          else
            task="$t"
          fi
          includes="$includes\\n  ${task}:\\n    taskfile: {{.DIR_TASKFILES}}/${task}.yml\\n    optional: true"
        done
        sed -i'.bu' "s={{.PATTERN_BEGIN}}=${includes}=g" "{{.D_FILE}}"
      - |
        templates_list=$(task 00:00-get-list-templates)
        for t in $(echo "{{.TASK_TEMPLATES}}" | tr "," " "); do
          if echo "$t" | grep -q "\[" 2>/dev/null; then
            task=$(echo "$t" | cut -d '[' -f 1)
            v=$(echo "$t" | sed -e 's/.*\[\(.*\)\]/\1/')
            version="v${v}"
          else
            task="$t"
            version="main"
          fi
          echo "Download template: $task version: $version"
          task download-template TEMPLATE=${task}.yml VERSION=$version PROXY={{.D_PROXY}} PROXY_CREDENTIALS={{.PROXY_CREDENTIALS}}
        done
      - echo "Install templates Start $(cat {{.FILE_TASK_START}}) - End $(date)"
    silent: true

  usage:
    desc: "[CORE] Show the usage of a task. Arguments: TSK|T=task-name [FILE|F=Taskfile.dist.yml] (*)"
    summary: |
      [CORE] Show the usage of a task.
      Usage: task [-t Taskfile.dist.yml] usage TSK|T=<task-name> [FILE|F=Taskfile.dist.yml]

      Arguments:
       TSK | T  Name of the task (required)
       FILE | F Taskfile path (optional, by default Taskfile.yml)
    vars:
      TSK: '{{default .T .TSK}}'
      FILE: '{{default .F .FILE}}'
      D_FILE: '{{default "Taskfile.yml" .FILE}}'
    cmds:
      - |
        if [ ! -f "{{.D_FILE}}" ]; then
          echo "{{.D_FILE}} does not exist!"
          exit 1
        fi
      - task -t "{{.D_FILE}}" --summary "{{.TSK}}" | sed '/^dependencies:$/,$d' | sed '/^commands:$/,$d'
    preconditions:
      - sh: test -n "{{.TSK}}" || test -n "{{.T}}"
        msg: "TSK|T argument is required"
    silent: true

  default:
    desc: "[CORE] List of available tasks. Arguments: [FILE|F=Taskfile.project.yml] (*)"
    summary: |
      [CORE] Show the list of available tasks.
      Usage: task [FILE|F=Taskfile.project.yml]

      Arguments:
       FILE | F Taskfile project path (optional, by default Taskfile.project.yml)
    vars:
      FILE: '{{default .F .FILE}}'
      D_FILE: '{{default "Taskfile.project.yml" .FILE}}'
    cmds:
      - |
        if [ -d "{{.DIR_TASKFILES}}" ]; then
          for template in {{.DIR_TASKFILES}}/*.yml
          do
            template_name=$(basename "$template")
            printf "\033[0;33m[ %s: ]\033[0m " "${template_name%.yml}" && task --list -t "$template" || true
            echo ""
          done
        fi
      - |
        if [ ! -z "{{.D_FILE}}" ]; then
            printf "\033[0;33m[ %s: ]\033[0m " "00" && task --list -t "{{.D_FILE}}" || true
        fi
      - echo ""
      - echo " <task install-templates [PROXY|P=http://proxy:8080]> to install or update templates (Variable TASK_TEMPLATES in Taskfile.project.yml file)."
      - echo " (*) <task usage TSK=task-name> will show the usage of the task."
    silent: true

  download-template:
    cmds:
      - |
        if [ -x "$(command -v curl)" ]; then
          if [ -z "{{.PROXY}}" ]; then
            curl --progress-bar -o "{{.DIR_TASKFILES}}/{{.TEMPLATE}}" "{{.TT_GIT_REPO}}/{{.VERSION}}/{{.DIR_TASKFILES}}/{{.TEMPLATE}}"
          else
            if [ -z "{{.PROXY_CREDENTIALS}}" ]; then
              echo "Proxy: {{.PROXY}}"
              curl -x "{{.PROXY}}" --progress-bar -o "{{.DIR_TASKFILES}}/{{.TEMPLATE}}" "{{.TT_GIT_REPO}}/{{.VERSION}}/{{.DIR_TASKFILES}}/{{.TEMPLATE}}"
            else
              echo "Proxy with auth: {{.PROXY}}"
              curl -U "{{.PROXY_CREDENTIALS}}" -x "{{.PROXY}}" --progress-bar -o "{{.DIR_TASKFILES}}/{{.TEMPLATE}}" "{{.TT_GIT_REPO}}/{{.VERSION}}/{{.DIR_TASKFILES}}/{{.TEMPLATE}}" --proxy-anyauth
            fi
          fi
        else
          if [ -z "{{.PROXY}}" ]; then
            wget -cq -O "{{.DIR_TASKFILES}}/{{.TEMPLATE}}" "{{.TT_GIT_REPO}}/{{.VERSION}}/{{.DIR_TASKFILES}}/{{.TEMPLATE}}"
          else
            if [ -z "{{.PROXY_CREDENTIALS}}" ]; then
              proxy={{.PROXY}}
              echo "Proxy: $proxy"
            else
              proto=$(echo {{.PROXY}} | sed -e's,^\(.*://\).*,\1,g')
              url=$(echo {{.PROXY}} | sed -e's,^.*://\(.*\),\1,g')
              proxy="${proto}{{.PROXY_CREDENTIALS}}@${url}"
              echo "Proxy: ${proto}****:****@${url}"
            fi
            export use_proxy=on && export http_proxy=$proxy && export https_proxy=$proxy && wget -cq -O "{{.DIR_TASKFILES}}/{{.TEMPLATE}}" "{{.TT_GIT_REPO}}/{{.VERSION}}/{{.DIR_TASKFILES}}/{{.TEMPLATE}}"
          fi
        fi
    preconditions:
      - sh: test -n "{{.TEMPLATE}}"
        msg: "TEMPLATE argument is required"
      - sh: test -n "{{.VERSION}}"
        msg: "VERSION argument is required"
      - sh: command -v curl || command -v wget
        msg: "curl or wget are not installed"
    silent: true