Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dnkoutso
Created March 20, 2020 17:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnkoutso/899eb25ac81e1c3ca1887534f9be379c to your computer and use it in GitHub Desktop.
Save dnkoutso/899eb25ac81e1c3ca1887534f9be379c to your computer and use it in GitHub Desktop.
A CocoaPods source that discovers podspecs locally.
module Pod
class LocalDependenciesSource < Source
attr_reader :sandbox
attr_reader :local_podspecs_by_name
def self.register(plugin_name, sandbox, local_podspec_dependencies)
return unless local_podspec_dependencies
# Register a source that can be used to provide local podspecs
HooksManager.register(plugin_name, :source_provider) do |context|
context.add_source(LocalDependenciesSource.new(sandbox, local_podspec_dependencies))
end
end
def initialize(sandbox, podspec_paths)
@repo = Pathname(Dir.pwd).expand_path
@sandbox = sandbox
@local_podspecs_by_name = parse_local_podspec_dependencies(podspec_paths)
end
# Methods overrides for Pod::Source (Based on CocoaPods 1.3.1)
def name
'LocalDependenciesSource'
end
def versions(name)
return local_podspecs_by_name[name][:versions]
end
def specification_path(name, version)
Pathname(File.join(Dir.pwd, local_podspecs_by_name[name][:path]))
end
def search(query)
podspec_name = query.name
# Check if we have been told where to find this dependency
return unless local_podspecs_by_name.keys.include?(podspec_name)
# Add the podspec to the sandbox so it will be treated as a local dependency
podspec_path = local_podspecs_by_name[podspec_name][:path]
# Use the version from the Podspec as the source of truth
spec = Specification.from_file(podspec_path)
local_podspecs_by_name[podspec_name][:versions] = [spec.version]
# Load the local path podspec into the sandbox for dependency resolver to know these as "local dependecies"
local_podspec = Pod::ExternalSources::PathSource.new(podspec_name, { path: File.expand_path(podspec_path, Dir.pwd) }, Dir.pwd)
local_podspec.fetch(sandbox)
# return the podspec
Specification::Set.new(query.name, self)
end
# Block any unnecessary Pod::Source methods to make failure point obvious on a CocoaPods upgrades
def specs_dir
raise Informative, 'Unused for the purposes of the LocalDependenciesSource'
end
def url
raise Informative, 'Unused for the purposes of the LocalDependenciesSource'
end
def pod_path(name)
raise Informative, 'Unused for the purposes of the LocalDependenciesSource'
end
def pods_for_specification_paths(spec_paths)
raise Informative, 'Unused for the purposes of the LocalDependenciesSource'
end
def pods
raise Informative, 'Unused for the purposes of the LocalDependenciesSource'
end
def update(show_output)
raise Informative, 'Unused for the purposes of the LocalDependenciesSource'
end
def git?
raise Informative, 'Unused for the purposes of the LocalDependenciesSource'
end
private
def parse_local_podspec_dependencies(local_podspec_paths)
parsed_podspecs = {}
local_podspec_paths.each do |podspec_path|
spec = Specification.from_file(podspec_path)
parsed_podspecs[spec.name] = { path: podspec_path }
end
parsed_podspecs
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment