Class: Pindo::XcodeBuildHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/module/xcode/xcode_build_helper.rb

Class Method Summary collapse

Class Method Details

.backup_podfile_lock(project_dir: nil, app_config_dir: nil, appversion: nil) ⇒ Object



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
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 63

def backup_podfile_lock(project_dir:nil, app_config_dir:nil, appversion:nil)

  begin
        proj_pod_file = File.join(project_dir, "Podfile.lock")
        FileUtils.cp_r(proj_pod_file, File.join(app_config_dir, "Podfile.lock"))
        git_addpush_repo(path:app_config_dir, message:"#{appversion} backup podfile.lock", commit_file_params:["Podfile.lock"])

        bytes = File.binread(proj_pod_file)
        checksum = Digest::MD5.hexdigest(bytes)
        build_verify_file = File.join(app_config_dir, "build_verify.json")
        build_verify_json = nil
        begin
            build_verify_json = JSON.parse(File.read(build_verify_file))
        rescue => error
        end
        build_verify_json = build_verify_json || {}
        build_verify_json["output_code_commit"] = git_latest_commit_id(local_repo_dir:project_dir)
        build_verify_json["output_config_commit"] = git_latest_commit_id(local_repo_dir:app_config_dir)
        build_verify_json["output_podfile_checksum"] = checksum
        build_verify_json["output_time"] = Time.now.strftime('%y/%m/%d %H:%M:%S')

        File.open(build_verify_file, "w") do |file|
            file.write(JSON.pretty_generate(build_verify_json))
            file.close
        end
        git_addpush_repo(path:app_config_dir, message:"backup #{appversion} output info", commit_file_params:["build_verify.json"])

  rescue => error
      raise Informative,  "保存Podfile.lock 文件失败!!!"
  end
end

.delete_libtarget_firebase_shell(project_path) ⇒ Object

删除 Unity-iPhone 项目中的 Firebase Crashlytics 脚本



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 12

def delete_libtarget_firebase_shell(project_path)
  puts "[-] 开始检查并删除 Unity-iPhone下的Firebase Crashlytics脚本..."
  if File.directory?(File.join(project_path, 'Unity')) && File.exist?(File.join(project_path, 'Unity', 'Unity-iPhone.xcodeproj'))
    unity_project_path = File.join(project_path, 'Unity', 'Unity-iPhone.xcodeproj')
    xcdoe_unitylib_project = Xcodeproj::Project::open(unity_project_path)
    xcdoe_unitylib_project.targets.each do |target|
      target.shell_script_build_phases&.each do |phase|
        if phase.name.eql?("Crashlytics Run Script")
          puts "    从target:#{target.name}中删除: #{phase.name} ..."
          target.build_phases.delete(phase)
        end
      end
    end
    xcdoe_unitylib_project.save()
    puts "[✔] 完成检查并删除 Unity-iPhone下的Firebase Crashlytics脚本..."
  end
end

.fix_xcode16_linker_flags(project_dir: nil) ⇒ Object

修复 Xcode 16 链接器兼容性问题自动移除 -ld_classic 和 -ld64 标志



343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 343

def fix_xcode16_linker_flags(project_dir: nil)
    begin
        # 查找所有 xcodeproj 文件
        workspace_file = Dir.glob(File.join(project_dir, "*.xcworkspace")).first
        project_files = []

        if workspace_file && File.exist?(workspace_file)
            # 如果存在 workspace,从中提取所有项目
            workspace_data_file = File.join(workspace_file, "contents.xcworkspacedata")
            if File.exist?(workspace_data_file)
                require 'rexml/document'
                doc = REXML::Document.new(File.read(workspace_data_file))
                doc.elements.each('Workspace/FileRef') do |file_ref|
                    location = file_ref.attributes['location']
                    if location && location.start_with?('group:')
                        relative_path = location.sub('group:', '')
                        if relative_path.end_with?('.xcodeproj')
                            full_path = File.join(project_dir, relative_path)
                            project_files << full_path if File.exist?(full_path)
                        end
                    end
                end
            end
        else
            # 直接查找项目文件
            project_files = Dir.glob(File.join(project_dir, "**/*.xcodeproj"))
        end

        return if project_files.empty?

        fixed_count = 0

        project_files.each do |project_path|
            begin
                project = Xcodeproj::Project.open(project_path)
                project_modified = false

                project.targets.each do |target|
                    target.build_configurations.each do |config|
                        ldflags = config.build_settings['OTHER_LDFLAGS']
                        next unless ldflags

                        original_flags = ldflags.dup

                        # 移除过时的链接器标志
                        if ldflags.is_a?(Array)
                            ldflags.delete('-ld_classic')
                            ldflags.delete('-ld64')
                        elsif ldflags.is_a?(String)
                            ldflags = ldflags.gsub(/-ld_classic|-ld64/, '').strip
                        end

                        if original_flags != ldflags
                            config.build_settings['OTHER_LDFLAGS'] = ldflags
                            project_modified = true
                        end
                    end
                end

                if project_modified
                    project.save
                    fixed_count += 1
                end

            rescue => e
                # 静默处理错误,不中断构建
            end
        end

        if fixed_count > 0
            puts "✅ 修复 Xcode 16 链接器兼容性配置".green
        end

    rescue => error
        puts "⚠️  修复链接器标志时出现错误: #{error.message}".yellow
        # 不中断构建流程
    end
