Class: KZ::KZGenerator

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

Instance Method Summary collapse

Constructor Details

#initialize(main_project) ⇒ KZGenerator

Returns a new instance of KZGenerator.



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

def initialize(main_project)
  @all_kz_pod_targets = KZGlobalHelper.instance.kz_analyzer.all_kz_pod_targets
  @main_project = main_project
end

Instance Method Details

#add_flexlib_xml_build_rules(project, native_target) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cocoapods-kz/helpers/kz_generator_hmap.rb', line 25

def add_flexlib_xml_build_rules(project, native_target)
  return unless KZ::KZGlobalHelper.instance.have_flexLib_pod_target

  native_target.build_configurations.each do |config|
    config.build_settings["KZ_XML_FLEX_COMPILER"] = KZ.deal_path_for_xcconfig(FLEX_COMPLIER_PATH, false)
    config.build_settings["KZ_XML_FLEX_DIR"] = "${TARGET_TEMP_DIR}/XmlFlexs"
    config.build_settings["KZ_XML_FLEX_BUILD_DIR"] = "${TARGET_BUILD_DIR}/${PRODUCT_NAME}.bundle"
  end

  xml_rule = new_build_rule(project, native_target, '[KZ] Custom Xml Build')
  xml_rule.run_once_per_architecture = '0'
  xml_rule.file_type = 'text.xml'
  xml_rule.input_files = Array['${INPUT_FILE_PATH}']
  xml_rule.output_files = Array['${KZ_XML_FLEX_DIR}/${INPUT_FILE_BASE}.flex']
  xml_rule.script = KZ.deal_path_for_xcconfig(KZ_XML_BUILD_PATH, true)
end

#add_force_load_exe_path_build_phase(native_target, input_path, output_path) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/cocoapods-kz/helpers/kz_generator_hmap.rb', line 42

def add_force_load_exe_path_build_phase(native_target, input_path, output_path)
  build_phase = native_target.new_shell_script_build_phase('[KZ] Froce Load File Output Path')
  build_phase.show_env_vars_in_log = '0'
  build_phase.shell_path = '/bin/zsh'
  build_phase.input_paths = Array[input_path]
  build_phase.output_paths = Array[output_path]
  build_phase.shell_script = KZ.deal_path_for_xcconfig(KZ_FIX_FORCE_LOAD_EXE, true)
end

#add_framework_generator_build_phase(native_target, input_paths) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/cocoapods-kz/helpers/kz_generator_hmap.rb', line 14

def add_framework_generator_build_phase(native_target, input_paths)
  return if KZ::KZGlobalHelper.instance.disable_generate_framework

  build_phase = native_target.new_shell_script_build_phase('[KZ] Generate Framework')
  build_phase.show_env_vars_in_log = '0'
  build_phase.shell_path = '/bin/zsh'
  build_phase.input_paths = Array[input_paths]
  build_phase.output_paths = Array['${KZ_FRAMEWORK_CACHE_PATH}/${PRODUCT_NAME}.xcframework']
  build_phase.shell_script = KZ.deal_path_for_xcconfig(KZ_GENERATE_FRAMEWORK_PATH, true)
end

#add_improve_yaml_build_phase(project, native_target, input_path, output_path) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/cocoapods-kz/helpers/kz_generator_hmap.rb', line 51

def add_improve_yaml_build_phase(project, native_target, input_path, output_path)
  build_phase = new_shell_script_build_phase(project, native_target, '[KZ] Improve Custom Yaml', true)
  build_phase.show_env_vars_in_log = '0'
  build_phase.shell_path = '/bin/zsh'
  build_phase.input_paths = Array[input_path]
  build_phase.output_paths = Array[output_path]
  build_phase.shell_script = KZ.deal_path_for_xcconfig(KZ_IMPROVE_CUSTOM_YAML_PATH, true)
end

#analyze_on_demand_resources_config_plist(plist_hash, kz_pod_target, current_all_tags) ⇒ Object



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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/cocoapods-kz/helpers/kz_generator_on_demand_resources.rb', line 52

