美文网首页
cocoapods源码3 - command.rb

cocoapods源码3 - command.rb

作者: xiongzenghui | 来源:发表于2018-08-11 20:28 被阅读4次

1、CocoaPods/lib/cocoapods/command.rb

require 'colored2'
require 'claide'
require 'molinillo/errors'

module Molinillo
  class ResolverError
    include CLAide::InformativeError
  end
end

# 扩展Pod模块
module Pod
  # 1、获取出错信息
  class PlainInformative
    include CLAide::InformativeError
  end

  # 2、使用第三方命令行参数解析库CLAide
  class Command < CLAide::Command
    # 2.1、包含rb中定义的模块,用于让Pod::Command::xxx 包含
    # - 1)--no-repo-update
    # - 2)--project-directory=/project/dir/
    require 'cocoapods/command/options/repo_update'
    require 'cocoapods/command/options/project_directory'
    include Options

    # 2.2、添加其他的 child command
=begin
    ➜  ~ pod
Usage:

    $ pod COMMAND

      CocoaPods, the Cocoa library package manager.

Commands:

    + cache         Manipulate the CocoaPods cache
    + deintegrate   Deintegrate CocoaPods from your project
    + env           Display pod environment
    + init          Generate a Podfile for the current directory
    + install       Install project dependencies according to versions from a
                    Podfile.lock
    + ipc           Inter-process communication
    + lib           Develop pods
    + list          List pods
    + outdated      Show outdated project dependencies
    + plugins       Show available CocoaPods plugins
    + repo          Manage spec-repositories
    + search        Search for pods
    + setup         Setup the CocoaPods environment
    + spec          Manage pod specs
    + trunk         Interact with the CocoaPods API (e.g. publishing new specs)
    + try           Try a Pod!
    + update        Update outdated project dependencies and create new Podfile.lock
=end
    require 'cocoapods/command/cache'
    require 'cocoapods/command/env'
    require 'cocoapods/command/init'
    require 'cocoapods/command/install'
    require 'cocoapods/command/ipc'
    require 'cocoapods/command/lib'
    require 'cocoapods/command/list'
    require 'cocoapods/command/outdated'
    require 'cocoapods/command/repo'
    require 'cocoapods/command/setup'
    require 'cocoapods/command/spec'
    require 'cocoapods/command/update'

    # 2.3、抽象command(后面必须带 child command)
    self.abstract_command = true

    # 2.4、command 名字
    self.command = 'pod'

    # 2.5、command 描述信息
    self.version = VERSION
    self.description = 'CocoaPods, the Cocoa library package manager.'

    # 2.6、设置要加载的 plugin rb 文件
    # self.plugin_prefixes = ['claide', 'cocoapods']
    # - 1)claide_plugin.rb
    # - 2)cocoapods_plugin.rb
    self.plugin_prefixes = %w(claide cocoapods)

    # 2.7、添加 pod --silent 选项
    def self.options
      [
        ['--silent', 'Show nothing'],
      ].concat(super)
    end

    # 2.8、解析传入的命令行参数
    def self.run(argv)
      # 1. 权限检查、git版本检查、xcode license检查
      help! 'You cannot run CocoaPods as root.' if Process.uid == 0
      verify_minimum_git_version!
      verify_xcode_license_approved!

      # 2. CLAide::Command 构建命令行
      super(argv)
    ensure
      UI.print_warnings
    end

    # 2.9、用于输出错误信息
    def self.report_error(exception)
      case exception
      when Interrupt
        puts '[!] Cancelled'.red
        Config.instance.verbose? ? raise : exit(1)
      when SystemExit
        raise
      else
        if ENV['COCOA_PODS_ENV'] != 'development'
          puts UI::ErrorReport.report(exception)
          UI::ErrorReport.search_for_exceptions(exception)
          exit 1
        else
          raise exception
        end
      end
    end

    # 2.10
    # @todo If a command is run inside another one some settings which where
    #       true might return false.
    #
    # @todo We should probably not even load colored unless needed.
    #
    # @todo Move silent flag to CLAide.
    #
    # @note It is important that the commands don't override the default
    #       settings if their flag is missing (i.e. their value is nil)
    #
    def initialize(argv)
      super
      config.silent = argv.flag?('silent', config.silent)
      config.verbose = self.verbose? unless verbose.nil?
      unless self.ansi_output?
        Colored2.disable!
        String.send(:define_method, :colorize) { |string, _| string }
      end
    end

    # 2.11、Ensure that the【master spec repo】exists
    # @return [void]
    def ensure_master_spec_repo_exists!
      unless config.sources_manager.master_repo_functional?
        Setup.new(CLAide::ARGV.new([])).run
      end
    end

    #-------------------------------------------------------------------------#
    # 2.12
    include Config::Mixin

    ####################### 2.13 private #######################
    private

    # Returns a new {Gem::Version} based on the systems `git` version.
    #
    # @return [Gem::Version]
    #
    def self.git_version
      raw_version = Executable.capture_command('git', ['--version']).first
      unless match = raw_version.scan(/\d+\.\d+\.\d+/).first
        raise "Failed to extract git version from `git --version` (#{raw_version.inspect})"
      end
      Gem::Version.new(match)
    end

    # Checks that the git version is at least 1.8.5
    #
    # @raise If the git version is older than 1.8.5
    #
    # @return [void]
    #
    def self.verify_minimum_git_version!
      if git_version < Gem::Version.new('1.8.5')
        raise Informative, 'You need at least git version 1.8.5 to use CocoaPods'
      end
    end

    # Returns a new {Installer} parametrized from the {Config}.
    #
    # @return [Installer]
    #
    def installer_for_config
      Installer.new(config.sandbox, config.podfile, config.lockfile)
    end

    # Checks that the podfile exists.
    #
    # @raise  If the podfile does not exists.
    #
    # @return [void]
    #
    def verify_podfile_exists!
      unless config.podfile
        raise Informative, "No `Podfile' found in the project directory."
      end
    end

    # Checks that the lockfile exists.
    #
    # @raise  If the lockfile does not exists.
    #
    # @return [void]
    #
    def verify_lockfile_exists!
      unless config.lockfile
        raise Informative, "No `Podfile.lock' found in the project directory, run `pod install'."
      end
    end

    def self.verify_xcode_license_approved!
      if `/usr/bin/xcrun clang 2>&1` =~ /license/ && !$?.success?
        raise Informative, 'You have not agreed to the Xcode license, which ' \
          'you must do to use CocoaPods. Agree to the license by running: ' \
          '`xcodebuild -license`.'
      end
    end
  end
