-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRakefile
695 lines (597 loc) · 17.8 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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# encoding: utf-8
# @return [Hash<String>] The list of the names of the CocoaPods repositories
# which are web based and their dependencies.
#
WEB_REPOS = {
'blog.cocoapods.org' => [],
'cocoapods.org' => ['Humus'],
'feeds.cocoapods.org' => [],
'guides.cocoapods.org' => [],
'Humus' => [],
'metrics.cocoapods.org' => ['Humus'],
'search.cocoapods.org' => ['Humus'],
'trunk.cocoapods.org' => ['Humus'],
'trunk.cocoapods.org-api-doc' => [],
}
# @return [Array<String>] The list of the repos which should be cloned by
# default.
#
DEFAULT_REPOS = WEB_REPOS
# Task bootstrap / set-up
#-----------------------------------------------------------------------------#
def setup name
clone_repo name
bootstrap name
end
def bootstrap name = nil
strap = ->(dir) do
Dir.chdir(dir) do
subtitle "Bootstrapping #{dir}"
if has_rake_task?('bootstrap')
Bundler.with_clean_env do
sh "rake --no-search bootstrap"
end
end
end
end
if name
title "Bootstrapping the #{name} repository"
strap.call(name)
else
title "Bootstrapping all the repositories"
rakefile_repos.each do |dir|
strap.call(dir)
end
end
disk_usage = `du -h -c -d 0`.split(' ').first
puts "\nDisk usage: #{disk_usage}"
end
def clone_repo name
repos = fetch_default_repos
if name
repos = [repos.find { |repo| repo['name'] == name }]
end
if repos
title "Cloning the website repositories"
clone_repos(repos)
else
title "Could not find the repo you were looking for"
end
end
namespace :bootstrap do
desc "Bootstrap all CocoaPods repositories."
task :all do
bootstrap
end
# Add a task for each repo.
#
WEB_REPOS.keys.each do |name|
short_name = name.split('.').first.downcase
desc "Clones the #{name} repository and its dependencies"
task short_name do
if system('which bundle')
WEB_REPOS[name].each do |dependency_name|
setup dependency_name
end
setup name
else
$stderr.puts "\033[0;31m" \
"[!] Please install the bundler gem manually:\n" \
" $ [sudo] gem install bundler" \
"\e[0m"
exit 1
end
end
end
end
begin
# Task clone
#-----------------------------------------------------------------------------#
desc "Clones the web repositories"
task :clone, :name do |task, args|
clone_repo(args.name)
end
# Task install_system_deps
#-----------------------------------------------------------------------------#
desc "Installs application dependencies"
task :install_system_deps do
title "Installing application dependencies"
rakefile_repos.each do |dir|
Dir.chdir(dir) do
if has_rake_task?('install_tools')
subtitle "Installing dependencies of #{dir}"
sh "rake --no-search install_tools"
end
end
end
disk_usage = `du -h -c -d 0`.split(' ').first
puts "\nDisk usage: #{disk_usage}"
end
# Task bootstrap_repos
#-----------------------------------------------------------------------------#
desc "Runs the Bootstrap task on a specific repository"
task :bootstrap, :name do |task, args|
if name = args.name
bootstrap(name)
else
Bundler.with_clean_env do
sh 'bundle install'
end
end
end
# Task switch_to_ssh
#-----------------------------------------------------------------------------#
desc "Points the origin remote of all the git repos to use the SSH URL"
task :switch_to_ssh do
repos = fetch_default_repos
title "Setting SSH URLs"
repos.each do |repo|
name = repo['name']
url = repo['ssh_url']
subtitle(name)
Dir.chdir(name) do
sh "git remote set-url origin '#{url}'"
end
end
end
# Task pull
#-----------------------------------------------------------------------------#
desc "Pulls all the repositories & updates their submodules"
task :pull do
title "Pulling all the repositories"
if pull_current_repo(false)
puts yellow("\n[!] The Strata repository itself has been updated.\n" \
"You should run `rake bootstrap` to update all repositories\n" \
'and fetch the potentially new ones.')
else
updated_repos = []
repos.each do |dir|
Dir.chdir(dir) do
updated = pull_current_repo(true)
updated_repos << dir if updated
end
end
unless updated_repos.empty?
title "Summary"
updated_repos.each do |dir|
subtitle dir
Dir.chdir(dir) do
puts `git log ORIG_HEAD..`
end
end
end
end
end
desc "Gets the count of the open issues"
task :issues do
require 'open-uri'
require 'json'
title 'Fetching open issues'
WEB_REPOS.keys.dup.push('Strata').each do |name|
url = "https://api.github.com/repos/CocoaPods/#{name}/issues?state=open&per_page=100"
response = open(url).read
issues = JSON.parse(response)
pure_issues = issues.reject { |issue| issue.has_key?('pull_request') }
pull_requests = issues.select { |issue| issue.has_key?('pull_request') }
puts cyan("\n#{name}")
if issues.empty?
puts green "Awesome no open issues"
else
unless pull_requests.empty?
if pull_requests.count == 1
puts yellow("1 pull request")
elsif pull_requests.count > 1
puts yellow("#{pull_requests.count} pull requests")
end
if pull_requests.count <= 5
puts pull_requests.map{ |i| "- " + i['title'] }
end
end
if pure_issues.count == 100
puts yellow("100 or more open issues")
elsif pure_issues.count == 1
puts yellow("1 open issue")
elsif pure_issues.count > 1
puts yellow("#{pure_issues.count} open issues")
end
if pure_issues.count <= 5
puts pure_issues.map{ |i| "- " + i['title'] }
end
end
end
end
#-- Spec -------------------------------------------------------------------#
desc "Run all specs of all the sites"
task :spec do
title "Running specs"
WEB_REPOS.keys.reverse.each do |repo|
if Dir.exists?(repo)
Dir.chdir(repo) do
subtitle repo
sh 'bundle exec rake spec'
end
else
puts "Skipping running the spec of #{repo} as it is not cloned."
end
end
end
desc "Update the shared submodule"
task :update_shared do
shared_resources_repos.each do |repo|
sha = nil
Dir.chdir(repo + 'shared') do
sh "git checkout master && git pull origin master"
sha = `git rev-parse HEAD`
end
Dir.chdir(repo) do
if `git diff --quiet HEAD` && !$?.success?
message = "[Shared] Update to CocoaPods/shared_resources@#{sha}"
sh "git add shared && git commit -m '#{message}'"
subtitle "Updated the shared submodule in #{repo}"
end
end
end
end
namespace :db do
desc "Create databases for web properties"
task :create do
title "Creating databases"
run_humus_rake_task("db:create")
end
desc "Drop databases for web properties"
task :drop do
title "Dropping databases"
run_humus_rake_task("db:drop")
end
desc "Reset databases for web properties"
task :reset do
title "Resetting reset"
run_humus_rake_task("db:reset")
end
desc "Migrate databases for web properties"
task :migrate do
title "Performing migration"
run_humus_rake_task("db:migrate")
end
end
desc 'Prints the repositories with un-merged branches or a dirty working copy and lists the gems with commits after the last release.'
task :status do
title 'Checking status'
dirs_not_in_master = repos.reject do |dir|
Dir.chdir(dir) do
branch = `git rev-parse --abbrev-ref HEAD`.chomp
%w(master develop).include?(branch)
end
end
unless dirs_not_in_master.empty?
subtitle 'Repositories not in master/develop branch'
puts "- #{dirs_not_in_master.join("\n- ")}"
end
dirs_with_unmerged_branches = repos.map do |dir|
Dir.chdir(dir) do
base_branch = default_branch
branches = git_branch_list(" --no-merged #{base_branch}")
branches.reject! { |b| b.end_with?('-stable') }
"#{dir}: #{branches.join(', ')}" unless branches.empty?
end
end.compact
unless dirs_with_unmerged_branches.empty?
subtitle 'Repositories with un-merged branches'
puts "- #{dirs_with_unmerged_branches.join("\n- ")}"
end
dirty_dirs = repos.reject do |dir|
Dir.chdir(dir) do
`git diff --quiet`
exit_status = $?.exitstatus
`git diff --cached --quiet`
cached_exit_status = $?.exitstatus
exit_status.zero? && cached_exit_status.zero?
end
end
unless dirty_dirs.empty?
subtitle 'Repositories with a dirty working copy'
puts "- #{dirty_dirs.join("\n- ")}"
end
subtitle 'Gems with releases'
has_pending_releases = false
name_commits_tags = gem_dirs.map do |dir|
tag = last_tag(dir)
if tag != ''
Dir.chdir(dir) do
# subtract 2 to account for the empty changelog section commit & merge of tag
commits_since_last_tag = `git rev-list #{tag}..HEAD --count`.chomp.to_i - 2
commits_since_last_tag = 0 if commits_since_last_tag <= 0
unless commits_since_last_tag.zero?
has_pending_releases = true
[dir, commits_since_last_tag, tag]
end
end
end
end
name_commits_tags = name_commits_tags.compact.sort_by { |value| value[1] }.reverse
name_commits_tags.each do |name_commits_tag|
puts "\n- #{name_commits_tag[0]}\n #{name_commits_tag[1]} commits since #{name_commits_tag[2]}"
end
puts 'All the gems are up to date' unless has_pending_releases
end
rescue LoadError
$stderr.puts "\033[0;31m" \
'[!] Some Rake tasks haven been disabled because the environment' \
' couldn’t be loaded. Be sure to run `rake bootstrap` first.' \
"\e[0m"
end
#-----------------------------------------------------------------------------#
# HELPERS
#-----------------------------------------------------------------------------#
# Repos Helpers
#-----------------------------------------------------------------------------#
# @return [Array<Hash>] The list of the CocoaPods repositories which contain a
# Gem as returned by the GitHub API.
#
def fetch_default_repos
fetch_repos.select do |repo|
DEFAULT_REPOS.include?(repo['name'])
end
end
# @return [Array<Hash>] The list of the CocoaPods repositories as returned by
# the GitHub API.
#
def fetch_repos
require 'json'
require 'open-uri'
require 'net/http'
title "Fetching repositories list"
url = 'https://api.github.com/orgs/CocoaPods/repos?type=public'
repos = []
loop do
response = Net::HTTP.get_response(URI(url))
body = response.body
repos.concat(JSON.parse(body))
link = Array(response['link']).first
if match = link.match(/<([^<]*)>; rel="next"/)
url = match.captures.first
else
break
end
end
repos.reject! { |repo| repo['name'] == 'Strata' }
puts "Found #{repos.count} public repositories"
repos
end
# Clones the given repos to a directory named after themselves unless the
# directory already exists.
#
# @param [Array<Hash>] The description of the repositories.
#
# @return [void]
#
def clone_repos(repos)
repos.each do |repo|
name = repo['name']
subtitle "Cloning #{name}"
url = repo['clone_url']
if File.exist?(name)
puts "Already cloned"
else
sh "git clone #{url} #{name}"
# Run the new repo's bootstrap rake task
Dir.chdir(name) do
gem 'bundler'
require 'bundler'
Bundler.with_clean_env do
system 'rake bootstrap'
end
end
end
end
end
# Pull the repo in the current working directory
#
# @param [Bool] Whether we want to update the submodules as well
#
# @return [Bool] true if the repo has updates that were pulled
# false if there was nothing to update (repo already up-to-date or ahead)
#
def pull_current_repo(update_submodules)
subtitle "Pulling #{File.basename(Dir.getwd)}"
sh "git remote update"
status = `git status -uno`
unless status.include?('up-to-date') || status.include?('ahead')
sh "git pull --no-commit"
sh "git submodule update" if update_submodules
return true
end
false
end
# Checks the given repo for a release and fails the task if any issue exits
# listing them.
#
# @param [String] gem_dir The repo to check.
# @param [String] version The version which should be released.
#
def check_repo_for_release(repo_dir, version)
errors = []
Dir.chdir(repo_dir) do
if `git symbolic-ref HEAD 2>/dev/null`.strip.split('/').last != 'master'
errors << "You need to be on the `master` branch in order to do a release."
end
if `git tag`.strip.split("\n").include?(version.to_s)
errors << "A tag for version `#{version}` already exists."
end
diff_lines = `git diff --name-only`.strip.split("\n")
if diff_lines.size == 0
errors << "Change the version number of the gem yourself"
end
diff_lines.delete('Gemfile.lock')
diff_lines.delete('CHANGELOG.md')
unless diff_lines.count == 1
# TODO Check that is only the version file changed
error = "Only change the version, the CHANGELOG.md and the Gemfile.lock files"
error << "\n- " + diff_lines.join("\n- ")
errors << error
end
unless Pathname.new('CHANGELOG.md').read.lines.include?("## #{version}\n")
errors << "The CHANGELOG.md doesn't include the released version " \
"`## #{version}`.Update it manually."
end
end
unless errors.empty?
errors.each do |error|
$stderr.puts(red("[!] #{error}"))
end
exit 1
end
end
# @return [Array<String>] All the checked out repos
#
def repos
Dir['*/'].map { |dir| dir[0...-1] }
end
# @return [Array<String>] All the directories that contains a Rakefile,
#
def rakefile_repos
Dir['*/Rakefile'].map { |file| File.dirname(file) }
end
# @return [Array<Pathname>] All the directories that contains the shared submodule.
#
def shared_resources_repos
Pathname.glob('*/shared').map(&:dirname)
end
# @return [Array<String>]
#
def git_branch_list(arguments = nil)
branches = `git branch #{arguments}`.split("\n")
branches.map { |line| line.split(' ').last }
end
def default_branch
default_branches = ['master', 'develop']
branches = git_branch_list
common = branches & default_branches
if common.count == 1
common.first
end
end
def make_github_release(repo, version, tag, access_token)
body = changelog_for_repo(repo, version)
REST.post("https://api.github.com/repos/CocoaPods/#{repo}/releases",
{
tag_name: tag,
name: version.to_s,
body: body,
prerelease: version.prerelease?,
}.to_json,
{
'Content-Type' => 'application/json',
'User-Agent' => 'runscope/0.1,segiddins',
'Accept' => '*/*',
'Accept-Encoding' => 'gzip, deflate',
'Authorizaton' => "token #{access_token}",
},
)
end
def changelog_for_repo(repo, version)
changelog_path = File.expand_path(repo + '/CHANGELOG.md')
if File.exists?(changelog_path)
title_token = '## '
current_verison_title = title_token + version.to_s
text = File.open(changelog_path, "r:UTF-8") { |f| f.read }
lines = text.split("\n")
current_version_index = lines.find_index { |line| line =~ (/^#{current_verison_title}/) }
unless current_version_index
raise "Update the changelog for the last version (#{version})"
end
current_version_index += 1
previous_version_lines = lines[(current_version_index+1)...-1]
previous_version_index = current_version_index + (previous_version_lines.find_index { |line| line =~ (/^#{title_token}/) && !line.include?('rc') } || lines.count)
relevant = lines[current_version_index..previous_version_index]
relevant.join("\n").strip
end
end
def run_humus_rake_task(task)
if Dir.exists?("Humus")
Dir.chdir("Humus") do
sh 'rake ' + task + ' RACK_ENV=test'
sh 'rake ' + task + ' RACK_ENV=development'
end
end
end
# Gem Helpers
#-----------------------------------------------------------------------------#
# @return [Array<String>] the directory of the gems.
#
def gem_dirs
gemspecs = Dir['*/*.gemspec']
gemspecs.map { |path| File.dirname(path) }.uniq
end
def spec(gem_dir)
files = Dir.glob("#{gem_dir}/*.gemspec")
unless files.count == 1
error("Unable to select a gemspec in #{gem_dir}")
end
spec_path = files.first
spec = Gem::Specification::load(spec_path.to_s)
end
def gem_version(gem_dir)
spec(gem_dir).version
end
def gem_name(gem_dir)
spec(gem_dir).name
end
def last_tag(dir)
Dir.chdir(dir) do
`git describe --abbrev=0 2>/dev/null`.chomp
end
end
# Other Helpers
#-----------------------------------------------------------------------------#
# @return [Bool] Whether the Rakefile in the current working directory has a
# task with the given name.
#
def has_rake_task?(task)
`rake --no-search --tasks #{task}`.include?("rake #{task}")
end
def silent_sh(command)
require 'english'
output = `#{command} 2>&1`
unless $CHILD_STATUS.success?
puts output
exit 1
end
output
end
# UI
#-----------------------------------------------------------------------------#
# Prints a title.
#
def title(string)
puts
puts "-" * 80
puts cyan(string)
puts "-" * 80
end
def subtitle(string)
puts "\n#{green(string)}"
end
def error(string)
raise red("[!] #{string}")
end
# Colorizes a string to green.
#
def green(string)
"\033[0;32m#{string}\e[0m"
end
# Colorizes a string to yellow.
#
def yellow(string)
"\033[0;33m#{string}\e[0m"
end
# Colorizes a string to red.
#
def red(string)
"\033[0;31m#{string}\e[0m"
end
# Colorizes a string to cyan.
#
def cyan(string)
"\033[0;36m#{string}\033[0m"
end