def analyze_on_demand_resources_config_plist(plist_hash, kz_pod_target, current_all_tags)
  resources_path = kz_pod_target.pod_config_cache_path(false, false, true)
  FileUtils.rm_r(resources_path) if File.exist?(resources_path)
  FileUtils.mkdir_p(resources_path)
  kz_pod_target.on_demand_resources_config_path = resources_path.to_s

  # 生成AssetPackOutputSpecifications.plist,用于Images.xcassets编译
  asset_pack_output_specifications_resources = []

  # 用于生成OnDemandResources.plist文件,放到main bundle中标记按需加载资源
  on_demand_resources_plist = {}
  bundle_resource_request_tags = {}
  on_demand_resources_plist["NSBundleResourceRequestTags"] = bundle_resource_request_tags
  bundle_resource_request_asset_packs = {}
  on_demand_resources_plist["NSBundleResourceRequestAssetPacks"] = bundle_resource_request_asset_packs

  # 用于odr资源编译时读取所需的参数
  odr_build_info = {}

  # 从配置plist中读取'Initial Install Tags'资源
  initial_plist_hash = plist_hash["Initial Install Tags"]
  on_demand_resources_from_plist(initial_plist_hash, kz_pod_target, 1, resources_path, odr_build_info, current_all_tags) do |tag, bundle_id, bundle_path, normal_file_names, have_xcassets_paths|
    # 如有配置中有.xcassets资源,需要先标记,等待编译时单独再单独编译
    if have_xcassets_paths
      resource_item = {}
      resource_item["bundle-id"] = bundle_id
      resource_item["bundle-path"] = bundle_path
      resource_item["tags"] = [tag]
      asset_pack_output_specifications_resources << resource_item
    end

    bundle_resource_request_tags[tag] = { "NSAssetPacks" => [bundle_id] }
    bundle_resource_request_asset_packs[bundle_id] = normal_file_names if normal_file_names.size > 0

    KZLog.log("已经完成'#{kz_pod_target.name}'中'#{tag}'ODR配置,资源id为:'#{bundle_id}'")
  end

  # Prefetched Tag Order
  prefetched_plist_hash = plist_hash["Prefetched Tag Order"]
  on_demand_resources_from_plist(prefetched_plist_hash, kz_pod_target, 0.5, resources_path, odr_build_info, current_all_tags) do |tag, bundle_id, bunlde_path, normal_file_names, have_xcassets_paths|
    if have_xcassets_paths
      resource_item = {}
      resource_item["bundle-id"] = bundle_id
      resource_item["bundle-path"] = bunlde_path
      resource_item["tags"] = [tag]
      asset_pack_output_specifications_resources << resource_item
    end

    bundle_resource_request_tags[tag] = { "NSAssetPacks" => [bundle_id] }
    bundle_resource_request_asset_packs[bundle_id] = normal_file_names if normal_file_names.size > 0

    KZLog.log("已经完成'#{kz_pod_target.name}'中'#{tag}'ODR配置,资源id为:'#{bundle_id}'")
  end

  # Download Only On Demand
  download_plist_hash = plist_hash["Download Only On Demand"]
  on_demand_resources_from_plist(download_plist_hash, kz_pod_target, 0, resources_path, odr_build_info, current_all_tags) do |tag, bundle_id, bunlde_path, normal_file_names, have_xcassets_paths|
    if have_xcassets_paths
      resource_item = {}
      resource_item["bundle-id"] = bundle_id
      resource_item["bundle-path"] = bunlde_path
      resource_item["tags"] = [tag]
      asset_pack_output_specifications_resources << resource_item
    end

    bundle_resource_request_tags[tag] = { "NSAssetPacks" => [bundle_id] }
    bundle_resource_request_asset_packs[bundle_id] = normal_file_names if normal_file_names.size > 0

    KZLog.log("已经完成'#{kz_pod_target.name}'中'#{tag}'ODR配置,资源id为:'#{bundle_id}'")
  end

  if asset_pack_output_specifications_resources.size > 0
    write_to_path(asset_pack_output_specifications_resources, resources_path + "AssetPackOutputSpecifications.plist")
  end

  if bundle_resource_request_tags.size > 0 || bundle_resource_request_asset_packs.size > 0
    write_to_path(on_demand_resources_plist, resources_path + "OnDemandResources.plist")
  end

  unless odr_build_info.empty?
    all_xcassets_dest_plist_path = kz_pod_target.pod_config_cache_path(false, false, true) + "odr_build_info.plist"
    write_to_path(odr_build_info, all_xcassets_dest_plist_path)
  end
end

#clean_hmap_cache(kz_pod_target) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/cocoapods-kz/helpers/kz_generator_hmap.rb', line 196

