-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathRakefile
More file actions
206 lines (173 loc) · 6.23 KB
/
Rakefile
File metadata and controls
206 lines (173 loc) · 6.23 KB
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
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'
}
# Thrown by tasks to present a "friendly" error message and abort the task.
class TaskError < StandardError
end
# Handles Sord examples, including checkout and running Sord to generate types.
class ExampleRunner
attr_reader :mode, :mode_arg, :clean
alias clean? clean
# @return [<Symbol>] Names of gems where Sord failed.
attr_accessor :failed_examples
# @param [String] mode "rbi" or "rbs".
# @param [Boolean] clean Run Sord with additional options to generate minimal type output.
def initialize(mode:, clean: false)
@mode = mode
@clean = clean
if mode == 'rbi'
@mode_arg = '--rbi'
elsif mode == 'rbs'
@mode_arg = '--rbs'
else
raise TaskError, 'please specify \'rbi\' or \'rbs\'!'
end
@failed_examples = []
end
# Create the `sord_examples` directory, ready for checkouts.
# @raise [TaskError] If it already exists.
def create_examples_dir
if File.directory?(File.join(__dir__, 'sord_examples'))
raise TaskError, 'sord_examples directory already exists, please delete the directory or run a reseed!'
end
FileUtils.mkdir 'sord_examples'
end
# Check that the `sord_examples` directory exists.
# @raise [TaskError] If it doesn't.
def validate_examples_dir
unless File.directory?(File.join(__dir__, 'sord_examples'))
raise TaskError, 'The sord_examples directory does not exist. Have you run the seed task?'
end
end
# Check out a repository, add Sord to its Gemfile, and install its dependencies.
# @param [Symbol] name Name of the checkout.
# @param [String] url Git URL.
def prepare_checkout(name, url)
Dir.chdir(File.join(__dir__, 'sord_examples')) do
puts "Cloning #{name}..."
system("git clone #{url} --depth=1")
Dir.chdir(name.to_s) do
# Add sord to gemfile.
`echo "gem 'sord', path: '../../'" >> Gemfile`
# Run bundle install.
system('bundle install')
end
end
end
# Run Sord on a checked-out repository.
# @param [Symbol] name Name of the checkout.
def generate_types(name)
puts "Generating rbi for #{name}..."
Dir.chdir(File.join(__dir__, 'sord_examples', name.to_s)) do
if clean?
system("bundle exec sord ../#{name}.#{mode} #{mode_arg} --no-sord-comments --replace-errors-with-untyped --replace-unresolved-with-untyped")
else
system("bundle exec sord ../#{name}.#{mode} #{mode_arg}")
end
if $?.success?
puts "#{name}.#{mode} generated!"
else
puts "Sord exited with error"
failed_examples << name
end
end
end
# Check that all Sord executions were successful.
# @raise [TaskError] If any failed.
def validate_sord_success
if failed_examples.any?
raise TaskError, "Not all Sord runs were successful: #{failed_examples.map(&:to_s).join(', ')}"
end
end
end
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|
examples = ExampleRunner.new(**args)
examples.create_examples_dir
Bundler.with_clean_env do
REPOS.each do |name, url|
examples.prepare_checkout(name, url)
examples.generate_types(name)
end
end
examples.validate_sord_success
puts Rainbow("Seeding complete!").green
rescue TaskError => e
abort Rainbow(e.to_s).red
end
desc 'Regenerate the rbi files in sord_examples.'
task :reseed, [:mode, :clean] do |t, args|
examples = ExampleRunner.new(**args)
examples.validate_examples_dir
REPOS.keys.each do |name|
examples.generate_types(name)
end
examples.validate_sord_success
puts Rainbow("Re-seeding complete!").green
rescue TaskError => e
abort Rainbow(e.to_s).red
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