-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.rb
268 lines (235 loc) · 8.53 KB
/
common.rb
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
load 'rbeautify.rb'
def write_class(ruby_filename, class_template)
# s = class_template
p "write class to file #{ruby_filename}"
header = "# Generated by Abap2Ruby(https://github.com/jackieju/abap2ruby)\n"
s = RBeautify.beautify_string(header+class_template)
p s
begin
FileUtils.makedirs(File.dirname(ruby_filename))
aFile = File.new(ruby_filename, "w+")
aFile.puts s[0]
aFile.close
rescue Exception=>e
p e
end
# RBeautify.beautify_file(ruby_filename)
p "done"
end
def pp(m, stack=0)
m = format_msg(m, "", stack)
# puts m
begin
put m
rescue Exception=>e
print "#{m}\n"
end
end
def debug(s)
p s
end
def pdebug(s, stack=0)
depth = 0
sp = ""
begin
raise Exception.new
rescue Exception=>e
e.backtrace.each{|b|
if b =~ /in `C'/
# p "====got botoom====total #{e.backtrace.size}"
break
else
depth += 1
sp += "-+"
end
}
end
m = "{#{sp}#{depth}}#{s}"
if stack>0
m = format_msg(m, "", stack)
end
debug(m)
end
def dump_one_as_ruby(_v, module_name=nil)
#pp "dump ruby for #{_v.class_name}@#{_v}, #{module_name}", 20
# pp "dump #{v.inspect}", 10
s_methods =""
p "-----333:#{_v.class_name}, #{_v.methods.keys.inspect}"
_v.methods.each{|k,v|
p "====>method:#{k}, #{v[:decoration]}"
# p " methods signature:#{k}"
# p " methods name:#{v[:name]}"
# p " src:#{v[:src]}"
method_name = v[:name]
# if method_name =~ /SetJournalDocumentNumber/
# p "--->src111:#{v[:src]}"
# end
if v[:decoration] =~ /static/
method_name = "self.#{v[:name]}"
end
pre = ""
if v[:src] == nil
p "-===>444#{k}:#{v.inspect}"
v[:src] = "raise \"Not implemented\"\n"
else
#if v[:import]
# v[:import].each{|vn|
# pre +="#{vn} = nil\n"
# }
#end
if v[:others]
p "other:#{_v.class_name} #{method_name}:#{v[:others]}"
# assign variable, since eval cannot create new local variable
if v[:others][:export] && v[:others][:export].size >0
p "export:#{v[:others][:export].inspect}" if v[:others][:export].size <2
pre += v[:others][:export].join("=")+"=nil;\n"
end
if v[:others][:import]
#pre += v[:others][:import].join("=")+"=nil\n"
#v[:others][:import].each{|i|
# pre += i.sub(":", "=")+";"
#}
#pre += "\n"
#pre += "_i_=#{v[:import]}\n"
#pre +="if _a && _a.size>0
# _a.each_with_index{|a,i| _i_[i]=a}
#end\n"
pre += "if _a && _a.size>0\n"
v[:others][:import].each_with_index{|t,i|
t = t.split(":")[0]
if i == 0
pre += "#{t}=_a[0]\n"
else
pre += "#{t}=_a[#{i}] if _a.size>#{i}\n"
end
}
pre += "end\n"
end
end
end
if v[:src] #&& v[:src].strip != ""
p "src:#{v[:src]}"
doc = ""
if v[:others] && v[:others][:doc]
doc = v[:others][:doc]
end
method_template =<<HERE
#{doc}
def #{method_name}#{v[:head]}
#{pre}
#{v[:src]}
end
HERE
else
next
end
s_methods += method_template
}
# p "==>methods:#{methods}"
class_name = _v.class_name
includings = ""
_v.includings.each{|inc|
includings += "include #{inc}\n"
}
if class_name == "::"
class_name ="_global_"
class_template = <<HERE
#{s_methods}
#{_v.src}
HERE
else
if (module_name && module_name != "")
class_name = "#{module_name}::#{class_name}"
end
requ = ""
_v.require.each{|q|
requ += "require_relative \"#{q}\"\n"
}
if _v.class == ModuleDef
class_template =<<HERE
#{requ}
module #{class_name}
#{includings}
#{s_methods}
#{_v.src}
end
HERE
else
if _v.parent
class_template =<<HERE
#{requ}
class #{class_name} < #{_v.parent}
#{includings}
#{s_methods}
#{_v.src}
end
HERE
else
class_template =<<HERE
#{requ}
class #{class_name}
#{includings}
#{s_methods}
#{_v.src}
end
HERE
end
end
end
wfname = ""
wfname += "#{$output_dir}/" if $output_dir && $output_dir != ""
#if module_name && module_name != ""
# p "module_name:#{module_name}"
#
# mds = module_name.split("::")
# p "mds:#{mds}"
# mpath = mds.join("/")
# wfname += "#{mpath}/"
#end
# wfname += "#{v.class_name.downcase}.rb"
wfname += "#{class_name.gsub("::","/").downcase}.rb"
#if $output_dir && $output_dir != ""
# wfname = "#{$output_dir}/#{class_name.downcase}.rb"
#else
# wfname = "#{class_name.downcase}.rb"
#end
write_class(wfname, class_template)
$output_filelist = [] if !$output_filelist
$output_filelist.push(wfname)
end
#def dump_module_as_ruby(moduleDef, module_name=nil)
# v =moduleDef
# dump_classes_as_ruby(v.classes) if v.classes && v.classes.size > 0
# dump_one_as_ruby(v, module_name)
#end
def dump_classes_as_ruby(classdefs, module_name=nil)
# p "dump222 #{classdefs.inspect}"
classdefs.each{|k,v|
p "class #{k}"
p " type: #{v.name} #{v.class}"
p " class name: #{v.class_name}"
# p " parent: #{v.parent}"
p " parentScope: #{v.parentScope}@#{v.parentScope}" if v.parentScope
p " modules: #{v.modules.keys}"
p " classes: #{v.classes.size}"
p " methods: #{v.methods.size}"
p " src: #{v.src}" if v.src
m=""
if v.is_a?(ModuleDef)
p "--->333"
# dump_module_as_ruby(v, module_name)
if (module_name && module_name != "" && module_name != "::")
m = "#{module_name}::"
else
m =""
end
m += v.class_name if v.class_name != "::"
dump_classes_as_ruby(v.classes, m) if v.classes && v.classes.size > 0
dump_classes_as_ruby(v.modules, m) if v.modules && v.modules.size > 0
end
dump_one_as_ruby(v, module_name)
}
end
def is_number?(s)
s =~ /^\d+\w?$/
end