Module: Xcodeproj::Project::ProjectHelper

Includes:
Object
Defined in:
lib/xcodeproj/project/project_helper.rb

Targets collapse

Private Helpers collapse

Class Method Details

.common_build_settings(type, platform, deployment_target = nil, target_product_type = nil, language = :objc) ⇒ Hash

Returns the common build settings for a given platform and configuration name.

Parameters:

  • type (Symbol)

    the type of the build configuration, can be :release or :debug.

  • platform (Symbol)

    the platform for the build settings, can be :ios or :osx.

  • deployment_target (String) (defaults to: nil)

    the deployment target for the platform.

  • target_product_type (Symbol) (defaults to: nil)

    the product type of the target, can be any of Constants::PRODUCT_TYPE_UTI.values or Constants::PRODUCT_TYPE_UTI.keys. Default is :application.

  • language (Symbol) (defaults to: :objc)

    the primary language of the target, can be :objc or :swift.

Returns:

  • (Hash)

    The common build settings


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/xcodeproj/project/project_helper.rb', line 186

def self.common_build_settings(type, platform, deployment_target = nil, target_product_type = nil, language = :objc)
  target_product_type = (Constants::PRODUCT_TYPE_UTI.find { |_, v| v == target_product_type } || [target_product_type || :application])[0]
  common_settings = Constants::COMMON_BUILD_SETTINGS

  # Use intersecting settings for all key sets as base
  settings = deep_dup(common_settings[:all])

  # Match further common settings by key sets
  keys = [type, platform, target_product_type, language].compact
  key_combinations = (1..keys.length).flat_map { |n| keys.combination(n).to_a }
  key_combinations.each do |key_combination|
    settings.merge!(deep_dup(common_settings[key_combination] || {}))
  end

  if deployment_target
    if platform == :ios
      settings['IPHONEOS_DEPLOYMENT_TARGET'] = deployment_target
    elsif platform == :osx
      settings['MACOSX_DEPLOYMENT_TARGET'] = deployment_target
    end
  end

  settings
end

.configuration_list(project, platform, deployment_target = nil, target_product_type) ⇒ XCConfigurationList

Returns a new configuration list, populated with release and debug configurations with common build settings for the given platform.

Parameters:

  • project (Project)

    the project to which the configuration list should be added.

  • platform (Symbol)

    the platform for the configuration list, can be :ios or :osx.

  • deployment_target (String) (defaults to: nil)

    the deployment target for the platform.

  • target_product_type (Symbol)

    the product type of the target, can be any of Constants::PRODUCT_TYPE_UTI.values or Constants::PRODUCT_TYPE_UTI.keys.

Returns:


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/xcodeproj/project/project_helper.rb', line 145

def self.configuration_list(project, platform, deployment_target = nil, target_product_type)
  cl = project.new(XCConfigurationList)
  cl.default_configuration_is_visible = '0'
  cl.default_configuration_name = 'Release'

  release_conf = project.new(XCBuildConfiguration)
  release_conf.name = 'Release'
  release_conf.build_settings = common_build_settings(:release, platform, deployment_target, target_product_type)

  debug_conf = project.new(XCBuildConfiguration)
  debug_conf.name = 'Debug'
  debug_conf.build_settings = common_build_settings(:debug, platform, deployment_target, target_product_type)

  cl.build_configurations << release_conf
  cl.build_configurations << debug_conf
  cl
end

.deep_dup(object) ⇒ Object

Creates a deep copy of the given object

Parameters:

  • object (Object)

    the object to copy.

Returns:

  • (Object)

    The deeply copy of the obejct object.


218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/xcodeproj/project/project_helper.rb', line 218

def self.deep_dup(object)
  case object
  when Hash
    new_hash = {}
    object.each do |key, value|
      new_hash[key] = deep_dup(value)
    end
    new_hash
  when Array
    object.map { |value| deep_dup(value) }
  else
    object.dup
  end
end

.new_resources_bundle(project, name, platform, product_group) ⇒ PBXNativeTarget

Creates a new resource bundles target and adds it to the project.

The target is configured for the given platform and its file reference it is added to the Xcodeproj::Project#products_group.

The target is pre-populated with common build settings

Parameters:

  • project (Project)

    the project to which the target should be added.

  • name (String)

    the name of the resources bundle.

  • platform (Symbol)

    the platform of the resources bundle. Can be :ios or :osx.

  • product_group (PBXGroup)

    the product group, where to add to a file reference of the created target.

Returns:


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
# File 'lib/xcodeproj/project/project_helper.rb', line 87

def self.new_resources_bundle(project, name, platform, product_group)
  # Target
  target = project.new(PBXNativeTarget)
  project.targets << target
  target.name = name
  target.product_name = name
  target.product_type = Constants::PRODUCT_TYPE_UTI[:bundle]

  build_settings = common_build_settings(nil, platform, nil, target.product_type)

  # Configuration List
  cl = project.new(XCConfigurationList)
  cl.default_configuration_is_visible = '0'
  cl.default_configuration_name = 'Release'
  release_conf = project.new(XCBuildConfiguration)
  release_conf.name = 'Release'
  release_conf.build_settings = build_settings
  debug_conf = project.new(XCBuildConfiguration)
  debug_conf.name = 'Debug'
  debug_conf.build_settings = build_settings
  cl.build_configurations << release_conf
  cl.build_configurations << debug_conf
  target.build_configuration_list = cl

  # Product
  product = product_group.new_bundle(name)
  target.product_reference = product

  # Build phases
  target.build_phases << project.new(PBXSourcesBuildPhase)
  target.build_phases << project.new(PBXFrameworksBuildPhase)
  target.build_phases << project.new(PBXResourcesBuildPhase)

  target
end

.new_target(project, type, name, platform, deployment_target, product_group) ⇒ PBXNativeTarget

Creates a new target and adds it to the project.

The target is configured for the given platform and its file reference it is added to the Xcodeproj::Project#products_group.

The target is pre-populated with common build settings, and the appropriate Framework according to the platform is added to to its Frameworks phase.

Parameters:

  • project (Project)

    the project to which the target should be added.

  • type (Symbol)

    the type of target. Can be :application, :dynamic_library, framework or :static_library.

  • name (String)

    the name of the target product.

  • platform (Symbol)

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

  • deployment_target (String)

    the deployment target for the platform.

  • product_group (PBXGroup)

    the product group, where to add to a file reference of the created target.

Returns:


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/xcodeproj/project/project_helper.rb', line 41

def self.new_target(project, type, name, platform, deployment_target, product_group)
  # Target
  target = project.new(PBXNativeTarget)
  project.targets << target
  target.name = name
  target.product_name = name
  target.product_type = Constants::PRODUCT_TYPE_UTI[type]
  target.build_configuration_list = configuration_list(project, platform, deployment_target, type)

  # Product
  product = product_group.new_product_ref_for_target(name, type)
  target.product_reference = product

  # Build phases
  target.build_phases << project.new(PBXSourcesBuildPhase)
  target.build_phases << project.new(PBXFrameworksBuildPhase)

  # Frameworks
  framework_name = (platform == :ios) ? 'Foundation' : 'Cocoa'
  target.add_system_framework(framework_name)

  target
end