-
Notifications
You must be signed in to change notification settings - Fork 6
/
new-exercise
executable file
·58 lines (43 loc) · 1.18 KB
/
new-exercise
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
#!/usr/bin/env ruby
#TODO: modify project README to link new exercise
class ExerciseCreator
README_TEMPLATE = File.join(Dir.pwd, 'WORKSHEET.md')
def self.call
new
.make_exercise_root_dir
.make_exercise_ruby_dir
.copy_readme_template
end
def make_exercise_root_dir(path = directory_path)
Dir.mkdir(path) unless Dir.exist?(path)
self
end
def make_exercise_ruby_dir(path = File.join(directory_path, 'ruby'))
Dir.mkdir(path) unless Dir.exist?(path)
self
end
def copy_readme_template(path = File.join(directory_path, 'README.md'))
File.write(path, IO.read(README_TEMPLATE).sub('Exercise Name', chapter_name))
self
end
private
def directory_path
@directory_path ||= File.join(Dir.pwd, directory_name)
end
def directory_name
@directory_name ||= "#{chapter_number.rjust(2, '0')}-#{chapter_name}"
end
def chapter_number
@chapter_number ||= ARGV.fetch(0) do |_i|
puts 'Exercise number?'
gets.chomp
end
end
def chapter_name
@chapter_name ||= ARGV.fetch(1) do |_i|
puts 'Exercise name?'
gets.chomp.downcase.gsub(' ', '-')
end
end
end
ExerciseCreator.call if $0 == __FILE__