Class: Pod::Generator::TargetEnvironmentHeader

Inherits:
Object
  • Object
show all
Defined in:
lib/cocoapods/generator/target_environment_header.rb

Overview

Generates a header which allows to inspect at compile time the installed pods and the installed specifications of a pod.

Example output:

#define COCOAPODS_POD_AVAILABLE_ObjectiveSugar 1
#define COCOAPODS_VERSION_MAJOR_ObjectiveSugar 0
#define COCOAPODS_VERSION_MINOR_ObjectiveSugar 6
#define COCOAPODS_VERSION_PATCH_ObjectiveSugar 2

Example usage:

#ifdef COCOAPODS
  #ifdef COCOAPODS_POD_AVAILABLE_ObjectiveSugar
    #import "ObjectiveSugar.h"
  #endif
#else
  // Non CocoaPods code
#endif

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specs) ⇒ TargetEnvironmentHeader

Returns a new instance of TargetEnvironmentHeader.

Parameters:

  • pods (Array<LocalPod>)

    @see pods



32
33
34
# File 'lib/cocoapods/generator/target_environment_header.rb', line 32

def initialize(specs)
  @specs = specs
end

Instance Attribute Details

#specsArray<LocalPod> (readonly)

Returns the specifications installed for the target.

Returns:

  • (Array<LocalPod>)

    the specifications installed for the target.



28
29
30
# File 'lib/cocoapods/generator/target_environment_header.rb', line 28

def specs
  @specs
end

Instance Method Details

#save_as(pathname) ⇒ void

This method returns an undefined value.

Generates and saves the file.

Parameters:

  • pathname (Pathname)

    The path where to save the generated file.



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
# File 'lib/cocoapods/generator/target_environment_header.rb', line 43

def save_as(pathname)
  pathname.open('w') do |source|
    source.puts
    source.puts "// To check if a library is compiled with CocoaPods you"
    source.puts "// can use the `COCOAPODS` macro definition which is"
    source.puts "// defined in the xcconfigs so it is available in"
    source.puts "// headers also when they are imported in the client"
    source.puts "// project."
    source.puts
    source.puts
    specs.each do |spec|
      spec_name = safe_spec_name(spec.name)
      source.puts "// #{spec.name}"
      source.puts "#define COCOAPODS_POD_AVAILABLE_#{spec_name}"
      if spec.version.semantic?
        source.puts "#define COCOAPODS_VERSION_MAJOR_#{spec_name} #{spec.version.major}"
        source.puts "#define COCOAPODS_VERSION_MINOR_#{spec_name} #{spec.version.minor}"
        source.puts "#define COCOAPODS_VERSION_PATCH_#{spec_name} #{spec.version.patch}"
      else
        source.puts "// This library does not follow semantic-versioning,"
        source.puts "// so we were not able to define version macros."
        source.puts "// Please contact the author."
        source.puts "// Version: #{spec.version}."
      end
      source.puts
    end
  end
end