module Pod::Command::Links
Links
utility that provides functionality around managing CocoaPod links
Constants
- LINKED_DB
Defines the path where per pod links are stored (e.g. from pod link <foo> command)
- REGISTERED_DB
Defines the path where the links database is stored (e.g. from pod link)
Public Class Methods
Entry point for the `pod` hook to check if the current pod project should use a linked pod of installed from the pod requirements. In order for a link to be returned the following must hold true:
-
The pod must be registered (e.g. pod link)
-
The current pod project must have linked the registered link (e.g. pod link <name>)
@param name the name of the pod to find a link for
@returns the registered link for the given name or nil
# File lib/pod/links.rb, line 120 def self.get_link(name) if self.linked_pods.include?(name) return self.get_registered_link name end return nil end
Get list of pods that are linked for the current pod project
@return an array of installed links
# File lib/pod/links.rb, line 154 def self.installed_links installed = [] self.linked_pods.each do |pod| unless self.get_registered_link(pod).nil? installed.append(pod) end end return installed end
Creates a link for the given pod into the current project. The pod must be registered using `pod link`
@param pod the name of the pod to link into the current project
# File lib/pod/links.rb, line 51 def self.link(pod) # only allow registered links to be used registered_link = self.get_registered_link pod if registered_link.nil? Command::help! "Pod '#{pod}'' is not registered. Did you run `pod link` from the #{pod} directory?" end # add the linked pod linked_pods = [pod] if self.linked_db.has_key?(Dir.pwd) linked_pods = linked_pods.concat self.linked_db[Dir.pwd]['pods'] end self.print "Adding link to '#{pod}' > #{registered_link['path']}" self.write_db(LINKED_DB, self.linked_db, { Dir.pwd => { 'pods' => linked_pods.uniq } }) # install pod from link Pod::Command::Install.run(CLAide::ARGV.new ["--no-repo-update"]) end
List
the links.
-
If linked is true then list the linked pods in the current project
-
Id linked is false then list the registered links
@param linked flag to determine which links to list
# File lib/pod/links.rb, line 135 def self.list(linked = false) if linked self.print "Linked pods:" self.linked_pods.each do |pod| self.print "* #{pod}" end else self.print "Registered pods:" self.registerd_db.each do |pod, link| self.print "* #{pod} > #{link['path']}" end end end
Register a pod for local development in the current working directory. This working directory must have a .podspec defining the pod
# File lib/pod/links.rb, line 26 def self.register self.print "Registering '#{self.podspec.name}' > #{Dir.pwd}" self.write_db(REGISTERED_DB, self.registerd_db, { self.podspec.name => { "path" => Dir.pwd } }) end
Will unlink the give pod from the current pod project
@param pod the name of the pod to unlink
# File lib/pod/links.rb, line 80 def self.unlink(pod) if self.linked_db.has_key?(Dir.pwd) linked_pods = self.linked_db[Dir.pwd]['pods'] linked_pods.delete(pod) # # Update databased based on link state # if links exist, update list of links # if links do not exist, remove entry # self.print "Removing link to '#{pod}'" if linked_pods.empty? db = self.linked_db db.delete(Dir.pwd) self.write_db(LINKED_DB, db) else self.write_db(LINKED_DB, self.linked_db, { Dir.pwd => { 'pods' => linked_pods } }) end # install pod from repo Pod::Command::Install.run(CLAide::ARGV.new []) end end
Unregister a pod
# File lib/pod/links.rb, line 38 def self.unregister self.print "Unregistering '#{self.podspec.name}' > #{Dir.pwd}" db = self.registerd_db db.delete(self.podspec.name) self.write_db(REGISTERED_DB, db) end
Private Class Methods
Retrieve a link for the given name from the database. If the link does not exist in the for the given name then this will return nil
@param name the name of the link to retrieve from the database
@return the link for the given name or nil
# File lib/pod/links.rb, line 205 def self.get_registered_link(name) if self.registerd_db.has_key?(name) return self.registerd_db[name] end return nil end
Retrieve the linked database from disk
@returns the linked database
# File lib/pod/links.rb, line 190 def self.linked_db if File.exists?(LINKED_DB) return JSON.parse(File.read(LINKED_DB)) end return {} end
Retrieve the names of the linked pods for the current project (e.g. the current directory)
@returns a list of pods that are linked for the current project
# File lib/pod/links.rb, line 217 def self.linked_pods if self.linked_db.has_key?(Dir.pwd) return self.linked_db[Dir.pwd]['pods'] end return [] end
Read the podspec in the current working directory
@returns the podspec
# File lib/pod/links.rb, line 229 def self.podspec spec = Dir["#{Dir.pwd}/*.podspec"] if spec.empty? help! 'A .podspec must exist in the directory `pod link` is ran' end return Specification.from_file(spec.fetch(0)) end
Retrieve the registered links database from disk
@returns the registered links database
# File lib/pod/links.rb, line 178 def self.registerd_db if File.exists?(REGISTERED_DB) return JSON.parse(File.read(REGISTERED_DB)) end return {} end
Will write the provided database to disk with the newly provided link content
@param filename the name of the file to write the links to @param links the content to write to disk
# File lib/pod/links.rb, line 243 def self.write_db(db_path, db, entry = {}) dirname = File.dirname(db_path) unless File.directory?(dirname) FileUtils.mkdir_p(dirname) end File.open(db_path,'w') do |f| f.write(JSON.pretty_generate(db.merge(entry))) end end