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 } 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] } .map(&:to_ts) .join("") end def body_names b = allowed_body_types.map(&:camelize).join(PIPE) (b.length > 0) ? "(#{b})[] | undefined" : "undefined" end def args end def to_ts """ export interface #{camelize} { kind: #{name.inspect}; args: {#{arg_names} }; comment?: string | undefined; body?: #{body_names}; } """ 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) puts result.join