/
target.rb
340 lines (269 loc) · 11.5 KB
/
target.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
require 'xcodeproj'
require 'fileutils'
module FrameworkGenerate
class Target
attr_accessor :name, :platforms, :language, :info_plist, :bundle_id, :header, :include_files, :exclude_files, :resource_files, :dependencies, :type, :pre_build_scripts, :post_build_scripts, :test_target, :is_safe_for_extensions, :enable_code_coverage, :launch_arguments, :environment_variables
def initialize(name = nil, platforms = nil, language = nil, info_plist = nil, bundle_id = nil, header = nil, include_files = nil, exclude_files = nil, resource_files = nil, dependencies = nil, type = :framework, pre_build_scripts = nil, post_build_scripts = nil, test_target = nil, is_safe_for_extensions = false, enable_code_coverage = false, launch_arguments = nil, environment_variables = nil)
@name = name
@platforms = platforms
@language = language
@info_plist = info_plist
@bundle_id = bundle_id
@header = header
@include_files = include_files
@exclude_files = exclude_files
@resource_files = resource_files
@dependencies = dependencies
@type = type
@pre_build_scripts = pre_build_scripts
@post_build_scripts = post_build_scripts
@test_target = test_target
@is_safe_for_extensions = is_safe_for_extensions
@enable_code_coverage = enable_code_coverage
@launch_arguments = launch_arguments
@environment_variables = environment_variables
yield(self) if block_given?
end
def to_s
"Target<#{name}, #{info_plist}, #{bundle_id}, #{header}, #{include_files}, #{exclude_files}, #{dependencies}, #{type}, #{test_target}, #{is_safe_for_extensions}, #{enable_code_coverage}>"
end
def target_build_settings(settings, has_frameworks)
settings.delete('CODE_SIGN_IDENTITY')
settings.delete('CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING')
settings.delete('CLANG_WARN_COMMA')
settings.delete('CLANG_WARN_NON_LITERAL_NULL_CONVERSION')
settings.delete('CLANG_WARN_OBJC_LITERAL_CONVERSION')
settings.delete('CLANG_WARN_RANGE_LOOP_ANALYSIS')
settings.delete('CLANG_WARN_STRICT_PROTOTYPES')
settings['INFOPLIST_FILE'] = @info_plist
settings['PRODUCT_BUNDLE_IDENTIFIER'] = @bundle_id
settings['APPLICATION_EXTENSION_API_ONLY'] = @is_safe_for_extensions ? 'YES' : 'NO'
settings['SUPPORTED_PLATFORMS'] = FrameworkGenerate::Platform.supported_platforms(@platforms)
macos = FrameworkGenerate::Platform.find_platform(@platforms, :macos)
unless macos.nil?
settings['MACOSX_DEPLOYMENT_TARGET'] = FrameworkGenerate::Platform.deployment_target(macos)
if has_frameworks
settings['FRAMEWORK_SEARCH_PATHS[sdk=macosx*]'] = FrameworkGenerate::Platform.search_paths(macos)
end
end
ios = FrameworkGenerate::Platform.find_platform(@platforms, :ios)
unless ios.nil?
settings['IPHONEOS_DEPLOYMENT_TARGET'] = FrameworkGenerate::Platform.deployment_target(ios)
if has_frameworks
settings['FRAMEWORK_SEARCH_PATHS[sdk=iphone*]'] = FrameworkGenerate::Platform.search_paths(ios)
end
end
watchos = FrameworkGenerate::Platform.find_platform(@platforms, :watchos)
unless watchos.nil?
settings['WATCHOS_DEPLOYMENT_TARGET'] = FrameworkGenerate::Platform.deployment_target(watchos)
if has_frameworks
settings['FRAMEWORK_SEARCH_PATHS[sdk=watch*]'] = FrameworkGenerate::Platform.search_paths(watchos)
end
end
tvos = FrameworkGenerate::Platform.find_platform(@platforms, :tvos)
unless tvos.nil?
settings['TVOS_DEPLOYMENT_TARGET'] = FrameworkGenerate::Platform.deployment_target(tvos)
if has_frameworks
settings['FRAMEWORK_SEARCH_PATHS[sdk=appletv*]'] = FrameworkGenerate::Platform.search_paths(tvos)
end
end
settings['SWIFT_VERSION'] = @language.version
settings
end
def find_group(project, path)
folder_path = File.dirname(path)
project.main_group.find_subpath(folder_path, true)
end
def add_framework_header(project, target)
return if @header.nil?
header_path = @header
header_file_group = find_group(project, header_path)
header_file = header_file_group.new_reference(header_path)
header_build_file = target.headers_build_phase.add_file_reference(header_file, true)
header_build_file.settings ||= {}
header_build_file.settings['ATTRIBUTES'] = ['Public']
end
def add_info_plist(project)
info_plist_path = @info_plist
info_plist_group = find_group(project, info_plist_path)
has_info_plist = info_plist_group.find_file_by_path(info_plist_path)
info_plist_group.new_reference(@info_plist) unless has_info_plist
end
def add_supporting_files(project, target)
add_info_plist(project)
return if target.test_target_type?
add_framework_header(project, target)
end
def reject_excluded_files(exclude_files, path)
exclude_files.each do |files_to_exclude|
files_to_exclude.each do |file_to_exclude|
return true if File.fnmatch(file_to_exclude, path)
end
end
false
end
def add_source_files(project, target)
exclude_files = if @exclude_files.nil?
[]
else
@exclude_files.map do |files|
Dir[files]
end
end
source_files = @include_files.map do |files|
Dir[files].reject do |path|
reject_excluded_files(exclude_files, path)
end
end
source_files.each do |file_directory|
file_directory.each do |path|
source_file_group = find_group(project, path)
has_source_file = source_file_group.find_file_by_path(path)
unless has_source_file
source_file = source_file_group.new_reference(path)
target.source_build_phase.add_file_reference(source_file, true)
end
end
end
end
def append_framework_extension(framework)
return framework if File.extname(framework) == '.framework'
"#{framework}.framework"
end
def add_dependencies(project, target)
return if @dependencies.nil?
dependency_names = @dependencies.map do |dependency|
append_framework_extension(dependency)
end
frameworks = dependency_names.reject do |name|
!project.products.any? { |x| x.path == name }
end
frameworks = frameworks.map do |name|
project.products.find { |x| x.path == name }
end
frameworks.each do |path|
target.frameworks_build_phase.add_file_reference(path, true)
end
end
def copy_carthage_frameworks(project, build_phase, scripts_directory)
script_file_name = 'copy-carthage-frameworks.sh'
script_file_path = File.join(File.dirname(__FILE__), script_file_name)
if scripts_directory.nil?
script_file = File.open(script_file_path)
build_phase.shell_script = script_file.read
script_file.close
else
script_path = File.join(Dir.pwd, scripts_directory, script_file_name)
dirname = File.dirname(script_path)
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
FileUtils.cp(script_file_path, script_path, preserve: true)
xcode_path = File.join('${SRCROOT}', scripts_directory, script_file_name)
build_phase.shell_script = " exec \"#{xcode_path}\""
end
add_framework_to_copy_phase(project, build_phase)
end
def third_party_frameworks?(project)
return if @dependencies.nil?
dependency_names = @dependencies.map do |dependency|
append_framework_extension(dependency)
end
frameworks = dependency_names.reject do |name|
project.products.any? { |x| x.path == name }
end
frameworks.length > 0
end
def add_framework_to_copy_phase(project, build_phase)
return if @dependencies.nil?
dependency_names = @dependencies.map do |dependency|
append_framework_extension(dependency)
end
frameworks = dependency_names.reject do |name|
project.products.any? { |x| x.path == name }
end
frameworks.each do |path|
build_phase.input_paths << path
end
end
def add_resource_files(project, target)
return if @resource_files.nil?
files = @resource_files.map do |files|
Dir[files]
end
files.each do |file_directory|
file_directory.each do |path|
file_group = find_group(project, path)
has_file = file_group.find_file_by_path(path)
unless has_file
file = file_group.new_reference(path)
target.resources_build_phase.add_file_reference(file, true)
end
end
end
end
def add_build_scripts(target, scripts)
return if scripts.nil?
scripts.each do |script|
build_phase = target.new_shell_script_build_phase(script.name)
build_phase.shell_script = script.script
build_phase.input_paths = script.inputs
end
end
def add_pre_build_scripts(target)
add_build_scripts(target, @pre_build_scripts)
end
def add_post_build_scripts(target)
add_build_scripts(target, @post_build_scripts)
end
def add_launch_arguments(launch_action)
return if @launch_arguments.nil?
command_line_arguments = @launch_arguments.map do |launch_arguement|
{ argument: launch_arguement, enabled: true }
end
launch_action.command_line_arguments = Xcodeproj::XCScheme::CommandLineArguments.new(command_line_arguments)
end
def add_environment_variables(launch_action)
return if @environment_variables.nil?
environment_variables = @environment_variables.map do |key, value|
{ key: key, value: value, enabled: true }
end
launch_action.environment_variables = Xcodeproj::XCScheme::EnvironmentVariables.new(environment_variables)
end
def create(project, language, scripts_directory)
name = @name
type = @type
# Target
target = project.new(Xcodeproj::Project::Object::PBXNativeTarget)
project.targets << target
target.name = name
target.product_name = name
target.product_type = Xcodeproj::Constants::PRODUCT_TYPE_UTI[type]
target.build_configuration_list = Xcodeproj::Project::ProjectHelper.configuration_list(project, :osx, nil, type, language.type)
has_frameworks = third_party_frameworks?(project)
# Pre build script
add_pre_build_scripts(target)
add_supporting_files(project, target)
add_source_files(project, target)
target.build_configurations.each do |configuration|
target_build_settings(configuration.build_settings, has_frameworks)
end
# Product
product = project.products_group.new_product_ref_for_target(name, type)
target.product_reference = product
# Build phases
target.build_phases << project.new(Xcodeproj::Project::Object::PBXResourcesBuildPhase)
target.build_phases << project.new(Xcodeproj::Project::Object::PBXFrameworksBuildPhase)
# Post build script
add_post_build_scripts(target)
# Dependencies
add_dependencies(project, target)
# Resource files
add_resource_files(project, target)
# Copy frameworks to test target
if target.test_target_type? && has_frameworks
build_phase = target.new_shell_script_build_phase('Copy Carthage Frameworks')
copy_carthage_frameworks(project, build_phase, scripts_directory)
end
target
end
end
end