Class: Pod::Command::Dependencies

Inherits:
Pod::Command show all
Includes:
Command::ProjectDirectory
Defined in:
lib/pod/command/dependencies.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Dependencies

Returns a new instance of Dependencies.



27
28
29
30
31
32
33
34
# File 'lib/pod/command/dependencies.rb', line 27

def initialize(argv)
  @podspec_name = argv.shift_argument
  @ignore_lockfile = argv.flag?('ignore-lockfile', false)
  @repo_update = argv.flag?('repo-update', false)
  @produce_graphviz_output = argv.flag?('graphviz', false)
  @produce_image_output = argv.flag?('image', false)
  super
end

Class Method Details

.argumentsObject



21
22
23
24
25
# File 'lib/pod/command/dependencies.rb', line 21

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

.optionsObject



12
13
14
15
16
17
18
19
# File 'lib/pod/command/dependencies.rb', line 12

def self.options
  [
    ['--ignore-lockfile', 'Whether the lockfile should be ignored when calculating the dependency graph'],
    ['--repo-update', 'Fetch external podspecs and run `pod repo update` before calculating the dependency graph'],
    ['--graphviz', 'Outputs the dependency graph in Graphviz format to <podspec name>.gv or Podfile.gv'],
    ['--image', 'Outputs the dependency graph as an image to <podsepc name>.png or Podfile.png'],
  ].concat(super)
end

Instance Method Details

#dependenciesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pod/command/dependencies.rb', line 63

def dependencies
  @dependencies ||= begin
    analyzer = Installer::Analyzer.new(
      sandbox,
      podfile,
      @ignore_lockfile || @podspec ? nil : config.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, {})
    pods = lockfile.to_hash['PODS']
  end
end

#graphviz_dataObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/pod/command/dependencies.rb', line 108

def graphviz_data
  @graphviz ||= begin
    require 'graphviz'
    graph = GraphViz::new(output_file_basename, :type => :digraph)
    root = graph.add_node(output_file_basename)

    unless @podspec
      podfile_dependencies.each do |pod|
        pod_node = graph.add_node(pod)
        graph.add_edge(root, pod_node)
      end
    end

    pod_to_dependencies.each do |pod, dependencies|
      pod_node = graph.add_node(sanitized_pod_name(pod))
      dependencies.each do |dependency|
        dep_node = graph.add_node(sanitized_pod_name(dependency))
        graph.add_edge(pod_node, dep_node)
      end
    end

    graph
  end
end

#graphviz_dot_outputObject



169
170
171
# File 'lib/pod/command/dependencies.rb', line 169

def graphviz_dot_output
  graphviz_data.output( :dot => "#{output_file_basename}.gv")
end

#graphviz_image_outputObject



165
166
167
# File 'lib/pod/command/dependencies.rb', line 165

def graphviz_image_output
  graphviz_data.output( :png => "#{output_file_basename}.png")
end

#output_file_basenameObject

Basename to use for output files.



154
155
156
157
# File 'lib/pod/command/dependencies.rb', line 154

def output_file_basename
  return 'Podfile' unless @podspec_name
  File.basename(@podspec_name, File.extname(@podspec_name))
end

#pod_to_dependenciesObject

Returns a [String: [String]] containing resolved mappings from the name of a pod to an array of the names of its dependencies.



149
150
151
# File 'lib/pod/command/dependencies.rb', line 149

def pod_to_dependencies
  dependencies.map { |d| d.is_a?(Hash) ? d : { d => [] } }.reduce({}) { |combined, individual| combined.merge!(individual) }
end

#podfileObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/pod/command/dependencies.rb', line 80

def podfile
  @podfile ||= begin
    if podspec = @podspec
      platform = podspec.available_platforms.first
      platform_name, platform_version = platform.name, platform.deployment_target.to_s
      sources = SourcesManager.all.map(&:url)
      Podfile.new do
        install! :cocoapods, integrate_targets: false
        sources.each { |s| source s }
        platform platform_name, platform_version
        pod podspec.name, podspec: podspec.defined_in_file
      end
    else
      verify_podfile_exists!
      config.podfile
    end
  end
end

#podfile_dependenciesObject

Returns a Set of Strings of the names of dependencies specified in the Podfile.



139
140
141
# File 'lib/pod/command/dependencies.rb', line 139

def podfile_dependencies
  Set.new(podfile.target_definitions.values.map { |t| t.dependencies.map { |d| d.name } }.flatten)
end

#podspec_dependenciesObject

Returns a [String] of the names of dependencies specified in the podspec.



144
145
146
# File 'lib/pod/command/dependencies.rb', line 144

def podspec_dependencies
  @podspec.all_dependencies.map { |d| d.name }
end

#runObject



56
57
58
59
60
61
# File 'lib/pod/command/dependencies.rb', line 56

def run
  require 'yaml'
  graphviz_image_output if @produce_image_output
  graphviz_dot_output if @produce_graphviz_output
  yaml_output
end

#sandboxObject



99
100
101
102
103
104
105
106
# File 'lib/pod/command/dependencies.rb', line 99

def sandbox
  if @podspec
    require 'tmpdir'
    Sandbox.new(Dir.mktmpdir)
  else
    config.sandbox
  end
end

#sanitized_pod_name(name) ⇒ Object

Truncates the input string after a pod’s name removing version requirements, etc.



134
135
136
# File 'lib/pod/command/dependencies.rb', line 134

def sanitized_pod_name(name)
  Pod::Dependency.from_string(name).name
end

#validate!Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/pod/command/dependencies.rb', line 36

def validate!
  super
  if @podspec_name
    require 'pathname'
    path = Pathname.new(@podspec_name)
    if path.exist?
      @podspec = Specification.from_file(path)
    else
      @podspec = SourcesManager.
        search(Dependency.new(@podspec_name)).
        specification.
        subspec_by_name(@podspec_name)
    end
  end
  if (@produce_image_output || @produce_graphviz_output) && Executable.which('dot').nil?
    raise Informative, 'GraphViz must be installed and `dot` must be in ' \
      '$PATH to produce image or graphviz output.'
  end
end

#yaml_outputObject



159
160
161
162
163
# File 'lib/pod/command/dependencies.rb', line 159

def yaml_output
  UI.title 'Dependencies' do
    UI.puts dependencies.to_yaml
  end
end