end

.install_google_plist(project_dir: nil, app_config_dir: nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 95

def install_google_plist(project_dir:nil, app_config_dir:nil)

    project_fullname = Dir.glob(File.join(project_dir, "/*.xcodeproj")).max_by {|f| File.mtime(f)}
    project_obj = Xcodeproj::Project.open(project_fullname)
    select_target = project_obj.targets.select { |target| target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application])  }.first
    file_ref = select_target.resources_build_phase.files_references.select { |file| file.display_name.include?("GoogleService-Info.plist") }.first

    if !file_ref.nil?
        xcode_googleinfo_path = file_ref.real_path

        if !File.exist?(File.join(app_config_dir, "GoogleService-Info.plist"))
            raise Informative, "缺少 GoogleService-Info.plist ==> #{app_config_dir}!!!"
        else
            FileUtils.cp(File.join(app_config_dir, "GoogleService-Info.plist"), xcode_googleinfo_path)
        end

        if !File.exist?(xcode_googleinfo_path)
            raise Informative, "拷贝 GoogleService-Info.plist 失败!!==> #{xcode_googleinfo_path}!!!"
        end
    end
end

.modify_info_plist(project_dir: nil, proj_name: nil, config_json: nil) ⇒ Object



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
335
336
337
338
339
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 306

def modify_info_plist(project_dir:nil, proj_name:nil, config_json:nil)

    ## Main Info.plist
    proj_fullname = File.join(project_dir, proj_name) + ".xcodeproj"
    project_obj = Xcodeproj::Project.open(proj_fullname)

    info_plist_path = nil
    project_obj.targets.each do |target|
        temp_info = target.build_configurations.first.build_settings['INFOPLIST_FILE']
        if !temp_info.nil?
            info_plist_path = File.join(project_dir, temp_info)
        end

        # if target.product_type.to_s.eql?("com.apple.product-type.application") && !File.exist?(info_plist_path)
        #     info_plist_array = Dir.glob(File.join(project_dir, target.name, "**", "Info.plist"))
        #     if info_plist_array.size == 1
        #         info_plist_path = info_plist_array.first
        #     else
        #         raise Informative, "Missing Target #{target.name.to_s} Info.plist !!! Modify Info.plist Error !!!"
        #     end
        # end
        
        if target.product_type.to_s.eql?("com.apple.product-type.application") &&  (info_plist_path.nil? || !File.exist?(info_plist_path))
            raise Informative, "Target #{target.name.to_s} 没有找到Info.plist, 修改Info.plist出错了!!"
        end

        if target.product_type.to_s.eql?("com.apple.product-type.application") then
            modify_maintarget_info_plist(plist_file_name:info_plist_path, config_json:config_json, target_name:proj_name)
        end

        modify_infoplist_version(plist_file_name:info_plist_path, config_json:config_json, target_name:target.name.to_s)
    end

end

.modify_infoplist_version(plist_file_name: nil, config_json: nil, target_name: nil) ⇒ Object



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
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
214
215
216
217
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 159

