-
-
Notifications
You must be signed in to change notification settings - Fork 530
Expand file tree
/
Copy pathgenerate
More file actions
executable file
·62 lines (51 loc) · 1.64 KB
/
Copy pathgenerate
File metadata and controls
executable file
·62 lines (51 loc) · 1.64 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
#!/usr/bin/env ruby
require 'optparse'
require 'tempfile'
require_relative '../generatorv2/lib/generator'
# Helper methods
def exercises
Dir.entries('./exercises/practice')
.select { |file| File.directory? File.join('./exercises/practice', file) }
end
def underscore(str)
str.gsub(/[^\w\s-]/, '').gsub(/[-\s]/, '_').downcase
end
class VerificationError < StandardError
MESSAGE = 'The result generated for %<exercise>s, does not match the current file'
def initialize(message = MESSAGE)
super
end
end
# Parsing Code
parser = OptionParser.new
parser.on('-v', '--version', 'Print the version') do
puts File.read('./generatorv2/VERSION')
end
parser.on('-h', '--help', 'Prints help') do
puts parser
end
parser.on('-a', '--all', 'Generate all exercises') do
exercises.each do |exercise|
if File.exist?("./exercises/practice/#{exercise}/.meta/test_template.erb")
Generator.new(exercise).generate
end
end
end
parser.on('--verify', 'Verify all exercises') do
exercises.each do |exercise|
if File.exist?("./exercises/practice/#{exercise}/.meta/test_template.erb")
current_code = File.read("./exercises/practice/#{exercise}/#{underscore(exercise)}_test.rb")
f = File.new("./exercises/practice/#{exercise}/temp_test.rb", 'w+')
Generator.new(exercise).generate(f.path)
generated_code = f.read
File.delete(f.path)
fail VerificationError unless current_code == generated_code
end
rescue VerificationError => e
STDERR.puts e.message % {exercise:}
end
end
parser.on('-e', '--exercise EXERCISE', 'The exercise to generate') do |exercise|
Generator.new(exercise).generate
end
parser.parse!