Class: Pod::Command::Vbbuildsource

Inherits:
Pod::Command show all
Defined in:
lib/cocoapods-vbbuildsource/command/vbbuildsource.rb

Overview

TODO:

Create a PR to add your plugin to CocoaPods/cocoapods.org in the plugins.json file, once your plugin is released.

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.

  • move this file to lib/pod/command/list/deprecated.rb and update the class to exist in the the Pod::Command::List namespace

  • change this class to extend from List instead of Command. This tells the plugin system that it is a subcommand of list.

  • edit lib/cocoapods_plugins.rb to require this file

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Vbbuildsource

Returns a new instance of Vbbuildsource.



35
36
37
38
39
# 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

Class Method Details

.optionsObject



29
30
31
32
33
# File 'lib/cocoapods-vbbuildsource/command/vbbuildsource.rb', line 29

def self.options
  [
    ['--clean-module', '删除模式,用于删除已经下载好源文件的组件'],
  ]
end

Instance Method Details

#download_source_code_file(head_file_path) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/cocoapods-vbbuildsource/command/vbbuildsource.rb', line 101

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) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cocoapods-vbbuildsource/command/vbbuildsource.rb', line 86

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 字段
  binary_build_path_string = find_file_path_string_in_binary(framework_file_path,binary_file_path)
  help! "未找到库 #{@module_name} 的二进制path路径" unless binary_build_path_string
  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) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/cocoapods-vbbuildsource/command/vbbuildsource.rb', line 74

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

#runObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

  file_list = Array.new(pod_target.all_files.count) { |index|
    file = pod_target.all_files[index]
    if file.is_a? String
      Pathname.new(file)
    else
      file
    end
  }
  framework_file_path = file_list.detect {|each| each.extname.end_with? 'framework'}

  help! "该pod库 #{@module_name} 不是一个framework" unless framework_file_path

  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!Object



41
42
43
44
# File 'lib/cocoapods-vbbuildsource/command/vbbuildsource.rb', line 41

def validate!
  super
  help! 'A module_name name is required.' unless @module_name
end