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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#!/usr/bin/gawk -f
@include "include/Commons.awk"
@include "metainfo.awk"
function init() {
BuildPath = "build/"
Trans = BuildPath Command
ManPath = "man/"
Ronn = ManPath Command ".1.ronn"
RonnStyle = ManPath "styles/night.css"
Man = ManPath Command ".1"
}
# Task: clean
function clean() {
("rm -fr " parameterize(BuildPath)) | getline
return 0
}
# Task: man
function man() {
return system("ronn --manual=" parameterize(toupper(Command) " MANUAL") \
" --organization=" parameterize(Version) \
" --style=" parameterize(RonnStyle) \
" " Ronn)
}
# Task: build
function build(target, group, inline, line, temp) {
# Default target: bash
if (!target) target = "bash"
("mkdir -p " parameterize(BuildPath)) | getline
if (target == "bash" || target == "zsh") {
print "#!/usr/bin/env " target > Trans
print > Trans
print "read -r -d '' TRANS_PROGRAM << 'EOF'" > Trans
if (fileExists(EntryPoint))
while (getline line < EntryPoint) {
match(line, /^[[:space:]]*@include[[:space:]]*"(.*)"$/, group)
if (RSTART) {
# Include file
if (fileExists(group[1] ".awk"))
while (getline inline < (group[1] ".awk")) {
if (inline = squeeze(inline))
print inline > Trans # effective LOC
}
} else {
if (line && line !~ /^[[:space:]]*#!/) {
# Remove preceding spaces
gsub(/^[[:space:]]+/, "", line)
print line > Trans
}
}
}
print "EOF" > Trans
print "export TRANS_PROGRAM" > Trans
print "read -r -d '' TRANS_MANPAGE << 'EOF'" > Trans
if (fileExists(Man))
while (getline line < Man)
print line > Trans
print "EOF" > Trans
print "export TRANS_MANPAGE" > Trans
print "export COLUMNS" > Trans
print "gawk \"$TRANS_PROGRAM\" - \"$@\"" > Trans
("chmod +x " parameterize(Trans)) | getline
return 0
} else if (target == "awk" || target == "gawk") {
"uname -s" | getline temp
if (fileExists(EntryPoint))
while (getline line < EntryPoint) {
match(line, /^[[:space:]]*@include[[:space:]]*"(.*)"$/, group)
if (RSTART) {
# Include file
if (fileExists(group[1] ".awk"))
while (getline inline < (group[1] ".awk"))
print inline > Trans".awk"
} else {
if (temp == "Darwin" && line == "#!/usr/bin/gawk -f")
# OS X: gawk not in /usr/bin, use a better shebang
print "#!/usr/bin/env gawk -f" > Trans".awk"
else
print line > Trans".awk"
}
}
("chmod +x " parameterize(Trans".awk")) | getline
return 0
} else {
w("[FAILED] Unknown target: " AnsiCode["underline"] target AnsiCode["no underline"])
w(" Supported targets: " \
AnsiCode["underline"] "bash" AnsiCode["no underline"] ", " \
AnsiCode["underline"] "zsh" AnsiCode["no underline"] ", " \
AnsiCode["underline"] "gawk" AnsiCode["no underline"])
return 1
}
}
BEGIN {
init()
pos = 0
while (ARGV[++pos]) {
# -target [target]
match(ARGV[pos], /^--?target(=(.*)?)?$/, group)
if (RSTART) {
target = tolower(group[2] ? group[2] : ARGV[++pos])
continue
}
# [task]
match(ARGV[pos], /^[^\-]/, group)
if (RSTART) {
append(tasks, ARGV[pos])
continue
}
}
# Default task: build
if (!anything(tasks)) tasks[0] = "build"
for (i = 0; i < length(tasks); i++) {
task = tasks[i]
status = 0
switch (task) {
case "clean":
status = clean()
break
case "man":
status = man()
break
case "build":
status = build(target)
break
default: # unknown task
status = -1
}
if (status == 0) {
d("[OK] Task " AnsiCode["bold"] task AnsiCode["no bold"] " completed.")
} else if (status < 0) {
w("[FAILED] Unknown task: " AnsiCode["bold"] task AnsiCode["no bold"])
exit 1
} else {
w("[FAILED] Task " AnsiCode["bold"] task AnsiCode["no bold"] " failed.")
exit 1
}
}
}
|