-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclasses.bash
More file actions
185 lines (152 loc) · 4.34 KB
/
Copy pathclasses.bash
File metadata and controls
185 lines (152 loc) · 4.34 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# @description Creates instance of specified class.
#
# @example
# classes:new ClassName var 12345
# $var::methodFoo
#
# @arg $1 string Name of class which should be created
# @arg $2 string Name of variable which will be set to instance.
# @arg $@ any Args for construct.
#
# @see classes:require
classes:new() {
local __class="$1"
local __object="$2"
shift 2
local __identifier=${__class}_$(
md5sum - <<< "${__class}_${__object}_${RANDOM}" | awk '{print $1}'
)
local __namespace=":classes:objects:${__identifier}"
local __definition=$(
:classes:get-object-definition \
"$__class" "$__namespace" "$__identifier"
)
builtin eval $__object=\$__namespace
builtin eval "$__definition"
if typeset -f "${__namespace}::construct" &>/dev/null; then
builtin eval "${__namespace}::construct \"\$@\""
fi
}
# @description Source file with class declaration.
#
# Class should be declared using following syntax:
#
# ```bash
# @class ClassName
#
# @method methodFoo() {
# # here is available two variables
# # $this refers to current instance
# # $identifier string representation of object identifier
# echo "Was created with following identifier: $identifier"
# $this::methodBar arg1
# }
#
# @method methodBar() {
# echo "There is same identifier: $identifier"
# echo "args: $@"
# }
# ```
#
# @class - declare new class
# @method - declare new method
#
# @example
# classes:require foo.class.sh
# classes:new Foo var
# $var::methodFoo
#
# @arg $1 string Name of class which should be created
classes:require() {
local __filename="$1"
local __class=""
local __method_scope=""
alias '@class'=':classes:define-class'
alias '@method'=':classes:define-method;'
alias '@var'=':classes:variables ${identifier}'
local __shopt="$(shopt -p)"
shopt -s expand_aliases
builtin source "$__filename"
:classes:define-method
eval "$__shopt"
unalias '@class'
unalias '@method'
}
:classes:get-object-definition() {
local __class="$1"
local __namespace="$2"
local __identifier="$3"
declare -F \
| grep -F -- '-f :class:'$__class'::' \
| cut -d: -f5 \
| sed -re \
's|.*|'${__namespace}'::&() { \
:class:'${__class}'::& "'${__identifier}'" "${@}"; \
};|'
}
:classes:define-class() {
:classes:define-method
__class="$@"
}
:classes:define-method() {
local __actual_scope=$(:classes:get-scope)
if [[ "$__actual_scope" = "$__method_scope" ]]; then
return 0
fi
if [[ "$__method_scope" ]]; then
if __diff=$(
diff -u0 <(echo "$__method_scope") <(echo "$__actual_scope") \
| tail -n+4 \
| sed 's/^+//'
); then
:
else
__function=$__diff
local __definition="$(
:classes:get-method-definition "$__class" "$__function"
)"
builtin eval "$__definition"
unset -f "$__function"
fi
fi
__method_scope=$(:classes:get-scope)
}
:classes:get-scope() {
typeset -F | cut -d' ' -f3 | grep -v '^:' | grep -v '^tests:'
}
:classes:get-method-definition() {
local __class="$1"
local __function="$2"
cat <<-METHOD
:class:${__class}::${__function} () {
local __method__="$__function"
local __class__="$__class"
local identifier="\$1"
local this=":classes:objects:\${identifier}"
shift
$(declare -f "$__function" | tail -n+3)
METHOD
}
:classes:get-variables() {
local identifier="$1"
}
:classes:variables() {
local identifier="$1"
local name="$2"
local value="${3:-}"
if [[ "$value" ]]; then
if [[ ! -v _classes_objects_variables_${identifier}_${name} ]]; then
builtin eval \
"_classes_objects_variables_${identifier}+=(${name})"
fi
builtin eval "_classes_objects_variables_${identifier}_${name}=\$value"
builtin eval $name=\$value
return
fi
if [[ ! -v _classes_objects_variables_${identifier}_${name} ]]; then
echo "unexpected variable ($__class__::$__method__): ${name}"
exit 1
return 1
fi
builtin eval $name=\$_classes_objects_variables_${identifier}_${name}
}