-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-doc.sh
More file actions
executable file
·128 lines (116 loc) · 4.74 KB
/
Copy pathgenerate-doc.sh
File metadata and controls
executable file
·128 lines (116 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/bash
# Generate the README Inputs table from action.yml.
#
# Replaces tj-actions/auto-doc: parses the action's inputs and rewrites the
# GitHub-flavoured Markdown table between the AUTO-DOC-INPUT markers in
# README.md. Pure bash + awk so the repo needs no Python/uv toolchain.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
ACTION="${ROOT}/action.yml"
README="${ROOT}/README.md"
START="<!-- AUTO-DOC-INPUT:START - Do not remove or modify this section -->"
END="<!-- AUTO-DOC-INPUT:END -->"
if ! grep -qF "${START}" "${README}" || ! grep -qF "${END}" "${README}"; then
echo "AUTO-DOC-INPUT markers not found in README.md" >&2
exit 1
fi
# Render the README inputs table from action.yml. awk emits one finished
# Markdown row per input; only the constrained GitHub Action inputs schema is
# handled (2-space input names, 4-space properties, optional `|`/`>`
# block-scalar descriptions).
rows="$(
awk '
function trim(s){ sub(/^[ \t]+/, "", s); sub(/[ \t]+$/, "", s); return s }
# unwrap a scalar: strip matching surrounding quotes (inside which "#" is
# literal), otherwise strip an inline " # comment" from a plain scalar.
function scalar(s){
s = trim(s)
if (s ~ /^".*"$/ || s ~ /^'"'"'.*'"'"'$/) return substr(s, 2, length(s) - 2)
sub(/[ \t]+#.*$/, "", s)
return trim(s)
}
# render a (newline-joined) description into a single Markdown table cell:
# wrapped prose joins with spaces; "* " lines become <br>-separated bullets.
function render(raw, nl, i, line, arr, parts, np, out){
np = 0; nl = split(raw, arr, "\n")
for (i = 1; i <= nl; i++) {
line = trim(arr[i])
if (line == "") continue
if (substr(line, 1, 2) == "* ") parts[++np] = "<br>\342\200\242 " trim(substr(line, 3))
else if (np > 0) parts[np] = parts[np] " " line
else parts[++np] = line
}
out = ""
for (i = 1; i <= np; i++) out = out parts[i]
return out
}
# emit the finished Markdown row; the downstream sort orders rows by the
# input name that follows the identical "| `" prefix.
function flush( defcell){
if (cur == "") return
defcell = (def == "") ? "" : "`" def "`"
printf "| `%s` | string | %s | %s | %s |\n", cur, req, defcell, render(desc)
}
BEGIN { in_inputs = 0; collecting = 0; cur = "" }
{
line = $0; sub(/\r$/, "", line)
p = match(line, /[^ ]/); ind = (p ? p - 1 : length(line))
blank = (line ~ /^[ \t]*$/)
if (collecting) {
if (blank) { desc = desc "\n"; next }
if (ind > blockind) { desc = desc (desc == "" ? "" : "\n") line; next }
collecting = 0 # dedent: fall through and reprocess this line
}
if (blank) next
if (line ~ /^[ \t]*#/) next # full-line comment
if (ind == 0) { # top-level key
flush(); cur = ""
in_inputs = (line ~ /^inputs:[ \t]*$/) ? 1 : 0
next
}
if (!in_inputs) next
if (ind == 2) { # new input name
flush()
key = line; sub(/:.*$/, "", key); cur = trim(key)
req = "false"; def = ""; desc = ""
next
}
if (ind >= 4 && cur != "") { # input property
prop = trim(line)
if (prop ~ /^description:/) {
val = prop; sub(/^description:[ \t]*/, "", val)
if (val ~ /^[|>]/) { collecting = 1; blockind = ind; desc = "" }
else desc = scalar(val)
} else if (prop ~ /^default:/) {
val = prop; sub(/^default:[ \t]*/, "", val); def = scalar(val)
} else if (prop ~ /^required:/) {
val = prop; sub(/^required:[ \t]*/, "", val); val = tolower(scalar(val))
req = (val == "true" || val == "yes" || val == "on") ? "true" : "false"
}
next
}
}
END { flush() }
' "${ACTION}" | LC_ALL=C sort
)"
# Assemble the table; the rows already carry their Markdown formatting.
table="| Input | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |"
if [ -n "${rows}" ]; then
table="${table}
${rows}"
fi
# Splice the rendered block between the markers (inclusive). The block is read
# from a file rather than passed via `awk -v`, which rejects embedded newlines.
tmp="$(mktemp)"
blockfile="$(mktemp)"
trap 'rm -f "${tmp}" "${blockfile}"' EXIT
printf '%s\n\n%s\n\n%s\n' "${START}" "${table}" "${END}" > "${blockfile}"
awk -v start="${START}" -v end="${END}" -v blockfile="${blockfile}" '
index($0, start) { while ((getline l < blockfile) > 0) print l; close(blockfile); skip = 1; next }
skip && index($0, end) { skip = 0; next }
skip { next }
{ print }
' "${README}" > "${tmp}"
mv "${tmp}" "${README}"
# tmp + blockfile are cleaned by the EXIT trap above.