-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
80 lines (68 loc) · 2.13 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
require 'rake/testtask'
require 'erb'
require 'ostruct'
Rake::TestTask.new do |t|
# t.libs << "test"
t.test_files = FileList['test/*.rb']
# t.verbose = true
end
task :generate, [:problem_name] do |t, args|
generate_problem_file(args.problem_name)
generate_test_file(args.problem_name)
end
task :destroy, [:problem_name] do |t, args|
File.delete(
problem_file_name(args.problem_name),
test_file_name(args.problem_name)
)
end
def problem_file_name(problem_name)
"./problems/#{problem_name}.rb"
end
def test_file_name(problem_name)
"./test/#{problem_name}_test.rb"
end
def generate_problem_file(problem_name)
filename = problem_file_name(problem_name)
return if File.exist?(filename) && no?("File #{filename} already exists. Overwrite?")
words = problem_name.split('_')
problem_header = words.map { |s| s.capitalize }.join(' ')
context = {
problem_header: problem_header,
problem_method_name: words.slice(1, words.length - 1).join('_')
}
render_template_to_file(filename,'./templates/problem.rb.erb', context)
end
def generate_test_file(problem_name)
filename = test_file_name(problem_name)
return if File.exist?(filename) && no?("File #{filename} already exists. Overwrite?")
words = problem_name.split('_')
words = words.slice(1, words.length - 1)
problem_title = words.map { |s| s.capitalize }.join('')
context = {
require_arg: "./problems/#{problem_name}",
test_class_name: "Test#{problem_title}",
benchmark_class_name: "Benchmark#{problem_title}",
problem_method_name: words.join('_')
}
render_template_to_file(filename,'./templates/test.rb.erb', context)
end
def render_template_to_file(filename, template, context)
erb_template = ERB.new(File.read(template))
data = erb_template.result(OpenStruct.new(context).instance_eval { binding })
File.write(filename, data)
end
def yes?(question)
puts "#{question} (Y)es, (N)no"
loop do
case $stdin.gets.strip.downcase
when "n", "no"
return false
when "y", "yes"
return true
end
end
end
def no?(question)
!yes?(question)
end