Class: Foobara::RemoteGenerator::Generators::TypescriptFromManifestBaseGenerator
- Inherits:
-
FilesGenerator
- Object
- FilesGenerator
- Foobara::RemoteGenerator::Generators::TypescriptFromManifestBaseGenerator
- Defined in:
- foobara-typescript-remote-command-generator-1.5.0/src/generators/typescript_from_manifest_base_generator.rb
Direct Known Subclasses
Auth::AccessTokensGenerator, Auth::LoginCommandGenerator, Auth::LogoutCommandGenerator, Auth::RequiresAuthCommandGenerator, CommandGenerator, CommandInputsGenerator, DomainGenerator, ErrorGenerator, ManifestGenerator, OrganizationGenerator, ProcessorClassGenerator, RemoteCommandGenerator, TypeGenerator
Constant Summary
Constants included from TruncatedInspect
Instance Attribute Summary
Attributes inherited from FilesGenerator
#belongs_to_dependency_group, #relevant_manifest
Class Method Summary collapse
- .manifest_to_generator_classes(manifest) ⇒ Object
- .requires_auth_command_generatable?(manifest) ⇒ Boolean
- .requires_auth_generator_for(manifest) ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
Files generator checks that the relevant_manifest is the same but this is faster.
- #attributes_to_ts_type(attributes, dependency_group:, association_depth: AssociationDepth::AMBIGUOUS, model_and_entity_free: false, is_output: false, parent: nil) ⇒ Object
- #auto_dirty_queries? ⇒ Boolean
- #collision_winners ⇒ Object
- #custom_type_to_ts_type_name(type) ⇒ Object
- #dependency_group ⇒ Object
- #dependency_roots ⇒ Object
- #domain_name ⇒ Object
-
#foobara_type_to_ts_type(type_declaration, dependency_group: self.dependency_group, name: nil, association_depth: AssociationDepth::AMBIGUOUS, initial: true, model_and_entity_free: false, is_output: false, parent: nil) ⇒ Object
is_output means the value came from elsewhere and is fully formed.
- #hash ⇒ Object
- #import_destructure ⇒ Object
- #import_path ⇒ Object
- #import_path_array ⇒ Object
-
#initialize(relevant_manifest = (none_given = true)) ⇒ TypescriptFromManifestBaseGenerator
constructor
A new instance of TypescriptFromManifestBaseGenerator.
- #model_to_ts_model_name(model, association_depth: AssociationDepth::AMBIGUOUS, initial: true) ⇒ Object
- #organization_name ⇒ Object
- #parent ⇒ Object
- #path_to_root ⇒ Object
- #templates_dir ⇒ Object
- #ts_instance_full_name ⇒ Object
- #ts_instance_full_path ⇒ Object
- #ts_instance_path ⇒ Object
- #value_to_ts_value(value) ⇒ Object
- #will_define ⇒ Object
Methods inherited from FilesGenerator
#absolute_template_path, #applicable?, #dependencies, #dependencies_to_generate, #eql?, #erb_template, #generate, generator_for, #generator_for, generators_for, #generators_for, #method_missing, #respond_to_missing?, #target_dir, #target_path, #template_path, #template_string
Methods included from TruncatedInspect
Constructor Details
#initialize(relevant_manifest = (none_given = true)) ⇒ TypescriptFromManifestBaseGenerator
Returns a new instance of TypescriptFromManifestBaseGenerator.
116 117 118 119 120 121 122 123 124 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 116 def initialize(relevant_manifest = (none_given = true)) if none_given || relevant_manifest.is_a?(Manifest::BaseManifest) super else # simplecov:disable raise ArgumentError, "Expected a Foobara::Manifest, got #{relevant_manifest.class}" # simplecov:enable end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Foobara::FilesGenerator
Class Method Details
.manifest_to_generator_classes(manifest) ⇒ Object
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 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 47 def manifest_to_generator_classes(manifest) case manifest when Manifest::Command generator_classes = case manifest.full_command_name when "Foobara::Auth::RefreshLogin" Auth::RefreshLoginGenerator when "Foobara::Auth::Login" Auth::LoginGenerator when "Foobara::Auth::Logout" Auth::LogoutGenerator when /\bGetCurrentUser$/ requires_auth_generator_for(manifest) else if manifest.requires_authentication? requires_auth_generator_for(manifest) else CommandGenerator end end [ *generator_classes, CommandInputsGenerator, CommandResultGenerator, CommandCastResultGenerator, CommandErrorsGenerator, CommandErrorsIndexGenerator, CommandManifestGenerator ] when Manifest::Domain [ DomainGenerator, DomainConfigGenerator, DomainManifestGenerator ] when Manifest::Organization [ OrganizationGenerator, OrganizationConfigGenerator, OrganizationManifestGenerator ] when Manifest::Entity, Manifest::DetachedEntity [ EntityGenerator, EntityVariantsGenerator, EntityManifestGenerator ] when Manifest::Model [ ModelGenerator, ModelVariantsGenerator, ModelManifestGenerator ] when Manifest::Error ErrorGenerator when Manifest::ProcessorClass ProcessorClassGenerator when Manifest::RootManifest RootManifestGenerator when Manifest::Type TypeGenerator else # simplecov:disable raise "Not sure how build a generator for a #{manifest}" # simplecov:enable end end |
.requires_auth_command_generatable?(manifest) ⇒ Boolean
28 29 30 31 32 33 34 35 36 37 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 28 def requires_auth_command_generatable?(manifest) return false if RemoteGenerator. root = manifest.root_manifest commands = root[:command] || root["command"] || {} names = commands.keys.to_set(&:to_s) names.include?("Foobara::Auth::RefreshLogin") && names.intersect?(["Foobara::Auth::Login", "Foobara::Auth::Logout"].to_set) end |
.requires_auth_generator_for(manifest) ⇒ Object
39 40 41 42 43 44 45 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 39 def requires_auth_generator_for(manifest) if requires_auth_command_generatable?(manifest) Auth::RequiresAuthGenerator else CommandGenerator end end |
Instance Method Details
#==(other) ⇒ Object
Files generator checks that the relevant_manifest is the same but this is faster
439 440 441 442 443 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 439 def ==(other) self.class == other.class && manifest_path == other.manifest_path && root_manifest == other.root_manifest end |
#attributes_to_ts_type(attributes, dependency_group:, association_depth: AssociationDepth::AMBIGUOUS, model_and_entity_free: false, is_output: false, parent: nil) ⇒ Object
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 356 def attributes_to_ts_type( attributes, dependency_group:, association_depth: AssociationDepth::AMBIGUOUS, model_and_entity_free: false, is_output: false, parent: nil ) # TODO: if we don't actually have attribute_declarations because we # are trying to express attributes of any type, then we want Record<string, any> # or something. if attributes.has_attribute_declarations? guts = attributes.attribute_declarations.map do |attribute_name, attribute_declaration| exists = attributes.required?(attribute_name) if !exists && parent&.model? exists = parent.guaranteed_to_exist?(attribute_name) end if !exists && is_output default = attributes.default_for(attribute_name) if default || default == false || (parent&.detached_entity? && attribute_name == parent.primary_key_name.to_sym) exists = true end end " #{attribute_name}#{"?" unless exists}: #{ ( attribute_declaration, dependency_group:, association_depth:, initial: false, model_and_entity_free:, is_output: ) }" end.join("\n") "{\n#{guts}\n}" else # TODO: test this path with an :attributes type (not extended from :attributes but # the built-in type itself directly) # simplecov:disable "Record<string, any>" # simplecov:enable end end |
#auto_dirty_queries? ⇒ Boolean
454 455 456 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 454 def auto_dirty_queries? RemoteGenerator.auto_dirty_queries? end |
#collision_winners ⇒ Object
167 168 169 170 171 172 173 174 175 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 167 def collision_winners # TODO: odd that root_manifest exists but isn't decorated. We need a decorated and undecorated method root_manifest = Manifest::RootManifest.new(self.root_manifest) [*dependencies].select do |dependency| root_manifest.contains?(dependency.domain_reference, :domain) && dependency.domain == domain end end |
#custom_type_to_ts_type_name(type) ⇒ Object
431 432 433 434 435 436 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 431 def custom_type_to_ts_type_name(type) type = type.to_type if type.is_a?(Manifest::TypeDeclaration) generator = TypeGenerator.new(type) dependency_group.non_colliding_type(generator) end |
#dependency_group ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 136 def dependency_group @dependency_group ||= begin generators = dependencies.map do |dependency| generator_for(dependency) end DependencyGroup.new(generators, deps_are_for: self, name: scoped_full_path.join("."), will_define:, winners: collision_winners) end end |
#dependency_roots ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 154 def dependency_roots return @dependency_roots if defined?(@dependency_roots) unless dependency_group # simplecov:disable raise "This generator was created without a " \ "dependency_group and therefore cannot call #{__method__}" # simplecov:enable end @dependency_roots = dependency_group.non_colliding_dependency_roots.sort_by(&:scoped_full_name) end |
#domain_name ⇒ Object
190 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 190 def domain_name = relevant_manifest.domain_name |
#foobara_type_to_ts_type(type_declaration, dependency_group: self.dependency_group, name: nil, association_depth: AssociationDepth::AMBIGUOUS, initial: true, model_and_entity_free: false, is_output: false, parent: nil) ⇒ Object
is_output means the value came from elsewhere and is fully formed. This is helpful for specifying what is expected to be present. If this is provided, then things like attribute properties that have defaults will be considered required and present.
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 233 def ( type_declaration, dependency_group: self.dependency_group, name: nil, association_depth: AssociationDepth::AMBIGUOUS, initial: true, model_and_entity_free: false, is_output: false, parent: nil ) if type_declaration.is_a?(Manifest::Error) error_generator = generator_for(type_declaration) return dependency_group.non_colliding_type(error_generator) end type_string = if type_declaration.is_a?(Manifest::Attributes) ts_type = attributes_to_ts_type( type_declaration, association_depth:, dependency_group:, model_and_entity_free:, is_output:, parent: ) if type_declaration.has_attribute_declarations? is_empty = type_declaration.attribute_declarations.empty? if name # TODO: test this code path or delete it # simplecov:disable return is_empty ? "undefined" : "interface #{name} #{ts_type}" # simplecov:enable else is_empty ? "undefined" : ts_type end else # TODO: test this path with an :attributes type (not extended from :attributes but # the built-in type itself directly) # simplecov:disable ts_type # simplecov:enable end elsif type_declaration.is_a?(Manifest::Array) # TODO: which association_depth do we pass here? ts_type = ( type_declaration.element_type, association_depth:, dependency_group:, initial: false, model_and_entity_free:, is_output: ) "#{ts_type}[]" else type_symbol = type_declaration.type case type_symbol when "string", "boolean" type_symbol when "number", "integer", "float" "number" # TODO: should apply relevant processors to make email a real email type instead of "string" when "symbol", "email" "string" when "duck" "any" when "datetime", "date" "Date" else if type_declaration.model? if model_and_entity_free model_type = type_declaration.to_type if type_declaration.detached_entity? ( model_type.primary_key_type, association_depth:, dependency_group:, initial:, model_and_entity_free:, is_output:, parent: model_type ) else attributes_to_ts_type( model_type.attributes_type, association_depth:, dependency_group:, model_and_entity_free:, is_output:, parent: model_type ) end else model_to_ts_model_name(type_declaration, association_depth:, initial:) end elsif type_declaration.custom? custom_type_to_ts_type_name(type_declaration) end end end if type_string unless type_declaration.reference? if type_declaration.one_of type_string = type_declaration.one_of.map(&:inspect).join(" | ") end if type_declaration.allows_nil? type_string = "#{type_string} | null" end end # TODO: Add description as a comment? name ? "type #{name} = #{type_string}" : type_string else # simplecov:disable raise "Not sure how to convert #{type_declaration} to a TS type" # simplecov:enable end end |
#hash ⇒ Object
445 446 447 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 445 def hash manifest_path.hash end |
#import_destructure ⇒ Object
200 201 202 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 200 def import_destructure "{ #{scoped_short_name} }" end |
#import_path ⇒ Object
192 193 194 195 196 197 198 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 192 def import_path if import_path_array.size == 1 "./#{import_path_array.first}" else import_path_array.join("/") end end |
#import_path_array ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 204 def import_path_array path = if target_path.last == "index.ts" target_path[0..-2] else target_path end path[-1] = path.last.gsub(/\.ts$/, "") path end |
#model_to_ts_model_name(model, association_depth: AssociationDepth::AMBIGUOUS, initial: true) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 406 def model_to_ts_model_name(model, association_depth: AssociationDepth::AMBIGUOUS, initial: true) model = model.to_type if model.is_a?(Manifest::TypeDeclaration) generator_class = case association_depth when AssociationDepth::AMBIGUOUS ModelGenerator when AssociationDepth::ATOM if !initial && model.detached_entity? UnloadedEntityGenerator else AtomModelGenerator end when AssociationDepth::AGGREGATE AggregateModelGenerator else # simplecov:disable raise "Bad association_depth: #{association_depth}" # simplecov:enable end generator = generator_class.new(model) dependency_group.non_colliding_type(generator) end |
#organization_name ⇒ Object
189 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 189 def organization_name = relevant_manifest.organization_name |
#parent ⇒ Object
130 131 132 133 134 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 130 def parent if relevant_manifest.parent generator_for(relevant_manifest.parent) end end |
#path_to_root ⇒ Object
449 450 451 452 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 449 def path_to_root path = super path.empty? ? "./" : path end |
#templates_dir ⇒ Object
126 127 128 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 126 def templates_dir "#{__dir__}/../../templates" end |
#ts_instance_full_name ⇒ Object
181 182 183 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 181 def ts_instance_full_name ts_instance_full_path.join(".") end |
#ts_instance_full_path ⇒ Object
185 186 187 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 185 def ts_instance_full_path [*parent&.scoped_full_path, *ts_instance_path] end |
#ts_instance_path ⇒ Object
177 178 179 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 177 def ts_instance_path scoped_path end |
#value_to_ts_value(value) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 216 def value_to_ts_value(value) case value when ::String, Numeric value.inspect when ::Symbol value.to_s.inspect else # simplecov:disable raise "Not sure how to represent #{value} in typescript. Maybe implement it." # simplecov:enable end end |
#will_define ⇒ Object
150 151 152 |
# File 'src/generators/typescript_from_manifest_base_generator.rb', line 150 def will_define nil end |