-
Notifications
You must be signed in to change notification settings - Fork 16
/
Rakefile
296 lines (224 loc) · 9.28 KB
/
Rakefile
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
require 'rake/clean'
require 'rake/packagetask'
require_relative 'Scripts/console'
#
# TODO: deliverables should live in INTERMEDIATESDIR until the very last possible moment, when they have been fully verified.
#
XCODE_BUILD_FILTER = "xcpretty -c"
XCODE_TEST_FILTER = "xcpretty -tc"
#
# Constants
#
RELEASE_BRANCHES = ["develop", "master"]
BUILDDIR = File.absolute_path("Build")
INTERMEDIATESDIR = "#{BUILDDIR}/Intermediates"
DERIVEDDATA = "#{INTERMEDIATESDIR}/DerivedData"
#
# Environmental
#
# Switch.xcodeproj
PROJECT = FileList['*.xcodeproj'][0]
# Switch
PRODUCT = PROJECT.slice(0..(PROJECT.index('.') - 1))
def git_branch
branch = `git symbolic-ref HEAD 2> /dev/null | sed -e 's|^refs/heads/||'`.strip
if branch == ""
branch = `git show-ref --head -s --abbrev | head -n1`.strip
end
return branch
end
BRANCH = git_branch
#
# Synthesized
#
BRANCH_IS_RELEASE = RELEASE_BRANCHES.include?(BRANCH)
BRANCH_IS_GM = (BRANCH == "master")
DELIVERABLE_ARCHIVE = File.absolute_path("#{BUILDDIR}/#{PRODUCT}.xcarchive")
DELIVERABLE_APP = File.absolute_path("#{BUILDDIR}/#{PRODUCT}.app")
DELIVERABLE_ZIP = File.absolute_path("#{BUILDDIR}/#{PRODUCT}.zip")
PODS_PROJECT_FILE = 'Pods/Pods.xcodeproj/project.pbxproj'
def formatted_fail(message)
fail Console.background_red(Console.white(message))
end
def check_ruby_version
ruby_version = RUBY_VERSION.split('.')
required_version = '2.0.0'.split('.')
formatted_fail "#{deliverable_path} wasn't produced!" if ruby_version.count > 3
(0..(ruby_version.count - 1)).each do |index|
if ruby_version[index] < required_version[index]
formatted_fail "Sorry, this Rakefile requires Ruby #{required_version.join('.')} or newer."
elsif ruby_version[index] > required_version[index]
break
end
end
end
check_ruby_version
CLOBBER.include(FileList["#{File.expand_path('~')}/Library/Developer/Xcode/DerivedData/#{PRODUCT}-*"])
CLOBBER.include(DERIVEDDATA)
CLEAN.include(FileList[BUILDDIR])
XCODEFLAGS = [
"-workspace \"#{PRODUCT}.xcworkspace\"",
"-scheme \"#{PRODUCT}\"",
"-derivedDataPath \"#{DERIVEDDATA}\"",
].join(' ')
#
# Helpers
#
def prettify_xcode_task(task, test=false)
unless shell_non_fatal("set -e; set -o pipefail; #{task} | #{test ? XCODE_TEST_FILTER : XCODE_BUILD_FILTER}")
Console.print(Console.background_red(Console.white("Task failed, retrying without filter")))
shell(task)
end
end
def run_task(task, args=nil)
if args.nil?
Rake::Task[task].invoke
else
Rake::Task[task].invoke(args)
end
end
def verify_codesign(app_path)
shell "codesign --verify --verbose --deep \"#{app_path}\""
shell "spctl --assess --verbose=4 --type execute \"#{app_path}\""
end
def verify_deliverable(deliverable_path)
formatted_fail "#{deliverable_path} wasn't produced!" unless File.exists? deliverable_path
end
def xcode(action)
run_task DERIVEDDATA
prettify_xcode_task("xcodebuild #{XCODEFLAGS} #{action}")
end
def shell(action)
Console.puts(Console.bold(Console.black("#{action}")))
formatted_fail "Shell command failed: #{action}" unless system(action)
end
def shell_non_fatal(action)
Console.puts(Console.bold(Console.black("#{action}")))
return system(action)
end
def echo_step(step)
Console.puts(Console.bold(Console.black(Console.background_white(step))))
end
#
# Targets
#
directory BUILDDIR
directory DERIVEDDATA
directory INTERMEDIATESDIR
task :default => [:analyze, :test]
task :pods => [PODS_PROJECT_FILE]
file PODS_PROJECT_FILE => 'Podfile' do
echo_step "Updating Pods"
if not shell_non_fatal "pod install"
shell "pod repo update > /dev/null"
shell "pod install"
end
end
desc "Install/update dependencies required for building the project."
task :deps do
echo_step "Installing/updating dependencies"
# Rakefile deps
# TODO: check if xcpretty gem is already installed
shell "gem install xcpretty" if not shell_non_fatal "which xcpretty"
# Submodules
shell "git submodule sync"
shell "git submodule update --init --recursive"
# Pods
# TODO: check if cocoapods gem is already installed
shell "gem install cocoapods" if not shell_non_fatal "which pod"
Rake::FileTask[PODS_PROJECT_FILE].invoke
end
# XXX: can these just be tasks dependant on another task with an argument?
task :analyze do
xcode "analyze"
end
desc "Build project."
task :build do
xcode "build"
end
task :clean do
xcode "clean"
end
desc "Run project unit tests."
task :test => [:build] do
echo_step("Testing #{PRODUCT}")
prettify_xcode_task("xcodebuild -scheme \"#{PRODUCT}\" -workspace \"#{PRODUCT}.xcworkspace\" -derivedDataPath \"#{DERIVEDDATA}\" test", true)
end
# :archive "Builds #{PRODUCT}.xcarchive in #{BUILDDIR}"
task :archive => [DELIVERABLE_ARCHIVE]
task DELIVERABLE_ARCHIVE => [File.dirname(DELIVERABLE_ARCHIVE)] do
echo_step("Building archive bundle: #{File.basename(DELIVERABLE_ARCHIVE)}")
archive_path = DELIVERABLE_ARCHIVE.slice(0..(DELIVERABLE_ARCHIVE.rindex('.') - 1))
xcode "-archivePath \"#{archive_path}\" archive"
verify_deliverable DELIVERABLE_ARCHIVE
Console.puts "Finished: #{Console.green(DELIVERABLE_ARCHIVE)}\n"
end
desc "Build application deliverable."
task :app => [DELIVERABLE_APP]
task DELIVERABLE_APP => [DELIVERABLE_ARCHIVE, File.dirname(DELIVERABLE_APP)] do
echo_step("Building application bundle: #{File.basename(DELIVERABLE_APP)}")
FileUtils.rm_r DELIVERABLE_APP if File.exist? DELIVERABLE_APP
app_path = DELIVERABLE_APP.slice(0..(DELIVERABLE_APP.rindex('/') - 1))
shell "xcodebuild -exportArchive -exportOptionsPlist exportOptionsApp.plist -archivePath \"#{DELIVERABLE_ARCHIVE}\" -exportPath \"#{app_path}\""
verify_deliverable DELIVERABLE_APP
if shell_non_fatal "codesign --force --deep --sign \"Developer ID\" \"#{DELIVERABLE_APP}\""
verify_codesign DELIVERABLE_APP
else
# If you're reading this message, you probably don't have a Developer ID certificate for signing the app.
# This is fine if you want to use your own build, but if you're planning on distributing the deliverables you're going to need to get a Developer ID certificate.
Console.puts ' _____________________________________ '
Console.puts '/ WARNING: unable to sign app \\'
Console.puts '\\ deliverable with Developer ID! /'
Console.puts ' ------------------------------------- '
Console.puts ' \\ ^__^ '
Console.puts ' \\ (oo)\\_______ '
Console.puts ' (__)\\ )\\/\\ '
Console.puts ' ||----w | '
Console.puts ' || || '
end
Console.puts "Finished: #{Console.green(DELIVERABLE_APP)}\n"
end
task :zip => [DELIVERABLE_ZIP]
task DELIVERABLE_ZIP => [DELIVERABLE_APP, File.dirname(DELIVERABLE_ZIP), INTERMEDIATESDIR] do
echo_step("Building zip archive: #{File.basename(DELIVERABLE_ZIP)}")
FileUtils.rm DELIVERABLE_ZIP if File.exist? DELIVERABLE_ZIP
shell "cd \"#{File.dirname(DELIVERABLE_APP)}\" && zip --symlinks -rq9o \"#{DELIVERABLE_ZIP}\" \"#{File.basename(DELIVERABLE_APP)}\""
verify_deliverable DELIVERABLE_ZIP
# Verify that zip didn't ruin code signing (this happened to Switch 0.0.6)
unzipped_app = "#{INTERMEDIATESDIR}/#{File.basename(DELIVERABLE_APP)}"
FileUtils.rm_r unzipped_app if File.exist? unzipped_app
shell "cd \"#{INTERMEDIATESDIR}\" && unzip -q \"#{DELIVERABLE_ZIP}\""
verify_deliverable unzipped_app
# Verify that unzipped app and app deliverable do not differ.
shell "diff -r \"#{unzipped_app}\" \"#{DELIVERABLE_APP}\""
# XXX: this will fail if the application was not code signed!
# Verify unzipped application launches successfully.
shell "open \"#{unzipped_app}\""
# grep for "[/]Users/foo/bar" to prevent grep from showing up in the list of processes matching the query.
match = "[#{unzipped_app[0]}]#{unzipped_app[1..-1]}"
shell "ps auxwww | grep \"#{match}\" | awk '{ print $2; }' | xargs kill"
unless shell_non_fatal "spctl --assess --verbose=4 --type execute \"#{unzipped_app}\""
Console.puts Console.background_red(Console.white("WARNING: The application bundle inside #{File.basename(DELIVERABLE_ZIP)} is not properly code signed and is not suitable for distribution!"))
end
# Clean up
FileUtils.rm_r unzipped_app
Console.puts "Finished: #{Console.green(DELIVERABLE_ZIP)}\n"
end
task :release_ready? do
formatted_fail "Releases can only be made from branches: #{RELEASE_BRANCHES.inspect}" unless BRANCH_IS_RELEASE
gst = 'git status -uno --ignore-submodules=untracked'
formatted_fail "Uncommitted files detected!\n#{`#{gst} --short`}" unless `#{gst} --porcelain | wc -l`.strip == "0"
end
desc "Build release zip and submit symbols to Hockey."
task :release => [:release_ready?, :analyze, :test, :zip] do
shell("git push")
# The zip build step might succeed without a code signature, but a deliverable for release must be signed!
verify_codesign DELIVERABLE_APP
hockey_api_token_file = File.open("Secrets-local/Hockey.api_token.asc", "r")
hockey_api_token = hockey_api_token_file.read
hockey_app_id_file = File.open("Secrets-local/Hockey.app_id.asc", "r")
hockey_app_id = hockey_app_id_file.read
# TODO: get SHA from git
# TODO: release type from BRANCH_IS_GM
shell("/usr/local/bin/puck -submit=auto -download=true -source_path=\".\" -api_token=\"#{hockey_api_token}\" -app_id=\"#{hockey_app_id}\" \"#{DELIVERABLE_ARCHIVE}\"")
end