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