Class: Pod::Project

Inherits:
Xcodeproj::Project
  • Object
show all
Defined in:
lib/cocoapods/project.rb

Overview

The Pods project.

Model class which provides helpers for working with the Pods project through the installation process.

Groups collapse

Instance Attribute Summary collapse

Groups collapse

File references collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ Project

Returns a new instance of Project.

Parameters:

  • sandbox (Sandbox)

    @see #sandbox



19
20
21
22
23
24
25
# File 'lib/cocoapods/project.rb', line 19

def initialize(path = nil)
  super(nil) # Recreate the project from scratch for now.
  @path = path
  @support_files_group = new_group('Targets Support Files')

  @refs_by_absolute_path = {}
end

Instance Attribute Details

#pathPathname (readonly)

Returns the path of the xcodeproj file which stores the project.

Returns:

  • (Pathname)

    the path of the xcodeproj file which stores the project.



15
16
17
# File 'lib/cocoapods/project.rb', line 15

def path
  @path
end

#support_files_groupPBXGroup (readonly)

Returns the group where the support files for the Pod libraries should be added.

Returns:

  • (PBXGroup)

    the group where the support files for the Pod libraries should be added.



82
83
84
# File 'lib/cocoapods/project.rb', line 82

def support_files_group
  @support_files_group
end

Instance Method Details

#add_file_references(absolute_path, spec_name, parent_group) ⇒ void

Note:

With this set-up different subspecs might not reference the same file (i.e. the first will win). Not sure thought if this is a limitation or a feature.

This method returns an undefined value.

Adds a file reference for each one of the given files in the specified group, namespaced by specification unless a file reference for the given path already exits.

Parameters:

  • paths (Array<Pathname,String>)

    The files for which the file reference is needed.

  • spec_name (String)

    The full name of the specification.

  • parent_group (PBXGroup)

    The group where the file references should be added.



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/cocoapods/project.rb', line 156

def add_file_references(absolute_path, spec_name, parent_group)
  group = add_spec_group(spec_name, parent_group)
  absolute_path.each do |file|
    existing = file_reference(file)
    unless existing
      file = Pathname.new(file)
      ref = group.new_file(relativize(file))
      @refs_by_absolute_path[file] = ref
    end
  end
end

#add_podfile(podfile_path) ⇒ PBXFileReference

Adds a file reference to the podfile.

Parameters:

  • podfile_path (Pathname, String)

    the path of the podfile

Returns:

  • (PBXFileReference)

    the file reference.



188
189
190
191
192
193
# File 'lib/cocoapods/project.rb', line 188

def add_podfile(podfile_path)
  podfile_path = Pathname.new(podfile_path)
  podfile_ref  = new_file(relativize(podfile_path))
  podfile_ref.xc_language_specification_identifier = 'xcode.lang.ruby'
  podfile_ref
end

#add_spec_group(spec_name, root_group) ⇒ PBXGroup

Adds a group as child to the ‘Pods` group namespacing subspecs.

Parameters:

  • spec_name (String)

    The full name of the specification.

  • root_group (PBXGroup)

    The group where to add the specification. Either ‘Pods` or `Local Pods`.

Returns:

  • (PBXGroup)

    the group for the spec with the given name.



121
122
123
124
125
126
127
128
129
# File 'lib/cocoapods/project.rb', line 121

def add_spec_group(spec_name, root_group)
  current_group = root_group
  group = nil
  spec_name.split('/').each do |name|
    group = current_group[name] || current_group.new_group(name)
    current_group = group
  end
  group
end

#file_reference(absolute_path) ⇒ PBXFileReference, Nil

Returns the file reference for the given absolute file path.

Parameters:

  • absolute_path (Pathname, String)

    The absolute path of the file whose reference is needed.

Returns:

  • (PBXFileReference)

    The file reference.

  • (Nil)

    If no file reference could be found.



176
177
178
179
# File 'lib/cocoapods/project.rb', line 176

def file_reference(absolute_path)
  absolute_path = Pathname.new(absolute_path)
  refs_by_absolute_path[absolute_path]
end

#inspectString

Returns a string representation suited for debugging.

Returns:

  • (String)

    a string representation suited for debugging.



69
70
71
# File 'lib/cocoapods/project.rb', line 69

def inspect
  "#<#{self.class}> path:#{path}"
end

#local_podsPBXGroup

Returns the ‘Local Pods` group, creating it if needed. This group is used to contain locally sourced pods.

Returns:

  • (PBXGroup)

    the group.



97
98
99
# File 'lib/cocoapods/project.rb', line 97

def local_pods
  @local_pods ||= new_group('Local Pods')
end

#podsPBXGroup

Returns the ‘Pods` group, creating it if needed.

Returns:

  • (PBXGroup)

    the group.



88
89
90
# File 'lib/cocoapods/project.rb', line 88

def pods
  @pods ||= new_group('Pods')
end

#relativize(path) ⇒ Pathname

Note:

If the two absolute paths don’t share the same root directory an extra ‘../` is added to the result of Pathname#relative_path_from.

Returns the relative path from the project root.

Examples:


path = Pathname.new('/Users/dir')
@sandbox.root #=> Pathname('/tmp/CocoaPods/Lint/Pods')

@sandbox.relativize(path) #=> '../../../../Users/dir'
@sandbox.relativize(path) #=> '../../../../../Users/dir'

Parameters:

  • path (Pathname)

    The path that needs to be converted to the relative format.

Returns:

  • (Pathname)

    Returns the relative path from the project root.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cocoapods/project.rb', line 55

def relativize(path)
  unless path.absolute?
    raise StandardError, "[Bug] Attempt to add relative path `#{path}` to the Pods project"
  end

  result = path.relative_path_from(root)
  unless root.to_s.split('/')[1] == path.to_s.split('/')[1]
    result = Pathname.new('../') + result
  end
  result
end

#resourcesPBXGroup

Returns the ‘Local Pods` group, creating it if needed. This group is used to contain locally sourced pods.

Returns:

  • (PBXGroup)

    the group.



106
107
108
# File 'lib/cocoapods/project.rb', line 106

def resources
  @resources ||= new_group('Resources')
end

#rootPathname

Returns the directory where the project is stored.

Returns:

  • (Pathname)

    the directory where the project is stored.



34
35
36
# File 'lib/cocoapods/project.rb', line 34

def root
  @root ||= path.dirname
end