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

.deintegrate_and_install(project_dir) ⇒ Object

清理并重新安装 CocoaPods

Parameters:

  • project_dir (String)

    项目目录



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

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

  Dir.chdir(project_dir) do
    begin
      # 清理现有 Pods
      FileUtils.rm_rf("Podfile.lock") if File.exist?("Podfile.lock")
      FileUtils.rm_rf("Pods") if File.exist?("Pods")

      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)

    是否成功



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

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

.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)

    是否成功



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

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/pindo/module/xcode/cocoapods_helper.rb', line 88

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