def clean_hmap_cache(kz_pod_target)
  hmap_cache_path = kz_pod_target.pod_config_cache_path(false)

  Dir.foreach(hmap_cache_path) do |file_name|
    next if file_name == '.' || file_name == '..' || file_name == '.DS_Store'

    if file_name.end_with?(".hmap", ".json", "yaml")
      FileUtils.rm(hmap_cache_path + file_name) if File.exist?(hmap_cache_path + file_name)
    end
  end if File.exist?(hmap_cache_path)
end

#create_bundle_info_plist(tag, priority, normal_file_paths, resource_path) {|demand_bundle_id, demand_bundle_folder_name| ... } ⇒ Object

Yields:

  • (demand_bundle_id, demand_bundle_folder_name)


218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/cocoapods-kz/helpers/kz_generator_on_demand_resources.rb', line 218

def create_bundle_info_plist(tag, priority, normal_file_paths, resource_path)
  # project bundle identifier
  project_bundle_identifier = KZGlobalHelper.instance.on_demand_resources_info.bundle_id
  tag_md5 = md5_sign(tag)
  # bundle folder
  demand_bundle_folder_name = "#{project_bundle_identifier}.#{tag}-#{tag_md5}.assetpack"
  demand_bundle_folder = resource_path + "OnDemandResources/#{demand_bundle_folder_name}"
  # create bundle folder
  FileUtils.mkdir_p(demand_bundle_folder) unless FileTest::exist?(demand_bundle_folder)
  # copy file
  normal_file_paths.each do |file|
    if File.directory?(file)
      FileUtils.cp_r(file, demand_bundle_folder)
    else
      FileUtils.cp(file, demand_bundle_folder)
    end
  end

  bundle_info_plist = {}
  bundle_info_plist["Priority"] = priority if priority > 0
  bundle_info_plist["Tags"] = [tag]
  demand_bundle_id = "#{project_bundle_identifier}.asset-pack-#{tag_md5}"
  bundle_info_plist["CFBundleIdentifier"] = demand_bundle_id
  Xcodeproj::Plist.write_to_path(bundle_info_plist, demand_bundle_folder + "Info.plist")

  yield(demand_bundle_id, demand_bundle_folder_name)
end

#create_hampObject



97
98
99
100
101
102
103
104
105
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
175
176
177
178
179
# File 'lib/cocoapods-kz/helpers/kz_generator_hmap.rb', line 97

def create_hamp
  # 创建壳工程hmap
  main_sources_path = @main_project.project_dir + @main_project.root_object.display_name
  private_hmap_hash = {}
  traverse_folder(main_sources_path) do |header_path|
    header_pathname = Pathname.new(header_path)
    header_pathname_basename = header_pathname.basename.to_s

    header_hmap_value = {}
    header_hmap_value['suffix'] = header_pathname_basename
    header_hmap_value['prefix'] = header_pathname.dirname.to_s + '/'

    private_hmap_hash[header_pathname_basename] = header_hmap_value
  end

  unless private_hmap_hash.empty?
    save_hmap_file(private_hmap_hash, KZ_POD_CONFIG_ROOT, @main_project.root_object.display_name)
  end

  # 创建pod hmap
  @all_kz_pod_targets.each do |target_name, kz_pod_target|
    clean_hmap_cache(kz_pod_target)

    # 修复头文件导入方式
    all_repair_hmap_info = {}
    need_repair_import_targets = kz_pod_target.repair_import
    if need_repair_import_targets.count > 0
      need_repair_import_targets.each { |need_repair_import_target|
        repair_hmap_info = repair_hmap_info(need_repair_import_target)
        all_repair_hmap_info.merge!(repair_hmap_info)
      }
    end

    kz_pod_target.recursive_dependent_targets.each do |recursive_dependent_target|
      if recursive_dependent_target.need_repair_module_import
        header_paths = recursive_dependent_target.public_headers
        header_paths.each do |header_pathname|
          header_pathname_basename = header_pathname.basename.to_s

          header_hmap_value_quotes = {}
          header_hmap_value_quotes['suffix'] = header_pathname_basename
          header_hmap_value_quotes['prefix'] = recursive_dependent_target.product_basename + '/'
          all_repair_hmap_info["#{recursive_dependent_target.root_name}/#{header_pathname_basename}"] = header_hmap_value_quotes
        end
      end
    end

    if all_repair_hmap_info.count > 0
      hmap_cache_path = kz_pod_target.pod_config_cache_path(false)
      FileUtils.mkdir_p(hmap_cache_path) unless File.exist?(hmap_cache_path)

      save_hmap_file(all_repair_hmap_info, hmap_cache_path, target_name + '_repair')
      kz_pod_target.repair_header_search_path = hmap_cache_path + "#{target_name}_repair.hmap"

      KZLog.log("修复'#{kz_pod_target.name}'头文件导入", :success)
    end

    # 添加私有头文件引用
    if kz_pod_target.current_should_build?
      pod_hamp_info = pod_hmap_info(kz_pod_target)
      if pod_hamp_info.count > 0
        hmap_cache_path = kz_pod_target.pod_config_cache_path(false)
        FileUtils.mkdir_p(hmap_cache_path) unless File.exist?(hmap_cache_path)

        save_hmap_file(pod_hamp_info, hmap_cache_path, target_name)
        kz_pod_target.private_header_search_path = hmap_cache_path + "#{target_name}.hmap"
      end

      pod_yaml_info = pod_yaml_info(kz_pod_target)
      if pod_yaml_info.count > 0
        yaml_cache_path = kz_pod_target.pod_config_cache_path(false)
        FileUtils.mkdir_p(yaml_cache_path) unless File.exist?(yaml_cache_path)

        yaml_name = "#{target_name}_origin.yaml"
        save_yaml_file(pod_yaml_info, yaml_cache_path + yaml_name)
        kz_pod_target.custom_origin_yaml_path = yaml_cache_path + yaml_name
        kz_pod_target.custom_yaml_path = yaml_cache_path + "#{target_name}.yaml"
      end
    end
  end

  KZLog.log("完成所有hmap&yaml配置", :success)
