Handle 1.8 VS 1.9 differences in fetch exception.
[ruby_koans.git] / Rakefile
1 #!/usr/bin/env ruby
2 # -*- ruby -*-
3
4 require 'rake/clean'
5 require 'rdoc/task'
6
7 SRC_DIR      = 'src'
8 PROB_DIR     = 'koans'
9 DIST_DIR     = 'dist'
10
11 SRC_FILES = FileList["#{SRC_DIR}/*"]
12 KOAN_FILES = SRC_FILES.pathmap("#{PROB_DIR}/%f")
13
14 today    = Time.now.strftime("%Y-%m-%d")
15 TAR_FILE = "#{DIST_DIR}/rubykoans-#{today}.tgz"
16 ZIP_FILE = "#{DIST_DIR}/rubykoans-#{today}.zip"
17
18 CLEAN.include("**/*.rbc")
19 CLOBBER.include(DIST_DIR)
20
21 module Koans
22   extend Rake::DSL if defined?(Rake::DSL)
23
24   # Remove solution info from source
25   #   __(a,b)     => __
26   #   _n_(number) => __
27   #   # __        =>
28   def Koans.remove_solution(line)
29     line = line.gsub(/\b____\([^\)]+\)/, "____")
30     line = line.gsub(/\b___\([^\)]+\)/, "___")
31     line = line.gsub(/\b__\([^\)]+\)/, "__")
32     line = line.gsub(/\b_n_\([^\)]+\)/, "_n_")
33     line = line.gsub(%r(/\#\{__\}/), "/__/")
34     line = line.gsub(/\s*#\s*__\s*$/, '')
35     line
36   end
37
38   def Koans.make_koan_file(infile, outfile)
39     if infile =~ /edgecase/
40       cp infile, outfile
41     elsif infile =~ /autotest/
42       cp_r infile, outfile
43     else
44       open(infile) do |ins|
45         open(outfile, "w") do |outs|
46           state = :copy
47           ins.each do |line|
48             state = :skip if line =~ /^ *#--/
49             case state
50             when :copy
51               outs.puts remove_solution(line)
52             else
53               # do nothing
54             end
55             state = :copy if line =~ /^ *#\+\+/
56           end
57         end
58       end
59     end
60   end
61 end
62
63 module RubyImpls
64   # Calculate the list of relevant Ruby implementations.
65   def self.find_ruby_impls
66     rubys = `rvm list`.gsub(/=>/,'').split(/\n/).sort
67     expected.map { |impl|
68       last = rubys.grep(Regexp.new(Regexp.quote(impl))).last
69       last ? last.split.first : nil
70     }.compact
71   end
72
73   # Return a (cached) list of relevant Ruby implementations.
74   def self.list
75     @list ||= find_ruby_impls
76   end
77
78   # List of expected ruby implementations.
79   def self.expected
80     %w(ruby-1.8.6 ruby-1.8.7 ruby-1.9.2 jruby ree)
81   end
82 end
83
84 task :default => :walk_the_path
85
86 task :walk_the_path do
87   cd 'koans'
88   ruby 'path_to_enlightenment.rb'
89 end
90
91 Rake::RDocTask.new do |rd|
92   rd.main = "README.rdoc"
93   rd.rdoc_files.include("README.rdoc", "koans/*.rb")
94 end
95
96 directory DIST_DIR
97 directory PROB_DIR
98
99 file ZIP_FILE => KOAN_FILES + [DIST_DIR] do
100   sh "zip #{ZIP_FILE} #{PROB_DIR}/*"
101 end
102
103 file TAR_FILE => KOAN_FILES + [DIST_DIR] do
104   sh "tar zcvf #{TAR_FILE} #{PROB_DIR}"
105 end
106
107 desc "Create packaged files for distribution"
108 task :package => [TAR_FILE, ZIP_FILE]
109
110 desc "Upload the package files to the web server"
111 task :upload => [TAR_FILE, ZIP_FILE] do
112   sh "scp #{TAR_FILE} linode:sites/onestepback.org/download"
113   sh "scp #{ZIP_FILE} linode:sites/onestepback.org/download"
114 end
115
116 desc "Generate the Koans from the source files from scratch."
117 task :regen => [:clobber_koans, :gen]
118
119 desc "Generate the Koans from the changed source files."
120 task :gen => KOAN_FILES + [PROB_DIR + "/README.rdoc"]
121 task :clobber_koans do
122   rm_r PROB_DIR
123 end
124
125 file PROB_DIR + "/README.rdoc" => "README.rdoc" do |t|
126   cp "README.rdoc", t.name
127 end
128
129 SRC_FILES.each do |koan_src|
130   file koan_src.pathmap("#{PROB_DIR}/%f") => [PROB_DIR, koan_src] do |t|
131     Koans.make_koan_file koan_src, t.name
132   end
133 end
134
135 task :run do
136   puts 'koans'
137   Dir.chdir("src") do
138     puts "in #{Dir.pwd}"
139     sh "ruby path_to_enlightenment.rb"
140   end
141 end
142
143
144 desc "Pre-checkin tests (=> run_all)"
145 task :cruise => :run_all
146
147 desc "Run the completed koans againts a list of relevant Ruby Implementations"
148 task :run_all do
149   results = []
150   RubyImpls.list.each do |impl|
151     puts "=" * 40
152     puts "On Ruby #{impl}"
153     sh "rvm #{impl} rake run"
154     results << [impl, "RAN"]
155     puts
156   end
157   puts "=" * 40
158   puts "Summary:"
159   puts
160   results.each do |impl, res|
161     puts "#{impl} => RAN"
162   end
163   puts
164   RubyImpls.expected.each do |requested_impl|
165     impl_pattern = Regexp.new(Regexp.quote(requested_impl))
166     puts "No Results for #{requested_impl}" unless results.detect { |x| x.first =~ impl_pattern }
167   end
168 end