/
rdot
executable file
·1057 lines (1002 loc) · 32.5 KB
/
rdot
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
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/ruby -Ku
class String
def escape
result = ""
self.each_byte do |c|
if c.chr == ">"
result += ">"
elsif c.chr == "<"
result += "<"
elsif c.chr == "&"
result += "&"
else
result += c.chr
end
end
result
end
end
class Symbol
def <=>(other)
self.to_s <=> other.to_s
end
end
module RDot
class << self
def owner_class(method)
owner = method.owner
smod = owner.inspect
if smod[0..7] == "#<Class:"
sres = smod[8..smod.index(">")-1]
begin
eval sres
rescue Exception
owner
end
else
owner
end
end
attr_reader :sources
def register_source(mod,scope,symbol,source)
if Array === source && source.size != 0
if RDot::Options.verbose != :none
$stderr << "register source: #{mod}::#{symbol} => #{source[0]}\n"
end
if @sources == nil
@sources = {}
end
if @sources[mod] == nil
@sources[mod] = {}
end
if @sources[mod][scope] == nil
@sources[mod][scope] = {}
end
@sources[mod][scope][symbol] = source[0]
end
end
def get_source(mod,scope,symbol)
if @sources == nil || @sources[mod] == nil ||
@sources[mod][scope] == nil
nil
else
@sources[mod][scope][symbol]
end
end
def source_hash(source,attribute = false)
if String === source && source.length != 0
if /(eval)/ === source
{:file => "(eval)", :line => nil}
elsif /(__FORWARDABLE__)/ === source
{:file => "(forward)", :line => nil}
else
a = source.split(":")
r = {:file => a[0].strip, :line => a[1].to_i}
if a.length > 2 && ! attribute
r[:file] = '(indirect) ' + r[:file]
end
r
end
else
{:file => "(n/a)", :line => nil}
end
end
def auto_header(arity)
if arity == 0
""
else
args = []
(0..arity.abs-1).each do |iarg|
args << ("a"[0]+iarg).chr
end
if arity < 0
args[arity.abs-1] = "*#{args[arity.abs-1]}"
end
"(#{args.join(", ")})"
end
end
def method_header(object,source)
if RDot::Options.hide_arguments
"#{object.name}"
else
begin
line = source[:line]
file = source[:file]
# load file text
@files ||= {}
@files[file] ||= IO.readlines file
text = @files[file]
# make header
header = ""
scan_re = Regexp.compile "^(?:.*?;)?\\s*(?:def\\s+(?:\\w+\\.)?#{Regexp.escape(object.name)}|module_function)"
alias_re = Regexp.compile "^(?:.*?;)?\\s*alias(?:_method)?\\s+(?:\\w+\\.)?:?#{Regexp.escape(object.name)}\\s*,?\\s+:?([^\\s]*)\\s.*"
current_ln = line - 1
begin
current_txt = text[current_ln].gsub /#.*$/, ''
# may be alias...
if alias_re === current_txt
header = "#{object.name} -> #{current_txt.gsub(alias_re, '\1')}"
return header
end
header = current_txt + header
current_ln -= 1
end until current_ln <= 0 || scan_re === current_txt
header.gsub! /\s+/m, ' '
brace_re = Regexp.compile ".*?\\s*def\\s+(?:\\w+\\.)?(#{Regexp.escape(object.name)}\\s*\\((?:[^\\(\\)]+|\\([^\\(\\)]+\\))*?\\)).*"
nonbrace_re = Regexp.compile ".*?\\s*def\\s+(?:\\w+\\.)?(#{Regexp.escape(object.name)}.*?)(?:$|;.*)"
mf_re = Regexp.compile ".*?module_function\\s+.*?(#{Regexp.escape(object.name)}).*"
if mf_re === header
:module_function
elsif brace_re === header
header.gsub(brace_re, '\1')
else
header.gsub(nonbrace_re, '\1')
end
rescue
"#{object.name}#{auto_header(object.arity)}"
end
end
end
attr_reader :immediates
def register_immediate(mod)
if RDot::Options.verbose != :none
$stderr << "register module: #{mod}\n"
end
if @immediates == nil
@immediates = []
end
@immediates << mod
end
attr_reader :modules
def attribute?(mod,scope,symbol,source)
if /in `attr/ === source || /\w=$/ === symbol.to_s
true
else
if @modules[mod][scope] == nil ||
@modules[mod][scope][:attributes] == nil
false
else
@modules[mod][scope][:attributes].has_key? symbol
end
end
end
def register_attribute(mod,scope,symbol,source,preloaded,object)
if RDot::Options.verbose == :scan
$stderr << "scan attribute: #{mod}::#{symbol}\n"
end
if @modules[mod][scope] == nil
@modules[mod][scope] = {}
end
if @modules[mod][scope][:attributes] == nil
@modules[mod][scope][:attributes] = {}
end
if /=$/ === symbol.to_s
symbol = symbol.to_s[0..symbol.to_s.length-2].intern
access = {:write => true}
accessor = :writer
if @modules[mod][scope][:public] != nil &&
@modules[mod][scope][:public][symbol] != nil
register_attribute mod, scope, symbol,
@modules[mod][scope][:public][symbol][:source],
@modules[mod][scope][:public][symbol][:preloaded],
@modules[mod][scope][:public][symbol][:object]
@modules[mod][scope][:public].delete symbol
end
if @modules[mod][scope][:protected] != nil &&
@modules[mod][scope][:protected][symbol] != nil
register_attribute mod, scope, symbol,
@modules[mod][scope][:protected][symbol][:source],
@modules[mod][scope][:protected][symbol][:preloaded],
@modules[mod][scope][:protected][symbol][:object]
@modules[mod][scope][:protected].delete symbol
end
if @modules[mod][scope][:private] != nil &&
@modules[mod][scope][:private][symbol] != nil
register_attribute mod, scope, symbol,
@modules[mod][scope][:private][symbol][:source],
@modules[mod][scope][:private][symbol][:preloaded],
@modules[mod][scope][:private][symbol][:object]
@modules[mod][scope][:private].delete symbol
end
else
access = {:read => true}
accessor = :reader
end
hash = source_hash(source,true)
if @modules[mod][scope][:attributes][symbol] == nil
@modules[mod][scope][:attributes][symbol] = hash
end
if @modules[mod][scope][:attributes][symbol][:line] == nil
@modules[mod][scope][:attributes][symbol].merge! hash
end
@modules[mod][scope][:attributes][symbol].merge! access
if /eval/ === source
@modules[mod][scope][:attributes][symbol][:evaluated] = true
end
if @modules[mod][scope][:attributes][symbol][accessor] != object
@modules[mod][scope][:attributes][symbol][:preloaded] = preloaded
end
@modules[mod][scope][:attributes][symbol][accessor] = object
end
def register_method(mod,scope,access,symbol,preloaded)
begin
if scope == :class
object = mod.method(symbol).unbind
else
object = mod.instance_method(symbol)
end
rescue
object = nil
end
if object && owner_class(object) == mod
source = get_source(mod,scope,symbol)
if attribute? mod, scope, symbol, source
register_attribute mod, scope, symbol, source, preloaded,
object
else
if RDot::Options.verbose == :scan
$stderr << "scan method: #{mod}::#{symbol}\n"
end
if @modules[mod][scope] == nil
@modules[mod][scope] = {}
end
if @modules[mod][scope][access] == nil
@modules[mod][scope][access] = {}
end
if @modules[mod][scope][access][symbol] == nil
@modules[mod][scope][access][symbol] = {}
end
hash = source_hash(source)
@modules[mod][scope][access][symbol].merge! hash
if /eval/ === source
@modules[mod][scope][access][symbol][:evaluated] = true
end
if @modules[mod][scope][access][symbol][:object] != object
@modules[mod][scope][access][symbol][:preloaded] = preloaded
end
@modules[mod][scope][access][symbol][:header] = method_header(object,hash)
@modules[mod][scope][access][symbol][:source] = source
@modules[mod][scope][access][symbol][:object] = object
end
end
end
def register_const(mod,const,preloaded)
if mod.const_defined?(const)
begin
value = mod.const_get(const)
mod.ancestors.each do |anc|
if anc != mod && anc.const_defined?(const)
if anc.const_get(const) == value
return nil
end
end
end
if RDot::Options.verbose == :scan
$stderr << "scan const: #{mod}::#{const}\n"
end
if @modules[mod][:constants] == nil
@modules[mod][:constants] = {}
end
if @modules[mod][:constants][const] == nil
@modules[mod][:constants][const] = {}
end
oldvalue = @modules[mod][:constants][const][:value]
if oldvalue != value
@modules[mod][:constants][const][:value] = value
@modules[mod][:constants][const][:preloaded] = preloaded
end
rescue LoadError
end
end
end
def register_module(mod,preloaded)
if RDot::Options.verbose == :scan
$stderr << "scan module: #{mod}\n"
end
if @modules == nil
@modules = {}
end
if @modules[mod] == nil
@modules[mod] = {}
end
mod.public_methods(false).each do |method|
register_method mod, :class, :public, method.intern, preloaded
end
mod.protected_methods(false).each do |method|
register_method mod, :class, :protected, method.intern,
preloaded
end
mod.private_methods(false).each do |method|
register_method mod, :class, :private, method.intern, preloaded
end
mod.public_instance_methods(false).each do |method|
register_method mod, :instance, :public, method.intern,
preloaded
end
mod.protected_instance_methods(false).each do |method|
register_method mod, :instance, :protected, method.intern,
preloaded
end
mod.private_instance_methods(false).each do |method|
register_method mod, :instance, :private, method.intern,
preloaded
end
mod.constants.each do |const|
register_const mod, const.intern, preloaded
end
end
def register_extend(mod,ext)
if @modules == nil
@modules = {}
end
if @modules[mod] == nil
@modules[mod] = {}
end
if @modules[mod][:extensions] == nil
@modules[mod][:extensions] = []
end
if ! @modules[mod][:extensions].include?(ext)
@modules[mod][:extensions] << ext
end
end
def scan_modules(preloaded)
ObjectSpace.each_object Module do |mod|
register_module mod, preloaded
end
end
def module_preloaded?(mod)
if @immediates != nil && @immediates.include?(mod)
return false
end
[:class, :instance].each do |scope|
accesses = [:attribute, :public]
if RDot::Options.show_protected
accesses << :protected
end
if RDot::Options.show_private
accesses << :private
end
accesses.each do |access|
if @modules != nil && @modules[mod] != nil &&
@modules[mod][scope] != nil &&
@modules[mod][scope][access] != nil
@modules[mod][scope][access].each do |key,value|
if value[:preloaded] == false
return false
end
end
end
end
end
if @modules != nil && @modules[mod] != nil &&
@modules[mod][:constants] != nil
@modules[mod][:constants].each do |key,value|
if value[:preloaded] == false
return false
end
end
end
return true
end
def module_evaluated?(mod)
[:class, :instance].each do |scope|
accesses = [:attribute, :public]
if RDot::Options.show_protected
accesses << :protected
end
if RDot::Options.show_private
accesses << :private
end
accesses.each do |access|
if @modules != nil && @modules[mod] != nil &&
@modules[mod][scope] != nil &&
@modules[mod][scope][access] != nil
@modules[mod][scope][access].each do |key,value|
if value[:evaluated]
return true
end
end
end
end
end
return false
end
def processed?(mod)
if @processed == nil
@processed = []
end
@processed.include? mod
end
def module_modules(mod)
ancs = mod.ancestors
ancs -= [mod]
if Class === mod
if mod.superclass != nil
ancs -= mod.superclass.ancestors
end
end
result = ancs.clone
ancs.each do |anc|
result -= (anc.ancestors - [anc])
end
result
end
def node_color(mod)
if module_preloaded? mod
if Class === mod
if mod <= Exception
RDot::Options.color_exception_preloaded
else
RDot::Options.color_class_preloaded
end
else
RDot::Options.color_module_preloaded
end
else
if module_evaluated? mod
if Class === mod
if mod <= Exception
RDot::Options.color_exception_evaluated
else
RDot::Options.color_class_evaluated
end
else
"red"
end
else
if Class === mod
if mod <= Exception
RDot::Options.color_exception
else
RDot::Options.color_class
end
else
RDot::Options.color_module
end
end
end
end
def node_name(mod)
"node_#{mod.inspect.tr("#<>() =,;:","_")}"
end
def access_color(access)
if access == :protected
RDot::Options.color_protected
elsif access == :private
RDot::Options.color_private
else
"white"
end
end
def nn(value)
if value != nil
value
else
" "
end
end
def attr_header(value)
result = "#{value[0].to_s.escape} ["
result += "r" if value[1][:read]
result += "w" if value[1][:write]
result += "]"
result
end
def node_rows(mod)
result = ""
if @modules[mod] != nil
tds0 = []
if ! RDot::Options.hide_constants
tds = []
if @modules[mod][:constants] != nil
@modules[mod][:constants].sort.each do |value|
if RDot::Options.show_preloaded ||
! value[1][:preloaded]
if ! value[1][:preloaded]
tds << "<TD ALIGN=\"LEFT\" COLSPAN=\"3\">#{value[0].to_s.escape} <#{value[1][:value].class.inspect.escape}></TD>"
else
tds << "<TD ALIGN=\"LEFT\" COLSPAN=\"3\"><FONT COLOR=\"#666666\">#{value[0].to_s.escape} <#{value[1][:value].class.inspect.escape}></FONT></TD>"
end
end
end
end
if tds.size != 0
tds0 << "<TD ROWSPAN=\"#{tds.size}\" VALIGN=\"TOP\" ALIGN=\"RIGHT\">const</TD>"+tds.join("</TR><TR>")
end
end
if ! RDot::Options.hide_methods
[:class, :instance].each do |scope|
tds = []
if @modules[mod][scope] != nil
if @modules[mod][scope][:attributes] != nil
@modules[mod][scope][:attributes].sort.each do |value|
if RDot::Options.show_preloaded ||
! value[1][:preloaded]
if ! value[1][:preloaded]
tds << "<TD ALIGN=\"LEFT\">#{attr_header(value)}</TD>"+
"<TD ALIGN=\"LEFT\">#{value[1][:file]}</TD>"+
"<TD ALIGN=\"RIGHT\">#{value[1][:line]}</TD>"
else
tds << "<TD ALIGN=\"LEFT\"><FONT COLOR=\"#666666\">#{attr_header(value).escape}</FONT></TD>"+
"<TD ALIGN=\"LEFT\"><FONT COLOR=\"#666666\">#{value[1][:file]}</FONT></TD>"+
"<TD ALIGN=\"RIGHT\"><FONT COLOR=\"#666666\">#{nn(value[1][:line])}</FONT></TD>"
end
end
end
end
accesses = [:public]
accesses << :protected if RDot::Options.show_protected
accesses << :private if RDot::Options.show_private
accesses.each do |access|
if @modules[mod][scope][access] != nil
@modules[mod][scope][access].sort.each do |value|
if RDot::Options.show_preloaded ||
! value[1][:preloaded]
header = value[1][:header]
if header == :module_function
header = @modules[mod][:instance][:private][value[0]][:header]
end
if ! value[1][:preloaded]
tds << "<TD ALIGN=\"LEFT\" BGCOLOR=\"#{access_color(access)}\">#{header.escape}</TD>"+
"<TD ALIGN=\"LEFT\" BGCOLOR=\"#{access_color(access)}\">#{value[1][:file]}</TD>"+
"<TD ALIGN=\"RIGHT\" BGCOLOR=\"#{access_color(access)}\">#{value[1][:line]}</TD>"
else
tds << "<TD ALIGN=\"LEFT\" BGCOLOR=\"#{access_color(access)}\"><FONT COLOR=\"#666666\">#{header.escape}</FONT></TD>"+
"<TD ALIGN=\"LEFT\" BGCOLOR=\"#{access_color(access)}\"><FONT COLOR=\"#666666\">#{value[1][:file]}</FONT></TD>"+
"<TD ALIGN=\"RIGHT\" BGCOLOR=\"#{access_color(access)}\"><FONT COLOR=\"#666666\">#{nn(value[1][:line])}</FONT></TD>"
end
end
end
end
end
end
if tds.size != 0
tds0 << "<TD ROWSPAN=\"#{tds.size}\" VALIGN=\"TOP\" ALIGN=\"RIGHT\">#{scope}</TD>"+tds.join("</TR><TR>")
end
end
end
if tds0.size != 0
result = "<TR>#{tds0.join("</TR>\n<TR>")}</TR>"
end
end
result
end
def node_label(mod)
"<<TABLE CELLBORDER=\"0\" CELLSPACING=\"0\"><TR>"+
"<TD ALIGN=\"RIGHT\" BGCOLOR=\"#{node_color(mod)}\">"+
"<FONT POINT-SIZE=\"12\">#{mod.class.name.downcase}</FONT>"+
"</TD><TD COLSPAN=\"3\" ALIGN=\"LEFT\" "+
"BGCOLOR=\"#{node_color(mod)}\">"+
"<FONT POINT-SIZE=\"12\">#{mod.inspect.escape}</FONT></TD></TR>"+
node_rows(mod)+
"</TABLE>>"
end
def out_module(mod)
if ! processed?(mod)
@processed << mod
RDot::Options.output << "\t#{node_name(mod)}" +
"[label=#{node_label(mod)}];\n"
ancs = mod.ancestors - [mod]
if Class === mod
if mod.superclass != nil
out_module mod.superclass
RDot::Options.output << "\t\t" +
"#{node_name(mod.superclass)} -> " +
"#{node_name(mod)}[color=\"" +
"#{RDot::Options.color_inherited}\"];\n"
ancs -= mod.superclass.ancestors
end
end
dels = ancs.clone
dels.each do |del|
ancs -= (del.ancestors - [del])
end
ancs.each do |anc|
out_module anc
RDot::Options.output << "\t\t" +
"#{node_name(anc)} -> " +
"#{node_name(mod)}[color=\"" +
"#{RDot::Options.color_included}\"];\n"
end
if @modules[mod] != nil &&
@modules[mod][:extensions] != nil
@modules[mod][:extensions].each do |ext|
out_module ext
RDot::Options.output << "\t\t" +
"#{node_name(ext)} -> " +
"#{node_name(mod)}[color=\"" +
"#{RDot::Options.color_extended}\"];\n"
end
end
end
end
def graph_label
if RDot::Options.title == nil
"\"RDot: #{RDot::Options.includes.join(", ")}\""
else
"\"#{RDot::Options.title}\""
end
end
def out_modules
RDot::Options.output << "digraph graph_RDot{\n"
RDot::Options.output << "\tgraph[rankdir=LR,\n\t\tsplines=true,\n"+
"\t\tfontname=\"#{RDot::Options.caption_fontname}\",\n"+
"\t\tfontsize=24,\n\t\tlabelloc=t,\n"+
"\t\tlabel=#{graph_label}\n\t];\n"
RDot::Options.output << "\tnode[shape=plaintext,\n"+
"\t\tfontname=\"#{RDot::Options.fontname}\",\n\t\tfontsize=9\n\t];\n"
RDot::Options.output << "\tedge[dir=back,\n\t\tarrowtail=vee\n\t];\n"
RDot::Options.output << "\n"
ObjectSpace.each_object Module do |mod|
if ! RDot::Options.except?(mod)
if RDot::Options.show_preloaded || ! module_preloaded?(mod)
out_module mod
end
end
end
RDot::Options.output << "}\n"
end
end
end
module RDot
VERSION = "0.9.3"
if $stdout.stat.chardev?
TITLE = "\e[1mRDot v#{RDot::VERSION}\e[0m - GraphViz class diagrams" +
" for Ruby\n\thttp://github.com/shikhalev/rdot/"
USAGE = "\e[1mUsage:\e[0m rdot [options] <libs>\n\n\e[1mOptions:\e[0m\n"
else
TITLE = "RDot v#{RDot::VERSION} - GraphViz class diagrams for Ruby\n" +
"\thttp://github.com/shikhalev/rdot/"
USAGE = "Usage: rdot [options] <libs>\n\nOptions:\n"
end
AUTHOR = "Ivan Shikhalev <shikhalev@gmail.com>\n" +
"\thttp://github.com/shikhalev/"
LICENSE = "GNU General Public License, see COPYING\n" +
"\thttp://www.gnu.org/licenses/gpl.html"
module Options
@output = $stdout
@excludes = []
@includes = []
@title = nil
@fontname = 'Monospace'
@caption_fontname = 'Sans-Serif'
@color_class = "mediumaquamarine"
@color_class_preloaded = "mediumseagreen"
@color_class_evaluated = "aquamarine"
@color_exception = "lightcoral"
@color_exception_preloaded = "chocolate"
@color_exception_evaluated = "lightpink"
@color_module = "skyblue"
@color_module_preloaded = "steelblue"
@color_inherited = "steelblue"
@color_included = "skyblue"
@color_extended = "olivedrab"
@color_protected = "#EEEEEE"
@color_private = "#DDDDDD"
@show_protected = false
@show_private = false
@show_preloaded = false
@hide_methods = false
@hide_constants = false
@hide_arguments = false
@no_trace = false
@exceptions = []
@verbose = :none
class << self
attr_reader :output, :includes, :excludes, :title,
:fontname, :caption_fontname,
:color_class,
:color_class_preloaded, :color_class_evaluated,
:color_exception, :color_exception_preloaded,
:color_exception_evaluated, :color_module,
:color_module_preloaded, :color_inherited, :color_included,
:color_extended, :color_protected, :color_private,
:show_protected, :show_private, :show_preloaded,
:hide_constants, :hide_methods, :hide_arguments, :no_trace,
:exceptions, :verbose
def except?(mod)
@exceptions.each do |cls|
if mod <= cls
return true
end
end
return false
end
end
end
end
class Module
def method_added(method)
RDot.register_source self, :instance, method, caller
end
def singleton_method_added(method)
RDot.register_source self, :class, method, caller
end
def extended(obj)
if Module === obj
RDot.register_extend obj, self
end
end
end
RDot.scan_modules(true)
require "optparse"
module RDot
module Options
opts = OptionParser.new RDot::USAGE
opts.on("-h","--help",
"Show short help and exit.") do
puts "#{RDot::TITLE}\n\n#{opts.to_s}"
exit 0
end
opts.on("-I","--info","=[INFO]",Array,
"Show some program information and exit.",
"Argument may by comma-separated set of:",
" 'about' - title with version",
" and homepage,",
" 'author' - author name with e-mail,",
" 'license' - license info,",
" 'usage' - command line arguments,",
" 'version' - current version;",
"or one of predefined sets:",
" 'all' = 'about,author,license,usage',",
" 'help' = 'about,usage',",
" 'info' (or no argument) =",
" 'about,author,license'.") do |arg|
if arg == nil || arg == ["info"]
arg = ["about","author","license"]
elsif arg == ["all"]
arg = ["about","author","license","usage"]
elsif arg == ["help"]
arg = ["about","usage"]
end
arg.each_with_index do |value,index|
case value
when "about"
puts "#{RDot::TITLE}"
if index < arg.size-1
puts ""
end
when "author"
if arg.size > 1
if $stdout.stat.chardev?
puts "\e[1mAuthor:\e[0m #{RDot::AUTHOR}"
else
puts "Author: #{RDot::AUTHOR}"
end
else
puts "#{RDot::AUTHOR}"
end
when "license"
if arg.size > 1
if $stdout.stat.chardev?
puts "\e[1mLicense:\e[0m #{RDot::LICENSE}"
else
puts "License: #{RDot::LICENSE}"
end
else
puts "#{RDot::LICENSE}"
end
when "usage"
if index > 0 && ! (arg[index-1] == "about")
puts ""
end
puts "#{opts.to_s}"
when "version"
if ! arg.include? "about"
if arg.size > 1
if $stdout.stat.chardev?
puts "\e[1mVersion:\e[0m #{RDot::VERSION}"
else
puts "Version: #{RDot::VERSION}"
end
else
puts "#{RDot::VERSION}"
end
end
end
end
exit 0
end
opts.on("--about",
"Show program title with version and exit.") do
puts "#{RDot::TITLE}"
exit 0
end
opts.on("--author",
"Show author information and exit.") do
puts "#{RDot::AUTHOR}"
exit 0
end
opts.on("-L","--license",
"Show license information and exit.") do
puts "#{RDot::LICENSE}"
exit 0
end
opts.on("-?","--usage",
"Show command line arguments and exit.") do
puts "#{opts.to_s}"
exit 0
end
opts.on("-V","--version",
"Show version and exit.") do
puts "#{RDot::VERSION}"
exit 0
end
opts.on("-x","--exclude","-p","--preload","=LIBS",Array,
"Comma-separated list of libraries",
"which must be excluded from output graph.",
"This libs will be preloaded before start",
"registering any changes like standard",
"objects.") do |arg|
@excludes += arg
end
opts.on("-i","--include","--input","=LIBS",Array,
"Comma-separated list of libraries for",
"processing. Also libs may be specified",
"in command line without any key.") do |arg|
@includes += arg
end
opts.on("-o","--output","=[FILE]",String,
"Send output to file instead stdout.",
"'rdot.dot' if no argument specified.") do |arg|
if arg == nil
arg = 'rdot.dot'
end
@output = File.open(arg,"w")
end
opts.on("-t","--title","=TITLE",String,
"Set graph title.") do |arg|
@title = arg
end
opts.on '-f', '--font', '=NAME', String, 'Set font name.' do |arg|
@fontname = arg
end
opts.on '-F', '--caption-font', '=NAME', String, 'Set caption fontname.' do |arg|
@caption_fontname = arg
end
opts.on("--color-class","=COLOR",String,
"Set class-node color. See:",
"http://graphviz.org/doc/info/colors.html") do |arg|
@color_class = arg
end
opts.on("--color-class-preloaded","=COLOR",String,
"Set node color for preloaded classes.") do |arg|
@color_class_preloaded = arg
end
opts.on("--color-class-evaluated","=COLOR",String,
"Set node color for evaluated classes.") do |arg|
@color_class_evaluated = arg
end
opts.on("--color-exception","=COLOR",String,
"Set node color for exception classes.") do |arg|
@color_exception = arg
end
opts.on("--color-exception-preloaded","=COLOR",String,
"Set node color for preloaded exceptions.") do |arg|
@color_exception_preloaded = arg
end
opts.on("--color-exception-evaluated","=COLOR",String,
"Set node color for evaluated exceptions.") do |arg|
@color_exception_evaluated = arg
end
opts.on("--color-module","=COLOR",String,
"Set node color for modules.") do |arg|
@color_module = arg
end
opts.on("--color-module-preloaded","=COLOR",String,
"Set node color for preloaded modules.") do |arg|
@color_module_preloaded = arg
end
opts.on("--color-inherited","=COLOR",String,
"Set inheritance-edge color.") do |arg|
@color_inherited = arg
end
opts.on("--color-included","=COLOR",String,
"Set include-edge color.") do |arg|
@color_included = arg
end
opts.on("--color-extended","=COLOR",String,
"Set extension-edge color.") do |arg|
@color_extended = arg
end
opts.on("--color-protected","=COLOR",String,
"Set color marker for protected methods.") do |arg|
@color_protected = arg
end
opts.on("--color-private","=COLOR",String,
"Set color marker for private methods.") do |arg|
@color_private = arg
end
opts.on("-s","--show-protected",TrueClass,
"Show public and protected methods.") do
@show_protected = true
end
opts.on("-S","--show-private",TrueClass,
"Show public, protected and private methods.") do
@show_protected = true
@show_private = true
end
opts.on("-z","--show-preloaded",TrueClass,
"Show methods etc. for preloaded classes.",
"Use this to see the structure of standard",
"Ruby classes.") do
@show_preloaded = true
end
opts.on("-M","--hide-methods",TrueClass,
"Hide all methods from diagram.") do
@hide_methods = true
end
opts.on("-C","--hide-constants",TrueClass,
"Hide all constants.") do
@hide_constants = true
end
opts.on '-A', '--hide-arguments', TrueClass,
'Hide method arguments.' do
@hide_arguments = true
end
opts.on '-r', '--no-trace', TrueClass, 'Don\'t use trace func.',
'Use this if vizualized library make', 'tracing itself.' do
@no_trace = true
end
opts.on("-e","--except","=[LIST]",Array,
"Except classes (with children) from",
"processing. If no classes specified,",
"the Exception will excepted.") do |arg|
if arg == nil
@exceptions << Exception
else
arg.each do |value|
begin
cls = eval value
if Class === cls
@exceptions << cls
end
rescue Exception