cocoapods源码4 - command/options

2018-08-12  本文已影响10人  xiongzenghui

1、CocoaPods/lib/cocoapods/command/options/repo_update.rb

module Pod
  class Command
    module Options
      # Provides support for commands to skip updating the spec repositories.
      # RepoUpdate 提供给 Pod::Command::xxx include的模块
      module RepoUpdate
        # 继续扩展 Options 模块
        module Options
          def options
            [
              ['--no-repo-update', 'Skip running `pod repo update` before install'],
            ].concat(super)
          end
        end

        # RepoUpdate模块被include回调时,包含上面的module Options
        def self.included(base)
          # puts base
          # -------------------------------
          # => Pod::Command::Install
          # => Pod::Command::Outdated
          # => Pod::Command::Update
          # -------------------------------
          base.extend(Options)
        end
        
        # 获取 --repo-update 开关bool值,默认值为false
        def repo_update?(default: false)
          if @repo_update.nil?
            default
          else
            @repo_update
          end
        end

        # 获取claide解析完毕的 --repo-update 开关bool值
        def initialize(argv)
          @repo_update = argv.flag?('repo-update')
          super
        end
      end
    end
  end
end

2、CocoaPods/lib/cocoapods/command/options/project_directory.rb

module Pod
  class Command
    module Options
      # Provides support for commands to take a user-specified `project directory`
      # ProjectDirectory 提供给 Pod::Command::xxx include的模块
      module ProjectDirectory

        # ---------------------
        # 套路同上
        # ---------------------

        module Options
          def options
            [
              ['--project-directory=/project/dir/', 'The path to the root of the project directory'],
            ].concat(super)
          end
        end

        def self.included(base)
          # puts base
          # ------------------------------------
          # Pod::Command::Install
          # Pod::Command::IPC::Podfile
          # Pod::Command::IPC::PodfileJSON
          # Pod::Command::IPC::Repl
          # Pod::Command::Outdated
          # Pod::Command::Update
          # ------------------------------------
          base.extend(Options)
        end

        def initialize(argv)
          if project_directory = argv.option('project-directory')
            @project_directory = Pathname.new(project_directory).expand_path
          end
          config.installation_root = @project_directory
          super
        end

        # 命令行参数校验
        def validate!
          super
          # 必须是【目录】,否则抛异常结束执行
          if @project_directory && !@project_directory.directory? 
            raise Informative, "`#{@project_directory}` is not a valid directory."
          end
        end
      end
    end
  end
end
上一篇下一篇

猜你喜欢

热点阅读