end

#create_on_demand_resourcesObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cocoapods-kz/helpers/kz_generator_on_demand_resources.rb', line 7

def create_on_demand_resources
  kz_on_demand_resources_all_config_path = KZ_POD_CONFIG_ROOT + "KZOnDemandResourcesAllConfig.plist"
  FileUtils.rm(kz_on_demand_resources_all_config_path) if File.exist?(kz_on_demand_resources_all_config_path)

  # 汇总所有配置文件,用于代码中加载资源
  on_demand_resources_all_config = {}
  # 用于记录tag防止重复
  current_all_tags = {}
  @all_kz_pod_targets.each do |target_name, kz_pod_target|
    on_demand_resources_info = kz_pod_target.on_demand_resources_info
    next if on_demand_resources_info.empty?

    analyze_on_demand_resources_config_plist(on_demand_resources_info, kz_pod_target, current_all_tags)
    new_on_demand_resources_info = on_demand_resources_info.transform_values do |inner_hash|
      inner_hash.transform_values do |paths|
        paths.map { |path| File.basename(path) }
      end
    end
    on_demand_resources_all_config[target_name] = new_on_demand_resources_info
  end

  # 将汇总的配置写入app中,供代码读取
  if on_demand_resources_all_config.size > 0
    write_to_path(on_demand_resources_all_config, kz_on_demand_resources_all_config_path)
  else
    KZLog.log("KZOnDemandResourcesAllConfig.plist内容为空,创建失败", :warning)
  end

  KZLog.log("完成所有ODR配置", :success)
end

#find_xcassets_path(path) ⇒ Object



190
191
192
193
194
195
196
197
198
# File 'lib/cocoapods-kz/helpers/kz_generator_on_demand_resources.rb', line 190

def find_xcassets_path(path)
  return nil if path.root?

  if path.extname == ".xcassets"
    path
  else
    find_xcassets_path(path.parent)
  end
end

#get_all_xcassets(kz_pod_target) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cocoapods-kz/helpers/kz_generator_on_demand_resources.rb', line 38

def get_all_xcassets(kz_pod_target)
  all_xcassets_paths = []
  kz_pod_target.native_pod_target.file_accessors.each do |file_accessor|
    file_accessor.resource_bundles.map do |bundle_name, paths|
      paths.each do |path|
        if path.extname == ".xcassets"
          all_xcassets_paths << path
        end
      end
    end
  end
  all_xcassets_paths
end

#md5_sign(key) ⇒ Object



246
247
248
# File 'lib/cocoapods-kz/helpers/kz_generator_on_demand_resources.rb', line 246

def md5_sign(key)
  OpenSSL::Digest::MD5.hexdigest(key)
end

