Farmbot-Web-App/latest_corpus.rb

74 lines
1.6 KiB
Ruby
Raw Normal View History

2016-12-21 09:33:29 -07:00
require "json"
require "pry"
2016-12-21 11:30:57 -07:00
require "rails"
2016-12-21 12:25:56 -07:00
PIPE = "\n | "
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
2016-12-21 12:25:56 -07:00
allowed_args.map{|x| ARGS[x]}.map(&:to_ts).join("")
2016-12-21 11:30:57 -07:00
end
def body_names
2016-12-21 11:56:50 -07:00
b = allowed_body_types.map(&:camelize).join(PIPE)
2016-12-21 11:30:57 -07:00
b.length > 0 ? b : "undefined"
end
def args
end
def to_ts
"""
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;
body?: (#{body_names})[] | undefined;
2016-12-21 11:56:50 -07:00
}
2016-12-21 11:30:57 -07:00
"""
end
end
HASH = JSON.parse(File.read("./latest_corpus.json")).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 11:56:50 -07:00
ALL = NODES.map(&:name).map(&:camelize).join(PIPE);
2016-12-21 12:25:56 -07:00
result = NODES.map(&:to_ts)
2016-12-21 11:56:50 -07:00
result.push "\n export type CeleryNode = #{ALL};"
puts result.join("")