Class: Pod::DepFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/podfileDep/dep/find.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDepFinder

Returns a new instance of DepFinder.



19
20
21
22
# File 'lib/podfileDep/dep/find.rb', line 19

def initialize
  super
  init_lock
end

Instance Attribute Details

#find_nameObject

Parameters:

  • 要查询的组件名 (String)


11
12
13
# File 'lib/podfileDep/dep/find.rb', line 11

def find_name
  @find_name
end

#forwardObject

Parameters:

  • 是否打印正向依赖 (bool)


17
18
19
# File 'lib/podfileDep/dep/find.rb', line 17

def forward
  @forward
end

#lockfile_contentObject

Parameters:

  • podfileLock内容 (Hash)


8
9
10
# File 'lib/podfileDep/dep/find.rb', line 8

def lockfile_content
  @lockfile_content
end

#sort_countObject

Parameters:

  • 是否按数量从小到大排序 (bool)


14
15
16
# File 'lib/podfileDep/dep/find.rb', line 14

def sort_count
  @sort_count
end

Instance Method Details

#findObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/podfileDep/dep/find.rb', line 37

def find

  if @find_name
    find_single_dep
  else
    find_all_dep
  end

  puts "\n基础命令(默认输出反向依赖):pod dep"
  unless @forward
    puts "==> 输出所有正向依赖,可加参数 --forward".yellow
  end
  unless @sort_count
    puts "==> 按数量从少到多排序,可加参数 --sort".yellow
  end
  unless @find_name
    puts "==> 查询单个依赖的情况,可使用参数 --name=xx".yellow
  end

end

#find_all_depObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/podfileDep/dep/find.rb', line 58

def find_all_dep

  result_map = Hash.new
  # 循环遍历lockfile对象中的PODS数组
  @lockfile_content['PODS'].each do |pod|
    # 拿到所有的组件名
    key = pod.is_a?(Hash) ? pod.keys[0] : pod
    key = key.split(" (")[0]

    if @forward
      result_map[key] = pod.is_a?(Hash) ? pod.values[0] : Array.new
    else
      result_map[key] = find_reverse_with_name(key)
    end
  end

  msg = @forward ? "正向依赖" : "反向依赖"
  puts "打印#{msg}"

  sorted_hash = result_map
  if @sort_count
    puts "数量从少到多"
    sorted_hash = Hash[result_map.sort_by { |key, value| value.length }]
  end

  sorted_hash.each do |key, array|
    if @forward
      puts "组件#{key}的依赖有(#{array.length}个):".green
    else
      puts "依赖#{key}的组件有(#{array.length}个):".green
    end

    array.each do |obj|
      puts obj
    end
    puts "\n"
  end
end

#find_forward_with_name(name) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/podfileDep/dep/find.rb', line 117

def find_forward_with_name(name)
  results = Array.new
  @lockfile_content['PODS'].each do |pod|
    # 拿到所有的组件名
    key = pod.is_a?(Hash) ? pod.keys[0] : pod
    key = key.split(" (")[0]
    if key == name and pod.is_a?(Hash)
      pod.each_value do |value|
        results << value
      end
    end
  end
  results
end

#find_reverse_with_name(name) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/podfileDep/dep/find.rb', line 132

def find_reverse_with_name(name)
  results = Array.new
  # 循环遍历lockfile对象中的PODS数组
  @lockfile_content['PODS'].each do |pod|
    if pod.is_a?(Hash)
      pod.values.each do |value|
        # 如果value是一个数组,则判断数组中是否包含name
        if value.is_a?(Array)
          value.each { |sub_value|
            # 这里的处理包含了子模块的情况
            sub_module = sub_value.split(" (")[0]
            if  sub_module == name
              results << pod.keys.first
            end
          }
        end
      end
    end
  end

  # 去重
  results.uniq
end

#find_single_depObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/podfileDep/dep/find.rb', line 97

def find_single_dep

  # 反向依赖查询结果
  results = find_reverse_with_name(@find_name)
  puts "依赖#{@find_name}的组件有(#{results.length}个):".green
  results.each do |res|
    puts res
  end

  puts "\n"

  # 正向依赖查询结果
  forward_results = find_forward_with_name(@find_name)
  puts "#{@find_name}依赖的组件有(#{forward_results.length}个):".green
  forward_results.each do |res|
    puts res
  end

end

#init_lockObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/podfileDep/dep/find.rb', line 24

def init_lock
  # 读取名为Podfile.lock的yaml文件, 并通过分析里面的PODS数组,输出依赖的第三方库的名字
  unless File.exist?("Podfile.lock")
    puts "Podfile.lock文件不存在,请先执行pod install或pod update".red
  end

  @sort_count = false

  @lockfile_content = YAML.load_file('Podfile.lock')

end