Farmbot-Web-App/latest_corpus.rb

79 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 11:56:50 -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
"""
interface #{camelize} {
#{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(&:camelize).join(", ")
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
interface #{camelize}Args extends #{arg_names} { }
2016-12-21 11:30:57 -07:00
2016-12-21 11:56:50 -07:00
type #{camelize}BodyNode = #{ body_names };
2016-12-21 11:30:57 -07:00
2016-12-21 11:56:50 -07:00
export interface #{camelize} {
kind: #{name.inspect};
args: #{camelize}Args;
body: #{camelize}BodyNode[] | undefined;
}
2016-12-21 11:30:57 -07:00
"""
end
end
HASH = JSON.parse(File.read("./latest_corpus.json")).deep_symbolize_keys
ARGS = HASH[:args].map { |x| CSArg.new(x) }
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 11:30:57 -07:00
result = ARGS.map(&:to_ts)
result += NODES.map(&:to_ts)
2016-12-21 11:56:50 -07:00
result.push "\n export type CeleryNode = #{ALL};"
puts result.join("")