iOS工程09Cocoapods二进制组件化

1,845 阅读1分钟

一、插件

  1. header 搜索目录依次(源文件所在目录-->HEADER_SEARCH_PATHS/USER_HEADER_SEARCH_PATHS)
  2. 第一次搜索源文件所在目录--就进行挂载了hmap
  3. hmap就是在第一次挂载的时候就把对import路径->映射到源文件所在的目录地址上
  4. hmap映射资源的前提是,要有modulemap文件。

创建插件:

第一步:
pod plugins create cocoapods-ning-bin

第二步:
用VSCode打开:cocoapods-ning-bin

第三步:
配置:cocoapods-ning-bin.gemspec
添加依赖   spec.add_runtime_dependency 'cocoapods-project-gen'

第四步:
Gemfile 删除  gem 'cocoapods' 依赖 cocoapods-project-gen 已经依赖了

第五步:
来到终端执行  bundler install  //'bundler', '~> 1.3'

配置运行完成后:

进行调试工程

第六步:
配置launch.json 
"debuggerPort": "1235",
// ${workspaceRoot} = .vscode
// 调试插件 --》gems 2. 执行 --》cocoapods_plugin
"cwd": "${workspaceRoot}/cocoapods-ning-bin",
// cwd + bin/test-plugin = exec
"program": "bin/test-plugin",

第七步:
配置调试 test-plugin 命令:
if $PROGRAM_NAME == __FILE__
  ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', __dir__)
  require 'bundler/setup'
end

require 'cocoapods'

project_directory = File.expand_path('../LGApp')
# Pod::Command.run = pod
# Pod::Command.run(['install' = pod install
# pod install --project-directory='../LGApp'
Pod::Command.run(['install', "--project-directory=#{project_directory}"])

第八步:
配置 podfile:
platform :ios, '13.0'

source Pod::Installer::MASTER_SPECS_REPO_GIT_URL

plugin 'cocoapods-cat-bin'

target 'LGApp' do
  # use_frameworks!

  pod 'AFNetworking'
  pod 'SDWebImage'
  # pod 'GoogleUtilities'
  # pod 'Kingfisher'
  #  pod 'Texture'
  #  pod 'Releam'
end

# hook
# podfile hook 先, pod_targets
# plugin: 后, 拿不到有用的信息podspec,
# post_install do |installer|
#   ps = installer.pod_targets.map do |target|
#     # root_spec --> podpspec --> file
#     # podspec --> Spec
#     target.root_spec.defined_in_file
#   end
#   p ps
# end

编写插件:

第九步:编写插件
require 'cocoapods-ning-bin/command'
require 'cocoapods-project-gen'

# cocoapods_plugin.rb -->
# 头文件
# plugin hook
# 调试工程
module Pod
  class Installer
    # 先拿到原先的实现,交换
    # run_plugins_post_install_hooks起了一个别名 cat_run_plugins_pre_install_hooks
    alias cat_run_plugins_pre_install_hooks run_plugins_post_install_hooks
    def run_plugins_post_install_hooks
        CocoapodsNingBinHook.install = self
      cat_run_plugins_pre_install_hooks
    end
  end

  module CocoapodsNingBinHook
    # 类
    # CocoapodsNingBinHook --> 类 --》类属性install(get set)
    class << self
      attr_accessor :install
    end
    # cocoapods 内置的module 名称
    # CocoapodsNingBinHook类
    @hooks_manager = Pod::HooksManager

    @hooks_manager.register('cocoapods-ning-bin', :post_install) do |_context, _option|
      product_type = CocoapodsNingBinHook.install.pod_targets[0].product_type
      specs = CocoapodsNingBinHook.install.pod_targets.map(&:root_spec).map do |spec|
        spec.defined_in_file
      end
      arg = if product_type == :static_library
              '--use-libraries'
            elsif product_type == :static_framework
              '--use-static-frameworks'
            end

      #   generator = ProjectGen::ProjectGenerator.new(specs[0], [Pod::Installer::MASTER_SPECS_REPO_GIT_URL])
      #   generator.local          = false
      #   generator.no_clean       = true
      #   generator.allow_warnings = true
      #   generator.no_subspecs    = false
      #   generator.only_subspec   = nil
      #   generator.use_frameworks = false
      #   generator.use_modular_headers = true
      #   generator.use_static_frameworks = false
      #   generator.external_podspecs = specs.drop(1)
      #   xc_gen = ProjectGen::XcframeworkGen.new(generator)
      #   out = xc_gen.generate_xcframework(File.expand_path('../output', __dir__), build: @build)
      # hash [podspec: 产物目录]
      vs = ProjectGen::Command.run(['gen', '--no-local', "--output-dir=#{File.expand_path('../output', __dir__)}",
                                    '--sources=https://github.com/CocoaPods/Specs.git', *specs])
     # 1. podspec --》 json --》二进制
     # 2. zip
     # 3. upload -->
     # podfile --> pod 方法 --》option 
     vs.each_pair do |_key, value|
        zip_path = value.join("#{value.basename}.zip")
        ProjectGen::Utils.zip(value, zip_path)
    end
    end
  end
end

# Installer podfile --> pod target --> xc build product type podspec
# hook plugin --> project/podspecs -->  product type
# @hooks_manager.register('cocoapods-cat-bin', :pre_install) do |_, _options|
#     p 'pre_install'
#   end
#   # source_provider --> cocoapods依赖的source
#   # source_provider --> 私有source返回出去 --》
#   @hooks_manager.register('cocoapods-cat-bin', :source_provider) do |_context, _options|
#     p 'source_provider'
#     # 返回私有
#     # source = Pod::Source.new(Pod::Installer::MASTER_SPECS_REPO_GIT_URL)
#     # context.add_source(source)
#     # 私有返回出
#   end

打印输出环境变量:

Ruby

printf "\n\"env\": {\n \"PATH\": \"$PATH\",\n \"GEM_HOME\": \"$GEM_HOME\",\n \"GEM_PATH\": \"$GEM_PATH\",\n \"RUBY_VERSION\": \"$RUBY_VERSION\"\n}\n\n"

这里用到了cocoapods-project-gen 插件库,github上有。

可能遇到的问题?

SDWebImage 由于网络问题无法载入,手动配置输出到output-dir

xcframework gen --no-local --sources="https://github.com/CocoaPods/Specs.git" --use-libraries --sources=https://github.com/CocoaPods/Specs.git  --output-dir="./mm" /Users/ws/xxxxxxxxxx/SDWebImage.podspec.json
-> SDWebImage

中间error好几次,然后突然可以了。

image.png

image.png