Class: Pod::Generator::PrefixHeader

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

Overview

Generates a prefix header file for a Pods library. The prefix header is generated according to the platform of the target and the pods.

According to the platform the prefix header imports UIKit/UIKit.h or Cocoa/Cocoa.h.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_accessors, platform) ⇒ PrefixHeader

Returns a new instance of PrefixHeader.

Parameters:

  • platform (Platform)

    @see platform

  • consumers (Array<LocalPod>)

    @see consumers



28
29
30
31
32
# File 'lib/cocoapods/generator/prefix_header.rb', line 28

def initialize(file_accessors, platform)
  @file_accessors = file_accessors
  @platform = platform
  @imports = []
end

Instance Attribute Details

#file_accessorsArray<FileAccessor> (readonly)

Returns The file accessors for which to generate the prefix header.

Returns:

  • (Array<FileAccessor>)

    The file accessors for which to generate the prefix header.



13
14
15
# File 'lib/cocoapods/generator/prefix_header.rb', line 13

def file_accessors
  @file_accessors
end

#importsArray<String> (readonly)

Returns The list of the headers to import (with quotes).

Returns:

  • (Array<String>)

    The list of the headers to import (with quotes).



23
24
25
# File 'lib/cocoapods/generator/prefix_header.rb', line 23

def imports
  @imports
end

#platformPlatform (readonly)

Returns the platform for which the prefix header will be generated.

Returns:

  • (Platform)

    the platform for which the prefix header will be generated.



18
19
20
# File 'lib/cocoapods/generator/prefix_header.rb', line 18

def platform
  @platform
end

Instance Method Details

#generateString

TODO:

Subspecs can specify prefix header information too.

TODO:

Check to see if we have a similar duplication issue with file_accessor.prefix_header.

Note:

If the platform is iOS an import call to UIKit/UIKit.h is added to the top of the prefix header. For OS X Cocoa/Cocoa.h is imported.

Note:

Only unique prefix_header_contents are added to the prefix header.

Generates the contents of the prefix header according to the platform and the pods.

Returns:

  • (String)


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

def generate
  result =  "#ifdef __OBJC__\n"
  result << "#import #{platform == :ios ? '<UIKit/UIKit.h>' : '<Cocoa/Cocoa.h>'}\n"
  result << "#endif\n"

  imports.each do |import|
    result << %(\n#import "#{import}")
  end

  unique_prefix_header_contents = file_accessors.map do |file_accessor|
    file_accessor.spec_consumer.prefix_header_contents
  end.compact.uniq

  result << "\n"

  unique_prefix_header_contents.each do |prefix_header_contents|
    result << prefix_header_contents
    result << "\n"
  end

  file_accessors.each do |file_accessor|
    if prefix_header = file_accessor.prefix_header
      result << Pathname(prefix_header).read
    end
  end
  result
end

#save_as(path) ⇒ void

This method returns an undefined value.

Generates and saves the prefix header to the given path.

Parameters:

  • path (Pathname)

    the path where the prefix header should be stored.



85
86
87
# File 'lib/cocoapods/generator/prefix_header.rb', line 85

def save_as(path)
  path.open('w') { |header| header.write(generate) }
end