-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.rb
340 lines (276 loc) · 9.84 KB
/
build.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
# frozen_string_literal: true
require 'fileutils'
# Builder service to automate the whole build process.
class Bob
attr_reader :version
def initialize
unless ARGV.empty?
@version = ARGV[0]
return
end
puts <<~DESC
Missing version argument.
To run the command use:
ruby build.rb 3.0.0
DESC
exit 1
end
def work
puts "▸ Building version #{version}".cyan
prepare_environment
puts '▸ Cleaning...'.cyan
Xcode.clean
puts '▸ Building...'.cyan
Xcode.build
puts '▸ Generating SPM artefacts...'.cyan
SPM.new(version).generate_artefacts
puts '▸ Generating Cocoapods artefacts...'.cyan
Cocoapods.new(version).generate_artefacts
puts '▸ Done! 🚀'.cyan
end
class << self
def zip(working_directory:, files:, output:)
system("cd #{working_directory} && zip -rq #{output} #{files}", exception: true)
end
end
private
def prepare_environment
if ENV['NOTIFICARE_SDK_DISTRIBUTION_CERTIFICATE'].to_s.strip.empty?
puts 'Unable to find the code signing certificate information.'.red
exit 1
end
FileUtils.rm_rf '.build'
FileUtils.mkdir_p '.build/archives'
FileUtils.mkdir_p '.build/intermediates'
FileUtils.mkdir_p '.build/outputs'
FileUtils.mkdir_p '.build/tmp'
end
end
# Utility to enumerate the buildable modules.
class Framework
attr_reader :scheme, :spm_zip_filename, :spm_checksum_placeholder
def initialize(scheme:, spm_zip_filename:, spm_checksum_placeholder:)
@scheme = scheme
@spm_zip_filename = spm_zip_filename
@spm_checksum_placeholder = spm_checksum_placeholder
end
class << self
def all
[
Framework.new(scheme: 'NotificareKit',
spm_zip_filename: 'spm-notificare.zip',
spm_checksum_placeholder: '{{NOTIFICARE_CHECKSUM}}'),
Framework.new(scheme: 'NotificareAssetsKit',
spm_zip_filename: 'spm-notificare-assets.zip',
spm_checksum_placeholder: '{{NOTIFICARE_ASSETS_CHECKSUM}}'),
Framework.new(scheme: 'NotificareGeoKit',
spm_zip_filename: 'spm-notificare-geo.zip',
spm_checksum_placeholder: '{{NOTIFICARE_GEO_CHECKSUM}}'),
Framework.new(scheme: 'NotificareInAppMessagingKit',
spm_zip_filename: 'spm-notificare-in-app-messaging.zip',
spm_checksum_placeholder: '{{NOTIFICARE_IN_APP_MESSAGING_CHECKSUM}}'),
Framework.new(scheme: 'NotificareInboxKit',
spm_zip_filename: 'spm-notificare-inbox.zip',
spm_checksum_placeholder: '{{NOTIFICARE_INBOX_CHECKSUM}}'),
Framework.new(scheme: 'NotificareLoyaltyKit',
spm_zip_filename: 'spm-notificare-loyalty.zip',
spm_checksum_placeholder: '{{NOTIFICARE_LOYALTY_CHECKSUM}}'),
Framework.new(scheme: 'NotificareNotificationServiceExtensionKit',
spm_zip_filename: 'spm-notificare-notification-service-extension.zip',
spm_checksum_placeholder: '{{NOTIFICARE_NOTIFICATION_SERVICE_EXTENSION_CHECKSUM}}'),
Framework.new(scheme: 'NotificarePushKit',
spm_zip_filename: 'spm-notificare-push.zip',
spm_checksum_placeholder: '{{NOTIFICARE_PUSH_CHECKSUM}}'),
Framework.new(scheme: 'NotificarePushUIKit',
spm_zip_filename: 'spm-notificare-push-ui.zip',
spm_checksum_placeholder: '{{NOTIFICARE_PUSH_UI_CHECKSUM}}'),
Framework.new(scheme: 'NotificareScannablesKit',
spm_zip_filename: 'spm-notificare-scannables.zip',
spm_checksum_placeholder: '{{NOTIFICARE_SCANNABLES_CHECKSUM}}'),
Framework.new(scheme: 'NotificareUserInboxKit',
spm_zip_filename: 'spm-notificare-user-inbox.zip',
spm_checksum_placeholder: '{{NOTIFICARE_USER_INBOX_CHECKSUM}}'),
Framework.new(scheme: 'NotificareUtilitiesKit',
spm_zip_filename: 'spm-notificare-utilities.zip',
spm_checksum_placeholder: '{{NOTIFICARE_UTILITIES_CHECKSUM}}'),
]
end
end
end
# Utility run xcodebuild tasks.
class Xcode
class << self
def clean
Framework.all.each do |framework|
puts "▸ Cleaning #{framework.scheme}".green
command = <<~COMMAND
xcodebuild clean \\
-workspace Notificare.xcworkspace \\
-scheme #{framework.scheme} \\
-destination "generic/platform=iOS" \\
-sdk iphoneos \\
-quiet
COMMAND
system(command, exception: true)
end
end
def build
Framework.all.each do |framework|
puts "▸ Building #{framework.scheme}".green
create_ios_device_archive(framework)
create_ios_simulator_archive(framework)
create_xcframework(framework)
sign_xcframework(framework)
end
end
private
def create_ios_device_archive(framework)
puts "▸ Creating #{framework.scheme} iOS device archive".green
command = <<~COMMAND
xcodebuild archive \\
-workspace Notificare.xcworkspace \\
-scheme #{framework.scheme} \\
-archivePath ".build/archives/#{framework.scheme}-iOS.xcarchive" \\
-destination "generic/platform=iOS" \\
-configuration Release \\
-sdk iphoneos \\
-quiet \\
SKIP_INSTALL=NO \\
BUILD_LIBRARY_FOR_DISTRIBUTION=YES
COMMAND
system(command, exception: true)
end
def create_ios_simulator_archive(framework)
puts "▸ Creating #{framework.scheme} iOS simulator archive".green
command = <<~COMMAND
xcodebuild archive \\
-workspace Notificare.xcworkspace \\
-scheme #{framework.scheme} \\
-archivePath ".build/archives/#{framework.scheme}-iOS-simulator.xcarchive" \\
-destination "generic/platform=iOS Simulator" \\
-configuration Release \\
-sdk iphonesimulator \\
-quiet \\
SKIP_INSTALL=NO \\
BUILD_LIBRARY_FOR_DISTRIBUTION=YES
COMMAND
system(command, exception: true)
end
def create_xcframework(framework)
puts "▸ Creating #{framework.scheme} XCFramework".green
command = <<~COMMAND
xcodebuild -create-xcframework \\
-framework ".build/archives/#{framework.scheme}-iOS.xcarchive/Products/Library/Frameworks/#{framework.scheme}.framework" \\
-debug-symbols #{File.expand_path(".build/archives/#{framework.scheme}-iOS.xcarchive/dSYMs/#{framework.scheme}.framework.dSYM")} \\
-framework ".build/archives/#{framework.scheme}-iOS-simulator.xcarchive/Products/Library/Frameworks/#{framework.scheme}.framework" \\
-debug-symbols #{File.expand_path(".build/archives/#{framework.scheme}-iOS-simulator.xcarchive/dSYMs/#{framework.scheme}.framework.dSYM")} \\
-output ".build/intermediates/#{framework.scheme}.xcframework"
COMMAND
system(command, exception: true)
end
def sign_xcframework(framework)
puts "▸ Signing #{framework.scheme} XCFramework".green
command = <<~COMMAND
codesign --timestamp -v \\
--sign "$NOTIFICARE_SDK_DISTRIBUTION_CERTIFICATE" \\
".build/intermediates/#{framework.scheme}.xcframework"
COMMAND
system(command, exception: true)
end
end
end
# Utility to prepare SPM artefacts.
class SPM
def initialize(version)
@version = version
end
def generate_artefacts
Framework.all.each do |framework|
create_zip_file(framework)
create_checksum_file(framework)
end
create_config_file
end
private
def create_zip_file(framework)
FileUtils.cp_r ".build/intermediates/#{framework.scheme}.xcframework", '.build/tmp'
Bob.zip(working_directory: '.build/tmp',
files: "#{framework.scheme}.xcframework",
output: "../outputs/#{framework.spm_zip_filename}")
end
def create_config_file
config_file = File.read '.github/templates/Package.swift'
config_file = config_file.gsub(/{{(.*?)}}/) do |key|
if key == '{{VERSION}}'
@version
else
framework = Framework.all.find { |f| f.spm_checksum_placeholder == key }
calculate_checksum(framework)
end
end
File.write 'Package.swift', config_file
end
def calculate_checksum(framework)
`swift package compute-checksum .build/outputs/#{framework.spm_zip_filename}`.strip
end
def create_checksum_file(framework)
File.write ".build/outputs/#{framework.spm_zip_filename}.checksum.txt", calculate_checksum(framework)
end
end
# Utility to prepare Cocoapods artefacts.
class Cocoapods
def initialize(version)
@version = version
end
def generate_artefacts
prepare_temp_folder
create_zip_file
create_config_file
end
def create_config_file
config_file = File.read '.github/templates/Notificare.podspec'
config_file = config_file.gsub(/{{(.*?)}}/) do |key|
@version if key == '{{VERSION}}'
end
File.write 'Notificare.podspec', config_file
end
private
def prepare_temp_folder
FileUtils.mkdir_p '.build/tmp/Notificare'
FileUtils.cp 'LICENSE', '.build/tmp/Notificare'
Framework.all.each { |f| FileUtils.cp_r ".build/intermediates/#{f.scheme}.xcframework", '.build/tmp/Notificare' }
end
def create_zip_file
Bob.zip(working_directory: '.build/tmp',
files: 'Notificare',
output: '../outputs/cocoapods.zip')
end
end
class String
def red
colorize(31)
end
def green
colorize(32)
end
def yellow
colorize(33)
end
def blue
colorize(34)
end
def pink
colorize(35)
end
def cyan
colorize(36)
end
private
def colorize(color_code)
"\e[#{color_code}m#{self}\e[0m"
end
end
#
# Do the work. 🛠️
#
Bob.new.work