Class: KZ::KZPodTarget

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-kz/helpers/kz_pod_target.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(native_pod_target) ⇒ KZPodTarget

Returns a new instance of KZPodTarget.



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
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 51

def initialize(native_pod_target)
  @native_pod_target = native_pod_target
  @name = native_pod_target.name
  @root_name = native_pod_target.root_spec.name
  @version = native_pod_target.version
  @dependent_target_info = {}
  @repair_dependent_target_info = {}
  @repair_import = []
  @is_dev_pod = false
  @config_pod_mode = :kz_pod_origin_mode

  @platform_name = native_pod_target.platform.symbolic_name
  @product_name = native_pod_target.origin_product_name
  @product_basename = native_pod_target.origin_product_basename
  @need_repair_module_import = false
  @use_local_private_headers_path = false
  @force_load = false
  @use_modulemap = true
  @disable_to_simulator_frameworks = []

  native_pod_target.file_accessors.each do |file_accessor|
    file_accessor.kz_pod_target = self
  end

  native_pod_target.weakRef_kz_pod_target = WeakRef.new(self)
end

Instance Attribute Details

#config_pod_modeObject

重些config_pod_mode的get方法,force_config_pod_mode优先级最高



22
23
24
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 22

def config_pod_mode
  @config_pod_mode
end

#dependent_target_infoObject

当前target所有直接或间接依赖的target信息,是一个hash,key为target名字,value为KZPodTarget对象



16
17
18
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 16

def dependent_target_info
  @dependent_target_info
end

#disable_to_simulator_frameworksObject

Returns the value of attribute disable_to_simulator_frameworks.



49
50
51
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 49

def disable_to_simulator_frameworks
  @disable_to_simulator_frameworks
end

#force_config_pod_modeObject

pod kz install/update 中指定的pod模式,优先级最大



24
25
26
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 24

def force_config_pod_mode
  @force_config_pod_mode
end

#force_loadObject

Returns the value of attribute force_load.



44
45
46
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 44

def force_load
  @force_load
end

#force_load_infoObject

Returns the value of attribute force_load_info.



45
46
47
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 45

def force_load_info
  @force_load_info
end

#is_dev_podObject

Returns the value of attribute is_dev_pod.



20
21
22
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 20

def is_dev_pod
  @is_dev_pod
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 11

def name
  @name
end

#native_pod_targetObject (readonly)

Returns the value of attribute native_pod_target.



10
11
12
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 10

def native_pod_target
  @native_pod_target
end

#need_repair_module_importObject

当手动修改PRODUCT_NAME之后,#import<xx/xxx.h>需要修复为原使用方式,使用hmap进行映射



40
41
42
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 40

def need_repair_module_import
  @need_repair_module_import
end

#platform_nameObject

Returns the value of attribute platform_name.



30
31
32
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 30

def platform_name
  @platform_name
end

#private_header_search_pathObject

用于临时保存hamp过程的配置



27
28
29
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 27

def private_header_search_path
  @private_header_search_path
end

#product_basenameObject

对应xcode配置PRODUCT_NAME,当多个target的PRODUCT_NAME相同时,需要手动修改,防止打包报错



34
35
36
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 34

def product_basename
  @product_basename
end

#product_nameObject

target编译最终的产物名称



32
33
34
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 32

def product_name
  @product_name
end

#repair_dependent_target_infoObject

所有通过kz_pod,:dependency手动配置的依赖target



18
19
20
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 18

def repair_dependent_target_info
  @repair_dependent_target_info
end

#repair_header_search_pathObject

Returns the value of attribute repair_header_search_path.



28
29
30
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 28

def repair_header_search_path
  @repair_header_search_path
end

#repair_importObject (readonly)

Returns the value of attribute repair_import.



19
20
21
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 19

def repair_import
  @repair_import
end

#repair_modulemap_pathObject

