Module: SwiftGenerator

Defined in:
lib/swift_generator/specfile_parser.rb,
lib/swift_generator.rb,
lib/swift_generator/version.rb,
lib/swift_generator/code_generation/swift_file_template.rb,
lib/swift_generator/code_generation/swift_class_generation.rb

Overview

TODO: Use jamesbrooks/hash_validator to check for required and valid values

Defined Under Namespace

Classes: Generator, MutabilityType, SpecfileParser, SwiftBlockProperty, SwiftCategory, SwiftClass, SwiftDefinitionSet, SwiftEnum, SwiftEnumCase, SwiftFile, SwiftInitializer, SwiftMethod, SwiftMethodBase, SwiftNonPrimitive, SwiftPersistentProperty, SwiftProperty, SwiftProtocol, SwiftUnitTestClass

Constant Summary collapse

VERSION =
"0.3.2"

Class Method Summary collapse

Class Method Details

.write_category(c) ⇒ Object



456
457
458
459
460
461
462
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 456

def write_category( c )
Ribosome.dot("", binding)
Ribosome.dot("", binding)
Ribosome.dot("&{c.access_control_modifier}extension @{c.categorized_class_name} /*@{c.type_name || ' '}*/ {", binding)
  SwiftGenerator::write_class_body( c )
Ribosome.dot("}", binding)
end

.write_class(c) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 441

def write_class( c )
Ribosome.dot("", binding)
Ribosome.dot("", binding)
Ribosome.dot("&{c.access_control_modifier}class @{c.type_name}", binding)
    if c.inheritance_list.count > 0
Ribosome.add(" : @{c.inheritance_list.join( \", \" )}", binding)
    end
Ribosome.add("  {", binding)

  SwiftGenerator::write_class_body( c )
Ribosome.dot("}", binding)
end

.write_class_body(c) ⇒ Object



465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 465

def write_class_body( c )
    c.top_inner_comment_block().each do |line|
Ribosome.dot("    &{line}", binding)
    end

  # SwiftGenerator::write_property_declarations( c.transient_properties, "Transient" )
  # SwiftGenerator::write_property_declarations( c.persistent_properties, "Persistent" )
  SwiftGenerator::write_property_declarations( c.transient_properties )
  SwiftGenerator::write_property_declarations( c.persistent_properties )

  SwiftGenerator::write_methods( c.initializers )
  SwiftGenerator::write_methods( c.methods )
end

.write_element(element) ⇒ Object

Templated Output Methods



360
361
362
363
364
365
366
367
368
369
370
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 360

def write_element( element )
  if( element.kind_of? SwiftCategory )
    SwiftGenerator::write_category( element )
  elsif( element.kind_of? SwiftProtocol )
    SwiftGenerator::write_protocol( element )
  elsif( element.kind_of? SwiftEnum )
    SwiftGenerator::write_enum( element )
  elsif( element.kind_of? SwiftClass )      # Must be last as it is the superclass
    SwiftGenerator::write_class( element )
  end
end

.write_enum(e) ⇒ Object



413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 413

def write_enum( e )
Ribosome.dot("", binding)
Ribosome.dot("", binding)
Ribosome.dot("&{e.access_control_modifier}enum @{e.type_name}", binding)
    if e.inheritance_list.count > 0
Ribosome.add(" : @{e.inheritance_list.join( \", \" )}", binding)
    end
Ribosome.add("  {", binding)

  e.enum_cases.each_with_index do |enum_case, _|
    enum_case.declaration_lines().each do |declLine|
Ribosome.dot("    &{declLine}", binding)
    end
  end

  e.properties.each_with_index do |prop, i|
Ribosome.dot("", binding)
    prop.declaration_lines().each do |declLine|
Ribosome.dot("    &{declLine}", binding)
    end
  end

  SwiftGenerator::write_methods( e.methods )
Ribosome.dot(" }", binding)
end

.write_files_for_definition_set(definition_set) ⇒ Object



348
349
350
351
352
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 348

def write_files_for_definition_set( definition_set )
    for _, f in definition_set.output_files
        SwiftGenerator::writeGeneratedFile( f )
    end
