forked from makersacademy/ruby-kickstart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
102 lines (90 loc) · 3.39 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
gem 'rspec'
# overriding Rake method that displays tasks so that -T will display in the correct order
module Rake
class Application
def display_tasks_and_comments
displayable_tasks = tasks.select { |t| t.comment && t.name =~ options.show_task_pattern }.sort do
|t1,t2|
one_is_session = t1.name[/^\d+:(\d+|all)$/]
two_is_session = t2.name[/^\d+:(\d+|all)$/]
if one_is_session && two_is_session
session1 , prb1 = t1.name.split ':'
session2 , prb2 = t2.name.split ':'
if session1 == session2
if prb1 == 'all' then 1 elsif prb2 == 'all' then -1 else prb1.to_i <=> prb2.to_i end
else
session1.to_i <=> session2.to_i
end
elsif one_is_session
-1
elsif two_is_session
1
else
t1.name <=> t2.name
end
end
if options.full_description
displayable_tasks.each do |t|
puts "rake #{t.name_with_args}"
t.full_comment.split("\n").each do |line|
puts " #{line}"
end
puts
end
else
width = displayable_tasks.collect { |t| t.name_with_args.length }.max || 10
max_column = truncate_output? ? terminal_width - name.size - width - 7 : nil
displayable_tasks.each do |t|
printf "#{name} %-#{width}s # %s\n",
t.name_with_args, max_column ? truncate(t.comment, max_column) : t.comment
end
end
end
end
end
# overriding makedirs so that I can pass a path to make them from
# pass the path in a hash at the end if desired
# ie makedirs 'abc' , 'def' , :path => 'ghi'
def makedirs(*dirs)
path = dirs.pop[:path].sub(%r[/$],'') if dirs.last.is_a? Hash
dirs.map! { |dir| "#{path}/#{dir}" } if path
super(dirs)
end
# runs the spec against the challenge
# if the environment variable 'solved' has been set,
# then it runs them against the solved challenge instead
def run_spec(session_num,problem)
sess = "#{File.dirname(__FILE__)}/session#{session_num}"
if ENV['solved']
problem_dir = "#{sess}/solved/#{problem}.rb"
else
problem_dir = Dir["#{sess}/3-challenge/#{problem}_*.rb"].first
end
sh "ruby -c '#{problem_dir}'" # check syntax
sh "rspec -cr '#{problem_dir}' -r enumerator '#{sess}/4-spec/#{problem}.rb' --fail-fast" # run spec (c for colour, r to require the files, enumerator required for 1.8.6 compatibility)
end
# returns array of sessions
def get_sessions
Dir['*'].grep(/^session\d+$/).map { |folder_name| folder_name[/\d+$/] }
end
# returns hash where each key is the session to test
# each value is a range indicating the problems available for this session
def sessions_with_problems
get_sessions.inject Hash.new do |sessions_with_sections, session|
challenges = Dir["session#{session}/3-challenge/*.rb"]
sessions_with_sections[session] = challenges.map { |challenge| challenge[%r([^/]+?$)].to_i }.sort
sessions_with_sections
end
end
# create tasks for each challenge
sessions_with_problems.each do |session, problems|
namespace session do
problems.each do |problem|
desc "Test session#{session} problem#{problem}"
task(problem) { run_spec session , problem }
end
desc "Test session#{session} all problems"
task :all => problems.to_a
end
end
task :default => get_sessions.map { |session| "#{session}:all" }