当手动修改PRODUCT_NAME之后,产物名称会改变,在framework模式下对于OC组件需要使用modulemap文件配置寻找路径,对于Swift组件需要配置单独的include path



36
37
38
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 36

def repair_modulemap_path
  @repair_modulemap_path
end

#repair_swift_include_pathObject

Returns the value of attribute repair_swift_include_path.



37
38
39
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 37

def repair_swift_include_path
  @repair_swift_include_path
end

#root_nameObject (readonly)

部分pod配置了subspec,此时获取的name为cocoapods处理之后的名字,可以通过root_name拿到pod本名



13
14
15
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 13

def root_name
  @root_name
end

#use_local_private_headers_pathObject

Returns the value of attribute use_local_private_headers_path.



42
43
44
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 42

def use_local_private_headers_path
  @use_local_private_headers_path
end

#use_modulemapObject

Returns the value of attribute use_modulemap.



47
48
49
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 47

def use_modulemap
  @use_modulemap
end

#versionObject (readonly)

Returns the value of attribute version.



14
15
16
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 14

def version
  @version
end

Instance Method Details

#add_kz_pod_config(pod_config) ⇒ Object

配合kz_pod对target进行配置



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
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 127

def add_kz_pod_config(pod_config)
  return unless pod_config

  repair_dependency = pod_config[:dependency]
  if repair_dependency
    repair_dependency.each { |repair_dependent_pod_target_name|
      kz_repair_pod_target = yield(repair_dependent_pod_target_name)
      if kz_repair_pod_target
        native_dependent_targets_by_config = self.native_pod_target.dependent_targets_by_config
        native_dependent_targets_by_config.each do |_, dependent_targets|
          dependent_targets << kz_repair_pod_target.native_pod_target
        end
        self.native_pod_target.dependent_targets_by_config = native_dependent_targets_by_config

        @dependent_target_info[repair_dependent_pod_target_name] = kz_repair_pod_target
        @repair_dependent_target_info[repair_dependent_pod_target_name] = kz_repair_pod_target
      end
    }
  end

  repair_import = pod_config[:repair_import]
  if repair_import.is_a?(TrueClass)
    @repair_import = self.recursive_dependent_targets
  elsif repair_import.is_a?(Array)
    repair_import.each do |repair_import_target_name|
      kz_repair_pod_target = yield(repair_import_target_name)
      @repair_import << kz_repair_pod_target if kz_repair_pod_target
    end
  end

  force_load = pod_config[:force_load]
  if force_load.is_a?(TrueClass)
    @force_load = true
  end

  use_modulemap = pod_config[:use_modulemap]
  if use_modulemap.is_a?(FalseClass)
    @use_modulemap = false
  end

end

#all_headersObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 82

def all_headers
  @all_headers ||= begin
                     all_headers = []
                     native_pod_target.file_accessors.each do |file_accessor|
                       next if file_accessor.spec.test_specification

                       all_headers.concat(file_accessor.kz_headers)
                     end
                     all_headers
                   end
end

#all_need_repair_importObject

在当前target的所有依赖中,过滤有有修补的target



170
171
172
173
174
175
176
177
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 170

def all_need_repair_import
  all_need_repair_import = []

  self.recursive_dependent_targets.each do |dependent_target|
    all_need_repair_import << dependent_target if dependent_target.repair_import.count > 0
  end
  all_need_repair_import.uniq
end

#all_repair_dependent_target_infoObject

参考@repair_dependent_target_info,获取所有当前target直接或间接配置的依赖target 用于配置FRAMEWORK_SEARCH_PATHS内容



225
226
227
228
229
230
231
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 225

def all_repair_dependent_target_info
  all_repair_dependent_target_info = @repair_dependent_target_info
  @dependent_target_info.values.each do |dependent_target|
    all_repair_dependent_target_info.merge!(dependent_target.all_repair_dependent_target_info)
  end
  all_repair_dependent_target_info
end

#all_repair_header_search_pathsObject

获取当前target所有需要修补target的hmap



