This commit is contained in:
Marc Beninca 2025-08-02 01:19:38 +02:00
parent 4b65803ea1
commit 3401c12346
Signed by: marc.beninca
GPG key ID: 9C7613450C80C24F

View file

@ -1,235 +1,237 @@
# code
function alias_eval(alias, name, type) { function alias_eval(alias, name, type) {
text = alias "() { " text = alias "() { "
if (type == "function") { if (type == "function") {
text = text name " \"${@}\"" text = text name " \"${@}\""
} else if (type == "variable") { } else if (type == "variable") {
text = text "echo \"${" name "}\"" text = text "echo \"${" name "}\""
} }
print text "; }" print text "; }"
} }
function append(line) { function append(line) {
if (doc) { if (doc) {
doc = doc "\n" doc = doc "\n"
} }
doc = doc line doc = doc line
} }
function extract(string, type) { function extract(string, type) {
if (type == "alias") { if (type == "alias") {
split(string, array, "#=") split(string, array, "#=")
return trim(array[2]) return trim(array[2])
} else if (type == "command") { } else if (type == "command") {
split(string, array, "#/") split(string, array, "#/")
return trim(array[2]) return trim(array[2])
} else if (type == "doc") { } else if (type == "doc") {
split(string, array, "#") split(string, array, "#")
return trim(array[2]) return trim(array[2])
} else if (type == "function") { } else if (type == "function") {
split(string, array, "(") split(string, array, "(")
return trim(array[1]) return trim(array[1])
} else if (type == "module") { } else if (type == "module") {
split(string, array, "#\\.") split(string, array, "#\\.")
return trim(array[2]) return trim(array[2])
} else if (type == "shebang") { } else if (type == "shebang") {
split(string, array, "#!") split(string, array, "#!")
return trim(array[2]) return trim(array[2])
} else if ((type == "constant") || (type == "variable")) { } else if ((type == "constant") || (type == "variable")) {
split(string, array, "=") split(string, array, "=")
return trim(array[1]) return trim(array[1])
} }
} }
function output(name, type) { function output(name, type) {
print "↙ " type print "↙ " type
print name print name
print doc print doc
exit exit
} }
function reset() { function reset() {
if (f == "") { if (f == "") {
doc = "" doc = ""
} }
} }
function trim(string) { function trim(string) {
text = string text = string
sub("^[\t ]*", "", text) sub("^[\t ]*", "", text)
sub("[\t ]*$", "", text) sub("[\t ]*$", "", text)
return text return text
} }
BEGIN { BEGIN {
RE_ANY = "(.*)" RE_ANY = "(.*)"
RE_BEGIN = "^" RE_BEGIN = "^"
RE_CONST = "([_A-Z][_0-9A-Z]*)" RE_CONST = "([_A-Z][_0-9A-Z]*)"
RE_SET = "=.*" RE_SET = "=.*"
RE_SPACE = "[[:space:]]" RE_SPACE = "[[:space:]]"
RE_TSK = "(FIXME|TODO)" RE_TSK = "(FIXME|TODO)"
RE_VAR = "([_a-z][_0-9a-z]*)" RE_VAR = "([_a-z][_0-9a-z]*)"
RE_SPACES = RE_SPACE "*" RE_SPACES = RE_SPACE "*"
RE_END = RE_SPACES "$" RE_END = RE_SPACES "$"
RE_FUNC = RE_SPACES "\\(" RE_SPACES "\\)" RE_SPACES "{" RE_FUNC = RE_SPACES "\\(" RE_SPACES "\\)" RE_SPACES "{"
re["alias"] = RE_BEGIN "#=" RE_SPACES RE_VAR RE_END re["alias"] = RE_BEGIN "#=" RE_SPACES RE_VAR RE_END
re["binary"] = RE_BEGIN "#\\|" RE_SPACES RE_VAR RE_END re["binary"] = RE_BEGIN "#\\|" RE_SPACES RE_VAR RE_END
RE_CLOSE = RE_BEGIN "}" RE_SPACES RE_END RE_CLOSE = RE_BEGIN "}" RE_SPACES RE_END
re["command"] = RE_BEGIN "#/" RE_SPACES RE_VAR RE_END re["command"] = RE_BEGIN "#/" RE_SPACES RE_VAR RE_END
RE_COMMENT = RE_BEGIN "#" RE_ANY RE_END RE_COMMENT = RE_BEGIN "#" RE_ANY RE_END
re["constant"] = RE_BEGIN RE_CONST RE_SET RE_END re["constant"] = RE_BEGIN RE_CONST RE_SET RE_END
RE_DOC = RE_BEGIN RE_SPACES "#" RE_SPACE RE_ANY RE_END RE_DOC = RE_BEGIN RE_SPACES "#" RE_SPACE RE_ANY RE_END
re["function"] = RE_BEGIN RE_VAR RE_FUNC RE_END re["function"] = RE_BEGIN RE_VAR RE_FUNC RE_END
RE_MODULE = RE_BEGIN "#\\." RE_SPACES RE_ANY RE_END RE_MODULE = RE_BEGIN "#\\." RE_SPACES RE_ANY RE_END
RE_SHEBANG = RE_BEGIN "#!" RE_SPACES RE_ANY RE_END RE_SHEBANG = RE_BEGIN "#!" RE_SPACES RE_ANY RE_END
RE_TASK = RE_BEGIN RE_SPACES "#" RE_SPACES RE_TSK RE_ANY RE_END RE_TASK = RE_BEGIN RE_SPACES "#" RE_SPACES RE_TSK RE_ANY RE_END
re["variable"] = RE_BEGIN RE_VAR RE_SET RE_END re["variable"] = RE_BEGIN RE_VAR RE_SET RE_END
f = "" f = ""
match_alias = 0 match_alias = 0
match_command = 0 match_command = 0
match_module = 0 match_module = 0
match_task = 0 match_task = 0
reset() reset()
} }
{ {
if (action == "filter") { if (action == "filter") {
if (match($0, re[target])) { if (match($0, re[target])) {
unique[extract($0, target)] = "" unique[extract($0, target)] = ""
} }
} else if (action == "tasks") { } else if (action == "tasks") {
line++ line++
if (match($0, RE_MODULE)) { if (match($0, RE_MODULE)) {
line = 1 line = 1
if (output_tasks) { if (output_tasks) {
print "" print ""
print output_module print output_module
print output_tasks print output_tasks
output_tasks = "" output_tasks = ""
} }
doc = "" doc = ""
match_task = 0 match_task = 0
output_module = ". " extract($0, "module") output_module = ". " extract($0, "module")
} else if (match($0, RE_TASK)) { } else if (match($0, RE_TASK)) {
if (target) { if (target) {
if (target == extract($0, "task")) { if (target == extract($0, "task")) {
match_task = 1 match_task = 1
} }
} else { } else {
match_task = 1 match_task = 1
} }
append(line ": " $0) append(line ": " $0)
} else if (match($0, RE_COMMENT)) { } else if (match($0, RE_COMMENT)) {
append(line ": " $0) append(line ": " $0)
} else { } else {
if (match_task) { if (match_task) {
output_tasks = output_tasks "\n" doc output_tasks = output_tasks "\n" doc
} }
doc = "" doc = ""
match_task = 0 match_task = 0
} }
} else if (action == "eval") { } else if (action == "eval") {
if (match($0, re[target])) { if (match($0, re[target])) {
append(extract($0, target)) append(extract($0, target))
} else if (match($0, re["function"])) { } else if (match($0, re["function"])) {
split(doc, array, "\n") split(doc, array, "\n")
for (item in array) { for (item in array) {
alias_eval(array[item], extract($0, "function"), "function") alias_eval(array[item], extract($0, "function"), "function")
} }
reset() reset()
} else { } else {
reset() reset()
} }
} else if (action == "function") { } else if (action == "function") {
if (match($0, re["command"])) { if (match($0, re["command"])) {
append(extract($0, "command")) append(extract($0, "command"))
} else if (match($0, re["function"])) { } else if (match($0, re["function"])) {
split(doc, array, "\n") split(doc, array, "\n")
for (item in array) { for (item in array) {
if (array[item] == target) { if (array[item] == target) {
print extract($0, "function") print extract($0, "function")
exit exit
} }
} }
reset() reset()
} else { } else {
reset() reset()
} }
} else if (action == "doc") { } else if (action == "doc") {
# doc # doc
if (match($0, RE_SHEBANG)) { if (match($0, RE_SHEBANG)) {
append("! " extract($0, "shebang")) append("! " extract($0, "shebang"))
} else if (match($0, RE_DOC)) { } else if (match($0, RE_DOC)) {
if (f) { if (f) {
append($0) append($0)
} else { } else {
append(extract($0, "doc")) append(extract($0, "doc"))
} }
} else if (match($0, re["alias"])) { } else if (match($0, re["alias"])) {
matched = extract($0, "alias") matched = extract($0, "alias")
append("= " matched) append("= " matched)
if (matched == target) { if (matched == target) {
match_alias = 1 match_alias = 1
} }
} else if (match($0, re["command"])) { } else if (match($0, re["command"])) {
matched = extract($0, "command") matched = extract($0, "command")
append("/ " matched) append("/ " matched)
if (matched == target) { if (matched == target) {
match_command = 1 match_command = 1
} }
# set # set
} else if (match($0, re["constant"])) { } else if (match($0, re["constant"])) {
matched = extract($0, "constant") matched = extract($0, "constant")
if (matched == target) { if (matched == target) {
output(matched, "constant") output(matched, "constant")
} else { } else {
reset() reset()
} }
} else if (match($0, re["variable"])) { } else if (match($0, re["variable"])) {
matched = extract($0, "variable") matched = extract($0, "variable")
if (matched == target) { if (matched == target) {
output(matched, "variable") output(matched, "variable")
} else { } else {
reset() reset()
} }
# others # others
} else if (match($0, RE_MODULE)) { } else if (match($0, RE_MODULE)) {
reset() reset()
if (extract($0, "module") == target) { if (extract($0, "module") == target) {
match_module = 1 match_module = 1
} }
} else if (match($0, re["function"])) { } else if (match($0, re["function"])) {
f = extract($0, "function") f = extract($0, "function")
} else if (match($0, RE_CLOSE)) { } else if (match($0, RE_CLOSE)) {
if (match_alias) { if (match_alias) {
print "= " target print "= " target
output(f, "function") output(f, "function")
} else if (match_command) { } else if (match_command) {
print "/ " target print "/ " target
output(f, "function") output(f, "function")
} else if (f == target) { } else if (f == target) {
output(f, "function") output(f, "function")
} else { } else {
f = "" f = ""
reset() reset()
} }
} else { } else {
if (match_module) { if (match_module) {
output(target, "module") output(target, "module")
} else { } else {
reset() reset()
} }
} }
} }
} }
END { END {
if (action == "filter") { if (action == "filter") {
for (item in unique) { for (item in unique) {
print item print item
} }
} }
} }