|
| 1 | +# this script assumes gawk! (busybox "awk" is not quite sufficient) |
| 2 | + |
| 3 | +# see https://github.com/docker-library/php or https://github.com/docker-library/golang for examples of usage ("apply-templates.sh") |
| 4 | + |
| 5 | +# escape an arbitrary string for passing back to jq as program input |
| 6 | +function jq_escape(str, # parameters |
| 7 | + prog, e, out) # locals |
| 8 | +{ |
| 9 | + prog = "jq --raw-input --slurp ." |
| 10 | + printf "%s", str |& prog |
| 11 | + close(prog, "to") |
| 12 | + prog |& getline out |
| 13 | + e = close(prog) |
| 14 | + if (e != 0) { |
| 15 | + exit(e) |
| 16 | + } |
| 17 | + return out |
| 18 | +} |
| 19 | + |
| 20 | +BEGIN { |
| 21 | + jq_expr_defs = "" |
| 22 | + jq_expr = "" |
| 23 | + agg_jq = "" |
| 24 | + agg_text = "" |
| 25 | + |
| 26 | + OPEN = "{{" |
| 27 | + CLOSE = "}}" |
| 28 | + CLOSE_EAT_EOL = "-" CLOSE ORS |
| 29 | +} |
| 30 | + |
| 31 | +function trim(str) { |
| 32 | + sub(/^[[:space:]]+/, "", str) |
| 33 | + sub(/[[:space:]]+$/, "", str) |
| 34 | + return str |
| 35 | +} |
| 36 | +function append(str) { |
| 37 | + if (jq_expr && jq_expr !~ /\($/ && str !~ /^\)/) { |
| 38 | + jq_expr = jq_expr "\n+ " |
| 39 | + } else if (jq_expr) { |
| 40 | + jq_expr = jq_expr "\n" |
| 41 | + } |
| 42 | + jq_expr = jq_expr str |
| 43 | +} |
| 44 | +function append_string(str) { |
| 45 | + if (!str) return |
| 46 | + str = jq_escape(str) |
| 47 | + append(str) |
| 48 | +} |
| 49 | +function append_jq(expr) { |
| 50 | + if (!expr) return |
| 51 | + expr = trim(expr) |
| 52 | + if (!expr) return |
| 53 | + if (expr ~ /^(def|include|import)[[:space:]]/) { # a few things need to go at the start of our "script" |
| 54 | + jq_expr_defs = jq_expr_defs expr ";\n" |
| 55 | + return |
| 56 | + } |
| 57 | + # if expr doesn't begin with ")" or end with "(", wrap it in parenthesis (so our addition chain works properly) |
| 58 | + if (expr !~ /^\)/) expr = "(" expr |
| 59 | + if (expr !~ /\($/) expr = expr ")" |
| 60 | + append(expr) |
| 61 | +} |
| 62 | + |
| 63 | +{ |
| 64 | + line = $0 ORS |
| 65 | + |
| 66 | + i = 0 |
| 67 | + if (agg_jq || (i = index(line, OPEN))) { |
| 68 | + if (i) { |
| 69 | + agg_text = agg_text substr(line, 1, i - 1) |
| 70 | + line = substr(line, i) |
| 71 | + } |
| 72 | + append_string(agg_text) |
| 73 | + agg_text = "" |
| 74 | + |
| 75 | + agg_jq = agg_jq line |
| 76 | + line = "" |
| 77 | + |
| 78 | + if (!index(agg_jq, CLOSE)) { |
| 79 | + next |
| 80 | + } |
| 81 | + |
| 82 | + while (i = index(agg_jq, OPEN)) { |
| 83 | + line = substr(agg_jq, 1, i - 1) |
| 84 | + agg_jq = substr(agg_jq, i + length(OPEN)) |
| 85 | + if (i = index(agg_jq, CLOSE_EAT_EOL)) { |
| 86 | + expr = substr(agg_jq, 1, i - 1) |
| 87 | + agg_jq = substr(agg_jq, i + length(CLOSE_EAT_EOL)) |
| 88 | + } |
| 89 | + else { |
| 90 | + i = index(agg_jq, CLOSE) |
| 91 | + expr = substr(agg_jq, 1, i - 1) |
| 92 | + agg_jq = substr(agg_jq, i + length(CLOSE)) |
| 93 | + } |
| 94 | + append_string(line) |
| 95 | + append_jq(expr) |
| 96 | + } |
| 97 | + line = agg_jq |
| 98 | + agg_jq = "" |
| 99 | + } |
| 100 | + |
| 101 | + if (line) { |
| 102 | + agg_text = agg_text line |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +END { |
| 107 | + append_string(agg_text) |
| 108 | + agg_text = "" |
| 109 | + |
| 110 | + append_jq(agg_jq) |
| 111 | + agg_jq = "" |
| 112 | + |
| 113 | + jq_expr = "if env.version then .[env.version] else . end | (\n" jq_expr "\n)" |
| 114 | + jq_expr = jq_expr_defs jq_expr |
| 115 | + |
| 116 | + if (ENVIRON["DEBUG"]) { |
| 117 | + print jq_expr > "/dev/stderr" |
| 118 | + } |
| 119 | + |
| 120 | + prog = "jq --join-output --from-file /dev/stdin versions.json" |
| 121 | + printf "%s", jq_expr | prog |
| 122 | + |
| 123 | + e = close(prog) |
| 124 | + if (e != 0) { |
| 125 | + exit(e) |
| 126 | + } |
| 127 | +} |
0 commit comments