end

2、主要完成构造如下命令行选项

Usage:

    $ pod COMMAND

      CocoaPods, the Cocoa library package manager.

Commands:

    + cache         Manipulate the CocoaPods cache
    + deintegrate   Deintegrate CocoaPods from your project
    + env           Display pod environment
    + init          Generate a Podfile for the current directory
    + install       Install project dependencies according to versions from a
                    Podfile.lock
    + ipc           Inter-process communication
    + lib           Develop pods
    + list          List pods
    + outdated      Show outdated project dependencies
    + plugins       Show available CocoaPods plugins
    + repo          Manage spec-repositories
    + search        Search for pods
    + setup         Setup the CocoaPods environment
    + spec          Manage pod specs
    + trunk         Interact with the CocoaPods API (e.g. publishing new specs)
    + try           Try a Pod!
    + update        Update outdated project dependencies and create new Podfile.lock

Options:

    --silent        Show nothing
    --version       Show the version of the tool
    --verbose       Show more debugging information
    --no-ansi       Show output without ANSI codes
    --help          Show help banner of specified command

相关文章

  • cocoapods源码3 - command.rb

    1、CocoaPods/lib/cocoapods/command.rb 2、主要完成构造如下命令行选项

  • Cocoapods源码调试

    下载pod源码 下载地址[https://github.com/CocoaPods/CocoaPods] 工程目录...

  • CocoaPods使用

    什么是CocoaPods CocoaPods就是一种依赖管理工具。CocoaPods项目的源码在Github上管理...

  • mac 最新接CocoaPods 安装步骤

    一、CocoaPods简介 CocoaPods负责管理iOS项目中第三方框架。CocoaPods的项目源码在Git...

  • Cocoapods安装

    CocoaPods的简介: CocoaPods负责管理iOS项目中第三方框架。CocoaPods的项目源码在Git...

  • iOS 制作pod私有库 详细步骤

    什么是CocoaPods CocoaPods负责管理iOS项目中第三方框架。CocoaPods的项目源码在Gith...

  • CocoaPods 简介、安装和使用

    介绍 iOS 程序提供依赖管理的工具--CocoaPods。CocoaPods项目的源码 在 Github 上管理...

  • 公有仓库

    cocoaPods 公有仓库的建立 (分享自己的源码) 查看 cocoapods Specs 索引目录 两种方...

  • 学习Xcodeproj

    Xcodeproj源码: https://github.com/CocoaPods/Xcodeproj[https...

  • FBKVOController

    学习cocoapods下的KVOController github源码地址 facebook/KVOControl...

网友评论

      本文标题:cocoapods源码3 - command.rb

      本文链接:https://www.haomeiwen.com/subject/urxjbftx.html