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
网友评论