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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
# File 'lib/create_uml_class.rb', line 106
def create_uml_class(in_dir, out_file)
out = []
out.push "@startuml"
puts "in_dir = #{in_dir}"
main_composition_list = []
main_method_list = []
global_var = []
Dir.glob("#{in_dir}/**/*.{rb,ru}") do |f|
if f =~ Regexp.new(@config["exclude_path"])
puts "skip #{f}"
next
end
puts f
buf = ""
Tempfile.create("rufo") do |tmp_file|
FileUtils.cp(f, tmp_file.path)
kernel = Facter.value(:kernel)
if kernel == "windows"
open("|rubyw #{get_rufo_path} #{tmp_file.path}") do |f|
if f.read =~ /error/
puts "rufo error #{f}"
return
else
buf = File.binread tmp_file.path
end
end
else
open("|rufo #{tmp_file.path}") do |f|
if f.read =~ /error/
puts "rufo error #{f}"
return
else
buf = File.binread tmp_file.path
end
end
end
end
buf.gsub!(/(([\/\"\'].*?[\/\"\'])|([^\/\"\'\)\s]*#.+?$))/) do |m|
if m[0] == "#" and m[0] != "{"
""
else
m
end
end
buf = delete_here_doc(buf)
out_list = []
cstruct_list = []
block_count = 0
method_type = :public
class_name = ""
buf.each_line do |line|
next if line =~ /^[\r\n]*$/
indent_num = line.match(/^[ ]+/).to_s.size / 2
if block_count == indent_num
elsif block_count > indent_num
block_count = indent_num
else
block_count = indent_num
end
if line =~ /^\s*class\s/
unless line =~ /<</ work = line.gsub(/class\s/, "")
class_name = work.split("<")[0].to_s.chomp.match(/[A-Z][A-Za-z0-9_:]+/).to_s
base_name = work.split("<")[1].to_s.chomp.match(/[A-Z][A-Za-z0-9_:]+/).to_s
class_name.gsub!(/::/, ".")
class_name.gsub!(/;/, "")
if out_list.size != 0 and out_list[-1].type == :class_start class_name = out_list[-1].name + "." + class_name
out_list[-1].name = class_name
cstruct_list[-1].name = class_name
else
out_list.push CStruct.new(:class_start, class_name, block_count, [], [], [], [])
cstruct_list.push CStruct.new(:class_end, class_name, block_count, [], [], [], [])
end
if base_name != ""
cstruct_list[-1].inherit_list.push base_name
end
end
next unless line =~ /end\s*$/ elsif line =~ /^\s*module\s/
module_name = line.split(" ")[1].to_s.chomp
module_name.gsub!(/^[:]+/, "")
module_name.gsub!(/::/, ".")
module_name.gsub!(/;/, "")
out_list.push CStruct.new(:module_start, module_name, block_count, [], [], [], [])
cstruct_list.push CStruct.new(:module_end, module_name, block_count, [], [], [], [])
next unless line =~ /end\s*$/ end
if line =~ /^\s*private$/
method_type = :private
elsif line =~ /^\s*protected$/
method_type = :protected
elsif line =~ /^\s*public$/
method_type = :public
end
if line =~ /^\s*def\s/
method = line.chomp.gsub(/\s*def\s*/, "")
unless method =~ /\(/
sp = method.split(" ")
if sp.size > 1
method = sp[0].to_s + "(" + sp[1..-1].to_s + ")"
else
method = method + "()"
end
end
if cstruct_list.size != 0
method_list = cstruct_list[-1].method_list
case method_type
when :public
method_list.push "+ #{method}"
when :private
method_list.push "- #{method}"
when :protected
method_list.push "# #{method}"
end
else
main_method_list.push "+ #{method}"
end
end
line.match(/[A-Z]([a-zA-Z:]+)\.[a-z]/) do |m|
c_name = m.to_s.split(".")[0]
if cstruct_list.size != 0
cstruct_list[-1].composition_list.push c_name
else
main_composition_list.push "main *-- #{c_name}"
end
end
if line =~ /\s*@\S+/
if cstruct_list.size != 0
line.match(/@[a-zA-Z0-9_]+/) { |m|
instance_var = cstruct_list[-1].var_list
val = m.to_s.gsub(/@/, "")
case method_type
when :public
instance_var.push "+ #{val}"
when :private
instance_var.push "- #{val}"
when :protected
instance_var.push "# #{val}"
end
}
end
end
line.match(/\$[a-zA-Z0-9_]+/) { |m|
global_var.push "+ #{m.to_s}"
}
if cstruct_list.size != 0
if block_count == cstruct_list[-1].block_count out_list.push cstruct_list[-1]
cstruct_list.slice!(-1) end
end
end
if block_count != 0
puts f
return ""
end
out = print_uml(out, out_list)
end
if main_method_list.size != 0 or
main_composition_list.size != 0 or
main_method_list.size != 0
out.push "class main {"
main_method_list.each do |mml|
out.push mml
end
global_var.uniq.each do |gv|
out.push gv
end
out.push "}"
main_composition_list.uniq.each do |mcl|
out.push mcl
end
end
out.push "@enduml"
return out.join("\n")
end
|