Module: Pod::PodUtil

Includes:
Config::Mixin
Included in:
Frameworker, MutiFrameworker, XBuilder, XBuilder::XcodeProjHelper
Defined in:
lib/cocoapods-xcframework/util/pod_util.rb

Instance Method Summary collapse

Instance Method Details

#build_static_sandboxObject



62
63
64
# File 'lib/cocoapods-xcframework/util/pod_util.rb', line 62

def build_static_sandbox
  Sandbox.new(config.sandbox_root)
end

#fix_bundle_file(origin_spec, spec_hash, project_dir, xcframework_path) ⇒ Object

脚本不会针对podspec中配置resources、resource_bundles动态生成bundle,故手动进行bundle路径修复



310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/cocoapods-xcframework/util/pod_util.rb', line 310

def fix_bundle_file(origin_spec, spec_hash, project_dir, xcframework_path)
  puts "fix_bundle_file".yellow
  origin_spec_hash = origin_spec.to_hash
  res_bundle=origin_spec_hash["resource_bundles"]
  if res_bundle
    puts "二进制库不支持动态生成bundle文件,请手动存放bundle文件 参照:s.resources = 'xx.bundle' ".red
    raise "#{res_bundle}"
  end
  res_bundle=origin_spec_hash["resources"]
  if res_bundle
    puts "s.resources:#{res_bundle}"
    bundle_path="#{project_dir}/#{res_bundle}"
    FileUtils.cp_r(bundle_path,xcframework_path)
    xcframework_comp = File.basename(xcframework_path)
    bundle_comp = File.basename(res_bundle)
    spec_hash["resources"] = "#{xcframework_comp}/#{bundle_comp}" # bundle资源存放规则,xcframework根目录
  end
  puts spec_hash.to_json
  spec_hash
end

#fix_header_file(spec_hash, xcframework_path) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/cocoapods-xcframework/util/pod_util.rb', line 285

def fix_header_file spec_hash, xcframework_path
  puts "Dir.glob(#{xcframework_path}/*/) : #{Dir.glob("#{xcframework_path}/*/")}"
  Dir.glob("#{xcframework_path}/*/").each do |file|
    ['ios','macos','tvos', 'watchos'].each do |prefix|
      last_path = file.split("/").last
      if last_path =~ /#{prefix}/ and not last_path =~ /simulator/ and not last_path =~ /maccatalyst/
        platform = to_native_platform prefix
        if spec_hash[platform]
          spec_hash[platform]["public_header_files"] = "#{spec_hash[:vendored_frameworks]}/#{last_path}/*/Headers/*.h"
          spec_hash[platform]["source_files"] = "#{spec_hash[:vendored_frameworks]}/#{last_path}/*/Headers/*.h"
        else
          spec_hash[platform] = {
            "public_header_files" => "#{spec_hash[:vendored_frameworks]}/#{last_path}/*/Headers/*.h",
            "source_files" => "#{spec_hash[:vendored_frameworks]}/#{last_path}/*/Headers/*.h"
          }
        end
      end
    end
  end
  puts "fix_header_file".yellow
  puts spec_hash.to_json
  spec_hash
end

#generic_new_podspec_hash(spec) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/cocoapods-xcframework/util/pod_util.rb', line 260

def generic_new_podspec_hash spec
  spec_hash = spec.to_hash
  [
    "source_files",
    "resources",
    "resource_bundles",
    "prefix_header_contents",
    "prefix_header_file",
    "header_dir",
    "header_mappings_dir",
    "script_phase",
    "public_header_files",
    "private_header_files",
    "vendored_frameworks",
    "vendored_libraries",
    "exclude_files",
    "preserve_paths",
    "module_map",
    "subspec"
  ].each do |key|
    spec_hash.delete "#{key}"
  end
  spec_hash
end

#installation_root(sandbox, spec, subspecs, sources, use_frameworks = true, use_modular_headers = true, enable_bitcode = false, support_dynamic = false) ⇒ Object



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
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/cocoapods-xcframework/util/pod_util.rb', line 66

def installation_root sandbox, spec, subspecs, sources,use_frameworks = true,use_modular_headers = true, enable_bitcode = false, support_dynamic=false
    podfile = podfile_from_spec(
    @path,
    spec,
    subspecs,
    sources,
    use_frameworks,
    use_modular_headers
  )
  other_flags=""
  if support_dynamic
    # 解析podspec获取依赖关系
    spec.dependencies.uniq.each do |dependency|
      other_flags << "-framework #{dependency.name} "
    end
    # spec.frameworks系统库没有提供公共方法,故手动解析引用系统库
    spec_hash = spec.to_hash
    spec_hash.uniq.each do |configuration|
      key = configuration[0]
      if key == 'frameworks'
        val = configuration[1]
        if val.is_a? String
          other_flags << "-framework #{val} "
        elsif val.is_a? Array
          val.uniq.each do |item|
            other_flags << "-framework #{item} "
          end
        else
          UI.puts "framework configuration:#{configuration} val:#{val}"
        end
      elsif key == 'libraries'
        val = configuration[1]
        if val.is_a? String
          other_flags << "-l #{val} "
        elsif val.is_a? Array
          val.uniq.each do |item|
            other_flags << "-l #{item} "
          end
        else
          UI.puts "libraries configuration:#{configuration} val:#{val}"
        end
      end
    end
  end
  UI.puts "support_dynamic:#{support_dynamic} other_flags: #{other_flags}"

  installer = Installer.new(sandbox, podfile)
  installer.repo_update = true
  installer.install!

  unless installer.nil? 
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |configuration| 
        if enable_bitcode && configuration.name == "Release"
          configuration.build_settings['ENABLE_BITCODE'] = 'YES'
          configuration.build_settings['BITCODE_GENERATION_MODE'] = 'bitcode'
        end
        ### support MacCatalyst 默认生成工程不支持 by hm 21/11/3
        configuration.build_settings['SUPPORTS_MACCATALYST'] = 'YES'
        ### support MacCatalyst end
      end
      if target.name == spec.name
        target.build_configurations.each do |configuration|
          configuration.build_settings['CLANG_MODULES_AUTOLINK'] = 'NO'
          # 动态链接库需要配置project工程库依赖,否则导致无法编译通过 by hm 23/7/13
          if !other_flags.empty?
            UI.puts "configuration other_flags: #{other_flags}"
            configuration.build_settings['OTHER_LDFLAGS'] = "#{other_flags}"
          end
          if support_dynamic
            configuration.build_settings['MACH_O_TYPE'] = 'mh_dylib'
          else
            configuration.build_settings['MACH_O_TYPE'] = 'staticlib'
          end
        end
      end
    end
    installer.pods_project.save
  end
  installer
end

#installation_root_muti(sandbox, configs, sources, use_frameworks = true, use_modular_headers = true, enable_bitcode = false) ⇒ Object



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
# File 'lib/cocoapods-xcframework/util/pod_util.rb', line 148

def installation_root_muti sandbox, configs, sources, use_frameworks = true, use_modular_headers = true, enable_bitcode = false
  podfile = podfile_from_muti_configs(
    configs,
    sources,
    use_frameworks,
    use_modular_headers
  )
  installer = Installer.new(sandbox, podfile)
  installer.repo_update = true
  installer.install!

  specs = configs.map do |cfg|
    cfg["name"]
  end
  unless installer.nil? 
    installer.pods_project.targets.each do |target|
      target.build_configurations.each do |configuration| 
        if enable_bitcode && configuration.name == "Release"
          configuration.build_settings['ENABLE_BITCODE'] = 'YES'
          configuration.build_settings['BITCODE_GENERATION_MODE'] = 'bitcode'
        end
        ### support MacCatalyst 默认生成工程不支持 by hm 21/11/3
        configuration.build_settings['SUPPORTS_MACCATALYST'] = 'YES'
        ### support MacCatalyst end
      end
      if specs.include? target.name
        target.build_configurations.each do |configuration|
          configuration.build_settings['CLANG_MODULES_AUTOLINK'] = 'NO'
        end
      end
    end
    installer.pods_project.save
  end
  installer
end

#muti_config_with_file(path) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cocoapods-xcframework/util/pod_util.rb', line 19

def muti_config_with_file(path)
  return nil if path.nil?
  path = Pathname.new(path)
  path = Pathname.new(Dir.pwd).join(path) unless path.absolute?
  @path = path.expand_path
  content = File.open(path, 'rb').read
  result = JSON.parse content
  if not result.is_a? Array
    UI.error "#{path} format not support"
    exit -1
  end
  result