#new_build_rule(project, native_target, name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cocoapods-kz/helpers/kz_generator_hmap.rb', line 60

def new_build_rule(project, native_target, name)
  new_rule = nil
  native_target.build_rules.each do |build_rule|
    new_rule = build_rule if build_rule.name == name
  end

  if new_rule == nil
    new_rule = project.new(Xcodeproj::Project::PBXBuildRule)
    native_target.build_rules << new_rule
  end

  new_rule.name = name
  new_rule.compiler_spec = 'com.apple.compilers.proxy.script'
  new_rule
end

#new_shell_script_build_phase(project, native_target, name, before_compile = false) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cocoapods-kz/helpers/kz_generator_hmap.rb', line 76

def new_shell_script_build_phase(project, native_target, name, before_compile = false)
  new_build_phase = project.new(Xcodeproj::Project::PBXShellScriptBuildPhase)
  new_build_phase.name = name
  if before_compile
    find_compile = false
    native_target.build_phases.each_with_index do |build_phase, index|
      if build_phase.is_a?(Xcodeproj::Project::PBXSourcesBuildPhase)
        find_compile = true
        native_target.build_phases.insert(index, new_build_phase)
        break
      end
    end
    unless find_compile
      native_target.build_phases << new_build_phase
    end
  else
    native_target.build_phases << new_build_phase
  end
  new_build_phase
end

#on_demand_resources_from_plist(plist_hash, kz_pod_target, priority, resources_path, odr_build_info, current_all_tags) ⇒ Object



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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/cocoapods-kz/helpers/kz_generator_on_demand_resources.rb', line 137

def on_demand_resources_from_plist(plist_hash, kz_pod_target, priority, resources_path, odr_build_info, current_all_tags)
  return unless (!plist_hash.nil? && plist_hash.size > 0)

  all_normal_file_names = []
  plist_hash.each do |tag, file_paths|
    if current_all_tags.key?(tag)
      KZLog.log("#{kz_pod_target.name}#{current_all_tags[tag]}中存在相同tag:#{tag}#{kz_pod_target.name}中的tag将被废弃", :warning)
      next
    else
      current_all_tags[tag] = kz_pod_target.name
    end

    normal_file_names = []
    normal_file_paths = []

    have_xcassets_paths = false
    file_paths.each do |file_path|
      next unless file_path.exist?

      if file_path.extname == ".imageset"
        have_xcassets_paths = true
        xcassets_path = find_xcassets_path(file_path)
        unless xcassets_path.nil?
          on_demand_resources_xcassets(kz_pod_target, odr_build_info, xcassets_path) do |tag_info|
            tag_info[file_path.relative_path_from(xcassets_path)] = tag
          end
        else
          KZLog.log("资源'#{file_path}'不在xcassets文件夹中,将作为普通文件做ODR配置", :warning)
        end
      else
        # 普通文件配置
        if File.directory?(file_path) && file_path.extname != "bundle"
          KZLog.log("资源'#{file_path}'是一个非bundle的文件夹,将被废弃", :warning)
          next
        end
        normal_file_paths << file_path
        normal_file_names << file_path.basename
        all_normal_file_names << file_path.basename
      end
    end

    create_bundle_info_plist(tag, priority, normal_file_paths, resources_path) do |bundle_id, bundle_path|
      yield(tag, bundle_id, bundle_path, normal_file_names, have_xcassets_paths)
    end
  end

  unless all_normal_file_names.empty?
    odr_build_info["normal_files"] ||= []
    odr_normal_files = odr_build_info["normal_files"]
    odr_normal_files.concat(all_normal_file_names)
  end
end

#on_demand_resources_xcassets(kz_pod_target, odr_build_info, xcassets_path) {|| ... } ⇒ Object

Yields:

  • ()


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/cocoapods-kz/helpers/kz_generator_on_demand_resources.rb', line 200

def on_demand_resources_xcassets(kz_pod_target, odr_build_info, xcassets_path)
  if odr_build_info["all_xcassets"].nil?
    all_xcassets_info = {}
    all_xcassets_paths = kz_pod_target.all_xcassets_paths
    all_xcassets_paths.each do |path|
      all_xcassets_info[path.basename.to_s] = { "path" => path.to_s, "tags" => {} }
    end
    odr_build_info["all_xcassets"] = all_xcassets_info
  end
  all_xcassets_info = odr_build_info["all_xcassets"]
  xcassets_name = xcassets_path.basename.to_s
  xcassets_info = all_xcassets_info[xcassets_name]
  if xcassets_info.nil?
    all_xcassets_info[xcassets_name] = { "path" => xcassets_path.to_s, "tags" => {} }
  end
  yield(all_xcassets_info[xcassets_name]["tags"])
