Module: Pod::Generator::AppTargetHelper

Defined in:
lib/cocoapods/generator/app_target_helper.rb

Overview

Stores the common logic for creating app targets within projects including generating standard import and main files for app hosts.

Constant Summary collapse

IOS_APP_HOST_MAIN_CONTENTS =
"#import <Foundation/Foundation.h>\n#import <UIKit/UIKit.h>\n\n@interface CPTestAppHostAppDelegate : UIResponder <UIApplicationDelegate>\n\n@property (nonatomic, strong) UIWindow *window;\n\n@end\n\n@implementation CPTestAppHostAppDelegate\n\n- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions\n{\n    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];\n    self.window.rootViewController = [UIViewController new];\n\n    [self.window makeKeyAndVisible];\n\n    return YES;\n}\n\n@end\n\nint main(int argc, char *argv[])\n{\n    @autoreleasepool\n    {\n        return UIApplicationMain(argc, argv, nil, NSStringFromClass([CPTestAppHostAppDelegate class]));\n    }\n}\n".freeze
MACOS_APP_APP_HOST_MAIN_CONTENTS =
"#import <Cocoa/Cocoa.h>\n\nint main(int argc, const char * argv[]) {\n    return NSApplicationMain(argc, argv);\n}\n".freeze

Class Method Summary collapse

Class Method Details

.add_app_host_main_file(project, target, platform, name = 'App') ⇒ Array<PBXBuildFile>

Creates and links a default app host ‘main.m’ file.

Parameters:

  • project (Project)

    the Xcodeproj to generate the target into.

  • target (PBXNativeTarget)

    the native target to link the generated main file into.

  • platform (Symbol)

    the platform of the target. Can be ‘:ios` or `:osx`, etc.

  • name (String) (defaults to: 'App')

    The name to use for the target, defaults to ‘App’.

Returns:

  • (Array<PBXBuildFile>)

    the created build file references.



91
92
93
94
95
# File 'lib/cocoapods/generator/app_target_helper.rb', line 91

def self.add_app_host_main_file(project, target, platform, name = 'App')
  source_file = AppTargetHelper.create_app_host_main_file(project, platform, name)
  source_file_ref = project.new_group(name, name).new_file(source_file)
  target.add_file_references([source_file_ref])
end

.add_app_project_import(project, target, pod_target, platform, name = 'App') ⇒ Array<PBXBuildFile>

Creates and links an import file for the given pod target and into the given native target.

Parameters:

  • project (Project)

    the Xcodeproj to generate the target into.

  • target (PBXNativeTarget)

    the native target to link the generated import file into.

  • pod_target (PodTarget)

    the pod target to use for when generating the contents of the import file.

  • platform (Symbol)

    the platform of the target. Can be ‘:ios` or `:osx`, etc.

  • name (String) (defaults to: 'App')

    The name to use for the target, defaults to ‘App’.

Returns:

  • (Array<PBXBuildFile>)

    the created build file references.



46
47
48
49
50
51
# File 'lib/cocoapods/generator/app_target_helper.rb', line 46

def self.add_app_project_import(project, target, pod_target, platform, name = 'App')
  source_file = AppTargetHelper.create_app_import_source_file(project, pod_target, platform, name)
  group = project[name] || project.new_group(name, name)
  source_file_ref = group.new_file(source_file)
  target.add_file_references([source_file_ref])
end

.add_app_target(project, platform, deployment_target, name = 'App') ⇒ PBXNativeTarget

Adds a single app target to the given project with the provided name.

