Class: ABIDump::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/abidump.rb

Class Method Summary collapse

Class Method Details

._dump(data, indent: 2) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/abidump.rb', line 114

def self._dump( data, indent: 2 )
  buf = YAML.dump( data )
  buf = buf.sub( /^---\n?/, '' )   ## remove leading --- if present

  # puts "---> debug:"
  # pp buf
  # puts "<---"

  buf.each_line do |line|
    print ' ' * indent
    puts line
  end
end

._norm_types(data) ⇒ Object



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
# File 'lib/abidump.rb', line 75

def self._norm_types( data )
   ## hack:  remove items and re-add to sort key order!!!
   ##
   ## clean-up / normalize type
   ##   if type && internalType is the same
   ##      than delete internalType for now - why? why not?
   data.each do |h|
      name          = h.delete('name')
      type          = h.delete('type')
      internal_type = h.delete('internalType')
      indexed       = h.delete('indexed')
      components    = h.delete('components')

      unless h.empty?
        puts "!! ERROR - found unknown props in abi inputs/outpus:"
        pp h
        exit 1
      end

      h['type'] = type   if type
      if type && internal_type && type != internal_type
        h['type'] += " (#{internal_type})"
      end

      h['indexed'] = indexed   unless indexed.nil?  ## note: indexed is a true/false prop

      ## note: change empty name e.g. '' to _  - why? why not?
      if name
        h['name']    = name.empty?  ? "_" : name
      end

      if components
         h['components'] = components
        _norm_types( components)
      end
   end
end

.do_dump(data) ⇒ Object



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
# File 'lib/abidump.rb', line 129

def self.do_dump( data )

  ## calc summary stats
  counter = {}
  data.each do |h|
     type = h['type']
     stat = counter[ type ] ||= { count: 0, names: [] }
     stat[:count] += 1

     name = h['name']
     stat[:names] << name   if name
  end


   data.each do |h|
      type    = h.delete('type')
      name    = h.delete('name')
      inputs  = h.delete('inputs')
      outputs = h.delete('outputs')

      puts
      print "==> #{type}"
      print " #{name}"    if name
      puts ":"
      _dump( h, indent: 6 )

      if inputs
        puts "    inputs (#{inputs.size}):"
        _norm_types( inputs )
        _dump( inputs, indent: 6 )
      end

      if outputs
        puts "    outputs (#{outputs.size}):"
        _norm_types( outputs )
        _dump( outputs, indent: 6 )
      end
   end

 ## always dump stats at the end - why? why not?
 puts
 puts "==> summary: "
 puts "    #{data.size} abi item(s):"
 counter.each do |(type, h)|
    count = h[:count]
    print "      #{count}"
    print " #{type}"
    print "s" if count > 1
    print "\n"

    names = h[:names]
    if names.size > 0
       print "         "
       print names.join( ', ' )
       print "\n"
    end
 end
end

.do_dump_json(data) ⇒ Object



70
71
72
73
# File 'lib/abidump.rb', line 70

def self.do_dump_json( data )
  buf = JSON.pretty_generate( data )
  puts buf
end

.do_dump_yaml(data) ⇒ Object



65
66
67
68
# File 'lib/abidump.rb', line 65

def self.do_dump_yaml( data )
  buf = YAML.dump( data )
  puts buf
end

.main(args = ARGV) ⇒ Object



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
# File 'lib/abidump.rb', line 10

def self.main( args=ARGV )
  puts "==> welcome to abidump tool with args:"
  pp args

  options = {
            }

  parser = OptionParser.new do |opts|

    opts.on("-j", "--json", "use json format (default: false)") do |value|
      options[ :json]  = true
    end

    opts.on("-y", "--yaml", "use yaml format (default: false)") do |value|
        options[ :yaml]  = true
    end

    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end

  parser.parse!( args )
  puts "options:"
  pp options

  puts "args:"
  pp args

  if args.size < 1
    puts "!! ERROR - no abi contract path found - use  $ abidump <path>"
    puts ""
    exit
  end

  path = args[0]

  ## read data
  data = read_json( path )

  if options[ :yaml ]
     do_dump_yaml( data )
  elsif options[ :json ]
     do_dump_json( data )
  else
     do_dump( data )
  end

  puts "bye"
end