forked from Unity-Technologies/unity-ads-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-project.rb
executable file
·259 lines (213 loc) · 10.5 KB
/
generate-project.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
#!/usr/bin/env ruby
require 'bundler'
require 'fileutils'
Bundler.require(:default)
# Handle command line arguments
opts = Trollop::options do
opt :configuration_type, "Type of configuration to use options: ['dev', 'library']",
:type => :string, :default => 'dev'
opt :test_target_name, "Name of the test target to build",
:type => :string, :default => 'UnityAdsTests'
opt :example_target_name, "Name of the example target to build",
:type => :string, :default => 'UnityAdsExample'
end
project_configuration_type_name = opts[:configuration_type]
test_target_name = opts[:test_target_name]
example_target_name = opts[:example_target_name]
if ARGV.length > 0
raise "Unkown arguments '#{ARGV}', check usage with '--help' flag!"
end
# create group for current dir_name
# add files from the current dir_name
# venture downwards if we have directories
def create_groups_from_dir(root_dir, parent_group, target, is_example_project = false)
Dir.glob(root_dir).select{|d| File.directory? d}.each do |subdirectory|
dir_name = File.basename(subdirectory)
Dir.chdir(subdirectory)
g = parent_group.new_group(dir_name)
if !is_example_project
Dir.glob("#{target}-Bridging-Header.h") do |f|
file_to_add = g.new_file(Dir.pwd + '/' + f)
target.build_configurations.each do |bc|
bc.build_settings['SWIFT_OBJC_BRIDGING_HEADER'] = "#{target}/#{f}"
end
end
end
Dir.glob("*.pch") do |f|
g.new_file(Dir.pwd + '/' + f)
target.build_configurations.each do |bc|
bc.build_settings['GCC_PREFIX_HEADER'] = "#{target}/#{f}"
end
end
Dir.glob("Info.plist") do |f|
file_to_add = g.new_file(Dir.pwd + '/' + f)
target.build_configurations.each do |bc|
bc.build_settings['INFOPLIST_FILE'] = "$(SRCROOT)/#{target}/#{f}"
end
end
Dir.glob("*.xcassets") do |f|
file_to_add = g.new_file(Dir.pwd + '/' + f)
target.add_resources([file_to_add])
end
Dir.glob("*.storyboard") do |f|
file_to_add = g.new_file(Dir.pwd + '/' + f)
target.add_resources([file_to_add])
end
Dir.glob("*.swift") do |f|
file_to_add = g.new_file(Dir.pwd + '/' + f)
target.source_build_phase.add_file_reference(file_to_add, true)
end
if !is_example_project
Dir.glob("*.m") do |f|
file_to_add = g.new_file(Dir.pwd + '/' + f)
target.source_build_phase.add_file_reference(file_to_add, true)
end
Dir.glob("*.h") do |f|
file_to_add = g.new_file(Dir.pwd + '/' + f)
added_file = target.headers_build_phase.add_file_reference(file_to_add, true)
added_file.settings ||= {}
if "#{f}" == "#{target}.h" or "#{f}" =~ /UADS(.*)MetaData\.h/ or "#{f}" =~ /UnityAdsExtended\.h/
added_file.settings['ATTRIBUTES'] = ['Public']
else
added_file.settings['ATTRIBUTES'] = ['Project']
end
end
end
create_groups_from_dir("#{Dir.pwd}/*", g, target)
Dir.chdir("../")
end
end
def generate_framework_project(xcode_project_name, project_name, test_target_name, example_target_name)
FileUtils.rm_rf(xcode_project_name)
project = Xcodeproj::Project.new(project_name)
# Construct targets
framework_target = project.new_target(:framework, project_name, :ios)
framework_test_target = project.new_target(:unit_test_bundle, project_name + "Tests", :ios)
@framework_example_target = project.new_target(:application, project_name + "Example", :ios)
# Use our function to add resources to targets from subdirectories
create_groups_from_dir("#{project_name}", project, framework_target)
create_groups_from_dir("#{test_target_name}", project, framework_test_target)
create_groups_from_dir("#{example_target_name}", project, @framework_example_target)
# Configure the framework target
framework_target.build_configurations.each do |bc|
bc.build_settings['PRODUCT_BUNDLE_IDENTIFIER'] = "com.unity3d.ads.UnityAds"
bc.build_settings['CURRENT_PROJECT_VERSION'] = 1
bc.build_settings['HEADER_SEARCH_PATHS'] = "UnityAds/"
bc.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = "7.0"
end
# Configure the example target
@framework_example_target.build_configurations.each do |bc|
bc.build_settings['PRODUCT_BUNDLE_IDENTIFIER'] = "com.unity3d.ads.example"
bc.build_settings['CURRENT_PROJECT_VERSION'] = 1
bc.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = "7.0"
bc.build_settings['LD_RUNPATH_SEARCH_PATHS'] = ["$(inherited)", "@executable_path/Frameworks", "@loader_path/Frameworks"]
end
# Configure the test target
framework_test_target.build_configurations.each do |bc|
bc.build_settings['PRODUCT_BUNDLE_IDENTIFIER'] = "com.unity3d.ads.UnityAdsTests"
bc.build_settings['FRAMEWORK_SEARCH_PATHS'] = ["$(PROJECT_DIR)/UnityAds", "$(inherited)"]
bc.build_settings['HEADER_SEARCH_PATHS'] = ["$(TARGET_TEMP_DIR)/../$(PROJECT_NAME).build/DerivedSources", "$(PROJECT_DIR)/UnityAds"]
bc.build_settings['LD_RUNPATH_SEARCH_PATHS'] = ["$(inherited)", "@executable_path/Frameworks", "@loader_path/Frameworks"]
bc.build_settings['TEST_HOST'] = "$(BUILT_PRODUCTS_DIR)/UnityAdsExample.app/UnityAdsExample"
end
# Add target dependencies
framework_test_target.add_dependency(framework_target)
framework_test_target.add_dependency(@framework_example_target)
@framework_example_target.add_dependency(framework_target)
# Add copy files build phase to example target build configuration
copy_framework_to_example_phase = project.new(Xcodeproj::Project::PBXCopyFilesBuildPhase)
copy_framework_to_example_phase.symbol_dst_subfolder_spec = :frameworks
framework_file = project.products[0]
copied_framework_file_reference = copy_framework_to_example_phase.add_file_reference(framework_file)
copied_framework_file_reference.settings ||= {}
copied_framework_file_reference.settings['ATTRIBUTES'] = ['CodeSignOnCopy']
@framework_example_target.build_phases << copy_framework_to_example_phase
# Configure the example target as the test host for the test target
project.root_object.attributes["TargetAttributes"] = Hash["#{framework_test_target.uuid}" => Hash["TestTargetID" => "#{@framework_example_target.uuid}"]]
# Load Saved Scheme
scheme_dir = 'xcschemes/'
scheme_name = 'UnityAds.xcscheme'
temp_file = File.join(scheme_dir, scheme_name)
scheme = Xcodeproj::XCScheme.new(temp_file)
# Serialize and save project + scheme
project.save(xcode_project_name)
scheme.save_as(xcode_project_name, project_name, true)
end
def generate_static_library_project(xcode_project_name, project_name)
FileUtils.rm_rf(xcode_project_name)
project = Xcodeproj::Project.new(project_name)
# Construct targets
framework_target = project.new_target(:bundle, project_name, :ios)
# Use our function to add resources to targets from subdirectories
create_groups_from_dir("#{project_name}", project, framework_target)
# Configure the example target
framework_target.build_configurations.each do |bc|
bc.build_settings['PRODUCT_BUNDLE_IDENTIFIER'] = "com.unity3d.ads.UnityAds"
bc.build_settings['CURRENT_PROJECT_VERSION'] = 1
bc.build_settings['HEADER_SEARCH_PATHS'] = "UnityAds/"
bc.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = "$(TARGET_$(CURRENT_ARCH))"
bc.build_settings['SDKROOT'] = "iphoneos"
bc.build_settings['ONLY_ACTIVE_ARCH'] = "NO"
bc.build_settings['ENABLE_BITCODE'] = "YES"
bc.build_settings['BITCODE_GENERATION_MODE'] = "bitcode"
bc.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = "7.0"
bc.build_settings['ARCHS'] = "$(ARCHS_$(XCODE_VERSION_MAJOR))"
bc.build_settings['HIDE_BITCODE_SYMBOLS'] = "NO"
bc.build_settings['STRIP_BITCODE_FROM_COPIED_FILES'] = "NO"
bc.build_settings['CLANG_ENABLE_MODULES'] = "YES"
bc.build_settings['ARCHS_0800'] = "$(ARCHS_STANDARD) armv7s"
bc.build_settings['ARCHS_0700'] = "$(ARCHS_STANDARD) armv7s"
bc.build_settings['ARCHS_0600'] = "$(ARCHS_STANDARD) armv7s"
bc.build_settings['ARCHS_0500'] = "$(ARCHS_STANDARD_INCLUDING_64_BIT)"
bc.build_settings['ARCHS_0400'] = "$(ARCHS_STANDARD_32_BIT)"
bc.build_settings['TARGET_arm64'] = "7.0"
bc.build_settings['TARGET_armv7'] = "7.0"
bc.build_settings['TARGET_x86_64'] = "7.0"
bc.build_settings['TARGET_i386'] = "7.0"
bc.build_settings['TARGET_armv7s'] = "7.0"
bc.build_settings['HIDE_BITCODE_SYMBOLS'] = "NO"
bc.build_settings['STRIP_BITCODE_FROM_COPIED_FILES'] = "NO"
bc.build_settings['CLANG_ENABLE_MODULES'] = "YES"
bc.build_settings['WRAPPER_EXTENSION'] = "framework"
bc.build_settings['FRAMEWORK_VERSION'] = "A"
bc.build_settings['MACH_O_TYPE'] = "mh_object"
bc.build_settings['DYLIB_CURRENT_VERSION'] = "1"
bc.build_settings['DEAD_CODE_STRIPPING'] = "NO"
bc.build_settings['SKIP_INSTALL'] = "NO"
bc.build_settings['INSTALL_PATH'] = "$(BUILT_PRODUCTS_DIR)"
bc.build_settings['DYLIB_COMPATIBILITY_VERSION'] = "1"
bc.build_settings['LINK_WITH_STANDARD_LIBRARIES'] = "NO"
bc.build_settings['GCC_PRECOMPILE_PREFIX_HEADER'] = "YES"
bc.build_settings['BUILD_DIR'] = "$(SRCROOT)/build"
bc.build_settings['CONTENTS_FOLDER_PATH'] = "$(WRAPPER_NAME)"
bc.build_settings['FRAMEWORK_SEARCH_PATHS'] = ""
bc.build_settings['GCC_PREFIX_HEADER'] = "UnityAds/PrefixHeader.pch"
bc.build_settings['INFOPLIST_FILE'] = "UnityAds/Info.plist"
bc.build_settings['INFOPLIST_PATH'] = "$(UNLOCALIZED_RESOURCES_FOLDER_PATH)/Info.plist"
bc.build_settings['PRODUCT_NAME'] = "$(TARGET_NAME)"
bc.build_settings['COPY_PHASE_STRIP'] = "YES"
if bc.name == "Debug"
bc.build_settings['ENABLE_BITCODE'] = "NO"
end
end
setup_build_phase = project.new(Xcodeproj::Project::Object::PBXShellScriptBuildPhase)
setup_build_phase.shell_script = "./scripts/clean-framework.sh"
framework_target.build_phases.unshift(setup_build_phase)
header_build_phase = project.new(Xcodeproj::Project::Object::PBXShellScriptBuildPhase)
header_build_phase.shell_script = "./scripts/export-framework.sh"
framework_target.build_phases << header_build_phase
# Load Saved Scheme
scheme_dir = 'xcschemes/'
scheme_name = 'UnityAdsStaticLibrary.xcscheme'
temp_file = File.join(scheme_dir, scheme_name)
scheme = Xcodeproj::XCScheme.new(temp_file)
# Serialize and save project + scheme
project.save(xcode_project_name)
scheme.save_as(xcode_project_name, project_name, true)
end
if project_configuration_type_name == "dev"
generate_framework_project("UnityAds.xcodeproj", "UnityAds", test_target_name, example_target_name)
end
if project_configuration_type_name == "library"
generate_static_library_project("UnityAdsStaticLibrary.xcodeproj", "UnityAds")
end