Commit fdfca014 authored by Marius Rackwitz's avatar Marius Rackwitz

[Refactor] Extracted Generator::Header from PrefixHeader

Fixed wrong documented types drive-by.
parent 6555d4e0
...@@ -56,6 +56,7 @@ module Pod ...@@ -56,6 +56,7 @@ module Pod
autoload :BridgeSupport, 'cocoapods/generator/bridge_support' autoload :BridgeSupport, 'cocoapods/generator/bridge_support'
autoload :CopyResourcesScript, 'cocoapods/generator/copy_resources_script' autoload :CopyResourcesScript, 'cocoapods/generator/copy_resources_script'
autoload :DummySource, 'cocoapods/generator/dummy_source' autoload :DummySource, 'cocoapods/generator/dummy_source'
autoload :Header, 'cocoapods/generator/header'
autoload :PrefixHeader, 'cocoapods/generator/prefix_header' autoload :PrefixHeader, 'cocoapods/generator/prefix_header'
autoload :TargetEnvironmentHeader, 'cocoapods/generator/target_environment_header' autoload :TargetEnvironmentHeader, 'cocoapods/generator/target_environment_header'
autoload :XCConfig, 'cocoapods/generator/xcconfig' autoload :XCConfig, 'cocoapods/generator/xcconfig'
......
module Pod
module Generator
# Generates a header file.
#
# According to the platform the header imports `UIKit/UIKit.h` or
# `Cocoa/Cocoa.h`.
#
class Header
# @return [Symbol] the platform for which the prefix header will be
# generated.
#
attr_reader :platform
# @return [Array<String>] The list of the headers to import.
#
attr_reader :imports
# @param [Symbol] platform
# @see platform
#
def initialize(platform)
@platform = platform
@imports = []
end
# Generates the contents of the header according to the platform.
#
# @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.
#
# @return [String]
#
def generate
result = ""
result << generate_platform_import_header
unless imports.empty?
imports.each do |import|
result << %|\n#import "#{import}"|
end
end
result
end
# Generates and saves the header to the given path.
#
# @param [Pathname] path
# the path where the header should be stored.
#
# @return [void]
#
def save_as(path)
path.open('w') { |header| header.write(generate) }
end
#-----------------------------------------------------------------------#
protected
# Generates the contents of the header according to the platform.
#
# @note If the platform is iOS an import call to `UIKit/UIKit.h` is
# added to the top of the header. For OS X `Cocoa/Cocoa.h` is
# imported.
#
# @return [String]
#
def generate_platform_import_header
"#import #{platform == :ios ? '<UIKit/UIKit.h>' : '<Cocoa/Cocoa.h>'}\n"
end
end
end
end
...@@ -6,38 +6,26 @@ module Pod ...@@ -6,38 +6,26 @@ module Pod
# According to the platform the prefix header imports `UIKit/UIKit.h` or # According to the platform the prefix header imports `UIKit/UIKit.h` or
# `Cocoa/Cocoa.h`. # `Cocoa/Cocoa.h`.
# #
class PrefixHeader class PrefixHeader < Header
# @return [Array<FileAccessor>] The file accessors for which to generate # @return [Array<FileAccessor>] The file accessors for which to generate
# the prefix header. # the prefix header.
# #
attr_reader :file_accessors attr_reader :file_accessors
# @return [Platform] the platform for which the prefix header will be # @param [Array<FileAccessor>] file_accessors
# generated. # @see file_accessors
# #
attr_reader :platform # @param [Platform] platform
# @see platform
# @return [Array<String>] The list of the headers to import (with
# quotes).
#
attr_reader :imports
# @param [Platform] platform @see platform
# @param [Array<LocalPod>] consumers @see consumers
# #
def initialize(file_accessors, platform) def initialize(file_accessors, platform)
@file_accessors = file_accessors @file_accessors = file_accessors
@platform = platform super platform
@imports = []
end end
# Generates the contents of the prefix header according to the platform # Generates the contents of the prefix header according to the platform
# and the pods. # and the pods.
# #
# @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 # @note Only unique prefix_header_contents are added to the prefix
# header. # header.
# #
...@@ -48,20 +36,14 @@ module Pod ...@@ -48,20 +36,14 @@ module Pod
# file_accessor.prefix_header. # file_accessor.prefix_header.
# #
def generate def generate
result = "#ifdef __OBJC__\n" result = super
result << "#import #{platform == :ios ? '<UIKit/UIKit.h>' : '<Cocoa/Cocoa.h>'}\n"
result << "#endif\n"
imports.each do |import| result << "\n"
result << %(\n#import "#{import}")
end
unique_prefix_header_contents = file_accessors.map do |file_accessor| unique_prefix_header_contents = file_accessors.map do |file_accessor|
file_accessor.spec_consumer.prefix_header_contents file_accessor.spec_consumer.prefix_header_contents
end.compact.uniq end.compact.uniq
result << "\n"
unique_prefix_header_contents.each do |prefix_header_contents| unique_prefix_header_contents.each do |prefix_header_contents|
result << prefix_header_contents result << prefix_header_contents
result << "\n" result << "\n"
...@@ -75,15 +57,16 @@ module Pod ...@@ -75,15 +57,16 @@ module Pod
result result
end end
# Generates and saves the prefix header to the given path. protected
#
# @param [Pathname] path # Generates the contents of the header according to the platform.
# the path where the prefix header should be stored.
# #
# @return [void] # @return [String]
# #
def save_as(path) def generate_platform_import_header
path.open('w') { |header| header.write(generate) } result = "#ifdef __OBJC__\n"
result << super
result << "#endif\n"
end end
end end
end end
......
require File.expand_path('../../../spec_helper', __FILE__)
module Pod
describe Header = Generator::Header do
before do
@gen = Header.new(Pod::Platform.ios)
end
it 'includes the imports' do
@gen.imports << 'header.h'
@gen.generate.should == <<-EOS.strip_heredoc.chomp
#import <UIKit/UIKit.h>
#import "header.h"
EOS
end
it 'imports UIKit in iOS platforms' do
@gen.stubs(:platform).returns(Pod::Platform.ios)
@gen.generate.should.include?('#import <UIKit/UIKit.h>')
end
it 'imports Cocoa for OS X platforms' do
@gen.stubs(:platform).returns(Pod::Platform.osx)
@gen.generate.should.include?('#import <Cocoa/Cocoa.h>')
end
it 'writes the header file to the disk' do
path = temporary_directory + 'Test.h'
@gen.save_as(path)
path.read.should == <<-EOS.strip_heredoc
#import <UIKit/UIKit.h>
EOS
end
end
end
...@@ -80,16 +80,6 @@ module Pod ...@@ -80,16 +80,6 @@ module Pod
EOS EOS
end end
it 'imports UIKit in iOS platforms' do
@gen.stubs(:platform).returns(Pod::Platform.ios)
@gen.generate.should.include?('#import <UIKit/UIKit.h>')
end
it 'imports Cocoa for OS X platforms' do
@gen.stubs(:platform).returns(Pod::Platform.osx)
@gen.generate.should.include?('#import <Cocoa/Cocoa.h>')
end
it 'writes the prefix header file to the disk' do it 'writes the prefix header file to the disk' do
path = temporary_directory + 'Test.pch' path = temporary_directory + 'Test.pch'
@gen.save_as(path) @gen.save_as(path)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment