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

.add_membership_exception(sync_group, target, file_name) ⇒ Object

为 PBXFileSystemSynchronizedRootGroup 添加 membershipExceptions 将指定文件从 target 的编译成员中排除(Xcode 16+ 同步组机制)

Parameters:

  • sync_group (PBXFileSystemSynchronizedRootGroup)

    同步根组

  • target (PBXNativeTarget)

    目标 target

  • file_name (String)

    要排除的文件名(如 “Info.plist”)



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 524

def add_membership_exception(sync_group, target, file_name)
    # 查找该 target 已有的 exception set
    existing_exception = sync_group.exceptions.find { |e|
        e.is_a?(Xcodeproj::Project::Object::PBXFileSystemSynchronizedBuildFileExceptionSet) && e.target == target
    }

    if existing_exception
        # 已有 exception set,追加文件(避免重复)
        existing_exception.membership_exceptions ||= []
        unless existing_exception.membership_exceptions.include?(file_name)
            existing_exception.membership_exceptions << file_name
        end
    else
        # 创建新的 PBXFileSystemSynchronizedBuildFileExceptionSet
        exception_set = target.project.new(Xcodeproj::Project::Object::PBXFileSystemSynchronizedBuildFileExceptionSet)
        exception_set.target = target
        exception_set.membership_exceptions = [file_name]
        sync_group.exceptions << exception_set
    end
end

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



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

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

.build_project(project_dir:, icloud_id: nil) ⇒ String?

构建 Xcode 工程的主入口方法

Parameters:

  • project_dir (String)

    工程目录

  • icloud_id (String, nil) (defaults to: nil)

    iCloud ID(可选)

Returns:

  • (String, nil)

    生成的 IPA 文件路径,失败返回 nil



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 666

def build_project(project_dir:, icloud_id: nil)
    project_fullname = Dir.glob(File.join(project_dir, "/*.xcodeproj")).max_by { |f| File.mtime(f) }

    if project_fullname.nil?
        puts "未找到 Xcode 工程文件"
        return nil
    end

    # 修复 Xcode 16 链接器兼容性问题
    fix_xcode16_linker_flags(project_dir: project_dir)

    # 清理旧的 build 目录 (使用绝对路径)
    build_dir = File.join(project_dir, "build")
    FileUtils.rm_rf(build_dir) if File.exist?(build_dir)
    
    # 手动创建输出目录
    FileUtils.mkdir_p(build_dir)

    # 获取构建参数 (传入 output_directory)
    gym_options = get_gym_build_options(
        project_fullname: project_fullname,
        icloud_id: icloud_id,
        output_directory: build_dir
    )

    # 执行构建
    require 'gym'
    config = FastlaneCore::Configuration.create(Gym::Options.available_options, gym_options)
    Gym::Manager.new.work(config)

    # 检测平台类型
    require 'xcodeproj'
    project_obj = Xcodeproj::Project.open(project_fullname)
    project_build_platform = project_obj.root_object.build_configuration_list.get_setting("SDKROOT")["Release"]
    is_macos = !project_build_platform.nil? && project_build_platform.eql?("macosx")

    # 根据平台查找输出文件
    if is_macos
        # macOS 平台查找 .app 文件
        build_path = File.join(build_dir, "*.app")
        output_file = Dir.glob(build_path).select { |f| File.directory?(f) }.max_by { |f| File.mtime(f) }
        puts "macOS 平台: 查找 .app 文件" if output_file
    else
        # iOS 平台查找 .ipa 文件
        build_path = File.join(build_dir, "*.ipa")
        output_file = Dir.glob(build_path).max_by { |f| File.mtime(f) }
        puts "iOS 平台: 查找 .ipa 文件" if output_file
    end

    output_file
end

.delete_libtarget_firebase_shell(project_path) ⇒ Object

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



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

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

.ensure_info_plist_exists(project_dir:, target:, project_obj:) ⇒ String?

确保 Info.plist 文件存在,不存在时自动创建并配置 Xcode 工程仅在需要写入复杂配置(URL Schemes 等)时调用

Parameters:

  • project_dir (String)

    项目目录

  • target (Xcodeproj::Project::Object::PBXNativeTarget)

    Xcode target

  • project_obj (Xcodeproj::Project)

    Xcode 工程对象

Returns:

  • (String, nil)

    Info.plist 文件路径



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 407