end

#podfile_from_muti_configs(configs, sources, use_frameworks = true, use_modular_headers = true) ⇒ Object



215
216
217
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/cocoapods-xcframework/util/pod_util.rb', line 215

def podfile_from_muti_configs configs, sources, use_frameworks = true, use_modular_headers = true
  installation_root = config.installation_root.to_s
  static_library_enable = config.static_library_enable?
  Pod::Podfile.new do 
    sources.each {|s| source s}
    configs.each do |cfg|
      pod_spec_path = installation_root + "/#{cfg["name"]}/#{cfg["name"]}.podspec"
      pod_spec_json_path = pod_spec_path + ".json"
      (Pathname.glob(pod_spec_path) + Pathname.glob(pod_spec_json_path)).each do |real_path|
        spec = Pod::Specification.from_file real_path.to_s
        options = Hash.new 
        options[:podspec] = real_path.to_s
        if cfg["subspecs"]
          options[:subspecs] = cfg["subspecs"]
        else
          options[:subspecs] = spec.subspecs.map do |sub|
            sub.base_name
          end
        end
        # 非常奇怪,如果传一个空的数组过去就会出问题!!
        if options[:subspecs].length == 0
          options[:subspecs] = nil
        end
        spec.available_platforms.each do |plt|
          target "#{spec.name}-#{plt.name}" do 
            puts "#{plt.name} #{spec.name} #{options}"
            platform(plt.name, spec.deployment_target(plt.name))
            pod(spec.name, options)
          end
        end
      end
    end
    install!('cocoapods',
      :integrate_targets => false,
      :deterministic_uuids => false)

      if static_library_enable
        use_frameworks! :linkage => :static if use_frameworks
      else
        use_frameworks! if use_frameworks
      end
      use_modular_headers! if use_modular_headers
  end
end

#podfile_from_spec(path, spec, subspecs, sources, use_frameworks = true, use_modular_headers = true) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/cocoapods-xcframework/util/pod_util.rb', line 184

def podfile_from_spec path, spec, subspecs, sources, use_frameworks = true, use_modular_headers=true
  options = Hash.new
  options[:podspec] = path.to_s
  options[:subspecs] = spec.subspecs.map do |sub|
    sub.base_name
  end
  options[:subspecs] = subspecs if subspecs
  # 非常奇怪,如果传一个空的数组过去就会出问题!!
  if options[:subspecs].length == 0
    options[:subspecs] = nil
  end
  static_library_enable = config.static_library_enable?
  Pod::Podfile.new do
    sources.each {|s| source s}
    spec.available_platforms.each do |plt|
      target "#{spec.name}-#{plt.name}" do
        platform(plt.name, spec.deployment_target(plt.name))
        pod(spec.name, options)
      end
    end

    install!('cocoapods',:integrate_targets => false,:deterministic_uuids => false)
    if static_library_enable
      use_frameworks! :linkage => :static if use_frameworks
    else
      use_frameworks! if use_frameworks
    end
    use_modular_headers! if use_modular_headers
  end
end

#spec_with_name(name) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/cocoapods-xcframework/util/pod_util.rb', line 53

def spec_with_name(name)
  return if name.nil?

  set = Pod::Config.instance.sources_manager.search(Dependency.new(name))
  return nil if set.nil?

  set.specification.root
end

#spec_with_path(path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cocoapods-xcframework/util/pod_util.rb', line 33

def spec_with_path(path)
  return if path.nil?
  path = Pathname.new(path)
  path = Pathname.new(Dir.pwd).join(path) unless path.absolute?
  return unless path.exist?
  @path = path.expand_path

  if @path.directory?
    raise @path + ': is a directory.'
    return
  end

  unless ['.podspec', '.json'].include? @path.extname
    raise @path + ': is not a podspec.'
    return
  end

  Specification.from_file(@path)
end

#to_native_platform(name) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/cocoapods-xcframework/util/pod_util.rb', line 5

def to_native_platform name
  case name
  when 'iphoneos' then 'ios'
  when 'macOS' then 'osx'
  when 'appletvos' then 'tvos'
  when 'watchos' then 'watchos'
  when 'ios' then 'ios'
  when 'macos' then 'osx'
  when 'tvos' then 'tvos'
  else
    name
  end
end