|
| 1 | +#!/bin/bash |
| 2 | +# This is a Taskfile |
| 3 | +# (https://medium.com/@adrian_cooney/introducing-the-taskfile-5ddfe7ed83bd) |
| 4 | +# |
| 5 | +# It is a plain-old-bashfile, with a few helper functions. |
| 6 | +# Each task should be a single function. |
| 7 | +# Tasks that have dependencies can call the other functions as part of their |
| 8 | +# work; you can also create meta-tasks that simply call other tasks. |
| 9 | +# |
| 10 | +# To provide detailed help for a given task, write a function named {task}_help. |
| 11 | +# |
| 12 | +# Functions prefixed with tf_ or suffixed with _help will be omitted from help listings. |
| 13 | + |
| 14 | +set -e |
| 15 | + |
| 16 | +## Color functions |
| 17 | +## Usage: {color} "arguments" "go" "here" |
| 18 | +## Use these to add color to your output. |
| 19 | +tf_print_in_color() { |
| 20 | + local color="$1" |
| 21 | + shift |
| 22 | + if [[ -z ${NO_COLOR+x} ]]; then |
| 23 | + printf "$color%b\e[0m\n" "$*"; |
| 24 | + else |
| 25 | + printf "%b\n" "$*"; |
| 26 | + fi |
| 27 | +} |
| 28 | + |
| 29 | +tf_red() { tf_print_in_color "\e[31m" "$*"; } |
| 30 | +tf_green() { tf_print_in_color "\e[32m" "$*"; } |
| 31 | +tf_yellow() { tf_print_in_color "\e[33m" "$*"; } |
| 32 | +tf_blue() { tf_print_in_color "\e[34m" "$*"; } |
| 33 | +tf_magenta() { tf_print_in_color "\e[35m" "$*"; } |
| 34 | +tf_cyan() { tf_print_in_color "\e[36m" "$*"; } |
| 35 | +tf_bold() { tf_print_in_color "\e[1m" "$*"; } |
| 36 | +tf_underlined() { tf_print_in_color "\e[4m" "$*"; } |
| 37 | +tf_red_bold() { tf_print_in_color "\e[1;31m" "$*"; } |
| 38 | +tf_green_bold() { tf_print_in_color "\e[1;32m" "$*"; } |
| 39 | +tf_yellow_bold() { tf_print_in_color "\e[1;33m" "$*"; } |
| 40 | +tf_blue_bold() { tf_print_in_color "\e[1;34m" "$*"; } |
| 41 | +tf_magenta_bold() { tf_print_in_color "\e[1;35m" "$*"; } |
| 42 | +tf_cyan_bold() { tf_print_in_color "\e[1;36m" "$*"; } |
| 43 | +tf_red_underlined() { tf_print_in_color "\e[4;31m" "$*"; } |
| 44 | +tf_green_underlined() { tf_print_in_color "\e[4;32m" "$*"; } |
| 45 | +tf_yellow_underlined() { tf_print_in_color "\e[4;33m" "$*"; } |
| 46 | +tf_blue_underlined() { tf_print_in_color "\e[4;34m" "$*"; } |
| 47 | +tf_magenta_underlined() { tf_print_in_color "\e[4;35m" "$*"; } |
| 48 | +tf_cyan_underlined() { tf_print_in_color "\e[4;36m" "$*"; } |
| 49 | + |
| 50 | +## Help function |
| 51 | +function help { |
| 52 | + if [[ $# -gt 0 ]]; then |
| 53 | + if [[ "$(LC_ALL=C type -t "$1_help")" == "function" ]]; then |
| 54 | + "$1_help" |
| 55 | + else |
| 56 | + tf_blue "No additional help found for task '$1'" |
| 57 | + fi |
| 58 | + else |
| 59 | + tf_green "Usage:" |
| 60 | + echo " $0 <task> <args>" |
| 61 | + echo |
| 62 | + tf_green "Available Tasks:" |
| 63 | + compgen -A function | cat -n | grep -v -P "^\s+\d+\s+tf_" | grep -v -P "_help\$" | sed -r -e "s/^[[:space:]]+[[:digit:]]+[[:space:]]+/ /" |
| 64 | + echo |
| 65 | + tf_green "Run '$0 help {task}' to get help on a given task" |
| 66 | + fi |
| 67 | +} |
| 68 | + |
| 69 | +## Task functions |
| 70 | + |
| 71 | +# Helper function - implode |
| 72 | +# - First argument is string to use when imploding |
| 73 | +# - All remaining arguments are the values to implode |
| 74 | +# - Specify an array using "${array[@]}" as a second argument |
| 75 | +function tf_implode { |
| 76 | + local d=${1-} f=${2-} |
| 77 | + if shift 2; then |
| 78 | + printf %s "$f" "${@/#/$d}" |
| 79 | + fi |
| 80 | +} |
| 81 | + |
| 82 | +function stubs { |
| 83 | + local tmp |
| 84 | + local stub |
| 85 | + |
| 86 | + if [[ -z "${GH_TOKEN}" ]]; then |
| 87 | + tf_red "Missing GH_TOKEN; please provide it in your env or as part of your command line invocation" |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | + |
| 91 | + if ! command -v git; then |
| 92 | + tf_red "The 'git' command is required to use this task." |
| 93 | + exit 1 |
| 94 | + fi |
| 95 | + |
| 96 | + tmp="$(mktemp -d)" |
| 97 | + |
| 98 | + tf_cyan "Cloning zendhq repo" |
| 99 | + git clone "https://oauth2:${GH_TOKEN}@github.com/perforce-zend/zendhq.git" "${tmp}/zendhq" |
| 100 | + |
| 101 | + tf_cyan "Isolating stubs" |
| 102 | + mkdir -p "${tmp}/stubs" |
| 103 | + printf "<?php" > "${tmp}/stubs/stubs.php" |
| 104 | + for stub in "${tmp}"/zendhq/src/zendphp/ext/zendhq/jq/8.2/*.stub.php; do |
| 105 | + sed -E '/<\?php/d' < "${stub}" >> "${tmp}/stubs/stubs.php" |
| 106 | + done |
| 107 | + |
| 108 | + # @todo Update stubs such that: |
| 109 | + # - JobQueue constructor argument has a default argument |
| 110 | + # - Annotations are added for the JobOptions and QueueDefinition classes |
| 111 | + # to detail the priority/output capture allowed values |
| 112 | + |
| 113 | + tf_cyan "Copying stubs to package" |
| 114 | + cp -a "${tmp}/stubs/stubs.php" "src/" |
| 115 | + |
| 116 | + tf_cyan "Cleanup" |
| 117 | + rm -rf "${tmp}" |
| 118 | + |
| 119 | + tf_green "[DONE]" |
| 120 | +} |
| 121 | + |
| 122 | +function stubs_help { |
| 123 | + tf_green "stubs" |
| 124 | + echo " Retrieve JQ PHP API stub files, prepare them for use, and copy them" |
| 125 | + echo " into the applications." |
| 126 | + echo "" |
| 127 | + echo " This command requires the GH_TOKEN env be present. it will be used" |
| 128 | + echo " to seed the oauth2 token for cloning from Zend by Perforce's zendhq repository." |
| 129 | + echo " It should only be run by maintainers." |
| 130 | +} |
| 131 | + |
| 132 | +## Default task |
| 133 | +## Change this to call a different task by default |
| 134 | +function tf_default { |
| 135 | + help "$@" |
| 136 | +} |
| 137 | + |
| 138 | +# Switch to the directory containing the Taskfile |
| 139 | +cd "$(dirname "$(realpath "$0")")" |
| 140 | + |
| 141 | +"${@:-tf_default}" |
0 commit comments