Parameters:

  • project (Project)

    the Xcodeproj to generate the target into.

  • platform (Symbol)

    the platform of the target. Can be ‘:ios` or `:osx`, etc.

  • deployment_target (String)

    the deployment target for the platform.

  • name (String) (defaults to: 'App')

    The name to use for the target, defaults to ‘App’.

Returns:

  • (PBXNativeTarget)

    the new target that was created.



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

def self.add_app_target(project, platform, deployment_target, name = 'App')
  project.new_target(:application, name, platform, deployment_target)
end

.add_empty_swift_file(project, target, name = 'App') ⇒ Array<PBXBuildFile>

Creates and links an empty Swift file for the given target.

Parameters:

  • project (Project)

    the Xcodeproj to generate the target into.

  • target (PBXNativeTarget)

    the native target to link the generated import file into.

  • name (String) (defaults to: 'App')

    The name to use for the target, defaults to ‘App’.

Returns:

  • (Array<PBXBuildFile>)

    the created build file references.



66
67
68
69
70
71
72
73
# File 'lib/cocoapods/generator/app_target_helper.rb', line 66

def self.add_empty_swift_file(project, target, name = 'App')
  swift_file = project.path.dirname.+("#{name}/dummy.swift")
  swift_file.parent.mkpath
  File.write(swift_file, '')
  group = project[name] || project.new_group(name, name)
  swift_file_ref = group.new_file(swift_file)
  target.add_file_references([swift_file_ref])
end

.add_swift_version(target, swift_version) ⇒ void

This method returns an undefined value.

Adds the provided swift version into the given target.

Parameters:

  • target (PBXNativeTarget)

    the native target to add the swift version into.

  • swift_version (String)

    the swift version to set to.



121
122
123
124
125
126
# File 'lib/cocoapods/generator/app_target_helper.rb', line 121

def self.add_swift_version(target, swift_version)
  raise 'Cannot set empty Swift version to target.' if swift_version.blank?
  target.build_configurations.each do |configuration|
    configuration.build_settings['SWIFT_VERSION'] = swift_version
  end
end

.add_xctest_search_paths(target) ⇒ void

This method returns an undefined value.

Adds the xctest framework search paths into the given target.

Parameters:

  • target (PBXNativeTarget)

    the native target to add XCTest into.



104
105
106
107
108
109
# File 'lib/cocoapods/generator/app_target_helper.rb', line 104

def self.add_xctest_search_paths(target)
  target.build_configurations.each do |configuration|
    search_paths = configuration.build_settings['FRAMEWORK_SEARCH_PATHS'] ||= '$(inherited)'
    search_paths << ' "$(PLATFORM_DIR)/Developer/Library/Frameworks"'
  end
end

.create_app_host_main_file(project, platform, name = 'App') ⇒ Pathname

Creates a default app host ‘main.m’ file.

Parameters:

  • project (Project)

    the Xcodeproj to generate the target into.

  • platform (Symbol)

    the platform of the target. Can be ‘:ios` or `:osx`.

  • name (String) (defaults to: 'App')

    The name of the folder to use and save the generated main file.

Returns:

  • (Pathname)

    the new source file that was generated.



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/cocoapods/generator/app_target_helper.rb', line 188

def self.create_app_host_main_file(project, platform, name = 'App')
  source_file = project.path.dirname.+("#{name}/main.m")
  source_file.parent.mkpath
  source_file.open('w') do |f|
    case platform
    when :ios, :tvos
      f << IOS_APP_HOST_MAIN_CONTENTS
    when :osx
      f << MACOS_APP_APP_HOST_MAIN_CONTENTS
    end
  end
  source_file
end

.create_app_import_source_file(project, pod_target, platform, name = 'App') ⇒ Pathname

Creates a default import file for the given pod target.

Parameters:

  • project (Project)

    the Xcodeproj to generate the target into.

  • pod_target (PodTarget)

    the pod target to use for when generating the contents of the import file.

  • platform (Symbol)

    the platform of the target. Can be ‘:ios` or `:osx`, etc.

  • name (String) (defaults to: 'App')

    The name of the folder to use and save the generated main file.

Returns:

  • (Pathname)

    the new source file that was generated.



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

def self.create_app_import_source_file(project, pod_target, platform, name = 'App')
  language = pod_target.uses_swift? ? :swift : :objc

  if language == :swift
    source_file = project.path.dirname.+("#{name}/main.swift")
    source_file.parent.mkpath
    import_statement = pod_target.should_build? && pod_target.defines_module? ? "import #{pod_target.product_module_name}\n" : ''
    source_file.open('w') { |f| f << import_statement }
  else
    source_file = project.path.dirname.+("#{name}/main.m")
    source_file.parent.mkpath
    import_statement = if pod_target.should_build? && pod_target.defines_module?
                         "@import #{pod_target.product_module_name};\n"
                       else
                         header_name = "#{pod_target.product_module_name}/#{pod_target.product_module_name}.h"
                         if pod_target.sandbox.public_headers.root.+(header_name).file?
                           "#import <#{header_name}>\n"
                         else
                           ''
                         end
                       end
    source_file.open('w') do |f|
      f << "@import Foundation;\n"
      f << "@import UIKit;\n" if platform == :ios || platform == :tvos
      f << "@import Cocoa;\n" if platform == :osx
      f << "#{import_statement}int main() {}\n"
    end
  end
  source_file
end