213
214
215
216
217
218
219
220
221
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 213

def all_repair_header_search_paths
  all_repair_header_search_paths = []
  all_repair_header_search_paths << KZ.deal_path_for_xcconfig(@repair_header_search_path, true) if @repair_header_search_path
  self.all_need_repair_import.each do |need_repair_target|
    sub_repair_header_search_path = need_repair_target.repair_header_search_path
    all_repair_header_search_paths << KZ.deal_path_for_xcconfig(sub_repair_header_search_path, true) if sub_repair_header_search_path
  end
  all_repair_header_search_paths.uniq
end

#all_repair_modulemap_pathsObject

获取所有用于修复的modulemap路径,原因参考@repair_modulemap_path



259
260
261
262
263
264
265
266
267
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 259

def all_repair_modulemap_paths
  all_repair_modulemap_paths = []
  all_repair_modulemap_paths << repair_modulemap_path if self.repair_modulemap_path
  recursive_dependent_targets.each do |recursive_dependent_target|
    all_repair_modulemap_paths.concat(recursive_dependent_target.all_repair_modulemap_paths)
  end
  all_repair_modulemap_paths.uniq! if all_repair_modulemap_paths.count > 0
  all_repair_modulemap_paths
end

#all_repair_swift_include_pathsObject



269
270
271
272
273
274
275
276
277
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 269

def all_repair_swift_include_paths
  all_repair_swift_include_paths = []
  all_repair_swift_include_paths << repair_swift_include_path if self.repair_swift_include_path
  recursive_dependent_targets.each do |recursive_dependent_target|
    all_repair_swift_include_paths.concat(recursive_dependent_target.all_repair_swift_include_paths)
  end
  all_repair_swift_include_paths.uniq! if all_repair_swift_include_paths.count > 0
  all_repair_swift_include_paths
end

#configuration_build_dir(for_froce_load = false) ⇒ Object



329
330
331
332
333
334
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 329

def configuration_build_dir(for_froce_load = false)
  if for_froce_load
    return @native_pod_target.configuration_build_dir + "/.force_load"
  end
  @native_pod_target.configuration_build_dir
end

#current_should_build?Boolean

当前pod是否存在可编译的文件,配置framework后当前pod就不存在可编译文件

Returns:

  • (Boolean)


284
285
286
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 284

def current_should_build?
  @native_pod_target.should_build?
end

#current_uses_swift?Boolean

Returns:

  • (Boolean)


299
300
301
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 299

def current_uses_swift?
  @native_pod_target.uses_swift?
end

#header_search_paths(custom_paths) ⇒ Object

直接用于配置HEADER_SEARCH_PATHS



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 193

def header_search_paths(custom_paths)
  return '' unless current_should_build?

  header_search_paths = ''
  header_search_paths = KZ.deal_path_for_xcconfig(@private_header_search_path, true) if @private_header_search_path
  repair_header_search_paths = self.all_repair_header_search_paths.join(' ')
  if repair_header_search_paths.length > 0
    header_search_paths += (' ' + repair_header_search_paths)
  end
  if @use_local_private_headers_path
    header_search_paths += (' ' + KZ.deal_path_for_xcconfig(local_private_headers_path, true))
  end
  header_search_paths += (' ' + KZ.deal_path_for_xcconfig(self.native_pod_target.sandbox.public_headers.root, true))
  custom_paths.each do |custom_path|
    header_search_paths += (' ' + custom_path)
  end if custom_paths
  header_search_paths
end

#kz_module_nameObject

获取target中的module name,默认为product_module_name与target.name也相同。 部分target导入的framework名字与modulemap中的名字不对应,需要特殊处理 eg.AMapSearch,product_module_name为AMapSearch,modulemap中的名字为AMapSearchKit



244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 244

def kz_module_name
  return @native_pod_target.product_module_name  unless all_headers.count > 0

  one_header_path = all_headers.first.to_s
  if one_header_path.include?('.framework')
    one_header_path.split('/').each do |name|
      if name.include?('.framework')
        return name.split('.').first
      end
    end
  end
  return @native_pod_target.product_module_name
