-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_example.rb
More file actions
218 lines (184 loc) · 6.54 KB
/
transform_example.rb
File metadata and controls
218 lines (184 loc) · 6.54 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
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
require_relative 'ClassTracer.rb'
#For this one we're not going to profile anything.
#We're just going to load an existing archived profile and transform it out
#You could / should probably do this with some sort of templating system like mustache
class_archive = "SimpsonsProfile.json"
class_tracer = ClassTraceUtils::ClassTracer.restoreFromArchive(class_archive)
#Format our output
#just in case you want to only show certain classes
limit_to_classes = []
def suggest_types(types, nilable = nil)
newTypes = Set.new []
swift_type_suggestions = {
"TrueClass" => "Boolean",
"FalseClass" => "Boolean",
"Fixnum" => "Int",
"Array[]" => "[AnyObject]",
"NilClass" => "AnyObject"
}
if types.is_a? Set
types = types.to_a
end
types.each do |t|
next if t == "NilClass" && types.count > 1
next if t == "Array[]" && types.select{|t2| t2 =~ /^Array/ && t != t2 }.collect{|t2| t}.count > 0
next if t == "Hash" && types.select{|t2| t2 =~ /^Hash/ && t != t2 }.collect{|t2| t}.count > 0
if t =~ /^Array\[/
/\[(?<data_type>.*)\]/ =~ t
data_types = data_type.split(",")
#we can have arrays of multiple types
data_types.each do |data_type|
data_type = swift_type_suggestions[data_type] || data_type
data_type = "[#{data_type}]"
newTypes << data_type
end
next
end
if t =~ /^Hash:/
/:\[(?<data_type>.*)\]/ =~ t
data_type = swift_type_suggestions[data_type] || data_type
data_type = "[String: #{data_type}]"
#yeah, I know - but this is a demo of possible approaches, not an actual tool
# and chances are it's a string hash key, soooo....
newTypes << data_type
next
end
newT = swift_type_suggestions[t] || t
newTypes << newT
end
newTypes.to_a
end
class_list = class_tracer.classes
class_list.each do |c_name, c_val|
if limit_to_classes.count > 0
if !limit_to_classes.include?(c_name)
next
end
end
class_vars = {}
class_vars[:instance] = []
class_vars[:methods] = []
#super redundant code here
if !c_val.instance_vars.empty?
c_val.instance_vars.each do |i_name, i_val|
i_vars = {}
i_vars[:name] = i_name
suggested_types = suggest_types(i_val.types, i_val.nilable)
type = suggested_types.collect{|t| t}.join(", ")
if suggested_types.count > 1
type = "AnyObject"
if suggested_types.index{|s| s =~ /\[/ && s !~ /\:/}
type = "[#{type}]"
end
i_vars[:comments] = " '#{i_name}' has multiple types. Defaulting to AnyObject CHOOSE FROM *** ( #{suggested_types} ) *** "
end
nilable = i_val.nilable ? "?" : ""
i_vars[:def] = " var #{i_name} : #{type}#{nilable}"
class_vars[:instance] << i_vars
end
end
if !c_val.methods.empty?
c_val.methods.each do |m_name, m_val|
method_vars = {}
method_vars[:method] = {}
method_vars[:return] = {}
method_vars[:call] = {}
method_vars[:local] = {}
method_vars[:calling_args] = ""
method_returns = ""
calling_args = ""
return_types = suggest_types(m_val.return_types, nil)
return_type = return_types.collect{|t| t }.join(", ")
method_vars[:return][:name] = "RETURN"
if return_types.count > 1
return_type = "AnyObject"
if return_types.index{|s| s =~ /\[/ && s !~ /\:/}
return_type = "[#{return_type}]"
end
method_vars[:return][:comments] = " Returns multiple types. Defaulting to AnyObject. CHOOSE FROM *** ( #{return_types} ) *** "
end
return_nilable = m_val.return_types.select{ |t| t != "NilClass" }.collect{|t| t }.size > 0 ? "?" : ""
if return_types.index{|s| s =~ /\[/ && s !~ /\:/}
return_nilable = " //Return can be empty array"
end
return_type = "#{return_type}#{return_nilable}"
method_vars[:return][:def] = return_type
if !m_val.calling_vars.empty?
args_ar = []
m_val.calling_vars.each do |c_name, c_val|
method_vars[:call][c_name] = {}
types = suggest_types(c_val.types, nil)
type = types.collect{|t| t }.join(", ")
if types.count > 1
type = "AnyObject"
if types.index{|s| s =~ /\[/ && s !~ /\:/}
type = "[#{type}]"
end
method_vars[:call][c_name][:comments] = " Param '#{c_name}' contains multiple types. Defaulting to AnyObject. CHOOSE FROM *** ( #{types} ) *** "
end
nilable = c_val.nilable || c_val.arg_type == "[opt]" ? "?" : ""
if types.index{|s| s =~ /\[/ && s !~ /\:/}
nilable = " = []"
elsif types.index{|s| s =~ /\[/ && s =~ /\:/}
nilable = " = [:]"
end
arg_type = c_val.arg_type ? " [#{c_val.arg_type}] " : ""
method_vars[:call][c_name][:def] = "#{c_name}: #{type}#{nilable}"
args_ar << "#{c_name}: #{type}#{nilable}"
end
calling_args = args_ar.join(", ")
calling_args = "(#{calling_args})"
end
if !m_val.local_vars.empty?
m_val.local_vars.each do |l_name, l_val|
method_vars[:local][l_name] = {}
types = suggest_types(l_val.types, nil)
type = types.collect{|t| t }.join(", ")
if types.count > 1
type = "AnyObject"
if types.index{|s| s =~ /\[/ && s !~ /\:/}
type = "[#{type}]"
end
method_vars[:local][l_name][:comments] = " '#{l_name}' has multiple types. Defaulting to AnyObject. CHOOSE FROM *** ( #{types} ) *** "
end
arg_type = l_val.arg_type ? " [#{l_val.arg_type}] " : ""
nilable = l_val.nilable || arg_type == "opt" ? "?" : ""
method_vars[:local][l_name][:def] = "#{l_name}: #{type}#{nilable}"
end
end
func_type = m_val.calling_vars.empty? ? "var " : "func "
method_vars[:method][:def] = "#{func_type}#{m_name}#{calling_args}" + (return_type.empty? ? "" : " -> #{return_type}")
class_vars[:methods] << method_vars
end
end
#draw our "pseudo-Swift"
puts ""
puts "class #{c_val.class_name}"
puts "{"
class_vars[:instance].each do |i|
if i[:comments] != nil
puts " /// FIXME: #{i[:comments]}" if i[:comments] != nil
end
puts i[:def]
end
puts ""
class_vars[:methods].each do |i|
i[:call].each do |name, var|
puts " /// FIXME: #{var[:comments]}" if var[:comments] != nil
puts " /// :param: #{name}"
end
if i[:return] != nil
puts " /// FIXME: #{i[:return][:comments]}" if i[:return][:comments] != nil
puts " /// :returns: #{i[:return][:def]}"
end
puts " #{i[:method][:def]}"
puts " {"
i[:local].each do |name, var|
puts " /// FIXME: #{var[:comments]}" if var[:comments] != nil
puts " #{i[:local][name][:def]}"
end
puts " }"
puts ""
end
puts "}"
end