Class: Pod::Command::Debug

Inherits:
Pod::Command show all
Defined in:
lib/cocoapods-xp-binary-debug/command/debug.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) ⇒ Debug

Returns a new instance of Debug.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 40

def initialize(argv)
  @codeSource =  argv.option('source') || nil
  @names = argv.arguments! unless argv.arguments.empty?
  @list = argv.flag?('list', false )
  @all_clean = argv.flag?('all-clean', false )
  @clean = argv.flag?('clean', false )

  @config = Pod::Config.instance

  super
end

Class Method Details

.optionsObject



31
32
33
34
35
36
37
38
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 31

def self.options
  [
      ['--all-clean', '删除所有已经下载的源码'],
      ['--clean', '删除所有指定下载的源码'],
      ['--list', '展示所有一级下载的源码以及其大小'],
      ['--source', '源码路径,本地路径,会去自动链接本地源码']
  ]
end

Instance Method Details

#addObject

begin add ==============


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 76

def add
  if @names == nil
    raise "请输入要调试组件名,多个组件名称用空格分隔"
  end

  @names.each do  |name|
    lib_file = get_lib_path(name)
    unless File.exist?(lib_file)
      raise "找不到 #{lib_file}"
    end
    UI.puts "#{lib_file}"

    target_path =  @codeSource || download_source(name)

    link(lib_file,target_path,name)
  end
end

#all_cleanObject

clean begin ==============


222
223
224
225
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 222

def all_clean
  FileUtils.rm_rf(source_root) if File.directory?(source_root)
  UI.puts "清理完成 #{source_root}"
end

#check(lib_file, dir, basename) ⇒ Object



166
167
168
169
170
171
172
173
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 166

def check(lib_file,dir,basename)
  file = `dwarfdump "#{lib_file}" | grep -E "DW_AT_decl_file.*#{basename}.*\\.m|\\.c" | head -1 | cut -d \\" -f2`
  if File.exist?(file)
    raise "#{file} 不存在 请检测代码源是否正确~"
  end
  UI.puts "link successfully!"
  UI.puts "view linked source at path: #{dir}"
end

#cleanObject



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 227

def clean
  raise "请输入要删除的组件库" if @names.nil?
  @names.each do  |name|
    full_path = File.join(source_root,name)
    if File.directory?(full_path)
      FileUtils.rm_rf(full_path)
    else
      UI.puts "找不到 #{full_path}".yellow
    end
  end
  UI.puts "清理完成 #{@names.to_s}"
end

#download_source(name) ⇒ Object

下载源码到本地



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 95

def download_source(name)
  target_path =  File.join(source_root, name)
  UI.puts target_path
  FileUtils.rm_rf(target_path)

  find_dependency = find_dependency(name)

  spec = fetch_external_source(find_dependency, @config.podfile,@config.lockfile, @config.sandbox,true )

  download_request = Pod::Downloader::Request.new(:name => name, :spec => spec)
  Downloader.download(download_request, Pathname.new(target_path), :can_cache => true)

  target_path
end

#fetch_external_source(dependency, podfile, lockfile, sandbox, use_lockfile_options) ⇒ Object

获取external_source 下的仓库

Returns:

  • spec



124
125
126
127
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 124

def fetch_external_source(dependency ,podfile , lockfile, sandbox,use_lockfile_options)
  source = ExternalSources.from_dependency(dependency, podfile.defined_in_file, true)
  source.fetch(sandbox)
end

#find_dependency(name) ⇒ Object

找出依赖



111
112
113
114
115
116
117
118
119
120
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 111

def find_dependency (name)
  find_dependency = nil
  @config.podfile.dependencies.each do |dependency|
    if dependency.root_name.downcase == name.downcase
      find_dependency = dependency
      break
    end
  end
  find_dependency
end

#get_lib_path(name) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 175

def get_lib_path(name)
  dir = Pathname.new(File.join(Pathname.pwd,"Pods",name))
  # lib_name = "lib#{name}.a"
  #lib_path = File.join(dir,lib_name)

  # framework 路径
  lib_name = name
  lib_path = File.join(`#{dir}.framework`, lib_name)

  unless File.exist?(lib_path)
    lib_path = File.join(dir.children.first,lib_name)
  end

  lib_path
end

链接,.a文件位置, 源码目录,工程名=IMYFoundation



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 133

def link(lib_file,target_path,basename)
  dir = (`dwarfdump "#{lib_file}" | grep "AT_comp_dir" | head -1 | cut -d \\" -f2 `)
  sub_path = "#{basename}/bin-archive/#{basename}"
  dir = dir.gsub(sub_path, "").chomp
  # UI.puts "dir = #{dir}"

  unless File.exist?(dir)
    # UI.puts "不存在 = #{dir}"
    begin
      FileUtils.mkdir_p(dir)
    rescue SystemCallError
      #判断用户目录是否存在
      array = dir.split('/')
      if array.length > 3
        root_path = '/' + array[1] + '/' + array[2]
        unless File.exist?(root_path)
          raise "由于权限不足,请手动创建#{root_path} 后重试"
        end
      end
    end
  end

  # if Pathname.new(lib_file).extname == ".a"
    FileUtils.rm_rf(File.join(dir,basename))
    `ln -s #{target_path} #{dir}`
  # else
  #   FileUtils.rm_rf(File.join(dir,basename))
  #   `ln -s #{target_path} #{dir}/#{basename}`
  # end

  check(lib_file,dir,basename)
end

#listObject

list begin ==============


213
214
215
216
217
218
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 213

def list
  Dir.entries(source_root).each do |sub|
    UI.puts "- #{sub}" unless sub.include?('.')
  end
  UI.puts "加载完成"
end

#runObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cocoapods-xp-binary-debug/command/debug.rb', line 53

def run

  podfile_lock = File.join(Pathname.pwd,"Podfile.lock")
  raise "podfile.lock,不存在,请先pod install/update" unless File.exist?(podfile_lock)
  @lockfile ||= Lockfile.from_file(Pathname.new(podfile_lock) )

  if @list
    list
  elsif @clean
    clean
  elsif @all_clean
    all_clean
  elsif @names
    add
  end

  if @list && @clean && @names
    raise "请选择您要执行的命令。"
  end
end