Class: Pod::Command::Dependency

Inherits:
Pod::Command show all
Includes:
Command::ProjectDirectory
Defined in:
lib/cocoapods-dependency-html/command/dependency.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Dependency

Returns a new instance of Dependency.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 24

def initialize(argv)
  @name = argv.shift_argument

  @use_tree = argv.flag?('use-tree', false)
  @dependencies_regular_hash = Hash.new

  @dependency_names = Array.new
  @dependencies_hash = Hash.new
  @dependencies_tree_levels = Array.new
  @dependencies_level_hash = Hash.new


  @output_dependencies_hash = Hash.new
  @output_dependencies_hash["nodes"] = []
  @output_dependencies_hash["edges"] = []
  super
end

Class Method Details

.argumentsObject



12
13
14
15
16
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 12

def self.arguments
  [
    CLAide::Argument.new('NAME', false)
  ].concat(super)
end

.optionsObject



18
19
20
21
22
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 18

def self.options
  [
    ['--use-tree', 'Whether the equal level dependencies use tree'],
  ].concat(super)
end

Instance Method Details

#add_edge(source_id, target_id) ⇒ Object



358
359
360
361
362
363
364
365
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 358

def add_edge(source_id, target_id)
    edge = Hash.new
    edge["sourceID"] = source_id
    edge["targetID"] = target_id
    edge["attributes"] = {}
    edge["size"] = 1
    @output_dependencies_hash["edges"] << (edge)
end

#add_node(pod_name, size, index, level, is_left) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 341

def add_node(pod_name, size, index, level, is_left)
    node = Hash.new
    node["color"] = get_level_color(level)
    node["label"] = pod_name
    node["attributes"] = {}
    node["y"] = -2000 + level * 3000
    if is_left
      node["x"] = 0 + (index + 1) * 200
    else
      node["x"] = 0 - index * 200
    end

    node["id"] = pod_name
    node["size"] = size
    @output_dependencies_hash["nodes"] << (node)
end

#add_root(pod_name, size) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 291

def add_root(pod_name, size)
    node = Hash.new
    node["color"] = "#4f19c7"
    node["label"] = pod_name
    node["attributes"] = {}
    node["y"] = -2000
    node["x"] = 200
    node["id"] = pod_name
    node["size"] = size
    @output_dependencies_hash["nodes"] << (node)
end

#add_tree_node(pod_name, size, index, depth, is_left) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 323

def add_tree_node(pod_name, size, index, depth, is_left)
    node = Hash.new
    node["color"] = get_level_color(depth)
    node["label"] = pod_name
    node["attributes"] = {}
    node["y"] = -2000 + (depth + 1) * 1500

    if is_left
      node["x"] = 0 + (index + 1) * 400
    else
      node["x"] = 0 - index * 400
    end

    node["id"] = pod_name
    node["size"] = size
    @output_dependencies_hash["nodes"] << (node)
end

#clean_recordObject



210
211
212
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 210

def clean_record
  @dependencies_level_hash.clear
end

#dependenciesObject



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 367

def dependencies
  @dependencies ||= begin
    lockfile = config.lockfile unless @ignore_lockfile || @podspec

    if !lockfile || @repo_update
      analyzer = Installer::Analyzer.new(
        sandbox,
        podfile,
        lockfile
      )

      specs = config.with_changes(skip_repo_update: !@repo_update) do
        analyzer.analyze(@repo_update || @podspec).specs_by_target.values.flatten(1)
      end

      lockfile = Lockfile.generate(podfile, specs, {})
    end

    lockfile.to_hash['PODS']
  end
end

#get_level_color(level) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 307

def get_level_color level
    if level == 0
      return "#F75855"
    elsif level == 1
      return "#EC944B"
    elsif level == 2
      return "#ACD543"
    elsif level == 3
      return "#45D5AA"
    elsif level == 4
      return "#EB1E0F"
    else
      return "#45D5AA"
    end
end

#get_pod_dependency(pod_name) ⇒ Object



272
273
274
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 272

def get_pod_dependency pod_name
  return @dependencies_hash[pod_name]
end

#get_pod_level(pod_name) ⇒ Object



222
223
224
225
226
227
228
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 222

def get_pod_level pod_name
  if @dependencies_level_hash[pod_name]
    return @dependencies_level_hash[pod_name]
  else
    return 1
  end
end

#get_pod_name(pod) ⇒ Object



276
277
278
279
280
281
282
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 276

def get_pod_name pod
  pod_name_subspec = remove_version(pod)
  if pod_name_subspec.include? "/"
    return pod_name_subspec.split("/").first
  end
  pod_name_subspec
end

#get_pod_size(pod_name) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 253

def get_pod_size pod_name
  if @use_tree
    return 15
  else
    if @dependencies_hash["#{pod_name}"].class == Array
      return 10
    else
      return 10
    end
  end
end

#get_project_nameObject



248
249
250
251
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 248

def get_project_name
  path = `pwd`
  return path.split("/").last
end

#get_tree_level_color(pod_name) ⇒ Object



303
304
305
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 303

def get_tree_level_color pod_name

end

#is_subspec(pod_name) ⇒ Object



284
285
286
287
288
289
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 284

def is_subspec pod_name
    if pod_name.include? "/"
      return true
    end
   return false
end

#read_configObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 47

def read_config
  if File::exist?("./dependency_config")
    file = File.open("./dependency_config", 'r')
    @dependencies_regular_hash = JSON.load(file.read())
    return true
    if !@dependencies_regular_hash
      puts "dependency_config file cannot be converted to JSONObject"
      return false
    end
  else
    puts "Please add json file named dependency_config in project root path"
    return false
  end
