-
Notifications
You must be signed in to change notification settings - Fork 24
/
generate_snippets.rb
204 lines (190 loc) · 4.91 KB
/
generate_snippets.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
require 'json'
require 'pry'
def get_snippets(args)
return args.select{|i, v| v.keys.include? "Required" and v["Required"] }.map do |arg, value|
case value["Type"]
when "TypeString"
"\t#{arg} = \"\""
when "TypeInt"
"\t#{arg} = 1"
when "TypeFloat"
"\t#{arg} = 1.0"
when "TypeBool"
"\t#{arg} = false"
when "TypeList", "TypeSet", "TypeMap"
if not value["Elem"]["ElementsType"].nil?
case value["Elem"]["ElementsType"]
when "TypeString"
"\t#{arg} = [\"\"]"
when "TypeInt"
"\t#{arg} = [1]"
when "TypeFloat"
"\t#{arg} = [1.0]"
when "TypeMap"
snippet_result = "\t#{arg} {\n"
snippet_result += "\n\t\t\t}"
snippet_result
else
require 'pry'; binding.pry
end
else
snippet_result = "\t#{arg} {\n"
if not value["Elem"].empty?
case value["Elem"]["Type"]
when "SchemaInfo"
snippet_result += "\t\t\t" + get_snippets(value["Elem"]["Info"]).join("\n\t\t\t")
when "schema.ValueType", "SchemaElements"
else
require 'pry'; binding.pry
end
end
snippet_result += "\n\t\t\t}"
snippet_result
end
else
require 'pry'; binding.pry
end
end
end
result = JSON.parse(File.read(ARGV[1]))
dicts = [ARGV[0]]
provider_args = result["provider"].select{|i, v| v.keys.include? "Required" and v["Required"] }
current = 0
provider_args = provider_args.map do |i,v|
current += 1
case v["Type"]
when "TypeString"
"#{i} = \"${#{current}}\""
when "TypeInt"
"#{i} = ${#{current}}"
when "TypeFloat"
"#{i} = ${#{current}}"
when "TypeBool"
"#{i} = ${#{current}:false}"
else
require 'pry'; binding.pry
end
end
current = 0
snippets =<<EOF
snippet #{ARGV[0]}
\tprovider "#{ARGV[0]}" {
\t\t#{provider_args.join("\n\t\t")}
\t}
EOF
result["resources"].each do |i, v|
args = v.select{|l, m| m.keys.include? "Required" and m["Required"] }
current = 1
args = args.map do |o,p|
current += 1
case p["Type"]
when "TypeString"
"#{o} = \"${#{current}}\""
when "TypeInt"
"#{o} = ${#{current}}"
when "TypeFloat"
"#{o} = ${#{current}}"
when "TypeBool"
"#{o} = ${#{current}:false}"
when "TypeList", "TypeSet", "TypeMap"
if not p["Elem"]["ElementsType"].nil?
case p["Elem"]["ElementsType"]
when "TypeString"
"#{o} = [\"${#{current}}\"]"
when "TypeInt"
"#{o} = [${#{current}}]"
when "TypeFloat"
"#{o} = [${#{current}}]"
when "TypeMap"
snippet_result = "\t#{o} {\n"
snippet_result += "\n\t\t\t}"
snippet_result
else
require 'pry'; binding.pry
end
else
snippet_result = "#{o} {\n"
if not p["Elem"].empty?
case p["Elem"]["Type"]
when "SchemaInfo"
snippet_result += "\t\t" + get_snippets(p["Elem"]["Info"]).join("\n\t\t")
when "schema.ValueType", "SchemaElements"
else
require 'pry'; binding.pry
end
end
snippet_result += "\n\t\t}"
snippet_result
end
else
require 'pry'; binding.pry
end
end
#
dicts.push("r_#{i}")
snippets +=<<EOF
snippet r_#{i}
\tresource "#{i}" "${1}" {
\t\t#{args.join("\n\t\t")}
\t}
EOF
end
result["data-sources"].each do |i, v|
args = v.select{|l, m| m.keys.include? "Required" and m["Required"] }
current = 1
args = args.map do |o,p|
current += 1
case p["Type"]
when "TypeString"
"#{o} = \"${#{current}}\""
when "TypeInt"
"#{o} = ${#{current}}"
when "TypeFloat"
"#{o} = ${#{current}}"
when "TypeBool"
"#{o} = ${#{current}:false}"
when "TypeList", "TypeSet", "TypeMap"
if not p["Elem"]["ElementsType"].nil?
case p["Elem"]["ElementsType"]
when "TypeString"
"#{o} = [\"${#{current}}\"]"
when "TypeInt"
"#{o} = [${#{current}}]"
when "TypeFloat"
"#{o} = [${#{current}}]"
when "TypeMap"
snippet_result = "\t#{o} {\n"
snippet_result += "\n\t\t\t}"
snippet_result
else
require 'pry'; binding.pry
end
else
snippet_result = "#{o} {\n"
if not p["Elem"].empty?
case p["Elem"]["Type"]
when "SchemaInfo"
snippet_result += "\t\t\t" + get_snippets(p["Elem"]["Info"]).join("\n\t\t\t")
when "schema.ValueType", "SchemaElements"
else
require 'pry'; binding.pry
end
end
snippet_result += "\n\t\t\t}"
snippet_result
end
else
require 'pry'; binding.pry
end
end
#
dicts.push("d_#{i}")
snippets +=<<EOF
snippet d_#{i}
\tdata "#{i}" "${1}" {
\t\t#{args.join("\n\t\t")}
\t}
EOF
end
File.write("dicts/#{ARGV[2]}_#{ARGV[0]}.dicts", dicts.join("\n"))
puts snippets