-
Notifications
You must be signed in to change notification settings - Fork 18
/
Rakefile
153 lines (135 loc) · 4.99 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
require "bundler/gem_tasks"
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)
task :default => :spec
REPOS = {
addressable: 'https://github.com/sporkmonger/addressable',
bundler: 'https://github.com/bundler/bundler',
discordrb: 'https://github.com/meew0/discordrb',
gitlab: 'https://github.com/NARKOZ/gitlab',
'graphql-ruby': 'https://github.com/rmosolgo/graphql-ruby',
haml: 'https://github.com/haml/haml',
oga: 'https://gitlab.com/yorickpeterse/oga',
rouge: 'https://github.com/rouge-ruby/rouge',
'rspec-core': 'https://github.com/rspec/rspec-core',
yard: 'https://github.com/lsegal/yard',
zeitwerk: 'https://github.com/fxn/zeitwerk'
}
namespace :examples do
require 'fileutils'
require 'rainbow'
desc "Clone git repositories and run Sord on them as examples"
task :seed, [:mode, :clean] do |t, args|
if File.directory?('sord_examples')
puts Rainbow('sord_examples directory already exists, please delete the directory or run a reseed!').red
exit
end
if args[:mode] == 'rbi'
mode_arg = '--rbi'
elsif args[:mode] == 'rbs'
mode_arg = '--rbs'
else
puts Rainbow('please specify \'rbi\' or \'rbs\'!').red
exit
end
FileUtils.mkdir 'sord_examples'
FileUtils.cd 'sord_examples'
Bundler.with_clean_env do
# Shallow clone each of the repositories, then bundle install and run sord.
REPOS.each do |name, url|
puts "Cloning #{name}..."
system("git clone #{url} --depth=1")
FileUtils.cd name.to_s
# Add sord to gemfile.
`echo "gem 'sord', path: '../../'" >> Gemfile`
# Run bundle install.
system('bundle install')
# Generate sri
puts "Generating rbi for #{name}..."
if args[:clean]
system("bundle exec sord ../#{name}.#{args[:mode]} #{mode_arg} --no-sord-comments --replace-errors-with-untyped --replace-unresolved-with-untyped")
else
system("bundle exec sord ../#{name}.#{args[:mode]} #{mode_arg}")
end
puts "#{name}.#{args[:mode]} generated!"
FileUtils.cd '..'
end
end
puts Rainbow("Seeding complete!").green
end
desc 'Regenerate the rbi files in sord_examples.'
task :reseed, [:mode, :clean] do |t, args|
if Dir.exist?('sord_examples')
FileUtils.cd 'sord_examples'
else
raise Rainbow("The sord_examples directory does not exist. Have you run the seed task?").red.bold
end
if args[:mode] == 'rbi'
mode_arg = '--rbi'
elsif args[:mode] == 'rbs'
mode_arg = '--rbs'
else
puts Rainbow('please specify \'rbi\' or \'rbs\'!').red
exit
end
REPOS.keys.each do |name|
FileUtils.cd name.to_s
puts "Regenerating rbi file for #{name}..."
Bundler.with_clean_env do
if args[:clean]
system("bundle exec sord ../#{name}.#{args[:mode]} #{mode_arg} --no-regenerate --no-sord-comments --replace-errors-with-untyped --replace-unresolved-with-untyped")
else
system("bundle exec sord ../#{name}.#{args[:mode]} #{mode_arg} --no-regenerate")
end
end
FileUtils.cd '..'
end
puts Rainbow("Re-seeding complete!").green
end
desc 'Delete the sord_examples directory to allow the seeder to run again.'
task :reset do
FileUtils.rm_rf 'sord_examples' if File.directory?('sord_examples')
puts Rainbow('Reset complete.').green
end
desc 'Typecheck each of the sord_examples rbi files.'
task :typecheck, [:verbose] do |t, args|
results_hash = {}
REPOS.each do |name, url|
Bundler.with_clean_env do
puts "srb tc sord_examples/#{name}.rbi --ignore sord.rbi 2>&1"
if args[:verbose]
output = system("srb tc sord_examples/#{name}.rbi --ignore sord.rbi 2>&1")
else
output = `srb tc sord_examples/#{name}.rbi --ignore sord.rbi 2>&1`.split("\n").last
results_hash[:"#{name}"] = output
end
end
end
unless args[:verbose]
puts Rainbow("Errors").bold
longest_name = results_hash.keys.map { |name| name.length }.max
# Replace all values in results_hash with integer by parsing it.
results_hash.each do |name, result|
result.scan(/Errors\: (\d+)/) { |match| result = match.first.to_i }
results_hash[name] = (result == "No errors! Great job.") ? 0 : result
end
# Print the right-aligned name and the number of errors, with different colors depending on the number of errors.
results_hash.each do |name, result|
spaces_needed = longest_name - name.length
output = "#{' ' * spaces_needed}#{name}: #{result}"
case result
when 0..5
puts Rainbow(output).green.bright
when 6..25
puts Rainbow(output).green
when 26..50
puts Rainbow(output).red
else
puts Rainbow(output).red.bright
end
end
# Report the Total.
puts Rainbow("#{' ' * (longest_name - 'Total'.length)}Total: #{results_hash.values.inject(0, :+)}").bold
end
end
end