end

#record_each_be_dependencied(pod_name) ⇒ Object



214
215
216
217
218
219
220
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 214

def record_each_be_dependencied pod_name
  if @dependencies_level_hash[pod_name]
    @dependencies_level_hash[pod_name] = 2
  else
    @dependencies_level_hash[pod_name] = 1
  end
end

#remove_version(pod_name) ⇒ Object



265
266
267
268
269
270
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 265

def remove_version pod_name
    if pod_name.include? " "
      return pod_name.split(" ").first
    end
    pod_name
end

#runObject



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 62

def run
  is_success = read_config
  if !is_success
    return
  end

  UI.title "Calculating dependencies" do
    dependencies
  end

  UI.title 'Dependencies' do
    dependencies.map { |dep|
      if dep.is_a? Hash
        dep_hash = dep.to_h
        key = remove_version(dep_hash.keys.first)
        @dependencies_hash.store(key, dep_hash[dep_hash.keys.first])
        @dependency_names << key
      else
        @dependencies_hash.store(remove_version(dep), dep)
        @dependency_names << remove_version(dep)
      end
    }

    if @name
      UI.title "#{@name} Dependencies" do
        puts @dependencies_hash["#{@name}"]
      end
    end

    modules_arr = Array.new
    @dependencies_regular_hash.each {
      modules_arr << []
    }
    modules_arr << []

    @dependency_names.map { |dep|
    	temp_dep = get_pod_name(dep)
    	has_add = false
    	for i in 0..@dependencies_regular_hash.keys.count-1
    	  key = @dependencies_regular_hash.keys[i]
    	  regular_expression = @dependencies_regular_hash[key]
    		if temp_dep =~ /#{regular_expression}/
    		  	modules_arr[i] << dep
    			has_add = true
    			break
    		end
    	end
    	if !has_add
        if !is_subspec(dep)
          modules_arr.last << dep
        end
    	end
    }

    p modules_arr

    if @use_tree
      for arr in modules_arr
        split_modoule_with_tree(arr)
      end
    end

    root_sourceId = get_project_name
    add_root(root_sourceId, modules_arr[0].count)

    if @use_tree
      for i in 0..@dependencies_tree_levels.count - 1
        ## i == level
        for j in 0..@dependencies_tree_levels[i].count-1
          ## j == pod_name
          pod_name = @dependencies_tree_levels[i][j]
          if pod_name != nil || pod_name.class == String || pod_name.length > 0
            add_tree_node(pod_name, get_pod_size(pod_name), j, i, j % 2 == 0)
          end
        end
      end

      ## add root -> first level egde
      for pod_name in modules_arr[0]
        add_edge(root_sourceId, remove_version(pod_name))
      end

    else
      for j in 0..modules_arr.count-1
        pod_arr = modules_arr[j]
        for i in 0..pod_arr.count-1
           pod_name = pod_arr[i]
           add_node(pod_arr[i], get_pod_size(pod_name), i, j+1, i % 2 == 0)
           if j == 0
             add_edge(root_sourceId, remove_version(pod_arr[i]))
           end
        end
      end
    end

    @dependencies_hash.each { |key,value|
      if value.class == Array
         for i in 0..value.count - 1
           pod = value[i]
           if key != value[i]
             add_edge(key, remove_version(pod))
           end
         end
      end
    }

    p `rm -rf pod_dependency.json`
    p `touch pod_dependency.json`
    File.open("./pod_dependency.json","w") do |f|
      dep_json = JSON @output_dependencies_hash
      f.write("index(" + dep_json + ")")

      require 'yaml'
      require 'launchy'
      p `rm -rf dependency_graph.html`
      p `wget https://raw.githubusercontent.com/sfmDev/cocoapods-dependency-html/master/lib/cocoapods-dependency-html/command/front-end/dependency_graph.html`
      Launchy.open("./dependency_graph.html")
    end
  end
end

#sperate_modules_with_level(module_names) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 230

def sperate_modules_with_level module_names
  pod_sperate_arr = Array.new
  pod_sperate_hash = Hash.new
  for pod in module_names
    level = get_pod_level(pod)
    current_pods = pod_sperate_hash[level]
    current_pods = current_pods.to_s + "," + pod
    pod_sperate_hash[level] = current_pods
  end

  pod_sperate_hash.each { |key,value|
    index = key
    pods = value.split(",")
    pod_sperate_arr[index] = pods.delete_if { |item| item == nil || item.class != String || item.length == 0}
  }
  return pod_sperate_arr
end

#split_modoule_with_tree(modules) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 183

def split_modoule_with_tree modules
	for pod in modules
	   dep_array = @dependencies_hash[pod]

	   if dep_array.is_a? Array
       dep_array.map! { |pod_name|
         remove_version(pod_name)
       }
	     for i in 0..dep_array.count-1
	       if modules.include?(dep_array[i])
	          record_each_be_dependencied(dep_array[i])
	       end
	     end
	   end
	end

  temp_modules_dep = @dependencies_level_hash.keys.flatten
  first_level_arr = modules - temp_modules_dep

  @dependencies_tree_levels << first_level_arr

  clean_record
  if temp_modules_dep.count > 0
    split_modoule_with_tree(temp_modules_dep)
  end
end

#validate!Object



42
43
44
45
# File 'lib/cocoapods-dependency-html/command/dependency.rb', line 42

def validate!
  super
  puts @name
end