end

#pod_hmap_info(kz_pod_target) ⇒ Object



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

def pod_hmap_info(kz_pod_target)
  pod_hmap_info = {}
  module_name = kz_pod_target.kz_module_name

  # public headers
  public_headers = kz_pod_target.public_headers
  public_headers.each do |header_pathname|
    header_pathname_basename = header_pathname.basename.to_s

    header_hmap_value_quotes = {}
    header_hmap_value_quotes['suffix'] = header_pathname_basename
    header_hmap_value_quotes['prefix'] = module_name + '/'
    pod_hmap_info[header_pathname_basename] = header_hmap_value_quotes
    pod_hmap_info[module_name + '/' + header_pathname_basename] = header_hmap_value_quotes
  end

  # private headers
  all_headers = kz_pod_target.all_headers
  (all_headers - public_headers).each do |header_pathname|
    header_pathname_basename = header_pathname.basename.to_s

    header_hmap_value_quotes = {}
    header_hmap_value_quotes['suffix'] = header_pathname_basename
    header_hmap_value_quotes['prefix'] = header_pathname.dirname.to_s + '/'
    pod_hmap_info[header_pathname_basename] = header_hmap_value_quotes
  end

  # pch
  pchfile_path = kz_pod_target.prefix_header_path
  if pchfile_path.exist?
    suffix = pchfile_path.basename.to_s
    pod_hmap_info[suffix] = { 'suffix' => suffix, 'prefix' => "#{pchfile_path.dirname.to_s}/" }
  end

  # umbrella
  umbrella_path =  kz_pod_target.umbrella_header_path
  if umbrella_path.exist?
    suffix = umbrella_path.basename.to_s
    pod_hmap_info[suffix] = { 'suffix' => suffix, 'prefix' => "#{module_name}/" }
  end

  if kz_pod_target.current_uses_swift? && kz_pod_target.is_dev_pod
    # 修改SPBoss-Swift.h文件的导入方式
    swift_bridge_file_name = "#{module_name}-Swift.h"
    pod_hmap_info[swift_bridge_file_name] = { 'suffix' => swift_bridge_file_name, 'prefix' => "#{module_name}/" }
  end

  # 另存一份Headers,用于创建文件与import时的提示信息
  if kz_pod_target.is_dev_pod
    all_headers.each do |header_pathname|
      symlink_path = kz_pod_target.local_private_headers_path + header_pathname.basename
      File.symlink(header_pathname, symlink_path) unless File.symlink?(symlink_path) || File.exist?(symlink_path)
    end
    kz_pod_target.use_local_private_headers_path = true
  end
  pod_hmap_info
end

#pod_yaml_info(kz_pod_target) ⇒ Object



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

def pod_yaml_info(kz_pod_target)
  built_products_dir = "__built_products_dir__/"

  pod_yaml_info = {}
  pod_yaml_info["case-sensitive"] = "false"
  pod_yaml_info["version"] = "0"

  yaml_roots = []
  pod_yaml_info["roots"] = yaml_roots

  yaml_header_info = {}
  yaml_header_info["type"] = "directory"
  # 使用origin_product_name,是因为kz_analyzer中对于拥有相同product_name的target的product_name改成了target_name
  # 但是新版Xcode的编译过程包含一个scan步骤,会使用hamp中映射的framework,hmap中使用的是原始product_name,这里需要与之同步
  yaml_header_info["name"] = built_products_dir + "#{kz_pod_target.origin_product_name}/Headers"
  yaml_roots.append(yaml_header_info)

  public_headers = kz_pod_target.public_headers
  yaml_header_contents = []
  yaml_header_info["contents"] = yaml_header_contents
  public_headers.each do |header_pathname|
    yaml_header_content = {}
    yaml_header_content["type"] = "file"
    yaml_header_content["name"] = header_pathname.basename.to_s
    yaml_header_content["external-contents"] = header_pathname.to_s
    yaml_header_contents.append(yaml_header_content)
  end

  yaml_modulemap_info = {}
  yaml_modulemap_info["type"] = "directory"
  yaml_modulemap_info["name"] = built_products_dir + "#{kz_pod_target.origin_product_name}/Modules"
  yaml_roots.append(yaml_modulemap_info)

  yaml_modulemap_contents = []
  yaml_modulemap_info["contents"] = yaml_modulemap_contents

  yaml_modulemap_content = {}
  yaml_modulemap_content["type"] = "file"
  yaml_modulemap_content["name"] = "module.modulemap"
  yaml_modulemap_content["external-contents"] = built_products_dir + "#{kz_pod_target.origin_product_name}/Modules/module.modulemap"
  yaml_modulemap_contents.append(yaml_modulemap_content)

  pod_yaml_info
