forked from FarmBot/Farmbot-Web-App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatest_corpus.rb
executable file
·144 lines (119 loc) · 4.11 KB
/
latest_corpus.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
require "json"
require "pry"
require "rails/all"
require "open-uri"
require_relative "./app/lib/celery_script/node_specification"
require_relative "./app/lib/celery_script/argument_specification"
require_relative "./app/lib/celery_script/corpus"
require_relative "./app/lib/sequence_migration"
require_relative "./app/models/celery_script_settings_bag.rb"
require_relative "./app/models/sequence.rb"
Dir["./app/lib/sequence_migration/*.rb"].each {|file| require file }
UNDEFINED = "undefined"
PIPE = "\n | "
class CSArg
TRANSLATIONS = { "integer" => "number",
"string" => "string" }
attr_reader :name, :allowed_values
def initialize(name:, allowed_values:)
@name, @allowed_values = name, allowed_values
end
def camelize
name.camelize
end
def values
allowed_values.map { |v| TRANSLATIONS[v] || v.camelize }.join(PIPE)
end
def to_ts
"\n #{name}: #{values};"
end
end
class CSNode
attr_reader :name, :allowed_args, :allowed_body_types
def initialize(name:, allowed_args:, allowed_body_types: [])
@name,
@allowed_args,
@allowed_body_types = name, allowed_args, allowed_body_types
end
def camelize
name.camelize
end
def arg_names
allowed_args
.map{ |x| ARGS[x] }
.each { |x| raise "NON-EXISTENT ARG TYPE" unless x }
.map(&:to_ts)
.join("")
end
def items_joined_by_pipe
allowed_body_types.map(&:camelize).join(PIPE)
end
def body_names
b = items_joined_by_pipe
(b.length > 0) ? "(#{b})[] | undefined" : UNDEFINED
end
def has_body?
body_names != UNDEFINED
end
def body_type
"export type #{camelize}BodyItem = #{items_joined_by_pipe};" if has_body?
end
def body_attr
"body?: #{ has_body? ? (camelize + "BodyItem[] | undefined") : UNDEFINED };"
end
def to_ts
"""
#{body_type}
export interface #{camelize} {
kind: #{name.inspect};
args: {#{arg_names}
};
comment?: string | undefined;
#{body_attr}
}
"""
end
end
def const(key, val)
"\nexport const #{key} = #{val};"
end
def enum_type(key, val, inspect = true)
"\nexport type #{key} = #{(inspect ? val.map(&:inspect) : val).join(PIPE)};"
end
HASH = JSON.load(open("http://localhost:3000/api/corpuses/3")).deep_symbolize_keys
ARGS = {}
HASH[:args].map{ |x| CSArg.new(x) }.each{|x| ARGS[x.name] = x}
NODES = HASH[:nodes].map { |x| CSNode.new(x) }
result = NODES.map(&:to_ts)
result.unshift("""
/*
THIS INTERFACE WAS AUTO GENERATED ON #{Date.today}
DO NOT EDIT THIS FILE.
IT WILL BE OVERWRITTEN ON EVERY CELERYSCRIPT UPGRADE.
*/
""")
result.push(enum_type :CeleryNode, NODES.map(&:name).map(&:camelize), false)
result.push(const(:LATEST_VERSION, SequenceMigration::Base.latest_version))
result.push(const :DIGITAL, CeleryScriptSettingsBag::DIGITAL)
result.push(const :ANALOG, CeleryScriptSettingsBag::ANALOG)
result.push(enum_type :ALLOWED_PIN_MODES,
CeleryScriptSettingsBag::ALLOWED_PIN_MODES)
result.push(enum_type :ALLOWED_MESSAGE_TYPES,
CeleryScriptSettingsBag::ALLOWED_MESSAGE_TYPES)
result.push(enum_type :ALLOWED_CHANNEL_NAMES,
CeleryScriptSettingsBag::ALLOWED_CHANNEL_NAMES)
result.push(enum_type :ALLOWED_DATA_TYPES,
CeleryScriptSettingsBag::ALLOWED_DATA_TYPES)
result.push(enum_type :ALLOWED_OPS,
CeleryScriptSettingsBag::ALLOWED_OPS)
result.push(enum_type :ALLOWED_PACKAGES,
CeleryScriptSettingsBag::ALLOWED_PACKAGES)
result.push(enum_type :ALLOWED_AXIS, CeleryScriptSettingsBag::ALLOWED_AXIS)
result.push(enum_type :Color, Sequence::COLORS)
result.push(enum_type :LegalArgString, HASH[:args].map{ |x| x[:name] }.sort.uniq)
result.push(enum_type :LegalKindString, HASH[:nodes].map{ |x| x[:name] }.sort.uniq)
result.push(enum_type :LegalSequenceKind, CeleryScriptSettingsBag::STEPS.sort)
result.push(enum_type :DataChangeType, CeleryScriptSettingsBag::ALLOWED_CHAGES)
result.push(enum_type :ResourceName, CeleryScriptSettingsBag::RESOURCE_NAME)
result.push(enum_type :PointType, CeleryScriptSettingsBag::ALLOWED_POINTER_TYPE)
puts result.join.gsub("\n\n\n", "\n")