end

.write_methods(methods, for_protocol = false) ⇒ Object



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 514

def write_methods( methods, for_protocol=false )
    methods.each_with_index do |m, i|
        overrideString = m.override ? 'override ' : ''
Ribosome.dot("", binding)
    if ! m.comment.nil?
Ribosome.dot("   &{m.comment}", binding)
    end
    args = m.argStr.nil? || m.argStr.empty? ? m.argStr : ' ' + m.argStr + ' '
    access_control_modifier_str = m.access_control_modifiers.length > 0 ? m.access_control_modifiers.join( ", " ) + " " : ""
Ribosome.dot("    &{overrideString}&{access_control_modifier_str}&{m.func_fragment + \" \" unless m.func_fragment.length == 0}@{m.name}(&{args})", binding)
    if ! (m.returns.nil? || m.returns.length == 0 )
Ribosome.add(" -> @{m.returns}", binding)
    end
    unless for_protocol
Ribosome.add(" {", binding)
        m.bodyLines.each do |line|
Ribosome.dot("           &{line}", binding)
        end
Ribosome.dot("    }", binding)
    end
  end
end

.write_property_declarations(properties, property_type_label = nil) ⇒ Object



499
500
501
502
503
504
505
506
507
508
509
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 499

def write_property_declarations( properties, property_type_label=nil )
  properties.each_with_index do |prop, i|
    if i == 0 && ! property_type_label.nil?
Ribosome.dot("", binding)
Ribosome.dot("    // MARK: @{property_type_label} Properties", binding)
    end
    prop.declaration_lines().each do |declLine|
Ribosome.dot("    &{declLine}", binding)
    end
  end
end

.write_protocol(p) ⇒ Object



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 480

def write_protocol( p )
Ribosome.dot("", binding)
Ribosome.dot("", binding)
Ribosome.dot("&{p.access_control_modifier}protocol @{p.type_name}", binding)
    if p.inheritance_list.count > 0
Ribosome.add(" : @{p.inheritance_list.join( \", \" )}", binding)
    end
Ribosome.add("  {", binding)
  # SwiftGenerator::write_property_declarations( c.transient_properties, "Transient" )
  # SwiftGenerator::write_property_declarations( c.persistent_properties, "Persistent" )
  SwiftGenerator::write_property_declarations( p.transient_properties )
  SwiftGenerator::write_property_declarations( p.persistent_properties )

  SwiftGenerator::write_methods( p.initializers, for_protocol=true )
  SwiftGenerator::write_methods( p.methods, for_protocol=true )
Ribosome.dot("}", binding)
end

.writeGeneratedFile(f) ⇒ Object



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
405
406
407
408
# File 'lib/swift_generator/code_generation/swift_file_template.rb', line 374

def writeGeneratedFile( f )
    return if ( f.is_user_file && File.exist?( f.file_path ))

    Pathname.new( f.file_path ).parent().mkpath()

Ribosome.output( f.file_path )

Ribosome.dot("//", binding)
Ribosome.dot("//  @{ Pathname.new( f.file_name ).basename }", binding)
Ribosome.dot("//  @{f.company_name}", binding)
if $definition_file
Ribosome.dot("//", binding)
Ribosome.dot("//  Generated from @{$definition_file} on @{Time.now.strftime(\"%d/%m/%Y %H:%M\")}", binding)
end
if f.include_editing_warnings
    if !f.is_user_file
Ribosome.dot("//", binding)
Ribosome.dot("//  WARNING: This entire file is generated. DO NOT EDIT.", binding)
    else
Ribosome.dot("//", binding)
Ribosome.dot("//  This is a user-editable generated file. Delete or rename this file to have a new empty file generated", binding)
    end
end
Ribosome.dot("//", binding)
Ribosome.dot("//  Copyright (c) @{Time.now.strftime(\"\")} @{f.company_name}, Inc. All rights reserved.", binding)
Ribosome.dot("//", binding)

    for import_statement in f.import_statements.sort
Ribosome.dot("&{import_statement}", binding)
    end

  for element in f.elements
    SwiftGenerator::write_element(element)
  end
end