end

#local_private_headers_pathObject



321
322
323
324
325
326
327
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 321

def local_private_headers_path
  @local_private_headers_path ||= begin
                                    path = KZ_POD_CONFIG_POD_TARGETS + @name + "Headers"
                                    FileUtils.mkdir_p(path) unless File.exist?(path)
                                    path
                                  end
end

#origin_should_build?Boolean

原pod是否存在可编译的文件

Returns:

  • (Boolean)


289
290
291
292
293
294
295
296
297
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 289

def origin_should_build?
  return @origin_should_build if defined? @origin_should_build
  accessors = @native_pod_target.file_accessors.select { |fa| fa.spec.library_specification? }
  all_origin_compile_files = []
  accessors.each do |accessor|
    all_origin_compile_files.concat(accessor.origin_source_files - accessor.kz_headers)
  end
  @origin_should_build = !all_origin_compile_files.empty?
end

#origin_uses_swift?Boolean

Returns:

  • (Boolean)


303
304
305
306
307
308
309
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 303

def origin_uses_swift?
  return @uses_swift if defined? @uses_swift
  @uses_swift = (!origin_should_build? && !@native_pod_target.spec_swift_versions.empty?) ||
    @native_pod_target.file_accessors.select { |a| a.spec.library_specification? }.any? do |file_accessor|
      uses_swift_for_spec?(file_accessor.spec)
    end
end

#pod_config_cache_path(concat_version, vendored_framework = false) ⇒ Object

获取target对应的配置根目录,部分文件需要依赖版本进行存储



234
235
236
237
238
239
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 234

def pod_config_cache_path(concat_version, vendored_framework = false)
  kz_target_config_folder = KZ_POD_CONFIG_POD_TARGETS + @name
  kz_target_config_folder += "vendored_framework" if vendored_framework
  kz_target_config_folder += @version if concat_version
  kz_target_config_folder
end

#prefix_header_pathObject



279
280
281
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 279

def prefix_header_path
  @native_pod_target.prefix_header_path
end

#public_headersObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 94

def public_headers
  @public_headers ||= begin
                        public_headers = []
                        native_pod_target.file_accessors.each do |file_accessor|
                          next if file_accessor.spec.test_specification

                          public_headers.concat(file_accessor.kz_public_headers)
                        end
                        public_headers
                      end
end

#recursive_dependent_targetsObject

获取当前target所有依赖,包括手动补充的



180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 180

def recursive_dependent_targets
  recursive_dependent_targets = []
  @dependent_target_info.values.each do |dependent_target|
    recursive_dependent_targets << dependent_target
    recursive_dependent_targets.concat(dependent_target.recursive_dependent_targets)
  end
  if recursive_dependent_targets.count > 0
    recursive_dependent_targets.uniq
  end
  recursive_dependent_targets
end

#uses_swiftObject



78
79
80
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 78

def uses_swift
  native_pod_target.uses_swift?
end

#uses_swift_for_spec?(spec) ⇒ Boolean

Returns:

  • (Boolean)


311
312
313
314
315
316
317
318
319
# File 'lib/cocoapods-kz/helpers/kz_pod_target.rb', line 311

def uses_swift_for_spec?(spec)
  @uses_swift_for_spec_cache ||= {}
  return @uses_swift_for_spec_cache[spec.name] if @uses_swift_for_spec_cache.key?(spec.name)
  @uses_swift_for_spec_cache[spec.name] = begin
                                            file_accessor = @native_pod_target.file_accessors.find { |fa| fa.spec == spec }
                                            raise "[Bug] Unable to find file accessor for spec `#{spec.inspect}` in pod target `#{label}`" unless file_accessor
                                            file_accessor.origin_source_files.any? { |sf| sf.extname == '.swift' }
                                          end
end