def ensure_info_plist_exists(project_dir:, target:, project_obj:)
    # 1. 尝试从 build settings 获取已有路径
    raw_path = target.build_configurations.first.build_settings['INFOPLIST_FILE']
    existing_path = resolve_info_plist_path(project_dir, raw_path, target: target)
    return existing_path if existing_path

    # 2. 检查是否是 GENERATE_INFOPLIST_FILE=YES 的合法模式
    generate_flag = target.build_configurations.first.build_settings['GENERATE_INFOPLIST_FILE']
    is_generated_mode = (generate_flag.to_s.upcase == 'YES') && (raw_path.nil? || raw_path.empty?)

    # 3. 创建 Info.plist(GENERATE_INFOPLIST_FILE 模式下作为补充 plist,Xcode 会合并)
    target_name = target.name.to_s
    # 优先使用原始 INFOPLIST_FILE 路径(用已知变量替换后)创建,避免覆盖工程配置
    if raw_path && !raw_path.empty?
        # 用 resolve 中相同的变量替换逻辑解析路径(不检查文件是否存在)
        resolved_raw = raw_path
            .gsub(/\A["']|["']\z/, '')
            .gsub('$(SRCROOT)', project_dir).gsub('${SRCROOT}', project_dir)
            .gsub('$(PROJECT_DIR)', project_dir).gsub('${PROJECT_DIR}', project_dir)
            .gsub('$(TARGET_NAME)', target_name).gsub('${TARGET_NAME}', target_name)
        product_name = target.build_configurations.first.build_settings['PRODUCT_NAME'] || target_name
        product_name = product_name.gsub('$(TARGET_NAME)', target_name).gsub('${TARGET_NAME}', target_name)
        resolved_raw = resolved_raw.gsub('$(PRODUCT_NAME)', product_name).gsub('${PRODUCT_NAME}', product_name)

        # 如果仍含未解析变量,降级到 target_name/Info.plist
        if resolved_raw.match?(/\$[\({]/)
            relative_plist_path = "#{target_name}/Info.plist"
        elsif File.absolute_path?(resolved_raw)
            # 绝对路径转为相对路径
            relative_plist_path = resolved_raw.sub(/\A#{Regexp.escape(project_dir)}\//, '')
        else
            relative_plist_path = resolved_raw
        end
    else
        relative_plist_path = "#{target_name}/Info.plist"
    end
    info_plist_path = File.join(project_dir, relative_plist_path)

    # 如果文件已存在于磁盘(如 GENERATE_INFOPLIST_FILE 模式下项目自带的 Info.plist)
    if File.exist?(info_plist_path)
        if raw_path.nil? || raw_path.empty?
            # 需要设置 INFOPLIST_FILE,让 ProcessInfoPlistFile 处理我们写入的配置
            # (URL Schemes、Facebook、AdMob、版本号等)
            # 对于 synchronized group,先排除 membership 避免 "Multiple commands produce" 冲突
            plist_dir = File.dirname(relative_plist_path)
            plist_name = File.basename(relative_plist_path)
            sync_group = if plist_dir == "." || plist_dir.empty?
                             project_obj.main_group.find_subpath(target_name, false)
                         else
                             project_obj.main_group.find_subpath(plist_dir, false)
                         end
            if sync_group.is_a?(Xcodeproj::Project::Object::PBXFileSystemSynchronizedRootGroup)
                add_membership_exception(sync_group, target, plist_name)
            end

            target.build_configurations.each do |config|
                config.build_settings['INFOPLIST_FILE'] = relative_plist_path
            end
            project_obj.save
        end
        return info_plist_path
    end

    if is_generated_mode
        Funlog.instance.fancyinfo_warning("Target #{target_name} 使用 GENERATE_INFOPLIST_FILE 模式,创建补充 Info.plist 用于复杂配置")
    else
        Funlog.instance.fancyinfo_warning("Target #{target_name} 未找到 Info.plist,自动创建: #{relative_plist_path}")
    end

    # 3.1 创建目录和最小 Info.plist
    FileUtils.mkdir_p(File.dirname(info_plist_path))
    empty_plist = {}
    Xcodeproj::Plist.write_to_path(empty_plist, info_plist_path)

    # 3.2 将文件添加到 Xcode 工程并设置 INFOPLIST_FILE
    # 必须设置 INFOPLIST_FILE,否则写入的 URL Schemes、Facebook、AdMob 等配置
    # 不会被 ProcessInfoPlistFile 处理,不会进入最终产物
    plist_dir = File.dirname(relative_plist_path)
    plist_name = File.basename(relative_plist_path)
    target_group =
        if plist_dir == "." || plist_dir.empty?
            project_obj.main_group.find_subpath(target_name, true)
        else
            project_obj.main_group.find_subpath(plist_dir, true)
        end

    if target_group.is_a?(Xcodeproj::Project::Object::PBXFileSystemSynchronizedRootGroup)
        # Xcode 16+ synchronized group:文件自动同步到 Copy Bundle Resources,
        # 必须先排除 membership,再设置 INFOPLIST_FILE,避免 "Multiple commands produce" 冲突
        add_membership_exception(target_group, target, plist_name)
        Funlog.instance.fancyinfo_success("已将 Info.plist 从 target 编译成员中排除: #{plist_name}")
    elsif target_group.respond_to?(:files)
        # 传统 PBXGroup:手动添加文件引用
        unless target_group.files.any? { |f| f.path == plist_name } ||
               project_obj.files.any? { |f| f.path == relative_plist_path }
            target_group.new_file(plist_name)
            Funlog.instance.fancyinfo_success("已将 Info.plist 添加到 Xcode 工程引用: #{relative_plist_path}")
        end
    end

    # 设置 INFOPLIST_FILE,让 ProcessInfoPlistFile 处理此文件
    target.build_configurations.each do |config|
        config.build_settings['INFOPLIST_FILE'] = relative_plist_path
        # GENERATE_INFOPLIST_FILE 模式下保持 YES,Xcode 会合并 build settings 和 plist 文件
        config.build_settings['GENERATE_INFOPLIST_FILE'] ||= 'YES'
    end

    project_obj.save

    info_plist_path
end

.fix_xcode16_linker_flags(project_dir: nil) ⇒ Object

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



583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 583

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 26 链接器兼容性配置".green
        end

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

.get_gym_build_options(project_fullname:, icloud_id: nil, output_directory: nil) ⇒ Hash

获取 Gym 构建参数

Parameters:

  • project_fullname (String)

    工程文件完整路径

  • icloud_id (String, nil) (defaults to: nil)

    iCloud ID(可选)

  • output_directory (String) (defaults to: nil)

    输出目录绝对路径

Returns:

  • (Hash)

    Gym 构建参数



723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 723

def get_gym_build_options(project_fullname:, icloud_id: nil, output_directory: nil)
    if project_fullname.nil?
        raise "请指定要编译的工程"
    end

    project_path = File.dirname(project_fullname)
    proj_name = File.basename(project_fullname, ".xcodeproj")
    
    # 使用绝对路径
    project_fullname_abs = File.expand_path(project_fullname)
    project_path_abs = File.dirname(project_fullname_abs)
    
    project_obj = Xcodeproj::Project.open(project_fullname)

    project_build_platform = project_obj.root_object.build_configuration_list.get_setting("SDKROOT")["Release"]
    main_target = project_obj.targets.select { |target| target.respond_to?(:product_type) && target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application]) }.first
    provisioning_profile_name = main_target.build_configurations.first.build_settings['PROVISIONING_PROFILE_SPECIFIER'].downcase

    # 确定构建类型和 iCloud 环境
    build_type = "app-store"
    icloud_env = "Production"

    if !project_build_platform.nil? && project_build_platform.eql?("macosx")
        # macOS 平台
        if provisioning_profile_name.downcase.include?("development")
            build_type = "development"
            icloud_env = "Development"
        elsif provisioning_profile_name.downcase.include?("appstore")
            build_type = "mac-application"
            icloud_env = "Production"
        else
            build_type = "developer-id"
            icloud_env = "Production"
        end
    else
        # iOS 平台
        if provisioning_profile_name.downcase.include?("adhoc")
            build_type = "ad-hoc"
            icloud_env = "Development"
        elsif provisioning_profile_name.downcase.include?("development")
            build_type = "development"
            icloud_env = "Development"
        else
            build_type = "app-store"
            icloud_env = "Production"
        end
    end

    # 注意:Xcode 26+ 的 xcodebuild 会提示 "development" 已废弃建议用 "debugging",
    # 但 fastlane gym 的 export_method 白名单尚未支持 "debugging",继续使用 "development"

    # 检查 Unity 测试模板
    if build_type.eql?("app-store") || build_type.eql?("ad-hoc")
        if File.exist?(File.join(project_path, "Unity/Data/Raw/SettingsPluginFlag.txt"))
            raise Informative, "Unity代码包含测试模板,请重新导出Unity代码"
        end
    end

    # 获取应用名称
    app_ipaname_temp = main_target.build_configurations.first.build_settings['INFOPLIST_KEY_CFBundleDisplayName'] || main_target.display_name
    app_ipaname_temp = app_ipaname_temp.gsub(/ /, '')
    app_ipaname_temp = app_ipaname_temp.gsub(/\'/, '')

    # 确保输出目录是绝对路径
    output_directory ||= File.join(project_path_abs, "build")
    output_directory = File.expand_path(output_directory)

    # 构建基本参数
    values = {
        # 虽然 gym 支持 absolute path,但是为了保险起见,如果提供了 abs path project_fullname,
        # 我们这里还是传 relative 给 gym,但是我们在调用 gym 之前不 chdir。
        # 实际上 gym 的 behavior: 如果 project 是 relative,它相对于 cwd。
        # 所以我们必须给 gym 传入 ABSOLUTE PATH 的 project 才能避免 chdir 问题。
        project: project_fullname_abs,
        scheme: "#{proj_name}",
        configuration: "Release",
        clean: true,
        export_method: build_type,
        output_directory: output_directory,
        output_name: "#{app_ipaname_temp}.ipa"
    }

    # macOS 平台特殊处理
    if !project_build_platform.nil? && project_build_platform.eql?("macosx")
        values[:output_name] = nil
        values[:destination] = "generic/platform=macosx"
        # macOS 应用不需要导出 .ipa/.pkg,跳过打包步骤
        values[:skip_package_ipa] = true
        values[:skip_package_pkg] = true
    end

    # 如果使用 CocoaPods
    if File.exist?(File.join(project_path, "Podfile"))
        values[:workspace] = File.join(project_path_abs, "#{proj_name}.xcworkspace")
        values[:project] = nil
    end

    # 导出选项
    values[:export_options] ||= {}
    values[:export_options][:compileBitcode] = false
    values[:export_options][:stripSwiftSymbols] = true
    values[:export_options][:uploadSymbols] = false

    if !icloud_id.nil?
        values[:export_options][:iCloudContainerEnvironment] = icloud_env
    end

    # 从主工程显式读取 provisioning profile 映射,避免 gym 自动扫描 workspace
    # 时被其他工程(如 Unity-iPhone)中的旧 PROVISIONING_PROFILE UUID 污染
    provisioning_profiles = {}
    project_obj.targets.each do |target|
        next unless target.respond_to?(:product_type)

        release_config = target.build_configurations.find { |config| config.name == 'Release' } || target.build_configurations.first
        next if release_config.nil?

        bundle_id = release_config.resolve_build_setting('PRODUCT_BUNDLE_IDENTIFIER')
        bundle_id ||= release_config.build_settings['PRODUCT_BUNDLE_IDENTIFIER']

        # 跳过无效的 Bundle ID(空值、未解析变量、通配符)
        next if bundle_id.nil? || bundle_id.to_s.empty?
        next if bundle_id.to_s.include?('$(') || bundle_id.to_s.include?('*')

        # 优先读取 SDK 特定的配置,再回退到通用配置
        profile_specifier = release_config.build_settings['PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]']
        profile_specifier ||= release_config.build_settings['PROVISIONING_PROFILE_SPECIFIER']

        next if profile_specifier.nil? || profile_specifier.to_s.empty?

        provisioning_profiles[bundle_id.to_s] = profile_specifier.to_s
    end

    if !provisioning_profiles.empty?
        values[:export_options][:provisioningProfiles] = provisioning_profiles
    end

    values
end

.get_target_name_mapObject

获取扩展 Target 名称后缀到 config key 的映射注意:MainTarget 不在此映射中,主应用 target 直接使用 app_identifier



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 118

def get_target_name_map
    return {
        "Content" => "bundle_id_pushcontent",
        "Service" => "bundle_id_pushservice",
        "Keyboard" => "bundle_id_keyboard",
        "iMessage" => "bundle_id_imessage",
        "Siri" => "bundle_id_siri",
        "SiriUI" => "bundle_id_siriui",
        "Widget" => "bundle_id_widget",
        "Extension" => "bundle_id_extension",
        "ExtensionAd" => "bundle_id_extensionad",
        "ExtensionPorn" => "bundle_id_extensionporn",
        "WatchApp" => "bundle_id_watchapp",
        "WatchAppComplication" => "bundle_id_watchapp_extension"
    }
end

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



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

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.respond_to?(:product_type) && 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



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 545

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)

    project_obj.targets.each do |target|
        # 跳过非 native target(如 PBXAggregateTarget)
        next unless target.respond_to?(:product_type)

        is_app_target = target.product_type.to_s.eql?("com.apple.product-type.application")

        # 主应用 target:确保 Info.plist 存在(需要写入 URL Schemes 等复杂配置)
        # 非主应用 target:仅在已有 Info.plist 时修改,不强制创建
        if is_app_target
            info_plist_path = ensure_info_plist_exists(
                project_dir: project_dir,
                target: target,
                project_obj: project_obj
            )
        else
            raw_path = target.build_configurations.first.build_settings['INFOPLIST_FILE']
            info_plist_path = resolve_info_plist_path(project_dir, raw_path, target: target)
        end

        next if info_plist_path.nil?

        if is_app_target
            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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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/pindo/module/xcode/xcode_build_helper.rb', line 200

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



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
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
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 261

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



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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/pindo/module/xcode/xcode_build_helper.rb', line 135

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|
        # 跳过非 native target(如 PBXAggregateTarget)
        next unless target.respond_to?(:product_type)

        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_name_map = get_target_name_map

        # 根据 target 类型确定 bundle_id
        target_bundle_id = nil
        if target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application])
            # 主应用 target:使用 app_identifier
            target_bundle_id = config_json['app_info']['app_identifier']
        else
            # 扩展 target:从 target_name_map 查找对应的 bundle_id
            # target_name_map 的 value 是 bundle_id_* 格式,config_json 中实际字段名是 app_identifier_*
            target_name_map.each do |suffix, config_key|
                if target.name.to_s.end_with?(suffix)
                    json_key = config_key.sub('bundle_id_', 'app_identifier_')
                    target_bundle_id = config_json['app_info'][json_key]
                    break
                end
            end
        end

        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']
            if target_bundle_id && !target_bundle_id.empty? && !target_bundle_id.include?("*")
                config.build_settings['PRODUCT_BUNDLE_IDENTIFIER'] = target_bundle_id
            end
        end

        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
        else
            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



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

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

.resolve_info_plist_path(project_dir, raw_path, target: nil) ⇒ String?

解析 INFOPLIST_FILE 路径,处理 Xcode 变量和引号

Parameters:

  • project_dir (String)

    项目目录

  • raw_path (String)

    INFOPLIST_FILE 原始值

  • target (Xcodeproj::Project::Object::PBXNativeTarget, nil) (defaults to: nil)

    target(用于解析 TARGET_NAME 等变量)

Returns:

  • (String, nil)

    解析后的绝对路径,文件不存在时返回 nil



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

def resolve_info_plist_path(project_dir, raw_path, target: nil)
    return nil if raw_path.nil? || raw_path.empty?

    # 去除首尾引号
    resolved = raw_path.gsub(/\A["']|["']\z/, '')

    # 解析 Xcode 变量
    resolved = resolved
        .gsub('$(SRCROOT)', project_dir)
        .gsub('${SRCROOT}', project_dir)
        .gsub('$(PROJECT_DIR)', project_dir)
        .gsub('${PROJECT_DIR}', project_dir)

    # 解析 target 相关变量
    if target
        target_name = target.name.to_s
        resolved = resolved
            .gsub('$(TARGET_NAME)', target_name)
            .gsub('${TARGET_NAME}', target_name)

        # PRODUCT_NAME 从 build settings 获取,降级到 TARGET_NAME
        product_name = target.build_configurations.first.build_settings['PRODUCT_NAME'] || target_name
        # PRODUCT_NAME 本身可能是 $(TARGET_NAME),递归替换
        product_name = product_name
            .gsub('$(TARGET_NAME)', target_name)
            .gsub('${TARGET_NAME}', target_name)
        resolved = resolved
            .gsub('$(PRODUCT_NAME)', product_name)
            .gsub('${PRODUCT_NAME}', product_name)
    end

    # 如果仍有未解析的变量,尝试去掉变量部分做 glob 匹配
    if resolved.match?(/\$[\({]/)
        # 将未解析的变量替换为通配符,尝试 glob 查找
        glob_pattern = resolved.gsub(/\$[\({][^)\}]+[\)}]/, '*')
        glob_path = File.absolute_path?(glob_pattern) ? glob_pattern : File.join(project_dir, glob_pattern)
        matches = Dir.glob(glob_path)
        return matches.first if matches.size == 1
        if matches.size > 1
            Funlog.instance.fancyinfo_warning("INFOPLIST_FILE 变量解析后匹配到多个文件: #{matches.join(', ')},跳过")
        end
        return nil
    end

    # 如果是相对路径,拼接 project_dir
    abs_path = File.absolute_path?(resolved) ? resolved : File.join(project_dir, resolved)
    File.exist?(abs_path) ? abs_path : nil
end