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.


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

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)
  @use_podfile_targets = argv.flag?('use-podfile-targets', false)
  super
end

Class Method Details

.argumentsObject


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

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

.optionsObject


12
13
14
15
16
17
18
19
20
# 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 <podspec name>.png or Podfile.png'],
    ['--use-podfile-targets', 'Uses targets from the Podfile'],
  ].concat(super)
end

Instance Method Details

#dependenciesObject


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pod/command/dependencies.rb', line 71

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

#graphviz_dataObject


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
# File 'lib/pod/command/dependencies.rb', line 123

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

    if @use_podfile_targets
      unless @podspec
        podfile.target_definitions.values.each do |target|
          target_node = graph.add_node(target.name.to_s)
          if target.dependencies
            target.dependencies.each do |dependency|
              pod_node = graph.add_node(dependency.name.to_s)
              graph.add_edge(target_node, pod_node)
            end
          end
        end
      end
    else
      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
    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


193
194
195
# File 'lib/pod/command/dependencies.rb', line 193

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

#graphviz_image_outputObject


189
190
191
# File 'lib/pod/command/dependencies.rb', line 189

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

#output_file_basenameObject

Basename to use for output files.


178
179
180
181
# File 'lib/pod/command/dependencies.rb', line 178

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.


173
174
175
# File 'lib/pod/command/dependencies.rb', line 173

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

#podfileObject


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pod/command/dependencies.rb', line 93

def podfile
  @podfile ||= begin
    if podspec = @podspec
      platform = podspec.available_platforms.first
      platform_name = platform.name
      platform_version = platform.deployment_target.to_s
      source_urls = Config.instance.sources_manager.all.map(&:url).compact
      Podfile.new do
        install! 'cocoapods', integrate_targets: false, warn_for_multiple_pod_sources: false
        source_urls.each { |u| source(u) }
        platform platform_name, platform_version
        pod podspec.name, podspec: podspec.defined_in_file
        target 'Dependencies'
      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.


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

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

#runObject


61
62
63
64
65
66
67
68
69
# File 'lib/pod/command/dependencies.rb', line 61

def run
  require 'yaml'
  UI.title "Calculating dependencies" do
    dependencies
  end
  graphviz_image_output if @produce_image_output
  graphviz_dot_output if @produce_graphviz_output
  yaml_output
end

#sandboxObject


114
115
116
117
118
119
120
121
# File 'lib/pod/command/dependencies.rb', line 114

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.


163
164
165
# File 'lib/pod/command/dependencies.rb', line 163

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

#validate!Object


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

def validate!
  super
  if @podspec_name
    require 'pathname'
    path = Pathname.new(@podspec_name)
    if path.file?
      @podspec = Specification.from_file(path)
    else
      sets = Config.
        instance.
        sources_manager.
        search(Dependency.new(@podspec_name))
      spec = sets && sets.specification
      @podspec = spec && spec.subspec_by_name(@podspec_name)
      raise Informative, "Cannot find `#{@podspec_name}`." unless @podspec
    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


183
184
185
186
187
# File 'lib/pod/command/dependencies.rb', line 183

def yaml_output
  UI.title 'Dependencies' do
    UI.puts YAMLHelper.convert(dependencies)
  end
end