end

#repair_hmap_info(kz_pod_target) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/cocoapods-kz/helpers/kz_generator_hmap.rb', line 208

def repair_hmap_info(kz_pod_target)
  header_paths = kz_pod_target.public_headers
  header_paths.each_with_object({}) do |header_pathname, hmap_info|
    header_pathname_basename = header_pathname.basename.to_s

    header_hmap_value_quotes = {}
    header_hmap_value_quotes['suffix'] = header_pathname_basename
    header_hmap_value_quotes['prefix'] = header_pathname.dirname.to_s + '/'
    hmap_info[header_pathname_basename] = header_hmap_value_quotes

    header_hmap_value_slash = {}
    header_hmap_value_slash['suffix'] = header_pathname_basename
    prefix_name = kz_pod_target.product_basename
    if header_pathname.dirname.to_s.include?('.framework')
      header_pathname.dirname.to_s.split('/').each do |name|
        if name.include?('.framework')
          prefix_name = name.split('.').first
        end
      end
    end
    header_hmap_value_slash['prefix'] = prefix_name + '/'
    hmap_info[header_pathname_basename] = header_hmap_value_slash
  end
end

#save_hmap_file(hmap_hash, save_path, file_name) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/cocoapods-kz/helpers/kz_generator_hmap.rb', line 336

def save_hmap_file(hmap_hash, save_path, file_name)
  hmap_json = JSON.pretty_generate(hmap_hash)
  hmap_json_path = save_path + "#{file_name}.json"
  hmap_path = save_path + "#{file_name}.hmap"
  json_file = File.new(hmap_json_path, 'w')
  return unless json_file

  json_file.syswrite(hmap_json)
  json_file.close

  KZLog.run_shell("#{HMAP_EXECUTE_PATH} convert #{hmap_json_path} #{hmap_path}")
  KZLog.log("'#{file_name}.hamp'生成失败", :error) unless File.exist?(hmap_path)

  FileUtils.rm(hmap_json_path) unless KZ::KZGlobalHelper.instance.debug
end

#save_yaml_file(yaml_hash, save_path) ⇒ Object



352
353
354
355
356
357
358
359
# File 'lib/cocoapods-kz/helpers/kz_generator_hmap.rb', line 352

def save_yaml_file(yaml_hash, save_path)
  yaml_json = JSON.pretty_generate(yaml_hash)
  json_file = File.new(save_path, 'w')
  return unless json_file

  json_file.syswrite(yaml_json)
  json_file.close
end

#traverse_folder(folder_path) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/cocoapods-kz/helpers/kz_generator_hmap.rb', line 181

def traverse_folder(folder_path)
  Dir.foreach(folder_path) do |file_name|
    next if file_name == '.' || file_name == '..' || file_name == '.DS_Store'

    file_path = File.join(folder_path, file_name)
    if File.file?(file_path)
      yield(file_path) if file_name.end_with?('.h')
    elsif File.directory?(file_path)
      traverse_folder(file_path) do |path|
        yield(path)
      end
    end
  end
end

#write_to_path(hash, path) ⇒ Object

Xcodeproj::Plist.write_to_path只支持hash,如果是数组需要重写write方法

Raises:

  • (IOError)


251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/cocoapods-kz/helpers/kz_generator_on_demand_resources.rb', line 251

def write_to_path(hash, path)
  unless path.is_a?(String) || path.is_a?(Pathname)
    raise TypeError, "The given `#{path}` must be a string or 'pathname'."
  end
  path = path.to_s
  raise IOError, 'Empty path.' if path.empty?

  File.open(path, 'w') do |f|
    plist = Nanaimo::Plist.new(hash, :xml)
    Nanaimo::Writer::XMLWriter.new(plist, :pretty => true, :output => f, :strict => false).write
  end
end