Class: SwiftGenerator::SpecfileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/swift_generator/specfile_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specfile_path) ⇒ SpecfileParser

Returns a new instance of SpecfileParser.



16
17
18
# File 'lib/swift_generator/specfile_parser.rb', line 16

def initialize( specfile_path )
  @specfile_path = specfile_path
end

Instance Attribute Details

#specfile_pathObject

Returns the value of attribute specfile_path.



14
15
16
# File 'lib/swift_generator/specfile_parser.rb', line 14

def specfile_path
  @specfile_path
end

Instance Method Details

#add_property(swift_class, prop_spec) ⇒ Object



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
# File 'lib/swift_generator/specfile_parser.rb', line 77

def add_property( swift_class, prop_spec )
    property_name         = prop_spec[ "name" ]
    type_symbol           = prop_spec[ "type" ].to_sym
    is_persistent         = prop_spec[ "isPersistent" ]
    mutability            = prop_spec[ "mutability" ]   || "let"
    initialization_value  = prop_spec[ "initializationValue" ]
    collection_type       = prop_spec[ "collectionType" ]
    required              = prop_spec[ "required" ]     || true
    rest_omit             = prop_spec[ "restOmit" ]
    json_key              = prop_spec[ "jsonKey" ]      # only used by SwiftPersistentProperty

    mutability = mutability.to_sym

    if( is_persistent )
      SwiftPersistentProperty.new( swift_class,
                                   property_name,
                                   type_symbol,
                                   mutability,
                                   initialization_value,
                                   collection_type:collection_type,
                                   # required = required, ???
                                   json_key:json_key,
                                   rest_omit:rest_omit )
    else
      SwiftProperty.new( swift_class,
                         property_name,
                         property_type:type_symbol,
                         mutability:mutability,
                         initialization_value:initialization_value,
                         collection_type:collection_type,
                         required:required,
                         rest_omit:rest_omit )
    end
end

#process_specfileObject



20
21
22
23
24
25
26
# File 'lib/swift_generator/specfile_parser.rb', line 20

def process_specfile( )
  swift_definition_set = read_specfile
  swift_definition_set.run_generation_sequence

  SwiftGenerator::write_files_for_definition_set( swift_definition_set )

end

#read_specfileObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
# File 'lib/swift_generator/specfile_parser.rb', line 28

def read_specfile( )
  file_data = @specfile_path.read
  spec_hash = JSON.parse(file_data)

  generated_source_root = spec_hash[ "sourceRoot" ]
  generated_user_source_root = spec_hash[ "userClassSourceRoot" ]
  generated_test_root = spec_hash[ "testRoot" ]

  definition_set = SwiftDefinitionSet.new( generated_root:generated_source_root,
                                           generated_user_root:generated_user_source_root,
                                           generated_test_root:generated_test_root)

  characteristics_by_name = spec_hash[ "characteristicSets" ] || {}
  characteristics_by_name.each do |name, characteristics|
    #TODO validate
  end

  classes = spec_hash[ "classes" ]
  classes.each do | class_spec |
    specified_type_name     = class_spec[ "typeName" ]
    inheritance_list        = class_spec[ "inheritanceList" ]
    file_name               = class_spec[ "fileName" ]
    characteristics_name    = class_spec[ "characteristics" ]
    is_test_element         = class_spec[ "isTestElement" ] || false
    is_user_editable        = class_spec[ "isUserEditable" ] || false

    characteristics = characteristics_by_name[ characteristics_name ]
    characteristics ||= $default_swift_class_characteristics

    # def initialize (definition_set, specified_type_name, inheritance_list=[], file_name: nil,
    #                 characteristics:default_swift_class_characteristics, is_test_element: false, is_user_editable: false)
    swift_class = SwiftClass.new(
        definition_set,
        specified_type_name,
        inheritance_list,
        file_name: file_name,
        characteristics: characteristics,
        is_test_element: is_test_element,
        is_user_editable: is_user_editable )

    properties = class_spec[ "properties" ] || []
    properties.each do | prop_spec |
      add_property( swift_class, prop_spec )
    end
  end

  return definition_set
end