-
Notifications
You must be signed in to change notification settings - Fork 75
/
Rakefile
552 lines (451 loc) · 14.4 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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#
# Copyright (c) 2017-Present Jochen Pfeiffer
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#-- Bootstrap ----------------------------------------------------------------#
desc 'Initialize your working copy'
task :bootstrap do
check_executable('bundler')
install_gems
install_cocoapods
end
begin
require 'fileutils'
require 'octokit'
require 'cocoapods'
task default: :test
#-- Tests ------------------------------------------------------------------#
desc 'Run tests'
task :test do
xcodebuild_test "platform=iOS Simulator,name=iPhone 15 Pro Max,OS=17.2"
end
desc 'Run tests for DESTINATION env'
task :test_destination do
Rake::Task[:print_debug_info].invoke
xcodebuild_test ENV['DESTINATION']
end
desc 'Copy snapshot artifacts'
task :copy_snapshots do
sh 'rm -rf /tmp/snapshots'
simulator_directory = `xcrun simctl get_app_container booted org.cocoapods.demo.JJFloatingActionButton-Example data`.strip
unless simulator_directory.empty?
puts "Copying snapshots from #{simulator_directory}"
sh "cp -R #{simulator_directory}/tmp /tmp/snapshots"
else
puts "No simulator directory found"
end
end
desc 'Print debug info'
task :print_debug_info do
title 'Debug info'
sh 'xcodebuild -version'
sh 'xcodebuild -showsdks'
sh 'xcrun xctrace list devices'
end
desc 'Lint swift'
task :lint_swift do
title 'Linting swift'
check_executable('swiftlint')
sh "swiftlint"
end
desc 'Lint podspec'
task :lint_podspec do
title 'Linting podspec'
sh "bundle exec pod lib lint"
end
#-- Format -----------------------------------------------------------------#
desc 'Format code'
task :format do
title 'Formating code'
check_executable('swiftformat')
sh "swiftformat ."
end
desc 'Format and lint code'
task :format_and_lint do
Rake::Task[:format].invoke
Rake::Task[:lint_swift].invoke
end
#-- Dependencies -----------------------------------------------------------#
desc 'Install dependencies'
task :install_dependencies do
install_gems
install_cocoapods
end
desc 'Update dependencies'
task :update_dependencies do
update_gems
update_cocoapods
end
#-- Documentation ----------------------------------------------------------#
desc 'Generate documentation'
task :generate_documentation do
generate_documentation
test_documentation_coverage
end
desc 'Test documentation coverage'
task :test_documentation_coverage do
test_documentation_coverage
end
#-- Changelog --------------------------------------------------------------#
desc 'Generate changelog'
task :generate_changelog do
generate_changelog ""
end
#-- Record Video -----------------------------------------------------------#
desc 'Record video of booted simulator and convert to gif'
task :record_gif do
Rake::Task[:record_gif_with_name].invoke 'JJFloatingActionButton'
end
desc 'Record video of booted simulator and convert to gif with given name'
task :record_gif_with_name, :name do |task, args|
title 'Recording video'
check_executable('ffmpeg')
mov_path="./Images/#{args.name}.mov"
trap('SIGINT') { puts }
%x{xcrun simctl io booted recordVideo #{mov_path}}
title 'Converting to gif'
gif_path="./Images/#{args.name}.gif"
palette_path='./Images/palette.png'
filters='fps=30,setpts=1*PTS,scale=250:-1:flags=lanczos'
sh "ffmpeg -v warning -i #{mov_path} -vf '#{filters},palettegen' -y #{palette_path}"
if File.exist? palette_path
sh "ffmpeg -v warning -i #{mov_path} -i #{palette_path} -lavfi '#{filters} [x]; [x][1:v] paletteuse' -y #{gif_path}"
File.delete palette_path
end
end
#-- Release ----------------------------------------------------------------#
desc 'Initiate release workflow of type'
task :initiate_release, :type do |task, args|
ensure_clean_git_status
checkout_and_pull_master
create_github_release_trigger_tag args.type
end
desc 'Release next version of type'
task :release_next_version, :type do |task, args|
release_next_version args.type
end
desc 'Release version'
task :release_version, :version do |task, args|
release_version args.version
end
desc 'Delete GitHub release tag of type'
task :delete_github_release_trigger_tag, :type do |task, args|
delete_github_release_trigger_tag args.type
end
desc 'Push podspec'
task :push_podspec do
title "Pushing podspec"
sh 'bundle exec pod trunk push'
end
desc 'Create release on github'
task :create_github_release do
version = version_from_podspec
unless is_release_commit_for_version version
puts "Not a release commit."
next
end
title "Creating release on github"
repo = "jjochen/JJFloatingActionButton"
body = changelog_for_version version
options = {
:name => version,
:body => body,
:draft => false,
:prerelease => false
}
puts "repo: #{repo}"
puts "version: #{version}"
puts "body: \n#{body}"
client = Octokit::Client.new :access_token => ENV['JJ_GITHUB_TOKEN']
release = client.create_release repo, version, options
puts "#{release.name} created."
end
desc 'Close milestone on github'
task :close_github_milestone do
title "Closing milestone on github"
repo = "jjochen/JJFloatingActionButton"
version = version_from_podspec
puts "repo: #{repo}"
puts "version: #{version}"
client = Octokit::Client.new :access_token => ENV['JJ_GITHUB_TOKEN']
open_milestones = client.list_milestones repo, {:state => 'open'}
open_milestones.each do |milestone|
next unless milestone.title == version
number = milestone.number
puts "number: #{number}"
client.update_milestone repo, number, {:state => 'closed'}
puts "#{milestone.title} closed."
end
end
desc 'Update github releases'
task :update_github_releases do
title "Updating releases on github"
repo = "jjochen/JJFloatingActionButton"
client = Octokit::Client.new :access_token => ENV['JJ_GITHUB_TOKEN']
client.releases(repo).each do |release|
puts
url = release.url
puts "url: #{url}"
version = release.tag_name
puts "version: #{version}"
body = changelog_for_version version
puts "body: \n#{body}"
options = {
:name => version,
:body => body
}
release = client.update_release url, options unless body.empty?
puts "#{version} updated."
end
end
rescue LoadError, NameError => e
error_message 'Some Rake tasks haven been disabled because the environment' \
' couldn’t be loaded. Be sure to run `rake bootstrap` first.'
$stderr.puts e.message
$stderr.puts e.backtrace
$stderr.puts
end
#-- Helpers ------------------------------------------------------------------#
private
def title(title)
cyan_title = "\033[0;36m#{title}\033[0m"
puts
puts '-' * 80
puts cyan_title
puts '-' * 80
puts
end
def error_message(message)
red_message = "\033[0;31m[!] #{message}\e[0m"
$stderr.puts
$stderr.puts red_message
$stderr.puts
end
def check_executable(executable)
unless system("which #{executable}")
error_message "Please install '#{executable}' manually."
exit 1
end
end
def check_parameter(parameter)
if parameter.nil? || parameter.empty?
error_message "parameter can't be empty."
exit 1
end
end
def increment_semver(semver, increment_type = "patch")
if not (/\d+\.\d+\.\d+/).match(semver)
raise "Your semantic version must match the format 'X.X.X'."
end
if not ["patch", "minor", "major"].include?(increment_type)
raise "Only 'patch', 'minor', and 'major' are supported increment types."
end
major, minor, patch = semver.split(".")
case increment_type
when "patch"
patch = patch.to_i + 1
when "minor"
minor = minor.to_i + 1
patch = 0
when "major"
major = major.to_i + 1
minor = 0
patch = 0
end
return "#{major}.#{minor}.#{patch}"
end
def install_gems
title 'Installing gems'
sh 'bundle install'
end
def install_cocoapods
title 'Installing cocoapods'
sh 'bundle exec pod install --repo-update --project-directory=Example'
end
def update_gems
title 'Updating gems'
sh 'bundle update'
end
def update_cocoapods
title 'Updating cocoapods'
sh 'bundle exec pod update --project-directory=Example'
end
def xcodebuild_test(destination)
title 'Running tests'
check_parameter(destination)
sh "xcodebuild clean build test" \
" -workspace Example/JJFloatingActionButton.xcworkspace" \
" -scheme JJFloatingActionButton_Example" \
" -sdk iphonesimulator" \
" -destination \"#{destination}\"" \
" -enableCodeCoverage YES" \
" CODE_SIGN_IDENTITY=" \
" PROVISIONING_PROFILE=" \
" | xcpretty --report junit && exit ${PIPESTATUS[0]}"
end
def release_next_version(type)
title "Releasing #{type} version"
delete_github_release_trigger_tag(type)
version = version_from_podspec
new_version = increment_semver(version, type)
release_version new_version
end
def release_version(version)
ensure_clean_git_status
checkout_and_pull_master
ensure_clean_git_status
update_version_in_podspec version
update_version_in_example_project version
generate_changelog version
install_cocoapods
generate_documentation
create_release_branch_and_commit version
open_pull_request version
end
def ensure_clean_git_status
title "Ensuring clean git status"
unless `git diff --shortstat 2> /dev/null | tail -n1` == ''
error_message "Uncommited changes. Commit first."
exit 1
end
end
def is_release_commit_for_version(version)
commit_message = `git log -1 --pretty=%B 2> /dev/null`
return commit_message.start_with?("Release #{version} ")
end
def checkout_and_pull_master
title "Checkout and pull master"
sh "git checkout master"
sh "git pull"
end
def update_version_in_example_project(version)
title "Updating version in example project"
check_parameter(version)
sh "/usr/libexec/PlistBuddy -c \"Set :CFBundleShortVersionString #{version}\" ./Example/JJFloatingActionButton/Info.plist"
end
def generate_changelog(version)
title "Generating changelog"
unless version.nil? || version.empty?
sh "github_changelog_generator --future-release #{version}"
else
sh "github_changelog_generator"
end
end
def generate_documentation
title 'Generating documentation'
sh 'bundle exec jazzy'
end
def test_documentation_coverage
title 'Checking documentation coverage'
file_path = './docs/index.html'
search_string = '100% documented'
if File.foreach(file_path).grep(/#{Regexp.escape(search_string)}/).any?
puts "'#{search_string}' found in #{file_path}"
else
error_message "'#{search_string}' not found in #{file_path}"
exit 1
end
end
def update_version_in_podspec(version)
title "Updating version in podspec"
check_parameter(version)
file_name = 'JJFloatingActionButton.podspec'
contents = File.read(file_name)
new_contents = contents.gsub(/(spec\.version\s*=\s*)'.*'/, "\\1'#{version}'")
puts new_contents
File.open(file_name, "w") {|file| file.puts new_contents }
end
def version_from_podspec
spec = Pod::Specification.from_file('JJFloatingActionButton.podspec')
version = spec.version.to_s()
if version.nil? || version.empty?
error_message "podspec version not found."
exit 1
end
return version
end
def changelog_for_version(version)
check_parameter(version)
changelog = ""
File.open("CHANGELOG.md") do |f|
in_version = false
f.each_line do |line|
if in_version
if line.match(/^\#\# \[.*/) || line.match(/^\\\* \*.*/)
break
elsif
changelog.concat(line)
end
elsif line.match(/^\#\# \[#{version}\].*/)
in_version = true
end
end
end
if changelog.nil? || changelog.empty?
error_message "changelog for version #{version} not found."
exit 1
end
return changelog
end
def create_release_branch_and_commit(version)
title "Creating release branch and commit"
check_parameter(version)
release_branch = "release/#{version}"
sh "git checkout -b #{release_branch}"
sh "git add --all"
sh "git commit -v -m 'Release #{version}'"
sh "git push --set-upstream origin #{release_branch}"
end
def release_trigger_tag(type)
if not ["patch", "minor", "major"].include?(type)
raise "Only 'patch', 'minor', and 'major' are supported types. '#{type}' is not."
end
return "#{type}-release"
end
def create_github_release_trigger_tag(type)
title "Creating release tag"
tag = release_trigger_tag(type)
sh "git tag -a #{tag} -m 'Initiating #{type} release'"
sh "git push --set-upstream origin #{tag}"
sh "git tag -d #{tag}"
end
def delete_github_release_trigger_tag(type)
title "Deleting release tag"
tag = release_trigger_tag(type)
sh "git push origin --delete #{tag} || true"
end
def open_pull_request(version)
title "Opening pull request"
check_parameter(version)
repo = "jjochen/JJFloatingActionButton"
base = "master"
release_branch = "release/#{version}"
title = "Release #{version}"
puts "repo: #{repo}"
puts "base: #{base}"
puts "head: #{release_branch}"
client = Octokit::Client.new :access_token => ENV['JJ_GITHUB_TOKEN']
pull_request = client.create_pull_request repo, base, release_branch, title
puts "#{pull_request.title} created."
puts pull_request.html_url
client.add_labels_to_an_issue repo, pull_request.number, ['release']
puts "release label added."
sh "open #{pull_request.html_url}"
end