Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 481 lines (404 sloc) 16.468 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
require 'xcodeproj/config'

module Pod
  extend Config::Mixin

  def self._eval_podspec(path)
    string = File.open(path, 'r:utf-8') { |f| f.read }
    # TODO: work around for Rubinius incomplete encoding in 1.9 mode
    string.encode!('UTF-8') if string.respond_to?(:encoding) && string.encoding.name != "UTF-8"
    eval(string, nil, path.to_s)
  end

  class Specification
    autoload :Set, 'cocoapods/specification/set'
    autoload :Statistics, 'cocoapods/specification/statistics'

    ### Initalization

    # The file is expected to define and return a Pods::Specification.
    # If name is equals to nil it returns the top level Specification,
    # otherwise it returned the specification with the name that matches
    def self.from_file(path, subspec_name = nil)
      unless path.exist?
        raise Informative, "No podspec exists at path `#{path}'."
      end
      spec = ::Pod._eval_podspec(path)
      spec.defined_in_file = path
      spec.subspec_by_name(subspec_name)
    end

    def initialize(parent = nil, name = nil)
      @parent, @name = parent, name
      @define_for_platforms = [:osx, :ios]
      @clean_paths, @subspecs = [], []
      @deployment_target = {}
      unless parent
        @source = {:git => ''}
      end

      # multi-platform attributes
      %w[ source_files
resources
preserve_paths
exclude_header_search_paths
frameworks
libraries
dependencies
compiler_flags ].each do |attr|
        instance_variable_set( "@#{attr}", { :ios => [], :osx => [] } )
      end
      @xcconfig = { :ios => Xcodeproj::Config.new, :osx => Xcodeproj::Config.new }
      @header_dir = { :ios => nil, :osx => nil }

      yield self if block_given?
    end

    ### Meta programming

    # Creates a top level attribute reader. A lambda can
    # be passed to process the ivar before returning it
    def self.top_attr_reader(attr, read_lambda = nil)
      define_method(attr) do
        ivar = instance_variable_get("@#{attr}")
        @parent ? top_level_parent.send(attr) : ( read_lambda ? read_lambda.call(self, ivar) : ivar )
      end
    end

    # Creates a top level attribute writer. A lambda can
    # be passed to initalize the value
    def self.top_attr_writer(attr, init_lambda = nil)
      define_method("#{attr}=") do |value|
        raise Informative, "#{self.inspect} Can't set `#{attr}' for subspecs." if @parent
        instance_variable_set("@#{attr}", init_lambda ? init_lambda.call(value) : value);
      end
    end

    # Creates a top level attribute accessor. A lambda can
    # be passed to initialize the value in the attribute writer.
    def self.top_attr_accessor(attr, writer_labmda = nil)
      top_attr_reader attr
      top_attr_writer attr, writer_labmda
    end

    # Returns the value of the attribute for the active platform
    # chained with the upstream specifications. The ivar must store
    # the platform specific values as an array.
    def self.pltf_chained_attr_reader(attr)
      define_method(attr) do
        active_plaform_check
        ivar_value = instance_variable_get("@#{attr}")[active_platform]
        @parent ? @parent.send(attr) + ivar_value : ( ivar_value )
      end
    end

    def active_plaform_check
      raise Informative, "#{self.inspect} not activated for a platform before consumption." unless active_platform
    end

    # Attribute writer that works in conjuction with the PlatformProxy.
    def self.platform_attr_writer(attr, block = nil)
      define_method("#{attr}=") do |value|
        current = instance_variable_get("@#{attr}")
        @define_for_platforms.each do |platform|
          block ? current[platform] = block.call(value, current[platform]) : current[platform] = value
        end
      end
    end

    def self.pltf_chained_attr_accessor(attr, block = nil)
      pltf_chained_attr_reader(attr)
      platform_attr_writer(attr, block)
    end

    # The PlatformProxy works in conjuction with Specification#_on_platform.
    # It allows a syntax like `spec.ios.source_files = file`
    class PlatformProxy
      def initialize(specification, platform)
        @specification, @platform = specification, platform
      end

      %w{ source_files=
resource=
resources=
preserve_paths=
preserve_path=
xcconfig=
framework=
frameworks=
library=
libraries=
compiler_flags=
deployment_target=
header_dir=
dependency }.each do |method|
        define_method(method) do |args|
          @specification._on_platform(@platform) do
            @specification.send(method, args)
          end
        end
      end
    end

    def ios
      PlatformProxy.new(self, :ios)
    end

    def osx
      PlatformProxy.new(self, :osx)
    end

    ### Deprecated attributes - TODO: remove once master repo and fixtures have been updated

    attr_writer :part_of_dependency
    attr_writer :part_of

    top_attr_accessor :clean_paths, lambda { |patterns| pattern_list(patterns) }
    alias_method :clean_path=, :clean_paths=

    ### Regular attributes

    attr_accessor :parent
    attr_accessor :preferred_dependency

    def name
      @parent ? "#{@parent.name}/#{@name}" : @name
    end
    attr_writer :name

    ### Attributes that return the first value defined in the chain

    def platform
      @platform || ( @parent ? @parent.platform : nil )
    end

    def platform=(platform)
      @platform = Platform.new(*platform)
    end

    # If not platform is specified all the platforms are returned.
    def available_platforms
      platform.nil? ? @define_for_platforms.map { |platform| Platform.new(platform, deployment_target(platform)) } : [ platform ]
    end

    ### Top level attributes. These attributes represent the unique features of pod and can't be specified by subspecs.

    top_attr_accessor :defined_in_file
    top_attr_accessor :source
    top_attr_accessor :homepage
    top_attr_accessor :summary
    top_attr_accessor :documentation
    top_attr_accessor :requires_arc
    top_attr_accessor :license, lambda { |l| ( l.kind_of? String ) ? { :type => l } : l }
    top_attr_accessor :version, lambda { |v| Version.new(v) }
    top_attr_accessor :authors, lambda { |a| parse_authors(a) }

    top_attr_reader :description, lambda {|instance, ivar| ivar || instance.summary }
    top_attr_writer :description

    alias_method :author=, :authors=

    def self.parse_authors(*names_and_email_addresses)
      list = names_and_email_addresses.flatten
      unless list.first.is_a?(Hash)
        authors = list.last.is_a?(Hash) ? list.pop : {}
        list.each { |name| authors[name] = nil }
      end
      authors || list.first
    end

    ### Attributes **with** multiple platform support

    # @todo allow for subspecs
    #
    top_attr_accessor :header_mappings_dir, lambda { |file| Pathname.new(file) } # If not provided the headers files are flattened
    top_attr_accessor :prefix_header_file, lambda { |file| Pathname.new(file) }
    top_attr_accessor :prefix_header_contents


    pltf_chained_attr_accessor :source_files, lambda {|value, current| pattern_list(value) }
    pltf_chained_attr_accessor :resources, lambda {|value, current| pattern_list(value) }
    pltf_chained_attr_accessor :preserve_paths, lambda {|value, current| pattern_list(value) } # Paths that should not be cleaned
    pltf_chained_attr_accessor :exclude_header_search_paths, lambda {|value, current| pattern_list(value) } # Headers to be excluded from being added to search paths (RestKit)
    pltf_chained_attr_accessor :frameworks, lambda {|value, current| (current << value).flatten }
    pltf_chained_attr_accessor :libraries, lambda {|value, current| (current << value).flatten }

    alias_method :resource=, :resources=
    alias_method :preserve_path=, :preserve_paths=
    alias_method :framework=, :frameworks=
    alias_method :library=, :libraries=

    # @!method header_dir=
    #
    # @abstract The directory where to name space the headers files of
    # the specification.
    #
    # @param [String] The headers directory.
    #
    platform_attr_writer :header_dir, lambda { |dir, _| Pathname.new(dir) }

    # @abstract (see #header_dir=)
    #
    # @return [Pathname] The headers directory.
    #
    # @note If no value is provided it returns an empty {Pathname}.
    #
    def header_dir
      @header_dir[active_platform] || (@parent.header_dir if @parent) || Pathname.new('')
    end

    # @!method xcconfig=
    #
    platform_attr_writer :xcconfig, lambda {|value, current| current.tap { |c| c.merge!(value) } }

    def xcconfig
      raw_xconfig.dup.
        tap { |x| x.libraries.merge libraries }.
        tap { |x| x.frameworks.merge frameworks }
    end

    def raw_xconfig
      @parent ? @parent.raw_xconfig.merge(@xcconfig[active_platform]) : @xcconfig[active_platform]
    end


    def compiler_flags
      if @parent
        chained = @compiler_flags[active_platform].clone
        # TODO hack to get the parent's compiler flags without it being
        # converted to a String by Specification#compiler_flags.
        chained.unshift @parent.instance_variable_get(:@compiler_flags)[active_platform]
      else
        chained = @compiler_flags[active_platform].clone
        chained.unshift '-fobjc-arc' if @requires_arc
        chained.unshift ''
      end
      chained.join(' ')
    end

    platform_attr_writer :compiler_flags, lambda {|value, current| current << value }

    def dependency(*name_and_version_requirements)
      name, *version_requirements = name_and_version_requirements.flatten
      raise Informative, "A specification can't require self as a subspec" if name == self.name
      raise Informative, "A subspec can't require one of its parents specifications" if @parent && @parent.name.include?(name)
      dep = Dependency.new(name, *version_requirements)
      @define_for_platforms.each do |platform|
        @dependencies[platform] << dep
      end
      dep
    end

    # External dependencies are inherited by subspecs
    def external_dependencies(all_platforms = false)
      active_plaform_check unless all_platforms
      result = all_platforms ? @dependencies.values.flatten : @dependencies[active_platform]
      result += parent.external_dependencies if parent
      result
    end

    # A specification inherits the preferred_dependency or
    # all the compatible subspecs as dependencies
    def subspec_dependencies
      active_plaform_check
      specs = preferred_dependency ? [subspec_by_name("#{name}/#{preferred_dependency}")] : subspecs
      specs.compact \
        .select { |s| s.supports_platform?(active_platform) } \
        .map { |s| Dependency.new(s.name, version) }
    end

    def dependencies
      external_dependencies + subspec_dependencies
    end

    include Config::Mixin

    def top_level_parent
      @parent ? @parent.top_level_parent : self
    end

    def subspec?
      !@parent.nil?
    end

    def subspec(name, &block)
      subspec = Specification.new(self, name, &block)
      @subspecs << subspec
      subspec
    end
    attr_reader :subspecs

    def recursive_subspecs
      unless @recursive_subspecs
        mapper = lambda do |spec|
            spec.subspecs.map do |subspec|
              [subspec, *mapper.call(subspec)]
            end.flatten
          end
          @recursive_subspecs = mapper.call self
      end
      @recursive_subspecs
    end

    def subspec_by_name(name)
      return self if name.nil? || name == self.name
      # Remove this spec's name from the beginning of the name we’re looking for
      # and take the first component from the remainder, which is the spec we need
      # to find now.
      remainder = name[self.name.size+1..-1].split('/')
      subspec_name = remainder.shift
      subspec = subspecs.find { |s| s.name == "#{self.name}/#{subspec_name}" }
      # If this was the last component in the name, then return the subspec,
      # otherwise we recursively keep calling subspec_by_name until we reach the
      # last one and return that

      raise Informative, "Unable to find a subspec named `#{name}'." unless subspec
      remainder.empty? ? subspec : subspec.subspec_by_name(name)
    end

    def local?
      !source.nil? && !source[:local].nil?
    end

    def local_path
      Pathname.new(File.expand_path(source[:local]))
    end

    def pod_destroot
      if local?
        local_path
      else
        config.project_pods_root + top_level_parent.name
      end
    end

    def self.pattern_list(patterns)
      if patterns.is_a?(Array) && (!defined?(Rake) || !patterns.is_a?(Rake::FileList))
        patterns
      else
        [patterns]
      end
    end

    # This method takes a header path and returns the location it should have
    # in the pod's header dir.
    #
    # By default all headers are copied to the pod's header dir without any
    # namespacing. However if the top level attribute accessor header_mappings_dir
    # is specified the namespacing will be preserved from that directory.
    def copy_header_mapping(from)
      header_mappings_dir ? from.relative_path_from(header_mappings_dir) : from.basename
    end

    # This is a convenience method which gets called after all pods have been
    # downloaded, installed, and the Xcode project and related files have been
    # generated. (It receives the Pod::Installer::Target instance for the current
    # target.) Override this to, for instance, add to the prefix header:
    #
    # Pod::Spec.new do |s|
    # def s.post_install(target)
    # prefix_header = config.project_pods_root + target.prefix_header_filename
    # prefix_header.open('a') do |file|
    # file.puts(%{#ifdef __OBJC__\n#import "SSToolkitDefines.h"\n#endif})
    # end
    # end
    # end
    def post_install(target)
    end

    def podfile?
      false
    end

    # This is used by the specification set
    def dependency_by_top_level_spec_name(name)
      external_dependencies(true).each do |dep|
        return dep if dep.top_level_spec_name == name
      end
    end

    def to_s
      "#{name} (#{version})"
    end

    def inspect
      "#<#{self.class.name} for #{to_s}>"
    end

    def ==(other)
      object_id == other.object_id ||
        (self.class === other &&
         name && name == other.name &&
         version && version == other.version)
    end

    # Returns whether the specification is supported in a given platform
    def supports_platform?(*platform)
      platform = platform[0].is_a?(Platform) ? platform[0] : Platform.new(*platform)
      available_platforms.any? { |p| platform.supports?(p) }
    end

    # Defines the active platform for comsumption of the specification and
    # returns self for method chainability.
    # The active platform must the the same accross the chain so attributes
    # that are inherited can be correctly resolved.
    def activate_platform(*platform)
      platform = platform[0].is_a?(Platform) ? platform[0] : Platform.new(*platform)
      raise Informative, "#{to_s} is not compatible with #{platform}." unless supports_platform?(platform)
      top_level_parent.active_platform = platform.to_sym
      self
    end

    top_attr_accessor :active_platform

    ### Not attributes

    # @visibility private
    #
    # This is used by PlatformProxy to assign attributes for the scoped platform.
    def _on_platform(platform)
      before, @define_for_platforms = @define_for_platforms, [platform]
      yield
    ensure
      @define_for_platforms = before
    end

    # @visibility private
    #
    # This is multi-platform and to support
    # subspecs with different platforms is is resolved as the
    # first non nil value accross the chain.
    def deployment_target=(version)
      raise Informative, "The deployment target must be defined per platform like `s.ios.deployment_target = '5.0'`." unless @define_for_platforms.count == 1
      @deployment_target[@define_for_platforms.first] = version
    end

    def deployment_target(platform)
      @deployment_target[platform] || ( @parent ? @parent.deployment_target(platform) : nil )
    end
  end
  Spec = Specification
end
Something went wrong with that request. Please try again.