cocoapods源码5.4 - command/install.rb

1、cocoapods/lib/cocoapods/command/update.rb

module Pod
  class Command
    class Update < Command
      include RepoUpdate
      include ProjectDirectory

      self.summary = 'Update outdated project dependencies and create new ' \
        'Podfile.lock'

      self.description = <<-DESC
        Updates the Pods identified by the specified `POD_NAMES`, which is a
        space-delimited list of pod names. If no `POD_NAMES` are specified, it
        updates all the Pods, ignoring the contents of the Podfile.lock. This
        command is reserved for the update of dependencies; pod install should
        be used to install changes to the Podfile.
      DESC

      # 命令行参数:pod update [POD_NAMES] 
      self.arguments = [
        CLAide::Argument.new('POD_NAMES', false, true),
      ]
      
      # 命令行选项:
      # - 1) --sources
      # - 2) --exclude-pods
      def self.options
        [
          ['--sources=https://github.com/artsy/Specs,master', 'The sources from which to update dependent pods. ' \
           'Multiple sources must be comma-delimited. The master repo will not be included by default with this option.'],
          ['--exclude-pods=podName', 'Pods to exclude during update. Multiple pods must be comma-delimited.'],
        ].concat(super)
      end

      def initialize(argv)
        # puts argv.arguments
        # ➜  App pod update aa bb cc
        # aa
        # bb
        # cc
        # @pods = ["aa", "bb", "cc"]
        # ➜  App
        # 
        # argv.arguments!() 读取完参数后,会从数组中删除,后续则获取不到了
        # 
        
        # 1、获取 `pod update aa bb cc` 后的 [aa, bb, cc],即拿到要更新的所有的 pod_name
        @pods = argv.arguments! unless argv.arguments.empty?

        # 2、获取选项 --sources=https://github.com/artsy/Specs,master
        source_urls = argv.option('sources', '').split(',')

        # 3、获取选项 --exclude-pods=podname1,podname2,podname3
        excluded_pods = argv.option('exclude-pods', '').split(',')

        # 4、计算得到需要更新的所有pod_name,使用 @pods 对象成员数组保存
        unless source_urls.empty?
          # 4.1、获取每一个source_urls[i]对应的 pod_name
          # - 1)[1, 2, 3, 4].flat_map { |e| [e, -e] } #=> [1, -1, 2, -2, 3, -3, 4, -4]
          # - 2)[[1, 2], [3, 4]].flat_map { |e| e + [100] } #=> [1, 2, 100, 3, 4, 100]
          source_pods = source_urls.flat_map { |url| 
            config.sources_manager.source_with_name_or_url(url).pods
          }

          # 4.2、
          unless source_pods.empty?
            # 4.2.1、过滤出 Podfile.lock 中包含的 pod_name
            source_pods = source_pods.select { |pod| 
              config.lockfile.pod_names.include?(pod) 
            }

            # 4.2.2、
            if @pods
              # 命令行传入的需要更新的pod_name,添加上Podfile.lock中记录的pod_name
              @pods += source_pods 
            else
              # 命令行【没有】传入需要更新的pod_name,则直接赋值为Podfile.lock中记录的pod_name
              @pods = source_pods unless source_pods.empty? 
            end
          end
        end

        # 5、如果存在需要【禁止更新】的 pod_name 
        unless excluded_pods.empty?
          # 5.1、如果 @pods 没有值
          @pods ||= config.lockfile.pod_names.dup

          # 5.2、获取【没有被pod安装】的 pod_name
          non_installed_pods = (excluded_pods - @pods)

          # 5.3、报错提示:对一些没有被pod安装的 pod_name 进行更新
          unless non_installed_pods.empty?
            pluralized_words = non_installed_pods.length > 1 ? %w(Pods are) : %w(Pod is)
            message = "Trying to skip `#{non_installed_pods.join('`, `')}` #{pluralized_words.first} " \
                    "which #{pluralized_words.last} not installed"
            raise Informative, message
          end
          
          # 5.4、从 @pods 数组中,删除没有被pod安装的 pod_name
          @pods.delete_if { |pod| excluded_pods.include?(pod) }
        end

        super
      end

      # Check if all given pods are installed
      # 检查所有需要更新的pod_name,是否已经被pod安装过,
      # 如果没有被安装过的,则不能执行更新,则报错提示
      def verify_pods_are_installed!
        # 1、
        lockfile_roots = config.lockfile.pod_names.map { |p| Specification.root_name(p) }

        # 2、
        missing_pods = @pods.map { |p| Specification.root_name(p) }.select do |pod|
          !lockfile_roots.include?(pod)
        end
        
        # 3、
        unless missing_pods.empty?
          message = if missing_pods.length > 1
                      "Pods `#{missing_pods.join('`, `')}` are not " \
                          'installed and cannot be updated'
                    else
                      "The `#{missing_pods.first}` Pod is not installed " \
                          'and cannot be updated'
                    end
          raise Informative, message
        end
      end

      def run
        # 1、检查给出更新的pod_name中,是否都已经被pod安装
        verify_podfile_exists!
        
        # 2、
        installer = installer_for_config
        # installer = installer_for_config()

        # 3、默认更新 pod repo 
        installer.repo_update = repo_update?(:default => true)

        # 4、设置 update 标记位
        if @pods
          verify_lockfile_exists!
          verify_pods_are_installed!
          installer.update = { :pods => @pods }
        else
          UI.puts 'Update all pods'.yellow
          installer.update = true
        end

        # 5、install!() 
        installer.install!
      end
    end
  end
end

2、pod update

  • 1、pod update [podname1,podname2,podname3]
  • 2、pod update --sources=https://github.com/artsy/Specs,master
  • 3、pod update --exclude-pods=podname1,podname2,podname3

你可能感兴趣的:(cocoapods源码5.4 - command/install.rb)