Module: CocoapodsSourceFix

Defined in:
lib/cocoapods-source-fix.rb,
lib/cocoapods-source-fix/rule.rb,
lib/cocoapods-source-fix/logger.rb,
lib/cocoapods-source-fix/runner.rb,
lib/cocoapods-source-fix/version.rb,
lib/cocoapods-source-fix/configuration.rb

Defined Under Namespace

Modules: Logger Classes: Configuration, Rule, Runner

Constant Summary collapse

PLUGIN_NAME =
'cocoapods-source-fix'
VERSION =
'0.1.3'

Class Method Summary collapse

Class Method Details

.apply_installer_hook!Object

安装 monkey-patch:拦截 Pod::Installer#install!,在其执行完成后运行替换。 与 cocoapods-local-override 相同的机制——绕过 HooksManager 的 whitelist (whitelist 只放行 Podfile 中 plugin 声明的 hook),因此无需修改 Podfile。



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cocoapods-source-fix.rb', line 31

def apply_installer_hook!
  return if @installer_hook_applied
  @installer_hook_applied = true

  Pod::Installer.class_eval do
    unless method_defined?(:source_fix_original_install!)
      alias_method :source_fix_original_install!, :install!

      define_method :install! do |*args|
        source_fix_original_install!(*args)
        CocoapodsSourceFix.run_post_install(self)
      end
    end
  end
end

.run_post_install(installer) ⇒ Object

install! 完成后执行源码替换(加载即生效,无需 Podfile 声明 plugin)



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cocoapods-source-fix.rb', line 10

def run_post_install(installer)
  podfile_dir = podfile_dir_from(installer)
  config_path = Configuration.locate(podfile_dir)
  unless config_path
    Logger.info "未找到配置文件(#{Configuration::ENV_KEY}#{Configuration::CONFIG_FILE_NAME}),跳过替换"
    return
  end

  config = Configuration.new(config_path, podfile_dir: podfile_dir)
  Logger.info "使用配置文件:#{config_path}#{config.rules.size} 条规则)"
  # Runner 依赖 sandbox 定位 pod 目录,从 installer 实例提取并包装成轻量 context
  context = OpenStruct.new(
    sandbox: installer.sandbox,
    sandbox_root: installer.sandbox.root.to_s
  )
  Runner.new(context, config, podfile_dir: podfile_dir).run
end