Class: CocoapodsDependency::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods-dependency/analyze.rb

Overview

Analyze the project using cocoapods

Class Method Summary collapse

Class Method Details

.analyze(podfile_dir_path) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/cocoapods-dependency/analyze.rb', line 9

def self.analyze(podfile_dir_path)
  path = Pathname.new(podfile_dir_path)
  raise 'absolute path is needed' unless path.absolute?

  Dir.chdir(podfile_dir_path) do
    analyze_with_podfile(
      path,
      Pod::Podfile.from_file(path + 'Podfile'),
      Pod::Lockfile.from_file(path + 'Podfile.lock')
    )
  end
end

.analyze_with_podfile(_podfile_dir_path, podfile, lockfile = nil) ⇒ Object



42
43
44
45
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
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cocoapods-dependency/analyze.rb', line 42

def self.analyze_with_podfile(_podfile_dir_path, podfile, lockfile = nil)
  if _podfile_dir_path
    sandbox = Dir.mktmpdir
  else
    sandbox = Dir.pwd + '/Pods'
  end
  analyzer = Pod::Installer::Analyzer.new(
    Pod::Sandbox.new(sandbox),
    podfile,
    lockfile
  )

  specifications = analyzer.analyze.specifications.map(&:root).uniq

  podfile_dependencies = podfile_dependencies(podfile)

  map = {}
  specifications.each do |s|
    map[s.name] = if s.default_subspecs.count > 0
                    subspecs_with_name(s, s.default_subspecs) + s.dependencies
                  else
                    s.subspecs + s.dependencies
                  end
    subspecs_in_podfile = podfile_dependencies.select { |pd| pd.split('/')[0] == s.name }
    sp = subspecs_in_podfile.map { |sip| s.subspecs.find { |ss| ss.name == sip } }.compact
    map[s.name] = sp if sp.count != 0
    s.subspecs.each do |ss|
      map[ss.name] = ss.dependencies
    end
  end

  # for performance
  dependencies_map = {}
  specifications.each do |s|
    dependencies_map[s.name] = s
  end

  new_map = {}
  specifications.each do |s|
    new_map[s.name] = find_dependencies(s.name, map, [], dependencies_map, s.name).uniq.sort
  end

  new_map
end

.find_dependencies(name, map, res, dependencies_map, root_name) ⇒ Object



87
88
89
90
91
92
93
94
95
96
# File 'lib/cocoapods-dependency/analyze.rb', line 87

def self.find_dependencies(name, map, res, dependencies_map, root_name)
  return unless map[name]

  map[name].each do |k|
    find_dependencies(k.name, map, res, dependencies_map, root_name)
    dependency = dependencies_map[k.name.split('/')[0]]
    res.push dependency.name if dependency && dependency.name != root_name
  end
  res
end

.podfile_dependencies(podfile) ⇒ Object



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

def self.podfile_dependencies(podfile)
  res = []
  podfile.root_target_definitions.each do |td|
    children_definitions = td.recursive_children
    children_definitions.each do |cd|
      dependencies_hash_array = cd.send(:get_hash_value, 'dependencies')
      next if dependencies_hash_array.count.zero?

      dependencies_hash_array.each do |item|
        next if item.class.name != 'Hash'

        item.each do |name, _|
          res.push name
        end
      end
    end
  end
  res
end

.subspecs_with_name(spec, subspecs_short_names) ⇒ Object



98
99
100
101
102
# File 'lib/cocoapods-dependency/analyze.rb', line 98

def self.subspecs_with_name(spec, subspecs_short_names)
  subspecs_short_names.map do |name|
    spec.subspecs.find { |ss| ss.name.include? name }
  end
end