class Pod::Command::Vbbuildsource

This is an example of a cocoapods plugin adding a top-level subcommand to the 'pod' command.

You can also create subcommands of existing or new commands. Say you wanted to add a subcommand to `list` to show newly deprecated pods, (e.g. `pod list deprecated`), there are a few things that would need to change.

@todo Create a PR to add your plugin to CocoaPods/cocoapods.org

in the `plugins.json` file, once your plugin is released.

Public Class Methods

new(argv) click to toggle source
Calls superclass method
# File lib/cocoapods-vbbuildsource/command/vbbuildsource.rb, line 35
def initialize(argv)
  @module_name = argv.shift_argument
  @clean_module = argv.flag?('clean-module', false)
  super
end
options() click to toggle source
# File lib/cocoapods-vbbuildsource/command/vbbuildsource.rb, line 29
def self.options
  [
    ['--clean-module', '删除模式,用于删除已经下载好源文件的组件'],
  ]
end

Public Instance Methods

download_source_code_file(head_file_path) click to toggle source
# File lib/cocoapods-vbbuildsource/command/vbbuildsource.rb, line 95
def download_source_code_file head_file_path
  pod_spec = nil
  @installer.analysis_result.specifications.each do |spec|
    if spec.name == @module_name then
      pod_spec = spec
      break
    end
  end
  version = pod_spec.version.version
  help! '该库版本不符合二进制版本识别规则' unless version_is_binary version
  version = source_code_version version
  module_spec = Pod::Specification::Set::LazySpecification.new(pod_spec.name, Pod::Version.new(version), pod_spec.spec_source)
  module_request = Downloader::Request.new(:spec => module_spec)

  source_file_dir = find_spec_source_file_path module_spec
  source_path = File.dirname(head_file_path).split(source_file_dir).first
  puts "开始创建目录:#{source_path}"
  `sudo mkdir -p #{source_path}` unless File.exists?(source_path)
  `sudo chmod 777 #{File.split(source_path).first}`
  puts "创建目录成功#{source_path}"
  download_result = Downloader.download(module_request, source_path, :can_cache => true)
  source_path

end
find_binary_build_path(framework_file_path) click to toggle source
# File lib/cocoapods-vbbuildsource/command/vbbuildsource.rb, line 80
def find_binary_build_path framework_file_path

  binary_file_path = find_binary_file_in_framework framework_file_path
  help! "未找到库 #{@module_name} 的二进制文件" unless binary_file_path
  # 找到framework里面的某一个.h头文件的 DW_AT_decl_file 字段
  first_head_file = find_first_head_file_in_framework framework_file_path
  binary_build_path_string = `dwarfdump #{binary_file_path.to_path} | grep #{File.basename(first_head_file)} | grep 'DW_AT_decl_file'`.gsub!(/\s/, '')
  binary_build_path_list = binary_build_path_string.split('DW_AT_decl_file').delete_if(&:empty?).uniq
  # 删掉所有的括号引号
  binary_build_path_list = binary_build_path_list.each { |item| item.gsub!('("','').gsub!('")', '') }
  puts '这个库格式格式有异常。可能解析不成功,请将这个例子报告给 faliu' unless binary_build_path_list.count >= 1

  binary_build_path = binary_build_path_list.first
end
find_pod_target(installer) click to toggle source
# File lib/cocoapods-vbbuildsource/command/vbbuildsource.rb, line 68
def find_pod_target installer
  pod_target_list = installer.pod_targets
  pod_target = nil
  pod_target_list.each do |target|
    if target.pod_name == @module_name then
      pod_target = target
      break
    end
  end
  pod_target
end
run() click to toggle source
# File lib/cocoapods-vbbuildsource/command/vbbuildsource.rb, line 46
def run
  @installer = installer_for_config
  @installer.resolve_dependencies

  pod_target = find_pod_target @installer

  help! "该pod库 #{@module_name} 未找到" unless pod_target

  framework_file_path = pod_target.all_files.first

  framework_file_path = Pathname.new(framework_file_path) if framework_file_path.is_a? String

  help! "该pod库 #{@module_name} 不是一个framework" unless pod_target.all_files.count == 1 and framework_file_path.extname.end_with? 'framework'

  binary_build_path = find_binary_build_path framework_file_path

  module_source_path = download_source_code_file binary_build_path

  puts "#{@module_name} 库源文件 路径为 #{module_source_path}"
  puts "---------完成库#{@module_name} 文件切换为源码,请刷新xcode(切换页面或者单步调试一下)---------"
end
validate!() click to toggle source
Calls superclass method
# File lib/cocoapods-vbbuildsource/command/vbbuildsource.rb, line 41
def validate!
  super
  help! 'A module_name name is required.' unless @module_name
end

Private Instance Methods

find_binary_file_in_framework(framework_file_path) click to toggle source

找到二进制文件。

# File lib/cocoapods-vbbuildsource/command/vbbuildsource.rb, line 122
def find_binary_file_in_framework framework_file_path
  return nil unless framework_file_path.extname.end_with? '.framework'
  return Pathname.new("#{framework_file_path.to_path}/#{framework_file_path.basename.to_path.gsub!('.framework', '')}")
end
find_first_head_file_in_framework(framework_file_path, end_name='.h') click to toggle source
# File lib/cocoapods-vbbuildsource/command/vbbuildsource.rb, line 127
def find_first_head_file_in_framework(framework_file_path, end_name='.h')
  framework_file_path.children.collect do |child|
    if child.file? && child.extname.end_with?(end_name)
      return child
    elsif child.directory?
      filePath = find_first_head_file_in_framework child
      return filePath if filePath
    end
  end
  return nil
end
find_spec_source_file_path(specification) click to toggle source
# File lib/cocoapods-vbbuildsource/command/vbbuildsource.rb, line 148
def find_spec_source_file_path specification
  source_path = specification.specification.attributes_hash['source_files']
  source_path = specification.specification.subspecs.first.attributes_hash['source_files'] unless source_path
  help! "该库 #{@module_name} 的spec文件没填 source_file,暂不支持" unless source_path
  dir_list = source_path.split('/')
  help! "源码路径错误 #{source_path}" unless dir_list.count >= 2
  return "/#{dir_list.first}/#{dir_list[1]}"
end
source_code_version(version) click to toggle source
# File lib/cocoapods-vbbuildsource/command/vbbuildsource.rb, line 143
def source_code_version version
  return version.gsub!('-bin', '') if version.end_with? '-bin'
  return version.gsub!('-xc', '') if version.end_with? 'xc'
  help! '该库版本不符合二进制版本识别规则'
end
version_is_binary(version) click to toggle source
# File lib/cocoapods-vbbuildsource/command/vbbuildsource.rb, line 139
def version_is_binary version
  return true if version.end_with? '-bin' 
  return true if version.end_with? '-xc'
end