Class: Pindo::CocoaPodsHelper

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

Overview

CocoaPods 操作辅助类封装 CocoaPods 相关的操作,包括:

  • 更新私有索引库

  • 同步 podspec

  • 执行 pod install

Class Method Summary collapse

Class Method Details

.backup_podfile_lock(project_dir, app_config_dir, app_version) ⇒ Boolean

备份 Podfile.lock 到配置仓库

Parameters:

  • project_dir (String)

    项目目录

  • app_config_dir (String)

    应用配置目录

  • app_version (String)

    应用版本号

Returns:

  • (Boolean)

    是否成功



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
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/pindo/module/xcode/cocoapods_helper.rb', line 178

def backup_podfile_lock(project_dir, app_config_dir, app_version)
  begin
    proj_pod_file = File.join(project_dir, "Podfile.lock")
    unless File.exist?(proj_pod_file)
      puts "Podfile.lock 不存在,跳过备份"
      return false
    end

    # 复制 Podfile.lock 到配置仓库
    FileUtils.cp(proj_pod_file, File.join(app_config_dir, "Podfile.lock"))
    Pindo::GitHandler.git_addpush_repo(
      path: app_config_dir,
      message: "#{app_version} backup podfile.lock",
      commit_file_params: ["Podfile.lock"]
    )

    # 计算 Podfile.lock 的 MD5 checksum
    bytes = File.binread(proj_pod_file)
    checksum = Digest::MD5.hexdigest(bytes)

    # 更新 build_verify.json
    build_verify_file = File.join(app_config_dir, "build_verify.json")
    build_verify_json = {}
    if File.exist?(build_verify_file)
      begin
        build_verify_json = JSON.parse(File.read(build_verify_file))
      rescue => error
        # 忽略解析错误,使用空 hash
      end
    end

    build_verify_json["output_code_commit"] = Pindo::GitHandler.git_latest_commit_id(local_repo_dir: project_dir)
    build_verify_json["output_config_commit"] = Pindo::GitHandler.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

    Pindo::GitHandler.git_addpush_repo(
      path: app_config_dir,
      message: "backup #{app_version} output info",
      commit_file_params: ["build_verify.json"]
    )

    puts "✅ 已备份 Podfile.lock 到配置仓库"
    true
  rescue => error
    raise Informative, "保存 Podfile.lock 文件失败: #{error.message}"
  end
end

.copy_podfile_lock_from_config(project_dir, app_config_dir) ⇒ Boolean

从配置仓库拷贝 Podfile.lock 到项目目录

Parameters:

  • project_dir (String)

    项目目录

  • app_config_dir (String)

    应用配置目录

Returns:

  • (Boolean)

    是否成功



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/pindo/module/xcode/cocoapods_helper.rb', line 153

def copy_podfile_lock_from_config(project_dir, app_config_dir)
  begin
    config_pod_file = File.join(app_config_dir, "Podfile.lock")
    unless File.exist?(config_pod_file)
      puts "配置仓库中不存在 Podfile.lock,跳过拷贝"
      return false
    end

    # 拷贝 Podfile.lock 到项目目录
    project_pod_file = File.join(project_dir, "Podfile.lock")
    FileUtils.cp(config_pod_file, project_pod_file)

    puts "✅ 已从配置仓库拷贝 Podfile.lock"
    true
  rescue => error
    puts "拷贝 Podfile.lock 失败: #{error.message}"
    false
  end
end

.deintegrate_and_install(project_dir, clean_podfile_lock: true) ⇒ Object

清理并重新安装 CocoaPods

Parameters:

  • project_dir (String)

    项目目录

  • clean_podfile_lock (Boolean) (defaults to: true)

    是否在安装前删除 Podfile.lock,默认为 true



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

