Farmbot-Web-App/latest_corpus.rb

144 lines
4.0 KiB
Ruby
Raw Normal View History

2016-12-21 09:33:29 -07:00
require "json"
require "pry"
2017-01-05 15:16:59 -07:00
require "rails/all"
require "open-uri"
2016-12-29 13:01:52 -07:00
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"
2017-01-03 13:26:40 -07:00
require_relative "./app/models/sequence.rb"
2016-12-29 13:01:52 -07:00
Dir["./app/lib/sequence_migration/*.rb"].each {|file| require file }
2017-01-19 13:43:55 -07:00
UNDEFINED = "undefined"
2016-12-27 08:54:11 -07:00
PIPE = "\n | "
2016-12-21 12:25:56 -07:00
2016-12-21 11:30:57 -07:00
class CSArg
TRANSLATIONS = { "integer" => "number",
"string" => "string" }
attr_reader :name, :allowed_values
2016-12-21 09:33:29 -07:00
2016-12-21 11:30:57 -07:00
def initialize(name:, allowed_values:)
@name, @allowed_values = name, allowed_values
end
def camelize
name.camelize
end
def values
2016-12-21 11:56:50 -07:00
allowed_values.map { |v| TRANSLATIONS[v] || v.camelize }.join(PIPE)
2016-12-21 11:30:57 -07:00
end
def to_ts
2016-12-21 12:25:56 -07:00
"\n #{name}: #{values};"
2016-12-21 11:30:57 -07:00
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
2017-01-03 06:54:11 -07:00
allowed_args
.map{ |x| ARGS[x] }
2017-07-20 12:26:44 -06:00
.each { |x| raise "NON-EXISTENT ARG TYPE" unless x }
2017-01-03 06:54:11 -07:00
.map(&:to_ts)
.join("")
2016-12-21 11:30:57 -07:00
end
2017-01-26 10:44:59 -07:00
def items_joined_by_pipe
allowed_body_types.map(&:camelize).join(PIPE)
end
2016-12-21 11:30:57 -07:00
def body_names
2017-01-26 10:44:59 -07:00
b = items_joined_by_pipe
2017-01-19 13:43:55 -07:00
(b.length > 0) ? "(#{b})[] | undefined" : UNDEFINED
2016-12-21 11:30:57 -07:00
end
2017-01-19 13:43:55 -07:00
def has_body?
body_names != UNDEFINED
end
2016-12-21 11:30:57 -07:00
2017-01-19 13:43:55 -07:00
def body_type
2017-01-26 10:44:59 -07:00
"export type #{camelize}BodyItem = #{items_joined_by_pipe};" if has_body?
2017-01-19 13:43:55 -07:00
end
def body_attr
2017-01-26 10:44:59 -07:00
"body?: #{ has_body? ? (camelize + "BodyItem[] | undefined") : UNDEFINED };"
2016-12-21 11:30:57 -07:00
end
def to_ts
"""
2017-01-19 13:43:55 -07:00
#{body_type}
2016-12-21 11:56:50 -07:00
export interface #{camelize} {
kind: #{name.inspect};
2016-12-21 12:25:56 -07:00
args: {#{arg_names}
};
comment?: string | undefined;
2017-01-19 13:43:55 -07:00
#{body_attr}
2016-12-21 11:56:50 -07:00
}
2016-12-21 11:30:57 -07:00
"""
end
end
2016-12-29 13:01:52 -07:00
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
2017-01-05 15:16:59 -07:00
HASH = JSON.load(open("http://localhost:3000/api/corpuses/3")).deep_symbolize_keys
2016-12-21 12:25:56 -07:00
ARGS = {}
HASH[:args].map{ |x| CSArg.new(x) }.each{|x| ARGS[x.name] = x}
2016-12-21 11:30:57 -07:00
NODES = HASH[:nodes].map { |x| CSNode.new(x) }
2016-12-21 12:25:56 -07:00
result = NODES.map(&:to_ts)
2016-12-29 13:01:52 -07:00
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)
2017-01-03 06:54:11 -07:00
result.push(enum_type :ALLOWED_PACKAGES,
CeleryScriptSettingsBag::ALLOWED_PACKAGES)
2017-01-03 13:26:40 -07:00
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 :PointType, CeleryScriptSettingsBag::ALLOWED_POINTER_TYPE)
2017-01-26 10:44:59 -07:00
puts result.join.gsub("\n\n\n", "\n")