Class: KZ::KZSwiftClass

Inherits:
KZSwiftContext show all
Defined in:
lib/cocoapods-kz/helpers/repair_dynamic_swift.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_contexts) ⇒ KZSwiftClass

Returns a new instance of KZSwiftClass.



88
89
90
91
92
93
94
95
96
# File 'lib/cocoapods-kz/helpers/repair_dynamic_swift.rb', line 88

def initialize(type_contexts)
  @system_nsobject_class = [
    "NSObject"
  ]

  super(type_contexts) do |line|
    KZSwiftClass.check_line_have_class_tag(line)
  end
end

Class Method Details

.check_line_have_class_tag(line) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/cocoapods-kz/helpers/repair_dynamic_swift.rb', line 98

def self.check_line_have_class_tag(line)
  if line =~ /^(?:.*\s)?class\s/ && !line.include?("func") && !line.include?("var") && !line.include?("let")
    return true
  else
    return false
  end
end

Instance Method Details

#get_oc_feature_contextsObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/cocoapods-kz/helpers/repair_dynamic_swift.rb', line 106

def get_oc_feature_contexts
  new_class_context = []

  # 获取类、父类、协议信息
  class_name = ""
  superClass_and_protocol = []

  class_line = @type_str.gsub(/\s+/, "")
  class_line = class_line.split("class")[1]
  if class_line.include?(":")
    colon_infos = class_line.split(":")
    class_name = colon_infos[0]

    colon_right = colon_infos[1]
    colon_right = colon_right.sub("{", "")
    superClass_and_protocol = colon_right.split(",")
  else
    class_name = class_line.sub("{", "")
  end

  return @type_contexts if class_name == ""

  # 获取oc特性标识
  have_super_class = false
  if superClass_and_protocol.length > 0
    have_super_class = !superClass_and_protocol[0].end_with?("Protocol")
  end

  have_objc_class = false
  have_objc_members = false
  for ornament in @type_ornament
    if ornament.start_with?("@objc ") || ornament.start_with?("@objc(") || ornament == "@objc" || ornament == "@objc\n"
      have_objc_class = true
    end
    have_objc_members = true if ornament.start_with?("@objcMembers")
  end

  # 添加class ornament
  new_class_context.concat(@type_header)

  if !have_objc_members
    new_class_context.insert(0, "@objcMembers\n")
  end

  if !have_objc_class && !have_super_class
    new_class_context.insert(0, "@objc\n")
  end

  # 添加class header
  if !have_objc_class && !have_super_class
    if superClass_and_protocol.length > 0
      if !@system_nsobject_class.include?(superClass_and_protocol[0])
        @type_str = @type_str.sub(":", ": NSObject,")
      end
    else
      @type_str = @type_str.sub("{", ": NSObject {")
    end
  end
  new_class_context << @type_str

  # 添加class body
  new_swift_types = KZSwiftFileAnalyse.analyse_swift_file(@type_body, !have_super_class)
  new_swift_types.each do |swift_type|
    new_class_context.concat(swift_type.get_oc_feature_contexts())
  end
  new_class_context << "}\n"

  new_class_context
end