def modify_infoplist_version(plist_file_name:nil, config_json:nil, target_name:nil)

    if File.exist?(plist_file_name)
        info_plist_dict = Xcodeproj::Plist.read_from_path(plist_file_name)
        info_plist_dict["CFBundleIdentifier"] = "$(PRODUCT_BUNDLE_IDENTIFIER)"

        exe_binary_name = config_json['app_info']["app_display_name"]
        exe_binary_name = exe_binary_name.gsub(/ /, '');
        exe_binary_name = exe_binary_name.gsub(/\'/, '');

        info_plist_dict["CFBundleDisplayName"] = config_json['app_info']["app_display_name"]
        if !info_plist_dict["CFBundleName"].nil?
            info_plist_dict["CFBundleName"] = exe_binary_name
        end

        if config_json['app_info']["imessage_display_name"] && target_name && target_name.end_with?("iMessage")
            info_plist_dict["CFBundleDisplayName"] = config_json['app_info']["imessage_display_name"]
        end
        if config_json['app_info']["extension_display_name"] && target_name && target_name.end_with?("Extension")
            info_plist_dict["CFBundleDisplayName"] = config_json['app_info']["extension_display_name"]
        end


        if config_json['app_info']["extensionad_display_name"] && target_name && target_name.end_with?("ExtensionAd")
            info_plist_dict["CFBundleDisplayName"] = config_json['app_info']["extensionad_display_name"]
        end

        if config_json['app_info']["extensionporn_display_name"] && target_name && target_name.end_with?("ExtensionPorn")
            info_plist_dict["CFBundleDisplayName"] = config_json['app_info']["extensionporn_display_name"]
        end

        if config_json['app_info']["keyboard_display_name"] && target_name && target_name.end_with?("Keyboard")
            info_plist_dict["CFBundleDisplayName"] = config_json['app_info']["keyboard_display_name"]
        end


        if config_json['app_info']["siri_display_name"] && target_name && target_name.end_with?("Keyboard")
            info_plist_dict["CFBundleDisplayName"] = config_json['app_info']["siri_display_name"]
        end

        if config_json['app_info']["siriui_display_name"] && target_name && target_name.end_with?("iMessage")
            info_plist_dict["CFBundleDisplayName"] = config_json['app_info']["siriui_display_name"]
        end



        unless config_json['app_info']["app_version"]
            raise Informative, "config.json Missing app_info app_version !!!"
        end

        info_plist_dict["CFBundleShortVersionString"] = config_json['app_info']["app_version"]

        unless config_json['app_info']["app_build_version"]
            raise Informative, "config.json Missing app_info app_build_version !!!"
        end
        info_plist_dict["CFBundleVersion"] = config_json['app_info']["app_build_version"]
        Xcodeproj::Plist.write_to_path(info_plist_dict, plist_file_name)
    end
end

.modify_maintarget_info_plist(plist_file_name: nil, config_json: nil, target_name: nil) ⇒ Object



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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 220

def modify_maintarget_info_plist(plist_file_name:nil, config_json:nil, target_name:nil)
    if File.exist?(plist_file_name)
        info_plist_dict = Xcodeproj::Plist.read_from_path(plist_file_name)

        info_plist_dict.delete("AccountKitClientToken")

        if info_plist_dict["Fabric"]
            info_plist_dict.delete('Fabric')
        end

        if info_plist_dict["UIRequiredDeviceCapabilities"] && !info_plist_dict["UIRequiredDeviceCapabilities"].first.nil? && info_plist_dict["UIRequiredDeviceCapabilities"].first == "armv7"
            raise Informative, "Info.plist里面有多余的Key  UIRequiredDeviceCapabilities  armv7"
        end

        if !config_json['app_info']['admob_app_id'].nil? && config_json['app_info']['admob_app_id'].include?("__________config")
            raise Informative, "config.json 配置文件key :admob_app_id 包含初始值未修改, 配置正确的值或者删除!!!"
        end

        if !info_plist_dict["GADApplicationIdentifier"].nil? && config_json['app_info']['admob_app_id'].nil?
            raise Informative, "工程Info.plist中有 Admob 配置,config.json缺少 Admob 配置参数!!!"
        end

        if !config_json['app_info']['admob_app_id'].nil?
            info_plist_dict["GADApplicationIdentifier"] = config_json['app_info']['admob_app_id']
        end




        if !config_json['app_info']['applovin_app_id'].nil? && config_json['app_info']['applovin_app_id'].include?("__________config")
            raise Informative, "config.json 配置文件key :applovin_app_id 包含初始值未修改, 配置正确的值或者删除!!!"
        end

        if !info_plist_dict["AppLovinSdkKey"].nil? && config_json['app_info']['applovin_app_id'].nil?
            raise Informative, "工程Info.plist中有 AppLovin 配置,config.json缺少 AppLovin 配置参数!!!"
        end

        if !info_plist_dict["AppLovinSdkKey"].nil? && !config_json['app_info']['applovin_app_id'].nil?
            info_plist_dict["AppLovinSdkKey"] = config_json['app_info']['applovin_app_id']
        else
            info_plist_dict.delete('AppLovinSdkKey')
        end

        # if config_json['app_setting'] && config_json['app_setting']['applovin_app_id']
        #     info_plist_dict["AppLovinSdkKey"] = config_json['app_setting']['applovin_app_id']
        # elsif config_json['app_setting'] && config_json['app_setting']['kGUKeyApplovinAppId']
        #     info_plist_dict["AppLovinSdkKey"] = config_json['app_setting']['kGUKeyApplovinAppId']
        # else
        #     info_plist_dict.delete('AppLovinSdkKey')
        # end





        info_plist_dict["CFBundleURLTypes"] = []
        item0 = {}
        item0["CFBundleTypeRole"] = "Editor"
        item0["CFBundleURLName"] = "$(PRODUCT_BUNDLE_IDENTIFIER)"
        item0["CFBundleURLSchemes"] = []
        item0["CFBundleURLSchemes"] << "$(PRODUCT_BUNDLE_IDENTIFIER)"
        info_plist_dict["CFBundleURLTypes"] << item0

        if config_json['app_info'] && config_json['app_info']['facebook_app_id']
            info_plist_dict["FacebookAppID"] = config_json['app_info']['facebook_app_id']
            if config_json['app_info']['facebook_client_token'].nil?
                raise Informative, "config.json FB Token 是空的"
            end
            info_plist_dict["FacebookClientToken"] = config_json['app_info']['facebook_client_token']
            info_plist_dict["FacebookDisplayName"] = config_json['app_info']['app_display_name']
            item1 = {}
            item1["CFBundleTypeRole"] = "Editor"
            item1["CFBundleURLSchemes"] = []
            item1["CFBundleURLSchemes"] << "fb" + config_json['app_info']['facebook_app_id']
            info_plist_dict["CFBundleURLTypes"] << item1
        else
            info_plist_dict.delete('FacebookAppID')
            info_plist_dict.delete('FacebookClientToken')
            info_plist_dict.delete('FacebookDisplayName')
        end

        Xcodeproj::Plist.write_to_path(info_plist_dict, plist_file_name)
    end

end

.modify_project_config(project_dir: nil, proj_name: nil, config_json: nil) ⇒ Object



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
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 117

def modify_project_config(project_dir:nil, proj_name:nil,  config_json:nil)

    proj_fullname = File.join(project_dir, proj_name) + ".xcodeproj"
    project_obj = Xcodeproj::Project.open(proj_fullname)

    ios_deployment_targe = "14.0"
    if config_json && config_json['project_info'] &&  config_json['project_info']['ios_deployment_targe']
        ios_deployment_targe = config_json['project_info']['ios_deployment_targe']
    end

    project_obj.targets.each do |target|

        exe_binary_name = config_json['app_info']['app_display_name']
        exe_binary_name = exe_binary_name.gsub(/ /, '');
        exe_binary_name = exe_binary_name.gsub(/\'/, '');

        target.build_configurations.each do |config|
            config.build_settings['CURRENT_PROJECT_VERSION'] = config_json['app_info']['app_build_version']
            config.build_settings['MARKETING_VERSION'] = config_json['app_info']['app_version']
            config.build_settings['INFOPLIST_KEY_CFBundleDisplayName'] = config_json['app_info']['app_display_name']
        end

        target_name_map = get_target_name_map
       if target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application])
            target.build_configurations.each do |config|
                config.build_settings['PRODUCT_NAME'] = exe_binary_name
                config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = ios_deployment_targe
            end
        elsif
            target_name_map.each do |k, v|
                if target.name.to_s.end_with?(k)
                    target.build_configurations.each do |config|
                        config.build_settings['PRODUCT_NAME'] = exe_binary_name + k
                    end
                end
            end
        end
    end
    project_obj.save

