| 1 | #!/bin/bash |
| 2 | |
| 3 | # Welcome to amusement park! |
| 4 | |
| 5 | [[ "$1" = '--help' ]] || [[ "$1" = '-h' ]] && cat <<EOF && exit |
| 6 | NetBeans docker wrapper for php |
| 7 | =============================== |
| 8 | Anthony Axenov (c) 2023, The MIT License |
| 9 | https://axenov.dev |
| 10 | https://opensource.org/license/mit |
| 11 | Replacement host php interpreter with dockerized one to run & debug cli php scripts. |
| 12 | Usage: |
| 13 | ./$(basename $0) --container=<NAME> [--map=<PATH1>:<PATH2>] [PHP_ARGS] <SCRIPT> [SCRIPT_ARGS] |
| 14 | Arguments: |
| 15 | --container : docker container where your SCRIPT is located. Required. |
| 16 | --map : sources path mapped from the host to container. Not required. |
| 17 | PATH1 is an absolute path to php sources directory on the host. |
| 18 | PATH2 is an absolute path of the same directory inside of container. |
| 19 | Delimiter ':' is required. If PATH1, PATH2 or delimiter is missed |
| 20 | or value is empty then error will be thrown. |
| 21 | PHP_ARGS : arguments you can pass to real php interpreter according to its --help. |
| 22 | Not required. |
| 23 | SCRIPT : a path to script file (.php) to be executed in container. Required. |
| 24 | Note that this file must exist inside or be available from that container. |
| 25 | SCRIPT_ARGS : arguments to call your script with. They will be passed to script as is. |
| 26 | Not required. |
| 27 | Read this article to know how to set this helper as interpreter for NetBeans: |
| 28 | ru: https://axenov.dev/netbeans-php-docker-xdebug-cli |
| 29 | en: https://axenov.dev/en/netbeans-php-docker-xdebug-cli-en |
| 30 | EOF |
| 31 | |
| 32 | pwd=$(pwd) # current working directory |
| 33 | cmdline=($@) # copy currently called command line to array |
| 34 | collect_php_args=1 # should we collect php args or script ones? |
| 35 | quiet=0 # should we print some useful data before executing? |
| 36 | |
| 37 | # find a path where this wrapper is located |
| 38 | wrapper_dir="$(dirname $0)" |
| 39 | |
| 40 | # find a path where project is probably located |
| 41 | project_dir="$(dirname $wrapper_dir)" |
| 42 | |
| 43 | # here we check if this wrapper is global or local |
| 44 | # but if it is set as global from nbproject dir of |
| 45 | # current project then it is not detected as global |
| 46 | # anyway behavior will be correct |
| 47 | nbproject="$(basename $wrapper_dir)" |
| 48 | [ "$nbproject" = 'nbproject' ] && is_global=0 || is_global=1 |
| 49 | |
| 50 | # prepare new array to collect php args |
| 51 | declare -a php_cmd=("docker" "exec") |
| 52 | |
| 53 | # and another one for script args |
| 54 | declare -a script_args=() |
| 55 | |
| 56 | # and one more for directory mapping |
| 57 | declare -a map_arr=() |
| 58 | |
| 59 | # iterate over arguments we received from netbeans |
| 60 | for arg in "${cmdline[@]}"; do |
| 61 | |
| 62 | # if this is a container name |
| 63 | if [ "${arg::11}" = '--container' ]; then |
| 64 | container="${arg:12}" # save it |
| 65 | php_cmd+=("$container" 'php') # add php itself |
| 66 | continue # jump to next iteration |
| 67 | fi |
| 68 | |
| 69 | # if this is a path map |
| 70 | if [ "${arg::5}" = '--map' ]; then |
| 71 | map="${arg:6}" # save it |
| 72 | map_arr=(${map//:/ }) # split it and check if it is correct |
| 73 | if [ -z "${map_arr[0]}" ] || [ -z "${map_arr[1]}" ]; then |
| 74 | echo "ERROR: directory map is incorrect!" |
| 75 | echo "Use $0 --help to get info about how to use this wrapper." |
| 76 | echo "Exit code 3." |
| 77 | exit 3 |
| 78 | fi |
| 79 | continue # jump to next iteration |
| 80 | fi |
| 81 | |
| 82 | # if this is a container name |
| 83 | if [ "${arg::7}" = '--quiet' ]; then |
| 84 | quiet=1 |
| 85 | continue # jump to next iteration |
| 86 | fi |
| 87 | |
| 88 | # if this is an absolute path to a script file |
| 89 | if [ -f "$arg" ]; then |
| 90 | # make its path correct for container |
| 91 | if [ "$map" ]; then # when paths are mapped |
| 92 | # remove first part of map from an absolute filepath and append result to second map part |
| 93 | filepath="${map_arr[1]}${arg##${map_arr[0]}}" |
| 94 | else # when paths are NOT mapped |
| 95 | # remove project path from absolute filepath |
| 96 | filepath="${arg##$project_dir/}" |
| 97 | fi |
| 98 | php_cmd+=("$filepath") # append php args with filepath |
| 99 | collect_php_args=0 # now we need to collect script args |
| 100 | continue # jump to next iteration |
| 101 | fi |
| 102 | |
| 103 | if [ "$collect_php_args" = 1 ]; then # if we collect php args |
| 104 | php_cmd+=("$arg") # add current arg to php args as is |
| 105 | continue # jump to next iteration |
| 106 | fi |
| 107 | |
| 108 | script_args+=("$arg") # otherwise add current arg to script args as is |
| 109 | done |
| 110 | |
| 111 | # docker container name is required so we must halt here if there is no one |
| 112 | if [ -z "$container" ]; then |
| 113 | echo "ERROR: no docker container is specified!" >&2 |
| 114 | echo "Use $0 --help to get info about how to use this wrapper." >&2 |
| 115 | echo "Exit code 1." >&2 |
| 116 | exit 1 |
| 117 | fi |
| 118 | |
| 119 | # path to php script is also required so we must halt here too if there is no one |
| 120 | if [ -z "$filepath" ]; then |
| 121 | echo "ERROR: no script filepath is specified!" >&2 |
| 122 | echo "Use $0 --help to get info about how to use this wrapper." >&2 |
| 123 | echo "Exit code 2." >&2 |
| 124 | exit 2 |
| 125 | fi |
| 126 | |
| 127 | cmdline="${php_cmd[*]} ${script_args[*]}" # make a command to execute |
| 128 | |
| 129 | # print some important data collected above |
| 130 | if [ "$quiet" = 0 ]; then |
| 131 | echo "NetBeans docker wrapper for php" |
| 132 | echo "===============================" |
| 133 | echo -e "Container name: $container" |
| 134 | echo -e "Script path: $filepath" |
| 135 | echo -e "Directory mapping: ${map:-(none)}" |
| 136 | echo -e "Command line:\n$cmdline\n" |
| 137 | fi |
| 138 | |
| 139 | # some debug output |
| 140 | # echo "=== some debug output =========" |
| 141 | # cat <<EOF | column -t |
| 142 | # is_global $is_global |
| 143 | # container $container |
| 144 | # pwd $pwd |
| 145 | # wrapper_dir $wrapper_dir |
| 146 | # nbproject $nbproject |
| 147 | # project_dir $project_dir |
| 148 | # map $map |
| 149 | # map_arr[0] ${map_arr[0]} |
| 150 | # map_arr[1] ${map_arr[1]} |
| 151 | # filepath $filepath |
| 152 | # EOF |
| 153 | # echo "===============================" |
| 154 | |
| 155 | $cmdline # execute |
| 156 | |
| 157 | # that's folks! |
| 158 |
anthony / Interpreter helper to debug dockerized cli php scripts in NetBeans
Last active 2 months ago
Revision 887b5d245a05fbd0f81a02c0ed876cf913d9861f