-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.sh
160 lines (131 loc) · 2.46 KB
/
generate.sh
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
#!/bin/sh
set -eu
F="${0%.sh}_sh.go"
trap "rm -f '$F~'" EXIT
exec > "$F~"
cat <<EOT
package ${GOPACKAGE:-undefined}
//go:generate $0${1:+ $*}
// Code generated by $0. DO NOT EDIT.
EOT
generate_alias() {
local t="$1" pkg="$2" n=
shift 2
if [ $# -gt 1 ]; then
cat <<EOT
${t%:*} (
EOT
for n; do
cat <<EOT
// $n is an alias of the $pkg.$n ${t#*:}.
$n = ${pkg##*/}.$n
EOT
done
cat <<EOT
)
EOT
elif [ $# -eq 1 ]; then
n="$1"
cat <<EOT
// $n is an alias of the $pkg.$n ${t#*:}.
${t%:*} $n = ${pkg##*/}.$n
EOT
fi
}
generate_const() {
generate_alias const:constant "$@"
}
generate_types() {
generate_alias type "$@"
}
generate_var() {
generate_alias var:constant "$@"
}
generate_wrapped_alias() {
local t="$1" pkg="$2" f="$3" n=
shift 3
if [ $# -gt 1 ]; then
cat <<EOT
${t%:*} (
EOT
for n; do
cat <<EOT
// $n is a wrapped alias of the $pkg.$n ${t#*:}.
$n = $f(${pkg##*/}.$n)
EOT
done
cat <<EOT
)
EOT
elif [ $# -eq 1 ]; then
n="$1"
cat <<EOT
// $n is a wrapped alias of the $pkg.$n ${t#*:}.
${t%:*} $n = $f(${pkg##*/}.$n)
EOT
fi
}
generate_wrapped_const() {
generate_wrapped_alias "const:constant" "$@"
}
generate__proxy() {
local pkg="$1" fn="$2"
local vars="$3" types="$4" rets=
local args= params= v=
shift 4
# prepare list of arguments
for v in $vars; do
args="${args:+$args, }$v @@T@@"
params="${params:+$params, }$v"
done
# replace types one at the time
for v in $types; do
args="$(echo "$args" | sed -e "s/@@T@@/$v/")"
done
if [ $# -gt 1 ]; then
rets=$(echo "$*" | sed -e "s/ /, /g")
rets="($rets)"
else
rets="${1:-}"
fi
cat <<EOT
// $fn is a proxy function to $pkg.$fn(),
// see https://pkg.go.dev/$pkg#$fn for details.
func $fn($args)${rets:+ $rets} {
return ${pkg##*/}.$fn($params)
}
EOT
}
generate_proxy() {
local pkg="$1" fn="$2"
local vars="$(echo "$3" | tr ',' ' ')"
local types="$(echo "$4" | tr ',' ' ')"
local rets="$(echo "${5:-}" | tr ',' ' ')"
generate__proxy "$pkg" "$fn" "$vars" "$types" $rets
}
generate_proxies() {
local pkg="$1" x=
local fn= vars= types= rets=
shift
for x; do
fn="$(echo "$x" | cut -d: -f1)"
vars="$(echo "$x" | cut -d: -f2)"
types="$(echo "$x" | cut -d: -f3)"
rets="$(echo "$x" | cut -d: -f4)"
generate_proxy "$pkg" "$fn" "$vars" "$types" "$rets"
done
}
generate_done() {
# reformat
if ! gofmt -w -l -s "$F~"; then
diff -u "$F" "$F~" >&2
exit 1
fi
# diff
if ! diff -u "$F" "$F~" >&2; then
# replace if needed
mv "$F~" "$F"
else
rm -f "$F~"
fi
}