end

.pull_podfile_lock(project_dir: nil, app_config_dir: nil) ⇒ Object



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
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 30

def pull_podfile_lock(project_dir:nil, app_config_dir:nil)
    begin
        src_pod_file = File.join(app_config_dir, "Podfile.lock")
        build_verify_file = File.join(app_config_dir, "build_verify.json")
        build_verify_json = nil
        begin
            build_verify_json = JSON.parse(File.read(build_verify_file))
        rescue => error
        end
        build_verify_json = build_verify_json || {}
        build_verify_json["release_code_commit"] = git_latest_commit_id(local_repo_dir:project_dir)
        build_verify_json["release_config_commit"] = git_latest_commit_id(local_repo_dir:app_config_dir)
        bytes = File.binread(src_pod_file)
        checksum = Digest::MD5.hexdigest(bytes)
        build_verify_json["release_podfile_checksum"] = checksum
        build_verify_json["release_time"] = Time.now.strftime('%y/%m/%d %H:%M:%S')

        File.open(build_verify_file, "w") do |file|
            file.write(JSON.pretty_generate(build_verify_json))
            file.close
        end

        git_addpush_repo(path:app_config_dir, message:"back release info", commit_file_params:["build_verify.json"])

        if File.exist?(src_pod_file)
            FileUtils.cp_r(src_pod_file, File.join(project_dir, "Podfile.lock"))
        end
    rescue => error
        raise Informative,  "获取Podfile.lock 文件失败!!!"
    end
end