def deintegrate_and_install(project_dir, clean_podfile_lock: true)
  return unless File.exist?(File.join(project_dir, "Podfile"))

  Dir.chdir(project_dir) do
    begin
      # 如果需要,删除 Podfile.lock
      if clean_podfile_lock && File.exist?("Podfile.lock")
        FileUtils.rm_rf("Podfile.lock")
        puts "已删除 Podfile.lock"
      end

      # 删除 Pods 目录
      if File.exist?("Pods")
        FileUtils.rm_rf("Pods")
      end

      puts "正在执行 pod deintegrate..."
      system 'pod deintegrate'

      puts "正在执行 pod install..."
      system 'pod install'

      # 检查 pod install 是否成功
      unless File.exist?("Podfile.lock")
        raise "pod install 失败!!先 pod install 完成后再编译!"
      end
    rescue => error
      puts error.to_s
      raise "pod install 失败!!先 pod install 完成后再编译!"
    end
  end
end

.install_pods(project_dir) ⇒ Boolean

执行 pod install

Parameters:

  • project_dir (String)

    项目目录

Returns:

  • (Boolean)

    是否成功



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pindo/module/xcode/cocoapods_helper.rb', line 42

def install_pods(project_dir)
  podfile_path = File.join(project_dir, "Podfile")
  return false unless File.exist?(podfile_path)

  begin
    puts "正在执行 pod install..."
    Dir.chdir(project_dir) do
      Pod::Command::Install.run(['--clean-install'])
    end
    true
  rescue => e
    puts "Pod install 失败: #{e.message}"
    false
  end
end

.remove_test_pods(project_dir) ⇒ Boolean

移除测试相关的 Pod 模块

Parameters:

  • project_dir (String)

    项目目录

Returns:

  • (Boolean)

    是否成功



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

def remove_test_pods(project_dir)
  pod_file = File.join(project_dir, "Podfile")
  return false unless File.exist?(pod_file)

  begin
    # 测试插件列表
    test_plugins = [
      "FancySettingsPlugin",
      "TYSettingsPlugin",
      "CSSettingsPlugin",
      "FunnySettingsPlugin"
    ]

    # 使用 sed 命令删除测试插件
    test_plugins.each do |plugin_name|
      command = "sed -i \"\" \"/.*#{plugin_name}.*/d\" #{pod_file}"
      system command
    end

    puts "已移除测试 Pod 模块"
    true
  rescue => e
    puts "移除测试 Pod 模块失败: #{e.message}"
    false
  end
end

.update_pod_repo(install: false, project_dir: nil) ⇒ Boolean

更新私有 Pod 索引库

Parameters:

  • install (Boolean) (defaults to: false)

    是否执行 pod install

  • project_dir (String) (defaults to: nil)

    项目目录,默认为当前目录

Returns:

  • (Boolean)

    是否成功



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pindo/module/xcode/cocoapods_helper.rb', line 21

def update_pod_repo(install: false, project_dir: nil)
  project_dir ||= Dir.pwd

  # 获取私有 Pod 索引地址
  pod_index_url = get_private_pod_index_url
  raise "私有Pod索引地址未知!!" if pod_index_url.nil?

  # 更新私有索引库
  update_private_repo(pod_index_url)

  # 如果需要,执行 pod install
  if install
    install_pods(project_dir)
  end

  true
end

.using_pods?(project_dir, pod_names) ⇒ Boolean

检查是否使用了特定的 Pod

Parameters:

  • project_dir (String)

    项目目录

  • pod_names (Array<String>)

    Pod 名称列表

Returns:

  • (Boolean)

    是否使用了任一指定的 Pod



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

def using_pods?(project_dir, pod_names)
  pod_lock_file = File.join(project_dir, "Podfile.lock")
  return false unless File.exist?(pod_lock_file)

  begin
    pod_lock_json = Pod::YAMLHelper.load_file(pod_lock_file)
    pods_array = []

    if !pod_lock_json["SPEC REPOS"].nil?
      pod_lock_json["SPEC REPOS"].each do |source, pod_items|
        pods_array += pod_items
      end
    end

    pod_names.any? { |name| pods_array.include?(name) }
  rescue => e
    puts "检查 Podfile.lock 失败: #{e.message}"
    false
  end
end