Permalink
Browse files

[Source] Add support for data providers

  • Loading branch information...
1 parent bbe3f99 commit 4a1be34aa58e54c7c1b9be09a7d8bb3f1a802597 @fabiopelosin fabiopelosin committed Dec 2, 2013
@@ -2,6 +2,7 @@
require 'cocoapods-core/source/aggregate'
require 'cocoapods-core/source/health_reporter'
require 'cocoapods-core/source/abstract_data_provider'
+require 'cocoapods-core/source/file_system_data_provider'
require 'cocoapods-core/source/github_data_provider'
module Pod
@@ -18,20 +19,31 @@ module Pod
#
class Source
- # @return [Pathname] the location of the repo of the source.
+ # @return [AbstractDataProvider] the data provider for this source.
#
- attr_reader :repo
+ attr_accessor :data_provider
# @param [Pathname, String] repo @see #repo.
#
- def initialize(repo)
- @repo = Pathname.new(repo)
+ def initialize(repo = nil)
+ # TODO: Temporary solution to aide the transition
+ if repo.is_a?(String) || repo.is_a?(Pathname)
+ @data_provider = FileSystemDataProvider.new(repo)
+ else
+ @data_provider = repo
+ end
end
- # @return [String] the name of the source.
+ # @return [String] The name of the source.
#
def name
- repo.basename.to_s
+ data_provider.name
+ end
+
+ # @return [String] The type of the source.
+ #
+ def type
+ data_provider.type
end
alias_method :to_s, :name
@@ -47,37 +59,27 @@ def <=>(other)
name <=> other.name
end
- #-------------------------------------------------------------------------#
+ # @return [String] A description suitable for debugging.
+ #
+ def inspect
+ "#<#{self.class} name:#{name} type:#{type}>"
+ end
+
+ public
# @!group Queering the source
+ #-------------------------------------------------------------------------#
# @return [Array<String>] the list of the name of all the Pods.
#
- # @note Using Pathname#children is sensibly slower.
#
def pods
- specs_dir_as_string = specs_dir.to_s
- Dir.entries(specs_dir).select do |entry|
- valid_name = !(entry == '.' || entry == '..' || entry == '.git')
- valid_name && File.directory?(File.join(specs_dir_as_string, entry))
+ pods = data_provider.pods
+ unless pods
+ raise Informative, "Unable to find the #{data_provider.type} source " \
+ "named: `#{data_provider.name}`"
end
- end
-
- # Returns the set for the Pod with the given name.
- #
- # @param [String] pod_name
- # The name of the Pod.
- #
- # @return [Sets] the set.
- #
- def set(pod_name)
- Specification::Set.new(pod_name, self)
- end
-
- # @return [Array<Sets>] the sets of all the Pods.
- #
- def pod_sets
- pods.map { |pod_name| set(pod_name) }
+ pods
end
# @return [Array<Version>] all the available versions for the Pod, sorted
@@ -87,12 +89,8 @@ def pod_sets
# the name of the Pod.
#
def versions(name)
- pod_dir = specs_dir + name
- return unless pod_dir.exist?
- pod_dir.children.map do |v|
- basename = v.basename.to_s
- Version.new(basename) if v.directory? && basename[0, 1] != '.'
- end.compact.sort.reverse
+ versions = data_provider.versions(name)
+ versions.map { |version| Version.new(version) } if versions
end
# @return [Specification] the specification for a given version of Pod.
@@ -123,7 +121,7 @@ def specification_path(name, version)
raise StandardError, "Unable to find the specification #{name} " \
"(#{version}) in the #{name} source."
end
- specification_path
+ spec
end
# @return [Array<Specification>] all the specifications contained by the
@@ -141,9 +139,43 @@ def all_specs
specs.flatten.compact
end
- #-------------------------------------------------------------------------#
+ # Returns the set for the Pod with the given name.
+ #
+ # @param [String] pod_name
+ # The name of the Pod.
+ #
+ # @return [Sets] the set.
+ #
+ def set(pod_name)
+ Specification::Set.new(pod_name, self)
+ end
+
+ # @return [Array<Sets>] the sets of all the Pods.
+ #
+ def pod_sets
+ pods.map { |pod_name| set(pod_name) }
+ end
+
+ # Returns the path of the specification with the given name and version.
+ #
+ # @param [String] name
+ # the name of the Pod.
+ #
+ # @param [Version,String] version
+ # the version for the specification.
+ #
+ # @return [Pathname] The path of the specification.
+ #
+ # @todo Remove.
+ #
+ def specification_path(name, version)
+ data_provider.specification_path(name, version)
+ end
+
+ public
# @!group Searching the source
+ #-------------------------------------------------------------------------#
# @return [Set] a set for a given dependency. The set is identified by the
# name of the dependency and takes into account subspecs.
@@ -173,22 +205,28 @@ def search(query)
# @note full text search requires to load the specification for each pod,
# hence is considerably slower.
#
+ # @todo Rename to #search
+ #
def search_by_name(query, full_text_search = false)
regexp_query = /#{query}/i
if full_text_search
- pod_sets.reject do |set|
- texts = []
- begin
- s = set.specification
- texts << s.name
- texts += s.authors.keys
- texts << s.summary
- texts << s.description
- rescue
- CoreUI.warn "Skipping `#{set.name}` because the podspec " \
- "contains errors."
+ if data_provider.is_a?(FileSystemDataProvider)
+ pod_sets.reject do |set|
+ texts = []
+ begin
+ s = set.specification
+ texts << s.name
+ texts += s.authors.keys
+ texts << s.summary
+ texts << s.description
+ rescue
+ CoreUI.warn "Skipping `#{set.name}` because the podspec " \
+ "contains errors."
+ end
+ texts.grep(regexp_query).empty?
end
- texts.grep(regexp_query).empty?
+ else
+ []
end
else
names = pods.grep(regexp_query)
@@ -212,9 +250,10 @@ def fuzzy_search(query)
end
end
- #-------------------------------------------------------------------------#
+ public
# @!group Representations
+ #-------------------------------------------------------------------------#
# @return [Hash{String=>{String=>Specification}}] the static representation
# of all the specifications grouped first by name and then by
@@ -238,29 +277,26 @@ def to_yaml
private
- #-------------------------------------------------------------------------#
-
# @group Private Helpers
+ #-------------------------------------------------------------------------#
- # @return [Pathname] The directory where the specs are stored.
+ # Loads the specification for the given Pod gracefully.
#
- # @note In previous versions of CocoaPods they used to be stored in
- # the root of the repo. This lead to issues, especially with
- # the GitHub interface and now the are stored in a dedicated
- # folder.
+ # @param [String] name
+ # the name of the Pod.
#
- def specs_dir
- unless @specs_dir
- specs_sub_dir = repo + 'Specs'
- if specs_sub_dir.exist?
- @specs_dir = specs_sub_dir
- elsif repo.exist?
- @specs_dir = repo
- else
- raise Informative, "Unable to find a source named: `#{name}`"
- end
- end
- @specs_dir
+ # @return [Specification] The specification for the last version of the
+ # Pod.
+ # @return [Nil] If the spec could not be loaded.
+ #
+ def load_spec_gracefully(name)
+ versions = versions(name)
+ version = versions.sort.last if versions
+ specification(name, version) if version
+ rescue Informative
+ Pod::CoreUI.warn "Skipping `#{name}` because the podspec " \
+ "contains errors."
+ nil
end
#-------------------------------------------------------------------------#
@@ -111,7 +111,7 @@ def specification_path(name, version)
raise ArgumentError, "No version" unless version
return nil unless specs_dir
path = specs_dir + name + version.to_s
- specification_path = path + "#{name}.podspec.yaml"
+ specification_path = path + "#{name}.podspec.json"
specification_path.exist?
unless specification_path.exist?
specification_path = path + "#{name}.podspec"
@@ -124,7 +124,7 @@ def check_spec_path(name, version, spec)
# @return [void]
#
def check_stray_specs
- all_paths = Pathname.glob(source.repo + '**/*.podspec{,.json}')
+ all_paths = Pathname.glob(source.data_provider.repo + '**/*.podspec{,.json}')
stray_specs = all_paths - report.analyzed_paths
stray_specs.each do |path|
report.add_message(:error, "Stray spec", path)
@@ -220,7 +220,7 @@ def save_cache
def compute_creation_date(set)
date = get_value(set, :creation_date)
unless date
- Dir.chdir(set.sources.first.repo) do
+ Dir.chdir(set.sources.first.data_provider.repo) do
git_log = `git log --first-parent --format=%ct "#{set.name}"`
creation_date = git_log.split("\n").last.to_i
date = Time.at(creation_date)
@@ -20,7 +20,7 @@ module Pod
describe "#pods" do
it "returns the available Pods" do
- @sut.pods.should == ["BananaLib", "Faulty_spec", "JSONKit", "YAMLSpec"]
+ @sut.pods.should == ["BananaLib", "Faulty_spec", "IncorrectPath", "JSONKit", "JSONSpec"]
end
it "returns nil if no Pods could be found" do
@@ -39,7 +39,6 @@ module Pod
@sut.pods.should == []
end
-
it "uses the `Specs` dir if it is present" do
@sut.send(:specs_dir).to_s.should.end_with('test_repo/Specs')
end
@@ -109,10 +108,10 @@ module Pod
path.to_s.should.end_with?('test_repo/Specs/JSONKit/1.4/JSONKit.podspec')
end
- it "prefers YAML podspecs if one exists" do
+ it "prefers JSON podspecs if one exists" do
Pathname.any_instance.stubs(:exist?).returns(true)
- path = @sut.specification_path('JSONKit', "1.4")
- path.to_s.should.end_with?('test_repo/Specs/JSONKit/1.4/JSONKit.podspec.yaml')
+ path = @sut.specification_path('JSONSpec', "0.9")
+ path.to_s.should.end_with?('Specs/JSONSpec/0.9/JSONSpec.podspec.json')
end
it "raises if the name of the Pod is not provided" do
Oops, something went wrong.

0 comments on commit 4a1be34

Please sign in to comment.